From 81fdbeb7fa4d8a80d028a988c353c3fa16d4953b Mon Sep 17 00:00:00 2001 From: Aymen Dirar Date: Sun, 23 Oct 2022 23:06:51 -0700 Subject: wasm lol --- entries/adirar111/wasm/fib.wat | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 entries/adirar111/wasm/fib.wat (limited to 'entries/adirar111/wasm/fib.wat') diff --git a/entries/adirar111/wasm/fib.wat b/entries/adirar111/wasm/fib.wat new file mode 100644 index 0000000..b9eba78 --- /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 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)) + ) +) -- cgit v1.2.3-70-g09d2 From 51ebb554063aa504668e939c766236967829b94b Mon Sep 17 00:00:00 2001 From: Aymen Dirar Date: Sun, 23 Oct 2022 23:16:59 -0700 Subject: comment --- entries/adirar111/wasm/fib.wat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'entries/adirar111/wasm/fib.wat') diff --git a/entries/adirar111/wasm/fib.wat b/entries/adirar111/wasm/fib.wat index b9eba78..255fa12 100644 --- a/entries/adirar111/wasm/fib.wat +++ b/entries/adirar111/wasm/fib.wat @@ -1,5 +1,5 @@ ;; iterative fib(n) in web assembly text format (wat) -;; this gets compiled to fib.wasm binary +;; this gets compiled to the fib.wasm binary ;; and gets fetched via a data URL in index.js (module (export "fib" (func $fib)) -- cgit v1.2.3-70-g09d2 From f1dc8976a325442c616e05dc4ba9df887f859292 Mon Sep 17 00:00:00 2001 From: Aymen Dirar Date: Mon, 24 Oct 2022 20:38:35 -0700 Subject: make wasm prettier --- entries/adirar111/wasm/fib.wat | 53 ++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 36 deletions(-) (limited to 'entries/adirar111/wasm/fib.wat') diff --git a/entries/adirar111/wasm/fib.wat b/entries/adirar111/wasm/fib.wat index 255fa12..6ec9b69 100644 --- a/entries/adirar111/wasm/fib.wat +++ b/entries/adirar111/wasm/fib.wat @@ -4,43 +4,24 @@ (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) + (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)) ) - (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) - ) + (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))) ) - (br_if $loop - (i32.lt_s - (local.get $i) - (local.get $n) - ) - ) - ) - (return (local.get $sum)) + (return (local.get $sum)) ) ) -- cgit v1.2.3-70-g09d2