aboutsummaryrefslogtreecommitdiff
path: root/projects/12
diff options
context:
space:
mode:
Diffstat (limited to 'projects/12')
-rw-r--r--projects/12/Array.jack2
-rw-r--r--projects/12/ArrayTest/Array.jack26
-rw-r--r--projects/12/ArrayTest/Array.vm12
-rw-r--r--projects/12/ArrayTest/ArrayTest.out2
-rw-r--r--projects/12/ArrayTest/Main.vm131
-rw-r--r--projects/12/MemoryTest/Memory.jack24
-rw-r--r--projects/12/MemoryTest/Memory.vm118
-rw-r--r--projects/12/Sys.jack19
-rw-r--r--projects/12/SysTest/Main.vm281
-rw-r--r--projects/12/SysTest/Sys.jack51
-rw-r--r--projects/12/SysTest/Sys.vm74
11 files changed, 611 insertions, 129 deletions
diff --git a/projects/12/Array.jack b/projects/12/Array.jack
index ee31129..c712b69 100644
--- a/projects/12/Array.jack
+++ b/projects/12/Array.jack
@@ -15,9 +15,11 @@ class Array {
/** Constructs a new Array of the given size. */
function Array new(int size) {
+ return Memory.alloc(size);
}
/** Disposes this array. */
method void dispose() {
+ return Memory.deAlloc(this);
}
}
diff --git a/projects/12/ArrayTest/Array.jack b/projects/12/ArrayTest/Array.jack
new file mode 100644
index 0000000..343c25c
--- /dev/null
+++ b/projects/12/ArrayTest/Array.jack
@@ -0,0 +1,26 @@
+// This file is part of www.nand2tetris.org
+// and the book "The Elements of Computing Systems"
+// by Nisan and Schocken, MIT Press.
+// File name: projects/12/Array.jack
+
+/**
+ * Represents an array.
+ * In the Jack language, arrays are instances of the Array class.
+ * Once declared, the array entries can be accessed using the usual
+ * syntax arr[i]. Each array entry can hold a primitive data type as
+ * well as any object type. Different array entries can have different
+ * data types.
+ */
+class Array {
+
+ /** Constructs a new Array of the given size. */
+ function Array new(int size) {
+ return Memory.alloc(size);
+ }
+
+ /** Disposes this array. */
+ method void dispose() {
+ do Memory.deAlloc(this);
+ return;
+ }
+}
diff --git a/projects/12/ArrayTest/Array.vm b/projects/12/ArrayTest/Array.vm
new file mode 100644
index 0000000..c0cd797
--- /dev/null
+++ b/projects/12/ArrayTest/Array.vm
@@ -0,0 +1,12 @@
+function Array.new 0
+push argument 0
+call Memory.alloc 1
+return
+function Array.dispose 0
+push argument 0
+pop pointer 0
+push pointer 0
+call Memory.deAlloc 1
+pop temp 0
+push constant 0
+return
diff --git a/projects/12/ArrayTest/ArrayTest.out b/projects/12/ArrayTest/ArrayTest.out
new file mode 100644
index 0000000..e9f4e75
--- /dev/null
+++ b/projects/12/ArrayTest/ArrayTest.out
@@ -0,0 +1,2 @@
+|RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]|
+| 222 | 122 | 100 | 10 |
diff --git a/projects/12/ArrayTest/Main.vm b/projects/12/ArrayTest/Main.vm
new file mode 100644
index 0000000..141f20a
--- /dev/null
+++ b/projects/12/ArrayTest/Main.vm
@@ -0,0 +1,131 @@
+function Main.main 4
+push constant 8000
+pop local 0
+push constant 3
+call Array.new 1
+pop local 1
+push constant 2
+push local 1
+add
+push constant 222
+pop temp 0
+pop pointer 1
+push temp 0
+pop that 0
+push constant 0
+push local 0
+add
+push constant 2
+push local 1
+add
+pop pointer 1
+push that 0
+pop temp 0
+pop pointer 1
+push temp 0
+pop that 0
+push constant 3
+call Array.new 1
+pop local 2
+push constant 1
+push local 2
+add
+push constant 2
+push local 1
+add
+pop pointer 1
+push that 0
+push constant 100
+sub
+pop temp 0
+pop pointer 1
+push temp 0
+pop that 0
+push constant 1
+push local 0
+add
+push constant 1
+push local 2
+add
+pop pointer 1
+push that 0
+pop temp 0
+pop pointer 1
+push temp 0
+pop that 0
+push constant 500
+call Array.new 1
+pop local 3
+push constant 499
+push local 3
+add
+push constant 2
+push local 1
+add
+pop pointer 1
+push that 0
+push constant 1
+push local 2
+add
+pop pointer 1
+push that 0
+sub
+pop temp 0
+pop pointer 1
+push temp 0
+pop that 0
+push constant 2
+push local 0
+add
+push constant 499
+push local 3
+add
+pop pointer 1
+push that 0
+pop temp 0
+pop pointer 1
+push temp 0
+pop that 0
+push local 1
+call Array.dispose 1
+pop temp 0
+push local 2
+call Array.dispose 1
+pop temp 0
+push constant 3
+call Array.new 1
+pop local 2
+push constant 0
+push local 2
+add
+push constant 499
+push local 3
+add
+pop pointer 1
+push that 0
+push constant 90
+sub
+pop temp 0
+pop pointer 1
+push temp 0
+pop that 0
+push constant 3
+push local 0
+add
+push constant 0
+push local 2
+add
+pop pointer 1
+push that 0
+pop temp 0
+pop pointer 1
+push temp 0
+pop that 0
+push local 3
+call Array.dispose 1
+pop temp 0
+push local 2
+call Array.dispose 1
+pop temp 0
+push constant 0
+return
diff --git a/projects/12/MemoryTest/Memory.jack b/projects/12/MemoryTest/Memory.jack
index 8583b62..54e04aa 100644
--- a/projects/12/MemoryTest/Memory.jack
+++ b/projects/12/MemoryTest/Memory.jack
@@ -11,7 +11,7 @@
class Memory {
static Array memory;
static Array heap;
- static int first;
+ static int last;
/** Initializes the class. */
function void init() {
@@ -19,6 +19,7 @@ class Memory {
let heap = 2048;
let heap[0] = -1;
let heap[1] = 14334;
+ let last = 0;
return;
}
@@ -62,23 +63,10 @@ class Memory {
* it available for future allocations. */
function void deAlloc(Array o) {
var int oBase;
- var int current;
- var int prev;
- let prev = 0;
- let current = heap[prev];
let oBase = o - heap - 2;
- while ((current > -1) & (~(current = oBase))){
- let prev = current;
- let current = heap[current];
- }
- if (current = -1) {
- do String.println("The impossible happened");
- do Sys.error(1);
- return;
- } else {
- let heap[prev] = heap[current];
- let heap[prev + 1] = heap[prev] - prev - 2;
- return;
- }
+ let heap[oBase] = -1;
+ let heap[last] = oBase;
+ let last = oBase;
+ return;
}
}
diff --git a/projects/12/MemoryTest/Memory.vm b/projects/12/MemoryTest/Memory.vm
index c7410fc..0570b46 100644
--- a/projects/12/MemoryTest/Memory.vm
+++ b/projects/12/MemoryTest/Memory.vm
@@ -21,6 +21,8 @@ pop pointer 1
push temp 0
pop that 0
push constant 0
+pop static 2
+push constant 0
return
function Memory.peek 0
push argument 0
@@ -226,137 +228,31 @@ push constant 2
add
return
label IF_END0
-function Memory.deAlloc 3
-push constant 0
-pop local 2
-push local 2
-push static 1
-add
-pop pointer 1
-push that 0
-pop local 1
+function Memory.deAlloc 1
push argument 0
push static 1
sub
push constant 2
sub
pop local 0
-label WHILE_EXP0
-push local 1
-push constant 1
-neg
-gt
-push local 1
push local 0
-eq
-not
-and
-not
-if-goto WHILE_END0
-push local 1
-pop local 2
-push local 1
push static 1
add
-pop pointer 1
-push that 0
-pop local 1
-goto WHILE_EXP0
-label WHILE_END0
-push local 1
push constant 1
neg
-eq
-if-goto IF_TRUE0
-goto IF_FALSE0
-label IF_TRUE0
-push constant 23
-call String.new 1
-push constant 84
-call String.appendChar 2
-push constant 104
-call String.appendChar 2
-push constant 101
-call String.appendChar 2
-push constant 32
-call String.appendChar 2
-push constant 105
-call String.appendChar 2
-push constant 109
-call String.appendChar 2
-push constant 112
-call String.appendChar 2
-push constant 111
-call String.appendChar 2
-push constant 115
-call String.appendChar 2
-push constant 115
-call String.appendChar 2
-push constant 105
-call String.appendChar 2
-push constant 98
-call String.appendChar 2
-push constant 108
-call String.appendChar 2
-push constant 101
-call String.appendChar 2
-push constant 32
-call String.appendChar 2
-push constant 104
-call String.appendChar 2
-push constant 97
-call String.appendChar 2
-push constant 112
-call String.appendChar 2
-push constant 112
-call String.appendChar 2
-push constant 101
-call String.appendChar 2
-push constant 110
-call String.appendChar 2
-push constant 101
-call String.appendChar 2
-push constant 100
-call String.appendChar 2
-call String.println 1
-pop temp 0
-push constant 1
-call Sys.error 1
-pop temp 0
-push constant 0
-return
-goto IF_END0
-label IF_FALSE0
-push local 2
-push static 1
-add
-push local 1
-push static 1
-add
-pop pointer 1
-push that 0
pop temp 0
pop pointer 1
push temp 0
pop that 0
-push local 2
-push constant 1
-add
-push static 1
-add
-push local 2
+push static 2
push static 1
add
-pop pointer 1
-push that 0
-push local 2
-sub
-push constant 2
-sub
+push local 0
pop temp 0
pop pointer 1
push temp 0
pop that 0
+push local 0
+pop static 2
push constant 0
return
-label IF_END0
diff --git a/projects/12/Sys.jack b/projects/12/Sys.jack
index bee6f67..d6b99f6 100644
--- a/projects/12/Sys.jack
+++ b/projects/12/Sys.jack
@@ -10,18 +10,37 @@ class Sys {
/** Performs all the initializations required by the OS. */
function void init() {
+ do Array.init();
+ do Keyboard.init();
+ do Math.init();
+ do Memory.init();
+ do Output.init();
+ do Screen.init();
+ do String.init();
+ do Main.main();
}
/** Halts the program execution. */
function void halt() {
+ while (true){
+ }
+ return;
}
/** Waits approximately duration milliseconds and returns. */
function void wait(int duration) {
+ Output.printString("waiting")
+ let i = 0;
+ while (i < 50000){
+ let i = i + 1;
+ }
+ Output.printString("done")
}
/** Displays the given error code in the form "ERR<errorCode>",
* and halts the program's execution. */
function void error(int errorCode) {
+ Output.printString("ERR");
+ Output.printInt(errorCode);
}
}
diff --git a/projects/12/SysTest/Main.vm b/projects/12/SysTest/Main.vm
new file mode 100644
index 0000000..4890e2f
--- /dev/null
+++ b/projects/12/SysTest/Main.vm
@@ -0,0 +1,281 @@
+function Main.main 1
+push constant 10
+call String.new 1
+push constant 87
+call String.appendChar 2
+push constant 97
+call String.appendChar 2
+push constant 105
+call String.appendChar 2
+push constant 116
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 116
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 115
+call String.appendChar 2
+push constant 116
+call String.appendChar 2
+push constant 58
+call String.appendChar 2
+call Output.printString 1
+pop temp 0
+call Output.println 0
+pop temp 0
+push constant 64
+call String.new 1
+push constant 80
+call String.appendChar 2
+push constant 114
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 115
+call String.appendChar 2
+push constant 115
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 97
+call String.appendChar 2
+push constant 110
+call String.appendChar 2
+push constant 121
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 107
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 121
+call String.appendChar 2
+push constant 46
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 65
+call String.appendChar 2
+push constant 102
+call String.appendChar 2
+push constant 116
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 114
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 50
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 115
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 99
+call String.appendChar 2
+push constant 111
+call String.appendChar 2
+push constant 110
+call String.appendChar 2
+push constant 100
+call String.appendChar 2
+push constant 115
+call String.appendChar 2
+push constant 44
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 97
+call String.appendChar 2
+push constant 110
+call String.appendChar 2
+push constant 111
+call String.appendChar 2
+push constant 116
+call String.appendChar 2
+push constant 104
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 114
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 109
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 115
+call String.appendChar 2
+push constant 115
+call String.appendChar 2
+push constant 97
+call String.appendChar 2
+push constant 103
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 119
+call String.appendChar 2
+push constant 105
+call String.appendChar 2
+push constant 108
+call String.appendChar 2
+push constant 108
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 98
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 112
+call String.appendChar 2
+push constant 114
+call String.appendChar 2
+push constant 105
+call String.appendChar 2
+push constant 110
+call String.appendChar 2
+push constant 116
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 100
+call String.appendChar 2
+push constant 58
+call String.appendChar 2
+call Output.printString 1
+pop temp 0
+label WHILE_EXP0
+push local 0
+push constant 0
+eq
+not
+if-goto WHILE_END0
+call Keyboard.keyPressed 0
+pop local 0
+goto WHILE_EXP0
+label WHILE_END0
+label WHILE_EXP1
+push local 0
+push constant 0
+eq
+not
+not
+if-goto WHILE_END1
+call Keyboard.keyPressed 0
+pop local 0
+goto WHILE_EXP1
+label WHILE_END1
+push constant 2000
+call Sys.wait 1
+pop temp 0
+call Output.println 0
+pop temp 0
+push constant 45
+call String.new 1
+push constant 84
+call String.appendChar 2
+push constant 105
+call String.appendChar 2
+push constant 109
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 105
+call String.appendChar 2
+push constant 115
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 117
+call String.appendChar 2
+push constant 112
+call String.appendChar 2
+push constant 46
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 77
+call String.appendChar 2
+push constant 97
+call String.appendChar 2
+push constant 107
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 115
+call String.appendChar 2
+push constant 117
+call String.appendChar 2
+push constant 114
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 116
+call String.appendChar 2
+push constant 104
+call String.appendChar 2
+push constant 97
+call String.appendChar 2
+push constant 116
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 50
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 115
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 99
+call String.appendChar 2
+push constant 111
+call String.appendChar 2
+push constant 110
+call String.appendChar 2
+push constant 100
+call String.appendChar 2
+push constant 115
+call String.appendChar 2
+push constant 32
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 108
+call String.appendChar 2
+push constant 97
+call String.appendChar 2
+push constant 112
+call String.appendChar 2
+push constant 115
+call String.appendChar 2
+push constant 101
+call String.appendChar 2
+push constant 100
+call String.appendChar 2
+push constant 46
+call String.appendChar 2
+call Output.printString 1
+pop temp 0
+push constant 0
+return
diff --git a/projects/12/SysTest/Sys.jack b/projects/12/SysTest/Sys.jack
new file mode 100644
index 0000000..53c078c
--- /dev/null
+++ b/projects/12/SysTest/Sys.jack
@@ -0,0 +1,51 @@
+// This file is part of www.nand2tetris.org
+// and the book "The Elements of Computing Systems"
+// by Nisan and Schocken, MIT Press.
+// File name: projects/12/Sys.jack
+
+/**
+ * A library that supports various program execution services.
+ */
+class Sys {
+
+ /** Performs all the initializations required by the OS. */
+ function void init() {
+ do Keyboard.init();
+ do Math.init();
+ do Memory.init();
+ do Output.init();
+ do Screen.init();
+ do Main.main();
+ do Sys.halt();
+ return;
+ }
+
+ /** Halts the program execution. */
+ function void halt() {
+ while (true){
+ }
+ return;
+ }
+
+ /** Waits approximately duration milliseconds and returns. */
+ function void wait(int duration) {
+ var int i, j;
+ let i = 0;
+ while (i < duration){
+ let i = i + 1;
+ let j = 0;
+ while (j < 318){
+ let j = j + 1;
+ }
+ }
+ return;
+ }
+
+ /** Displays the given error code in the form "ERR<errorCode>",
+ * and halts the program's execution. */
+ function void error(int errorCode) {
+ do Output.printString("ERR");
+ do Output.printInt(errorCode);
+ return;
+ }
+}
diff --git a/projects/12/SysTest/Sys.vm b/projects/12/SysTest/Sys.vm
new file mode 100644
index 0000000..fec2862
--- /dev/null
+++ b/projects/12/SysTest/Sys.vm
@@ -0,0 +1,74 @@
+function Sys.init 0
+call Keyboard.init 0
+pop temp 0
+call Math.init 0
+pop temp 0
+call Memory.init 0
+pop temp 0
+call Output.init 0
+pop temp 0
+call Screen.init 0
+pop temp 0
+call Main.main 0
+pop temp 0
+call Sys.halt 0
+pop temp 0
+push constant 0
+return
+function Sys.halt 0
+label WHILE_EXP0
+push constant 0
+not
+not
+if-goto WHILE_END0
+goto WHILE_EXP0
+label WHILE_END0
+push constant 0
+return
+function Sys.wait 2
+push constant 0
+pop local 0
+label WHILE_EXP0
+push local 0
+push argument 0
+lt
+not
+if-goto WHILE_END0
+push local 0
+push constant 1
+add
+pop local 0
+push constant 0
+pop local 1
+label WHILE_EXP1
+push local 1
+push constant 318
+lt
+not
+if-goto WHILE_END1
+push local 1
+push constant 1
+add
+pop local 1
+goto WHILE_EXP1
+label WHILE_END1
+goto WHILE_EXP0
+label WHILE_END0
+push constant 0
+return
+function Sys.error 0
+push constant 3
+call String.new 1
+push constant 69
+call String.appendChar 2
+push constant 82
+call String.appendChar 2
+push constant 82
+call String.appendChar 2
+call Output.printString 1
+pop temp 0
+push argument 0
+call Output.printInt 1
+pop temp 0
+push constant 0
+return