aboutsummaryrefslogtreecommitdiff
path: root/entries/adirar111/wasm/fib.wat
diff options
context:
space:
mode:
authorfunemy2022-10-25 07:14:52 +0000
committerfunemy2022-10-25 07:14:52 +0000
commit998ef837b43203319397f191036a97a5adc42220 (patch)
tree920558bd60d282c663fd2da1cd4144cac1849a1d /entries/adirar111/wasm/fib.wat
parentfdac6c60e115297a58f5b81da0c4b7f18ac758f2 (diff)
parentce7544a6db594f7d3dfad0d7dc65d01515e57ad6 (diff)
Merge branch 'main' of github.com:braxtonhall/fib
Diffstat (limited to 'entries/adirar111/wasm/fib.wat')
-rw-r--r--entries/adirar111/wasm/fib.wat27
1 files changed, 27 insertions, 0 deletions
diff --git a/entries/adirar111/wasm/fib.wat b/entries/adirar111/wasm/fib.wat
new file mode 100644
index 0000000..6ec9b69
--- /dev/null
+++ b/entries/adirar111/wasm/fib.wat
@@ -0,0 +1,27 @@
+;; 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))
+ )
+)