Created
February 28, 2019 08:47
-
-
Save kfl/d99ac4834b23c51313c17b8730d30feb to your computer and use it in GitHub Desktop.
fact function in WebAssembly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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