Skip to content

Commit 71367d3

Browse files
committed
complete project 12
1 parent 9641e8f commit 71367d3

38 files changed

+7774
-124
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*.dat
44
.vscode
55
node_modules/
6+
tools/
67
package-lock.json
78
projects/10/**/*.js
89
projects/11/**/*.js

projects/12/Array.jack

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ class Array {
1515

1616
/** Constructs a new Array of the given size. */
1717
function Array new(int size) {
18+
return Memory.alloc(size);
1819
}
1920

2021
/** Disposes this array. */
2122
method void dispose() {
23+
do Memory.deAlloc(this);
24+
return;
2225
}
23-
}
26+
}

projects/12/ArrayTest/Array.jack

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// This file is part of www.nand2tetris.org
2+
// and the book "The Elements of Computing Systems"
3+
// by Nisan and Schocken, MIT Press.
4+
// File name: projects/12/Array.jack
5+
6+
/**
7+
* Represents an array.
8+
* In the Jack language, arrays are instances of the Array class.
9+
* Once declared, the array entries can be accessed using the usual
10+
* syntax arr[i]. Each array entry can hold a primitive data type as
11+
* well as any object type. Different array entries can have different
12+
* data types.
13+
*/
14+
class Array {
15+
16+
/** Constructs a new Array of the given size. */
17+
function Array new(int size) {
18+
return Memory.alloc(size);
19+
}
20+
21+
/** Disposes this array. */
22+
method void dispose() {
23+
do Memory.deAlloc(this);
24+
return;
25+
}
26+
}

projects/12/ArrayTest/Array.vm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function Array.new 0
2+
push argument 0
3+
call Memory.alloc 1
4+
return
5+
function Array.dispose 0
6+
push argument 0
7+
pop pointer 0
8+
push pointer 0
9+
call Memory.deAlloc 1
10+
pop temp 0
11+
push constant 0
12+
return

projects/12/ArrayTest/ArrayTest.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
|RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]|
2+
| 222 | 122 | 100 | 10 |

projects/12/ArrayTest/Main.vm

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
function Main.main 4
2+
push constant 8000
3+
pop local 0
4+
push constant 3
5+
call Array.new 1
6+
pop local 1
7+
push constant 2
8+
push local 1
9+
add
10+
push constant 222
11+
pop temp 0
12+
pop pointer 1
13+
push temp 0
14+
pop that 0
15+
push constant 0
16+
push local 0
17+
add
18+
push constant 2
19+
push local 1
20+
add
21+
pop pointer 1
22+
push that 0
23+
pop temp 0
24+
pop pointer 1
25+
push temp 0
26+
pop that 0
27+
push constant 3
28+
call Array.new 1
29+
pop local 2
30+
push constant 1
31+
push local 2
32+
add
33+
push constant 2
34+
push local 1
35+
add
36+
pop pointer 1
37+
push that 0
38+
push constant 100
39+
sub
40+
pop temp 0
41+
pop pointer 1
42+
push temp 0
43+
pop that 0
44+
push constant 1
45+
push local 0
46+
add
47+
push constant 1
48+
push local 2
49+
add
50+
pop pointer 1
51+
push that 0
52+
pop temp 0
53+
pop pointer 1
54+
push temp 0
55+
pop that 0
56+
push constant 500
57+
call Array.new 1
58+
pop local 3
59+
push constant 499
60+
push local 3
61+
add
62+
push constant 2
63+
push local 1
64+
add
65+
pop pointer 1
66+
push that 0
67+
push constant 1
68+
push local 2
69+
add
70+
pop pointer 1
71+
push that 0
72+
sub
73+
pop temp 0
74+
pop pointer 1
75+
push temp 0
76+
pop that 0
77+
push constant 2
78+
push local 0
79+
add
80+
push constant 499
81+
push local 3
82+
add
83+
pop pointer 1
84+
push that 0
85+
pop temp 0
86+
pop pointer 1
87+
push temp 0
88+
pop that 0
89+
push local 1
90+
call Array.dispose 1
91+
pop temp 0
92+
push local 2
93+
call Array.dispose 1
94+
pop temp 0
95+
push constant 3
96+
call Array.new 1
97+
pop local 2
98+
push constant 0
99+
push local 2
100+
add
101+
push constant 499
102+
push local 3
103+
add
104+
pop pointer 1
105+
push that 0
106+
push constant 90
107+
sub
108+
pop temp 0
109+
pop pointer 1
110+
push temp 0
111+
pop that 0
112+
push constant 3
113+
push local 0
114+
add
115+
push constant 0
116+
push local 2
117+
add
118+
pop pointer 1
119+
push that 0
120+
pop temp 0
121+
pop pointer 1
122+
push temp 0
123+
pop that 0
124+
push local 3
125+
call Array.dispose 1
126+
pop temp 0
127+
push local 2
128+
call Array.dispose 1
129+
pop temp 0
130+
push constant 0
131+
return

