aboutsummaryrefslogtreecommitdiff
path: root/projects/12/ArrayTest
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2018-01-17 16:44:59 +0100
committerYuchen Pei <me@ypei.me>2018-01-17 16:44:59 +0100
commitfd8e3bee660f22032b51bc12452353b141e125be (patch)
treed9f406d85fdac99844770818eeadc8954e387627 /projects/12/ArrayTest
parentc3598a26ab4ba0562ede74b37dc2a7aa6907640a (diff)
checkpoint
- finished Array - finished Sys - revised Memory
Diffstat (limited to 'projects/12/ArrayTest')
-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
4 files changed, 171 insertions, 0 deletions
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