From a0805258e358fd798334b334612f808eedbab126 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Mon, 18 Jun 2018 04:13:21 +0000 Subject: [PATCH] userland/src/lib: add bloghelloworld --- userland/src/lib/bloghelloworld.wast | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 userland/src/lib/bloghelloworld.wast diff --git a/userland/src/lib/bloghelloworld.wast b/userland/src/lib/bloghelloworld.wast new file mode 100644 index 0000000..dfa4f4a --- /dev/null +++ b/userland/src/lib/bloghelloworld.wast @@ -0,0 +1,40 @@ +(module + ;; import functions from env + (func $close (import "env" "close") (param i32) (result i32)) + (func $open (import "env" "open") (param i32 i32) (result i32)) + (func $read (import "env" "read") (param i32 i32 i32) (result i32)) + (func $write (import "env" "write") (param i32 i32 i32) (result i32)) + + ;; memory + (memory $mem 1) + + ;; constants + (data (i32.const 200) "data") + (data (i32.const 230) "Hello, world!\n") + + (func $main (result i32) + ;; $fd is the file descriptor of the file we're gonna open + (local $fd i32) + + ;; $fd = $open("data", O_CREAT|O_RDWR); + (set_local $fd + (call $open + ;; pointer to the file name + (i32.const 200) + ;; flags, 42 for O_CREAT,O_RDWR + (i32.const 42))) + + ;; $write($fd, "Hello, World!\n", 14); + (call $write + (get_local $fd) + (i32.const 230) + (i32.const 14)) + (drop) + + ;; $close($fd); + (call $close + (get_local $fd)) + (drop) + + (i32.const 0)) + (export "main" (func $main)))