Replies: 1 comment 1 reply
-
If you're not running a browser, I guess you're running in wasmer. You've got a few options for "syscalls" there, serveral versions of wasi (unstable, snapshot_preview1, wasix32v1, wasix64v1), or a relatively-old-version-of-emscripten-thing. I'll pick wasi_snapshot_preview1. As with all hello worlds, its pretty boring: (module
(import "wasi_snapshot_preview1" "fd_write"
(func $__wasi_fd_write (param i32 i32 i32 i32) (result i32)))
(func $_start
(drop (call $__wasi_fd_write
(i32.const 1)
(i32.const 0)
(i32.const 1)
(i32.const 4)))
)
(memory 1)
(export "memory" (memory 0))
(export "_start" (func $_start))
(data (i32.const 0) "\09\00\00\00\0d\00\00\00 Hello, wasi!\0ax")
) It just pre-loads an iov at address 0, which points to the hello world message directly behind the iov. How about something more interesting? (module
(import "wasi_snapshot_preview1" "proc_exit"
(func $__wasi_proc_exit (param i32)))
(import "wasi_snapshot_preview1" "fd_write"
(func $__wasi_fd_write (param i32 i32 i32 i32) (result i32)))
(import "wasi_snapshot_preview1" "args_sizes_get"
(func $__wasi_args_sizes_get (param i32 i32) (result i32)))
(import "wasi_snapshot_preview1" "args_get"
(func $__wasi_args_get (param i32 i32) (result i32)))
(func $_start
(call $print_c (i32.const 128))
(call $print_c (i32.const 256))
;; checking return values or making sure we have enough memory? Yolo.
(drop (call $__wasi_args_sizes_get (i32.const 504) (i32.const 508)))
(drop (call $__wasi_args_get (i32.const 512) (i32.add (i32.mul (i32.const 4) (i32.load (i32.const 508))) (i32.const 512))))
(call $print_c (i32.const 160))
(call $print_c (i32.load (i32.const 512)))
(call $print_c (i32.const 256))
(call $print_c (i32.const 192))
(call $print_i (i32.sub (i32.load (i32.const 504)) (i32.const 1)))
(call $print_c (i32.const 224))
(call $print_c (i32.const 256))
(call $__wasi_proc_exit (i32.const 0))
)
(func $print_b
(drop (call $__wasi_fd_write
(i32.const 1)
(i32.const 0)
(i32.const 1)
(i32.const 4)))
)
(func $print_i (param $v i32)
(local $i i32)
(local.set $i (i32.const 10))
(block $done
(loop $loop
(br_if $done (i32.eqz (local.get $i)))
(i32.store8 (i32.add (i32.const 8) (local.get $i)) (i32.add (i32.const 48) (i32.rem_u (local.get $v) (i32.const 10))) )
(local.set $v (i32.div_u (local.get $v) (i32.const 10)))
(br_if $done (i32.eqz (local.get $v)))
(local.set $i (i32.sub (local.get $i) (i32.const 1)))
(br $loop)
)
)
(i32.store (i32.const 0) (i32.add (i32.const 8) (local.get $i)))
(i32.store (i32.const 4) (i32.sub (i32.const 11) (local.get $i)))
(call $print_b)
)
(func $print_c (param $s i32)
(local $l i32)
(local.set $l (i32.const 0))
(i32.store (i32.const 0) (local.get $s))
(block $done
(loop $loop
(br_if $done (i32.eqz (i32.load8_u (local.get $s))))
(local.set $l (i32.add (local.get $l) (i32.const 1)))
(local.set $s (i32.add (local.get $s) (i32.const 1)))
(br $loop)
)
)
(i32.store (i32.const 4) (local.get $l))
(call $print_b)
)
(memory 1)
(export "memory" (memory 0))
(export "_start" (func $_start))
(data (i32.const 128) "Hello, wasi!\00X")
(data (i32.const 160) "My name is \00X")
(data (i32.const 192) "You passed \00X")
(data (i32.const 224) " other arguments.\00X")
(data (i32.const 256) "\0a\00X")
) Both examples ignore the possibility of incomplete writes. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've seen examples for all sorts of programming languages that compile to wasm, but I am looking for an example that is written in wasm directly. Some hello world example, that prints "Hello World" to the console and exits again.
I found a Hello World program elsewhere, but it just assumes that I am running in a browser, which I am not doing.
I would like to explore the wasm language, a bit. The motivation behind this is, I have my own toy programming language and I would like to explore a wasm backand for it. For this I would like to explore and understand the raw wasm format a bit better.
Beta Was this translation helpful? Give feedback.
All reactions