Note the directory structure might seem strange it was to make it easier to submit for the coursera course.
go version `1.23.2`
go run . <filename>
push segment index
pop segment index
local
— The local segment of the current function.argument
— The argument segment of the current function.this
— Points to the base address of the current object (used with objects).that
— Points to the base address of the current array (used with arrays).constant
— A constant value, e.g.,push constant 5
.static
— A segment that represents variables shared across different functions.pointer
— A segment to manipulatethis
andthat
.temp
— Temporary storage between RAM addresses 5–12.
- Every
push
command adds a value to the stack and increments theSP
pointer. - Every
pop
command removes a value from the stack and decrements theSP
pointer.
Returns integer:
add
,sub
,neg
Returns boolean:
eq
,gt
,lt
(comparison returns true/false)and
,or
,not
(logical operations)
label labelName
— Declares a label to be referenced later.goto labelName
— Unconditional jump to a label.if-goto labelName
— Conditional jump to a label if the topmost stack value is non-zero.
function functionName nArgs
— Declares a function withnArgs
local variables.call functionName nArgs
— Calls a function and passesnArgs
arguments.return
— Returns from the current function.
- When calling a function, a return address is pushed onto the stack to ensure the program flow returns to the correct location after the function completes.
The SP
predefined symbol points to the memory address just following the topmost stack value in the host RAM.
The LCL
, ARG
, THIS
, and THAT
symbols point to the base addresses of the virtual segments local
, argument
, this
, and that
respectively, in the currently running VM function.
These registers are general-purpose and can be used for any temporary storage.
Each static variable i
in file xxx.vm
is translated into the assembly symbol Xxx.i
.
- The VM-to-Hack translation often requires bootstrap code to initialize the
SP
and set up the environment before the VM code runs.
Address Range | Description |
---|---|
0 | SP (stack pointer) |
1 | LCL (local segment base) |
2 | ARG (argument segment base) |
3 | THIS (this segment base) |
4 | THAT (that segment base) |
5–12 | Temp segment |
13–15 | General-purpose registers |
16–255 | Static variables |
256–2047 | Stack |