nand2tetris/projects/08/FunctionCalls/NestedCall/Sys.vm

64 lines
1.3 KiB
Plaintext
Raw Normal View History

2020-05-19 12:42:52 +00:00
// Sys.vm for NestedCall test.
// Sys.init()
//
// Calls Sys.main() and stores return value in temp 1.
// Does not return. (Enters infinite loop.)
function Sys.init 0 // L1
2020-05-19 12:42:52 +00:00
push constant 4000 // test THIS and THAT context save
pop pointer 0 // L3
2020-05-19 12:42:52 +00:00
push constant 5000
pop pointer 1 // L5
2020-05-19 12:42:52 +00:00
call Sys.main 0
pop temp 1 // L7
2020-05-19 12:42:52 +00:00
label LOOP
goto LOOP // L9
2020-05-19 12:42:52 +00:00
// Sys.main()
//
// Sets locals 1, 2 and 3, leaving locals 0 and 4 unchanged to test
// default local initialization to 0. (RAM set to -1 by test setup.)
// Calls Sys.add12(123) and stores return value (135) in temp 0.
// Returns local 0 + local 1 + local 2 + local 3 + local 4 (456) to confirm
// that locals were not mangled by function call.
function Sys.main 5 // L10
2020-05-19 12:42:52 +00:00
push constant 4001
pop pointer 0
push constant 5001 // L13
2020-05-19 12:42:52 +00:00
pop pointer 1
push constant 200 // L15
2020-05-19 12:42:52 +00:00
pop local 1
push constant 40 // L17
2020-05-19 12:42:52 +00:00
pop local 2
push constant 6 // L19
2020-05-19 12:42:52 +00:00
pop local 3
push constant 123
call Sys.add12 1 // L22
2020-05-19 12:42:52 +00:00
pop temp 0
push local 0 // L24
2020-05-19 12:42:52 +00:00
push local 1
push local 2
push local 3 // L27
2020-05-19 12:42:52 +00:00
push local 4
add
add // L30
2020-05-19 12:42:52 +00:00
add
add // L32
return // L33
2020-05-19 12:42:52 +00:00
// Sys.add12(int n)
//
// Returns n+12.
function Sys.add12 0 // L34
2020-05-19 12:42:52 +00:00
push constant 4002
pop pointer 0
push constant 5002 // L37
2020-05-19 12:42:52 +00:00
pop pointer 1
push argument 0
push constant 12 // L40
2020-05-19 12:42:52 +00:00
add
return // L42