From fd8e3bee660f22032b51bc12452353b141e125be Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Wed, 17 Jan 2018 16:44:59 +0100 Subject: checkpoint - finished Array - finished Sys - revised Memory --- projects/12/ArrayTest/Array.jack | 26 +++++++ projects/12/ArrayTest/Array.vm | 12 ++++ projects/12/ArrayTest/ArrayTest.out | 2 + projects/12/ArrayTest/Main.vm | 131 ++++++++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 projects/12/ArrayTest/Array.jack create mode 100644 projects/12/ArrayTest/Array.vm create mode 100644 projects/12/ArrayTest/ArrayTest.out create mode 100644 projects/12/ArrayTest/Main.vm (limited to 'projects/12/ArrayTest') 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 -- cgit v1.2.3