Skip to content

Instantly share code, notes, and snippets.

@kfl
Created February 28, 2019 08:47
Show Gist options
  • Save kfl/d99ac4834b23c51313c17b8730d30feb to your computer and use it in GitHub Desktop.
Save kfl/d99ac4834b23c51313c17b8730d30feb to your computer and use it in GitHub Desktop.
fact function in WebAssembly
(module
(export "fact" (func $fact))
(func $fact (param $n i32) (result i32)
(local $result i32)
(set_local $result (i32.const 1))
(if (get_local $n)
(loop $start
(set_local $result (i32.mul (get_local $n) (get_local $result)))
(tee_local $n (i32.sub (get_local $n) (i32.const 1)))
(br_if $start)
)
)
(get_local $result)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment