You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.4 KiB
Plaintext

(import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param $file_descriptor i32) (param $io_vectors i32) (param $io_vecnum i32) (param $num_written_ptr i32) (result i32)))
(func $print (param $location i32) (param $length i32) (result i32)
(local $structLoc i32)
(local $resultLoc i32)
(local $resultCode i32)
i32.const 8 ;; 2 x 4bytes per i32
call $memory_alloc
local.set $structLoc
i32.const 4
call $memory_alloc
local.set $resultLoc
;; put location at offset 0
local.get $structLoc
local.get $location
i32.store
;; put length at offset 4
local.get $structLoc
i32.const 4
i32.add
local.get $length
i32.store
;; call params
i32.const 1 ;; file file_descriptor
local.get $structLoc ;; array pointer
i32.const 1 ;; array length
local.get $resultLoc ;; result pointer
call $fd_write
local.tee $resultCode
local.get $structLoc ;; free calling struct
call $memory_free
local.get $resultLoc ;; get bytes written on stack
i32.load
local.get $resultLoc ;; free bytes written location
call $memory_free
local.set $resultLoc ;; put in result variable
;; if code was 0, make sure all bytes were written
i32.eqz
if
local.get $resultLoc ;; should be value, not pointer any more
local.get $length
i32.lt_u
if
i32.const 1
return
end
i32.const 0
return
end
;; otherwise return code
local.get $resultCode
return
)