Bootstrap init
Pointers
0
256
SP
1
-1
LCL
2
-2
ARG
3
-3
THIS
4
-4
THAT
Stack
256
???
←SP
This is how my bootstrap code initializes the pointers before calling Sys.init().
Setting the LCL, ARG, THIS and THAT pointers to known illegal values helps identify when a pointer is used before it is initialized.
(If you are running the NestedCall test without bootstrap code, you will not see this state.)
Entry to Sys.init()
Pointers
0
261
SP
1
261
LCL
2
256
ARG
3
-3
THIS
4
-4
THAT
Stack
256
*
Return IP
←ARG
257
-1
Saved LCL
258
-2
Saved ARG
Sys.init
259
-3
Saved THIS
frame
260
-4
Saved THAT
261
???
←LCL, SP
This is how NestedCall.tst initializes the pointers and stack. This is what RAM looks like after my bootstrap calls Sys.init().
(If your VM translation includes the bootstrap, the -1 through -4 values may be different if your bootstrap initializes them.)
Entry to Sys.main()
Pointers
0
266
SP
1
266
LCL
2
261
ARG
3
4000
THIS
4
5000
THAT
Stack
256
*
Return IP
257
-1
Saved LCL
258
-2
Saved ARG
Sys.init
259
-3
Saved THIS
frame
260
-4
Saved THAT
261
*
Return IP
←ARG
262
261
Saved LCL
263
256
Saved ARG
Sys.main
264
4000
Saved THIS
frame
265
5000
Saved THAT
266
???
←LCL, SP
After Sys.main() prologue
Pointers
0
271
SP
1
266
LCL
2
261
ARG
3
4000
THIS
4
5000
THAT
Stack
256
*
Return IP
257
-1
Saved LCL
258
-2
Saved ARG
Sys.init
259
-3
Saved THIS
frame
260
-4
Saved THAT
261
*
Return IP
←ARG
262
261
Saved LCL
263
256
Saved ARG
Sys.main
264
4000
Saved THIS
frame
265
5000
Saved THAT
266
0
local 0
←LCL
267
0
local 1
268
0
local 2
269
0
local 3
270
0
local 4
271
???
←SP
The function prologue is the assembly language code generated for the “function” VM command.
Entry to Sys.add12(123)
Pointers
0
277
SP
1
277
LCL
2
271
ARG
3
4001
THIS
4
5001
THAT
Stack
256
*
Return IP
257
-1
Saved LCL
258
-2
Saved ARG
Sys.init
259
-3
Saved THIS
frame
260
-4
Saved THAT
261
*
Return IP
262
261
Saved LCL
263
256
Saved ARG
Sys.main
264
4000
Saved THIS
frame
265
5000
Saved THAT
266
0
local 0
267
200
local 1
268
40
local 2
269
6
local 3
270
0
local 4
271
123
argument 0
←ARG
272
*
Return IP
273
266
Saved LCL
Sys.add12
274
261
Saved ARG
frame
275
4001
Saved THIS
276
5001
Saved THAT
277
???
←LCL, SP
Before Sys.add12() return
Pointers
0
278
SP
1
277
LCL
2
271
ARG
3
4002
THIS
4
5002
THAT
Stack
256
*
Return IP
257
-1
Saved LCL
258
-2
Saved ARG
Sys.init
259
-3
Saved THIS
frame
260
-4
Saved THAT
261
*
Return IP
262
261
Saved LCL
263
256
Saved ARG
Sys.main
264
4000
Saved THIS
frame
265
5000
Saved THAT
266
0
local 0
267
200
local 1
268
40
local 2
269
6
local 3
270
0
local 4
271
123
argument 0
←ARG
272
*
Return IP
273
266
Saved LCL
Sys.add12
274
261
Saved ARG
frame
275
4001
Saved THIS
276
5001
Saved THAT
277
135
Return value
←LCL
278
???
←SP
After Sys.add12() return
Pointers
0
272
SP
1
266
LCL
2
261
ARG
3
4001
THIS
4
5001
THAT
Stack
256
*
Return IP
257
-1
Saved LCL
258
-2
Saved ARG
Sys.init
259
-3
Saved THIS
frame
260
-4
Saved THAT
261
*
Return IP
←ARG
262
261
Saved LCL
263
256
Saved ARG
Sys.main
264
4000
Saved THIS
frame
265
5000
Saved THAT
266
0
local 0
←LCL
267
200
local 1
268
40
local 2
269
6
local 3
270
0
local 4
271
135
Return value
272
???
←SP
Before Sys.main() return
Pointers
0
272
SP
1
266
LCL
2
261
ARG
3
4001
THIS
4
5001
THAT
Stack
256
*
Return IP
257
-1
Saved LCL
258
-2
Saved ARG
Sys.init
259
-3
Saved THIS
frame
260
-4
Saved THAT
261
*
Return IP
←ARG
262
261
Saved LCL
263
256
Saved ARG
Sys.main
264
4000
Saved THIS
frame
265
5000
Saved THAT
266
0
local 0
←LCL
267
200
local 1
268
40
local 2
269
6
local 3
270
0
local 4
271
246
Return value
272
???
←SP
After Sys.main() return
Pointers
0
262
SP
1
261
LCL
2
256
ARG
3
4000
THIS
4
5000
THAT
Stack
256
*
Return IP
←ARG
257
-1
Saved LCL
258
-2
Saved ARG
Sys.init
259
-3
Saved THIS
frame
260
-4
Saved THAT
261
246
Return value
←LCL
262
???
←SP
In Sys.init() halt loop
Pointers
0
261
SP
1
261
LCL
2
256
ARG
3
4000
THIS
4
5000
THAT
Stack
256
*
Return IP
←ARG
257
-1
Saved LCL
258
-2
Saved ARG
Sys.init
259
-3
Saved THIS
frame
260
-4
Saved THAT
261
???
←LCL, SP