Jump to content

Call a function: Difference between revisions

Line 5,309:
=={{header|WebAssembly}}==
 
<lang webassembly>(modulefunc $main (export "_start")
 
(memorylocal 12$result i32)
(export "memory" (memory 0))
 
;;Call a function with no arguments
(funccall $noargfunc
(i32.const 2)
(i32.const 3)
i32.add
drop ;;Discard the result
)
 
;;Multiply two numbers and store the result, flat syntax
(func $multipy (param $a i32) (param $b i32) (result i32)
locali32.getconst $a12
locali32.getconst $b3
call i32.mul$multipy
set_local $result
)
 
;;Multiply two numbers and store the result, indented syntax
(func $addinmemory (param $a i32) (param $b i32) (param $out i32)
(set_local $result
(i32.load (get_local $a))
(i32.load (get_localcall $b))multipy
(i32.mulconst 12)
(i32.store (get_localconst $out)3)
)
) )
 
;;Add two numbers in linear memory (similar to using pointers)
(func $main (export "_start")
(i32.store (i32.const 0) (i32.const 5))
(i32.store (i32.const 4) (i32.const 7))
 
(localcall $result i32)addinmemory
(i32.const 20)
 
(i32.const 34)
;;Call a function with no arguments
call(i32.const $noargfunc8)
 
;;Multiply two numbers and store the result, flat syntax
i32.const 12
i32.const 3
call $multipy
set_local $result
 
;;Multiply two numbers and store the result, indented syntax
(set_local $result
(call $multipy
(i32.const 12)
(i32.const 3)
)
)
 
;;Add two numbers in linear memory (similar to using pointers)
(i32.store (i32.const 0) (i32.const 5))
(i32.store (i32.const 4) (i32.const 7))
 
(call $addinmemory
(i32.const 0)
(i32.const 4)
(i32.const 8)
)
)
)</lang>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.