aboutsummaryrefslogtreecommitdiff
path: root/projects/11/ConvertToBin
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2018-01-15 12:07:13 +0100
committerYuchen Pei <me@ypei.me>2018-01-15 12:07:13 +0100
commit09c40f135cb3f69599457a5b278d29bcb38fa6ef (patch)
treed92034782bafc958fe1077ba575534d7a7b7f587 /projects/11/ConvertToBin
parent459776159903ab583c0791b1599ec4fc47da8f79 (diff)
checkpoint
Diffstat (limited to 'projects/11/ConvertToBin')
-rw-r--r--projects/11/ConvertToBin/Main.jack2
-rw-r--r--projects/11/ConvertToBin/Main.vm110
-rw-r--r--projects/11/ConvertToBin/setram.tst3
3 files changed, 115 insertions, 0 deletions
diff --git a/projects/11/ConvertToBin/Main.jack b/projects/11/ConvertToBin/Main.jack
index e627486..e1ab941 100644
--- a/projects/11/ConvertToBin/Main.jack
+++ b/projects/11/ConvertToBin/Main.jack
@@ -26,6 +26,8 @@ class Main {
*/
function void main() {
var int value;
+ do Memory.poke(8000, 3827);
+ //do Memory.poke(8000, -7);
do Main.fillMemory(8001, 16, -1); // sets RAM[8001]..RAM[8016] to -1
let value = Memory.peek(8000); // reads a value from RAM[8000]
do Main.convert(value); // performs the conversion
diff --git a/projects/11/ConvertToBin/Main.vm b/projects/11/ConvertToBin/Main.vm
new file mode 100644
index 0000000..15d63c0
--- /dev/null
+++ b/projects/11/ConvertToBin/Main.vm
@@ -0,0 +1,110 @@
+function Main.main 1
+push constant 8000
+push constant 3827
+call Memory.poke 2
+pop temp 0
+push constant 8001
+push constant 16
+push constant 1
+neg
+call Main.fillMemory 3
+pop temp 0
+push constant 8000
+call Memory.peek 1
+pop local 0
+push local 0
+call Main.convert 1
+pop temp 0
+push constant 0
+return
+function Main.convert 3
+push constant 1
+neg
+pop local 2
+label convert.While0
+push local 2
+not
+if-goto convert.EndWhile0
+push local 1
+push constant 1
+add
+pop local 1
+push local 0
+call Main.nextMask 1
+pop local 0
+push local 1
+push constant 16
+gt
+not
+not
+if-goto convert.While0.Else0
+push argument 0
+push local 0
+and
+push constant 0
+eq
+not
+not
+if-goto convert.While0.Else0.Else0
+push constant 8000
+push local 1
+add
+push constant 1
+call Memory.poke 2
+pop temp 0
+goto convert.While0.Else0.Endif0
+label convert.While0.Else0.Else0
+push constant 8000
+push local 1
+add
+push constant 0
+call Memory.poke 2
+pop temp 0
+label convert.While0.Else0.Endif0
+goto convert.While0.Endif0
+label convert.While0.Else0
+push constant 0
+pop local 2
+label convert.While0.Endif0
+goto convert.While0
+label convert.EndWhile0
+push constant 0
+return
+function Main.nextMask 0
+push argument 0
+push constant 0
+eq
+not
+if-goto nextMask.Else0
+push constant 1
+return
+goto nextMask.Endif0
+label nextMask.Else0
+push argument 0
+push constant 2
+call Math.multiply 2
+return
+label nextMask.Endif0
+function Main.fillMemory 0
+label fillMemory.While0
+push argument 1
+push constant 0
+gt
+not
+if-goto fillMemory.EndWhile0
+push argument 0
+push argument 2
+call Memory.poke 2
+pop temp 0
+push argument 1
+push constant 1
+sub
+pop argument 1
+push argument 0
+push constant 1
+add
+pop argument 0
+goto fillMemory.While0
+label fillMemory.EndWhile0
+push constant 0
+return
diff --git a/projects/11/ConvertToBin/setram.tst b/projects/11/ConvertToBin/setram.tst
new file mode 100644
index 0000000..a33eece
--- /dev/null
+++ b/projects/11/ConvertToBin/setram.tst
@@ -0,0 +1,3 @@
+load Main.vm
+set RAM[8000] 1023;
+vmstep;