projects/12/Keyboard.jack

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
* A library for handling user input from the keyboard.
88
*/
99
class Keyboard {
10+
static int kbdAddr;
11+
static Array keymap;
1012

1113
/** Initializes the keyboard. */
1214
function void init() {
13-
}
15+
let kbdAddr = 24576;
16+
return;
17+
}
1418

1519
/**
1620
* Returns the character of the currently pressed key on the keyboard;
@@ -33,6 +37,7 @@ class Keyboard {
3337
* F1 - F12 = 141 - 152
3438
*/
3539
function char keyPressed() {
40+
return Memory.peek(kbdAddr);
3641
}
3742

3843
/**
@@ -41,6 +46,20 @@ class Keyboard {
4146
* of the pressed key.
4247
*/
4348
function char readChar() {
49+
var char c;
50+
51+
while (Keyboard.keyPressed() = 0) {
52+
// do nothing
53+
}
54+
55+
let c = Keyboard.keyPressed();
56+
57+
while (~(Keyboard.keyPressed() = 0)) {
58+
// do nothing
59+
}
60+
61+
do Output.printChar(c);
62+
return c;
4463
}
4564

4665
/**
@@ -49,7 +68,30 @@ class Keyboard {
4968
* and returns its value. Also handles user backspaces.
5069
*/
5170
function String readLine(String message) {
52-
}
71+
var String s;
72+
var char c;
73+
let s = String.new(100);
74+
75+
do Output.printString(message);
76+
77+
while (true) {
78+
let c = Keyboard.readChar();
79+
// newline
80+
if (c = String.newLine()) {
81+
do Output.println();
82+
return s;
83+
} else {
84+
// backspace
85+
if (c = String.backSpace()) {
86+
do s.eraseLastChar();
87+
do Output.backSpace();
88+
} else {
89+
let s = s.appendChar(c);
90+
}
91+
}
92+
}
93+
return s;
94+
}
5395

5496
/**
5597
* Displays the message on the screen, reads from the keyboard the entered
@@ -58,5 +100,42 @@ class Keyboard {
58100
* entered text is detected). Also handles user backspaces.
59101
*/
60102
function int readInt(String message) {
103+
var int num, one;
104+
var char c;
105+
var boolean first;
106+
107+
let num = 0;
108+
let one = 1;
109+
let first = true;
110+
111+
do Output.printString(message);
112+
113+
while (true) {
114+
let c = Keyboard.readChar();
115+
116+
// - symbol
117+
if (first & (c = 45)) {
118+
let one = -1;
119+
}
120+
let first = false;
121+
122+
// newline
123+
if (c = String.newLine()) {
124+
do Output.println();
125+
return num * one;
126+
} else {
127+
// 0 ~ 9
128+
if ((c > 47) & (c < 58)) {
129+
let num = num * 10 + (c - 48);
130+
}
131+
132+
// backspace
133+
if (c = String.backSpace()) {
134+
let num = num / 10;
135+
do Output.backSpace();
136+
}
137+
}
138+
}
139+
return num * one;
61140
}
62-
}
141+
}

0 commit comments

Comments
 (0)