(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 $write (import "env" "write") (param i32 i32 i32) (result i32)) (func $read (import "env" "read") (param i32 i32 i32) (result i32)) (func $istty (import "env" "isatty") (param i32) (result i32)) (func $unlink (import "env" "unlink") (param i32) (result i32)) ;; memory (memory $mem 1) ;; constants (data (i32.const 200) "data") (data (i32.const 230) "Hello, world!\n") ;; functions (func $openFile (result i32) ;; open stdout (i32.const 200) (i32.const 42) (call $open)) (func $writeHelloWorld (param $fd i32) ;; write hello world (get_local $fd) (i32.const 230) (i32.const 14) (call $write) (drop)) (func $readHello (param $fd i32) ;; read hello world (get_local $fd) (i32.const 255) (i32.const 14) (call $read) (drop)) (func $removeFile (i32.const 200) (call $unlink) (drop)) (func $main (result i32) (local $fd i32) ;; set $fd to the file descriptor (call $openFile) (set_local $fd) (get_local $fd) (call $writeHelloWorld) ;; close file (get_local $fd) (call $close) ;; set $fd to the new file descriptor (call $openFile) (set_local $fd) ;; read Hello, World (get_local $fd) (call $readHello) ;; istty (get_local $fd) (call $istty) (drop) ;; close file again (get_local $fd) (call $close) ;; unlink filename (call $removeFile) ;; return 0 (i32.const 0) (set_local $fd) (drop)) (export "main" (func $main)))