From bbde9e43c3503e6d4ae2e180a6ac01305744ae68 Mon Sep 17 00:00:00 2001 From: Aymen Dirar Date: Sun, 23 Oct 2022 16:01:40 -0700 Subject: rename to `.s` --- entries/adirar111/y86/fib.s | 56 ++++++++++++++++++++++++++++++++++++++++++++ entries/adirar111/y86/fib.ys | 56 -------------------------------------------- 2 files changed, 56 insertions(+), 56 deletions(-) create mode 100644 entries/adirar111/y86/fib.s delete mode 100644 entries/adirar111/y86/fib.ys (limited to 'entries/adirar111') diff --git a/entries/adirar111/y86/fib.s b/entries/adirar111/y86/fib.s new file mode 100644 index 0000000..156c1ae --- /dev/null +++ b/entries/adirar111/y86/fib.s @@ -0,0 +1,56 @@ +# y86 implementation of +# def fibonacci(n): +# if n <= 1: +# return n +# return fibonacci(n-1) + fibonacci(n-2) + +.pos 0 +main: +irmovq stack, %rsp # initialize stack pointer +irmovq $13, %rdi # %rdi = n +call fib # fib(n) +halt + + +fib: +irmovq $2, %rsi +irmovq $1, %rdx +rrmovq %rdi, %rcx +rrmovq %rdi, %r8 + +subq %rsi, %rcx # %rcx = n-2 +subq %rdx, %r8 # %r8 = n-1 +jle base # goto base if n <= 1 + +recursed: +# save to stack +pushq %r8 # %r8 = n-1 + +# recurse +rrmovq %rcx, %rdi # %rdi = n-2 +call fib # fib(n-2) + +# restore from stack +popq %r8 # %r8 = n-1 + +# save to stack +pushq %rax # %rax = fib(n-2) + +# recurse +rrmovq %r8, %rdi # %rdi = n-1 +call fib # fib(n-1) + +# restore from stack +popq %r10 # r10 = fib(n-2) + +addq %r10, %rax # %rax = fib(n-2) + fib(n-1) +jmp end + +base: +rrmovq %rdi, %rax # return n + +end: +ret + +.pos 0x1000 +stack: diff --git a/entries/adirar111/y86/fib.ys b/entries/adirar111/y86/fib.ys deleted file mode 100644 index 156c1ae..0000000 --- a/entries/adirar111/y86/fib.ys +++ /dev/null @@ -1,56 +0,0 @@ -# y86 implementation of -# def fibonacci(n): -# if n <= 1: -# return n -# return fibonacci(n-1) + fibonacci(n-2) - -.pos 0 -main: -irmovq stack, %rsp # initialize stack pointer -irmovq $13, %rdi # %rdi = n -call fib # fib(n) -halt - - -fib: -irmovq $2, %rsi -irmovq $1, %rdx -rrmovq %rdi, %rcx -rrmovq %rdi, %r8 - -subq %rsi, %rcx # %rcx = n-2 -subq %rdx, %r8 # %r8 = n-1 -jle base # goto base if n <= 1 - -recursed: -# save to stack -pushq %r8 # %r8 = n-1 - -# recurse -rrmovq %rcx, %rdi # %rdi = n-2 -call fib # fib(n-2) - -# restore from stack -popq %r8 # %r8 = n-1 - -# save to stack -pushq %rax # %rax = fib(n-2) - -# recurse -rrmovq %r8, %rdi # %rdi = n-1 -call fib # fib(n-1) - -# restore from stack -popq %r10 # r10 = fib(n-2) - -addq %r10, %rax # %rax = fib(n-2) + fib(n-1) -jmp end - -base: -rrmovq %rdi, %rax # return n - -end: -ret - -.pos 0x1000 -stack: -- cgit v1.2.3-70-g09d2 From bdd284352a0cff33cff2fdc88b154301b63b2306 Mon Sep 17 00:00:00 2001 From: Aymen Dirar Date: Sun, 23 Oct 2022 16:04:16 -0700 Subject: links to try --- entries/adirar111/y86/fib.s | 3 +++ 1 file changed, 3 insertions(+) (limited to 'entries/adirar111') diff --git a/entries/adirar111/y86/fib.s b/entries/adirar111/y86/fib.s index 156c1ae..eae74fe 100644 --- a/entries/adirar111/y86/fib.s +++ b/entries/adirar111/y86/fib.s @@ -3,6 +3,9 @@ # if n <= 1: # return n # return fibonacci(n-1) + fibonacci(n-2) +# run it: +# * https://www.students.cs.ubc.ca/~cs-313/simulator/?arch=y86&impl=seq +# * https://www.eecs.yorku.ca/~jonatan/simulator/?arch=y86&impl=seq .pos 0 main: -- cgit v1.2.3-70-g09d2 From 7e1694ced1cfce43f79cb4641781d91ebcc15e90 Mon Sep 17 00:00:00 2001 From: Aymen Dirar Date: Sun, 23 Oct 2022 16:07:58 -0700 Subject: indent comment --- entries/adirar111/y86/fib.s | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'entries/adirar111') diff --git a/entries/adirar111/y86/fib.s b/entries/adirar111/y86/fib.s index eae74fe..8aa62d2 100644 --- a/entries/adirar111/y86/fib.s +++ b/entries/adirar111/y86/fib.s @@ -1,8 +1,10 @@ # y86 implementation of +# # def fibonacci(n): -# if n <= 1: -# return n -# return fibonacci(n-1) + fibonacci(n-2) +# if n <= 1: +# return n +# return fibonacci(n-1) + fibonacci(n-2) +# # run it: # * https://www.students.cs.ubc.ca/~cs-313/simulator/?arch=y86&impl=seq # * https://www.eecs.yorku.ca/~jonatan/simulator/?arch=y86&impl=seq -- cgit v1.2.3-70-g09d2 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.wasm | Bin 0 -> 144 bytes entries/adirar111/wasm/fib.wat | 46 ++++++++++++++++++++++++++++++++++++++ entries/adirar111/wasm/index.html | 21 +++++++++++++++++ entries/adirar111/wasm/index.js | 21 +++++++++++++++++ entries/adirar111/wasm/style.css | 27 ++++++++++++++++++++++ 5 files changed, 115 insertions(+) create mode 100644 entries/adirar111/wasm/fib.wasm create mode 100644 entries/adirar111/wasm/fib.wat create mode 100644 entries/adirar111/wasm/index.html create mode 100644 entries/adirar111/wasm/index.js create mode 100644 entries/adirar111/wasm/style.css (limited to 'entries/adirar111') diff --git a/entries/adirar111/wasm/fib.wasm b/entries/adirar111/wasm/fib.wasm new file mode 100644 index 0000000..6a93250 Binary files /dev/null and b/entries/adirar111/wasm/fib.wasm differ 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)) + ) +) diff --git a/entries/adirar111/wasm/index.html b/entries/adirar111/wasm/index.html new file mode 100644 index 0000000..8357a55 --- /dev/null +++ b/entries/adirar111/wasm/index.html @@ -0,0 +1,21 @@ + + +
+go ahead, type a number
+fib.wasm says:
+ + + + diff --git a/entries/adirar111/wasm/index.js b/entries/adirar111/wasm/index.js new file mode 100644 index 0000000..43ee9a6 --- /dev/null +++ b/entries/adirar111/wasm/index.js @@ -0,0 +1,21 @@ +const wasmBinaryBase64 = "AGFzbQEAAAABBgFgAX8BfwMCAQAHBwEDZmliAAAKRwFFAQR/IABBAkgEQCAADwtBACEBQQEhAkECIQMgAEEBaiEAA0AgAiEEIAIgAWohAiAEIQEgA0EBaiEDIAMgAEgNAAsgAg8LACgEbmFtZQEGAQADZmliAhkBAAUAAW4BBGxhc3QCA3N1bQMBaQQDdG1w" +const wasmBinaryDataURL = `data:application/wasm;base64,${wasmBinaryBase64}`; + +WebAssembly.instantiateStreaming(fetch(wasmBinaryDataURL)).then( + (wasmObj) => { + const {fib: fibWasm} = wasmObj.instance.exports + const fib = (event) => { + event.preventDefault() + const input = document.getElementById("input") + const result = document.getElementById("result") + const n = parseInt(input.value) + if (isNaN(n)) { + result.textContent = "that's not a number..." + return + } + result.textContent = fibWasm(n) + } + const button = document.getElementById("compute") + button.onclick = fib + } +); diff --git a/entries/adirar111/wasm/style.css b/entries/adirar111/wasm/style.css new file mode 100644 index 0000000..8ab403f --- /dev/null +++ b/entries/adirar111/wasm/style.css @@ -0,0 +1,27 @@ +html { + background: #5a46ee; + color: #fff; + font-family: "Source Code Pro" +} + +body { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; +} + +#input-area { + display: flex; + flex-direction: row; + justify-content: space-between; +} + +#input { + height: 40px; + font-size: 30px +} + +#result { + font-size: 50px +} -- 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') 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 5e2b3d53917bb6a39dbffd0db6888bacaa283536 Mon Sep 17 00:00:00 2001 From: Aymen Dirar Date: Sun, 23 Oct 2022 23:29:34 -0700 Subject: handle negative --- entries/adirar111/wasm/index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'entries/adirar111') diff --git a/entries/adirar111/wasm/index.js b/entries/adirar111/wasm/index.js index 43ee9a6..667ff82 100644 --- a/entries/adirar111/wasm/index.js +++ b/entries/adirar111/wasm/index.js @@ -10,8 +10,10 @@ WebAssembly.instantiateStreaming(fetch(wasmBinaryDataURL)).then( const result = document.getElementById("result") const n = parseInt(input.value) if (isNaN(n)) { - result.textContent = "that's not a number..." - return + return result.textContent = "that wasn't very wasm of you" + } + if (n < 0) { + return result.textContent = "why are you being so negative" } result.textContent = fibWasm(n) } -- cgit v1.2.3-70-g09d2