aboutsummaryrefslogtreecommitdiff
path: root/entries/adirar111/wasm/fib.wat
diff options
context:
space:
mode:
authorJames Yoo2022-10-24 23:47:51 +0000
committerJames Yoo2022-10-24 23:47:51 +0000
commit03315f012514bdcc5a4f654056f0103abe11eb83 (patch)
tree2b7c6e9233ecb503e7be0d8354483691f9a1e16c /entries/adirar111/wasm/fib.wat
parent0921d8222bb883ea86d51c7200a865a5e4dbc469 (diff)
parent7ed13a92711a35a9c263c1f53e33e308653ae727 (diff)
Merging local with remote main
Diffstat (limited to 'entries/adirar111/wasm/fib.wat')
-rw-r--r--entries/adirar111/wasm/fib.wat46
1 files changed, 46 insertions, 0 deletions
diff --git a/entries/adirar111/wasm/fib.wat b/entries/adirar111/wasm/fib.wat
new file mode 100644
index 0000000..255fa12
--- /dev/null
+++ b/entries/adirar111/wasm/fib.wat
@@ -0,0 +1,46 @@
+;; iterative fib(n) in web assembly text format (wat)
+;; this gets compiled to the fib.wasm binary
+;; and gets fetched via a data URL in index.js
+(module
+ (export "fib" (func $fib))
+ (func $fib (param $n i32) (result i32)
+ (local $last i32)
+ (local $sum i32)
+ (local $i i32)
+ (local $tmp i32)
+ (if
+ (i32.lt_s
+ (local.get $n)
+ (i32.const 2)
+ )
+ (return (local.get $n))
+ )
+ (local.set $last (i32.const 0))
+ (local.set $sum (i32.const 1))
+ (local.set $i (i32.const 2))
+ (local.set $n (i32.add (local.get $n) (i32.const 1)))
+ (loop $loop
+ (local.set $tmp (local.get $sum))
+ (local.set $sum
+ (i32.add
+ (local.get $sum)
+ (local.get $last)
+ )
+ )
+ (local.set $last (local.get $tmp))
+ (local.set $i
+ (i32.add
+ (local.get $i)
+ (i32.const 1)
+ )
+ )
+ (br_if $loop
+ (i32.lt_s
+ (local.get $i)
+ (local.get $n)
+ )
+ )
+ )
+ (return (local.get $sum))
+ )
+)