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