aboutsummaryrefslogtreecommitdiff
path: root/entries/adirar111
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
parent0921d8222bb883ea86d51c7200a865a5e4dbc469 (diff)
parent7ed13a92711a35a9c263c1f53e33e308653ae727 (diff)
Merging local with remote main
Diffstat (limited to 'entries/adirar111')
-rw-r--r--entries/adirar111/wasm/fib.wasmbin0 -> 144 bytes
-rw-r--r--entries/adirar111/wasm/fib.wat46
-rw-r--r--entries/adirar111/wasm/index.html21
-rw-r--r--entries/adirar111/wasm/index.js23
-rw-r--r--entries/adirar111/wasm/style.css27
-rw-r--r--entries/adirar111/y86/fib.s (renamed from entries/adirar111/y86/fib.ys)11
6 files changed, 125 insertions, 3 deletions
diff --git a/entries/adirar111/wasm/fib.wasm b/entries/adirar111/wasm/fib.wasm
new file mode 100644
index 0000000..6a93250
--- /dev/null
+++ b/entries/adirar111/wasm/fib.wasm
Binary files differ
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))
+ )
+)
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 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>wasm fib</title>
+ <link
+ href="https://fonts.googleapis.com/css2?family=Source+Code+Pro:wght@600;700&display=swap"
+ rel="stylesheet"
+ />
+ <link rel="stylesheet" href="style.css" />
+ </head>
+ <body>
+ <p>go ahead, type a number</p>
+ <div id="input-area">
+ <input id="input" type="text" autocomplete="off" />
+ <button id="compute">compute!</button>
+ </div>
+ <p>fib.wasm says:</p>
+ <span id="result"></span>
+ <script src="index.js"></script>
+ </body>
+</html>
diff --git a/entries/adirar111/wasm/index.js b/entries/adirar111/wasm/index.js
new file mode 100644
index 0000000..667ff82
--- /dev/null
+++ b/entries/adirar111/wasm/index.js
@@ -0,0 +1,23 @@
+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)) {
+ 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)
+ }
+ 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
+}
diff --git a/entries/adirar111/y86/fib.ys b/entries/adirar111/y86/fib.s
index 156c1ae..8aa62d2 100644
--- a/entries/adirar111/y86/fib.ys
+++ b/entries/adirar111/y86/fib.s
@@ -1,8 +1,13 @@
# 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
.pos 0
main: