This commit is contained in:
QkoSad
2025-07-16 13:00:37 +03:00
commit 7894b48931
806 changed files with 162532 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
// 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) {
if(size < 0){
do Sys.error(7);
}
return Memory.alloc(size);
}
/** Disposes this array. */
method void dispose() {
do Memory.deAlloc(this);
return;
}
}
+23
View File
@@ -0,0 +1,23 @@
function Array.new 0
push argument 0
push constant 0
lt
not
if-goto L0
push constant 7
call Sys.error 1
pop temp 0
goto L1
label L0
label L1
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
+2
View File
@@ -0,0 +1,2 @@
|RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]|
| 222 | 122 | 100 | 10 |
View File
+15
View File
@@ -0,0 +1,15 @@
// 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/ArrayTest/ArrayTest.tst
load,
output-file ArrayTest.out,
compare-to ArrayTest.cmp,
output-list RAM[8000]%D2.6.1 RAM[8001]%D2.6.1 RAM[8002]%D2.6.1 RAM[8003]%D2.6.1;
repeat 1000000 {
vmstep;
}
output;
+40
View File
@@ -0,0 +1,40 @@
// 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/ArrayTest/Main.jack
/** Test program for the OS Array class. */
class Main {
/** Performs several Array manipulations. */
function void main() {
var Array r; // stores test results
var Array a, b, c;
let r = 8000;
let a = Array.new(3);
let a[2] = 222;
let r[0] = a[2]; // RAM[8000] = 222
let b = Array.new(3);
let b[1] = a[2] - 100;
let r[1] = b[1]; // RAM[8001] = 122
let c = Array.new(500);
let c[499] = a[2] - b[1];
let r[2] = c[499]; // RAM[8002] = 100
do a.dispose();
do b.dispose();
let b = Array.new(3);
let b[0] = c[499] - 90;
let r[3] = b[0]; // RAM[8003] = 10
do c.dispose();
do b.dispose();
return;
}
}
+131
View File
@@ -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 local 1
push constant 2
push constant 222
pop temp 1
add
pop pointer 1
push temp 1
pop that 0
push local 0
push constant 0
push local 1
push constant 2
add
pop pointer 1
push that 0
pop temp 1
add
pop pointer 1
push temp 1
pop that 0
push constant 3
call Array.new 1
pop local 2
push local 2
push constant 1
push local 1
push constant 2
add
pop pointer 1
push that 0
push constant 100
sub
pop temp 1
add
pop pointer 1
push temp 1
pop that 0
push local 0
push constant 1
push local 2
push constant 1
add
pop pointer 1
push that 0
pop temp 1
add
pop pointer 1
push temp 1
pop that 0
push constant 500
call Array.new 1
pop local 3
push local 3
push constant 499
push local 1
push constant 2
add
pop pointer 1
push that 0
push local 2
push constant 1
add
pop pointer 1
push that 0
sub
pop temp 1
add
pop pointer 1
push temp 1
pop that 0
push local 0
push constant 2
push local 3
push constant 499
add
pop pointer 1
push that 0
pop temp 1
add
pop pointer 1
push temp 1
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 local 2
push constant 0
push local 3
push constant 499
add
pop pointer 1
push that 0
push constant 90
sub
pop temp 1
add
pop pointer 1
push temp 1
pop that 0
push local 0
push constant 3
push local 2
push constant 0
add
pop pointer 1
push that 0
pop temp 1
add
pop pointer 1
push temp 1
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