Skip to content

Commit d97ab8e

Browse files
author
Axel Kern
committed
Fix regression in OS VM files
The 2.5.7 source code archive contained outdated VM files of Memory.vm and Sys.vm. The official software suite 2.6 zip archive already contained an updated version of the OS VM files. The source code archive on the nand2tetris website was however never updated accordingly. The OS update addressed the following issues: 1. Sys.error included a call to String.new which could cause itself a runtime error via Memory.alloc, resulting in recursive calls to Sys.error and ultimately crashing with a stack overflow. 2. Memory.vm contains now a better allocation algorithm with defragmentation of adjacent blocks
1 parent 52d5666 commit d97ab8e

File tree

2 files changed

+118
-22
lines changed

2 files changed

+118
-22
lines changed

n2t-software-suite/OS/Memory.vm

Lines changed: 113 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ push temp 0
3737
pop that 0
3838
push constant 0
3939
return
40-
function Memory.alloc 1
40+
function Memory.alloc 2
4141
push argument 0
42-
push constant 1
42+
push constant 0
4343
lt
4444
if-goto IF_TRUE0
4545
goto IF_FALSE0
@@ -48,38 +48,135 @@ push constant 5
4848
call Sys.error 1
4949
pop temp 0
5050
label IF_FALSE0
51+
push argument 0
52+
push constant 0
53+
eq
54+
if-goto IF_TRUE1
55+
goto IF_FALSE1
56+
label IF_TRUE1
57+
push constant 1
58+
pop argument 0
59+
label IF_FALSE1
5160
push constant 2048
5261
pop local 0
5362
label WHILE_EXP0
63+
push local 0
64+
push constant 16383
65+
lt
5466
push constant 0
5567
push local 0
5668
add
5769
pop pointer 1
5870
push that 0
5971
push argument 0
6072
lt
73+
and
6174
not
6275
if-goto WHILE_END0
6376
push constant 1
6477
push local 0
6578
add
6679
pop pointer 1
6780
push that 0
81+
pop local 1
82+
push constant 0
83+
push local 0
84+
add
85+
pop pointer 1
86+
push that 0
87+
push constant 0
88+
eq
89+
push local 1
90+
push constant 16382
91+
gt
92+
or
93+
push constant 0
94+
push local 1
95+
add
96+
pop pointer 1
97+
push that 0
98+
push constant 0
99+
eq
100+
or
101+
if-goto IF_TRUE2
102+
goto IF_FALSE2
103+
label IF_TRUE2
104+
push local 1
68105
pop local 0
106+
goto IF_END2
107+
label IF_FALSE2
108+
push constant 0
109+
push local 0
110+
add
111+
push constant 1
112+
push local 0
113+
add
114+
pop pointer 1
115+
push that 0
116+
push local 0
117+
sub
118+
push constant 0
119+
push local 1
120+
add
121+
pop pointer 1
122+
push that 0
123+
add
124+
pop temp 0
125+
pop pointer 1
126+
push temp 0
127+
pop that 0
128+
push constant 1
129+
push local 1
130+
add
131+
pop pointer 1
132+
push that 0
133+
push local 1
134+
push constant 2
135+
add
136+
eq
137+
if-goto IF_TRUE3
138+
goto IF_FALSE3
139+
label IF_TRUE3
140+
push constant 1
141+
push local 0
142+
add
143+
push local 0
144+
push constant 2
145+
add
146+
pop temp 0
147+
pop pointer 1
148+
push temp 0
149+
pop that 0
150+
goto IF_END3
151+
label IF_FALSE3
152+
push constant 1
153+
push local 0
154+
add
155+
push constant 1
156+
push local 1
157+
add
158+
pop pointer 1
159+
push that 0
160+
pop temp 0
161+
pop pointer 1
162+
push temp 0
163+
pop that 0
164+
label IF_END3
165+
label IF_END2
69166
goto WHILE_EXP0
70167
label WHILE_END0
71168
push local 0
72169
push argument 0
73170
add
74171
push constant 16379
75172
gt
76-
if-goto IF_TRUE1
77-
goto IF_FALSE1
78-
label IF_TRUE1
173+
if-goto IF_TRUE4
174+
goto IF_FALSE4
175+
label IF_TRUE4
79176
push constant 6
80177
call Sys.error 1
81178
pop temp 0
82-
label IF_FALSE1
179+
label IF_FALSE4
83180
push constant 0
84181
push local 0
85182
add
@@ -89,9 +186,9 @@ push argument 0
89186
push constant 2
90187
add
91188
gt
92-
if-goto IF_TRUE2
93-
goto IF_FALSE2
94-
label IF_TRUE2
189+
if-goto IF_TRUE5
190+
goto IF_FALSE5
191+
label IF_TRUE5
95192
push argument 0
96193
push constant 2
97194
add
@@ -119,9 +216,9 @@ push local 0
119216
push constant 2
120217
add
121218
eq
122-
if-goto IF_TRUE3
123-
goto IF_FALSE3
124-
label IF_TRUE3
219+
if-goto IF_TRUE6
220+
goto IF_FALSE6
221+
label IF_TRUE6
125222
push argument 0
126223
push constant 3
127224
add
@@ -136,8 +233,8 @@ pop temp 0
136233
pop pointer 1
137234
push temp 0
138235
pop that 0
139-
goto IF_END3
140-
label IF_FALSE3
236+
goto IF_END6
237+
label IF_FALSE6
141238
push argument 0
142239
push constant 3
143240
add
@@ -152,7 +249,7 @@ pop temp 0
152249
pop pointer 1
153250
push temp 0
154251
pop that 0
155-
label IF_END3
252+
label IF_END6
156253
push constant 1
157254
push local 0
158255
add
@@ -165,7 +262,7 @@ pop temp 0
165262
pop pointer 1
166263
push temp 0
167264
pop that 0
168-
label IF_FALSE2
265+
label IF_FALSE5
169266
push constant 0
170267
push local 0
171268
add

n2t-software-suite/OS/Sys.vm

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,14 @@ label WHILE_END0
6565
push constant 0
6666
return
6767
function Sys.error 0
68-
push constant 3
69-
call String.new 1
7068
push constant 69
71-
call String.appendChar 2
69+
call Output.printChar 1
70+
pop temp 0
7271
push constant 82
73-
call String.appendChar 2
72+
call Output.printChar 1
73+
pop temp 0
7474
push constant 82
75-
call String.appendChar 2
76-
call Output.printString 1
75+
call Output.printChar 1
7776
pop temp 0
7877
push argument 0
7978
call Output.printInt 1

0 commit comments

Comments
 (0)