-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.wat
35 lines (29 loc) · 988 Bytes
/
hello.wat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(module $hello_wasi
;; type iov struct { iov_base, iov_len int32 }
;; func fd_write(fd int32, id *iov, iovs_len int32, nwritten *int32) (errno int32)
(import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32) (param i32) (param i32) (param i32) (result i32)))
(memory 1)(export "memory" (memory 0))
;; 前 8 个字节保留给 iov 数组, 字符串从地址 8 开始
(data (i32.const 8) "hello world\n")
;; _start 类似 main 函数, 自动执行
(func $main (export "_start")
;; iov.iov_base - 字符串地址为 8
i32.const 0
i32.const 8
i32.store
;; iov.iov_len - 字符串长度
block $label (result i32 i32)
i32.const 4
i32.const 12
end
i32.store
;; 输出到 stdout
i32.const 1 ;; 1 对应 stdout
i32.const 0 ;; *iovs - 前 8 个字节保留给 iov 数组
i32.const 1 ;; len(iovs) - 只有1个字符串
i32.const 20 ;; nwritten - 指针, 里面是要写到数据长度
call $fd_write
;; 忽略返回值
drop
)
)