7.11.Hack平台上的VM实现
\(7.11.\)VM Implementation on the Hack Platform
1.Program compilation and translation
- When we translate from Jack to VM, we lost the notion of constructors, methods, because everything becomes functions.
- When we translate from VM to assembly, we lost the notion of functions, because we just have a long stream of assembly commands.
2.VM convention
In order to tranlate the VM code into assembly code, we need to comply to certain conventions:
VM programming convention: One file in any VM program is expected to be named
Main.vm
; one VM function in this file is expected to be namedmain
.VM implementation convention: When the VM implementation starts running, or is reset, it starts executing the argument-less OS function
Sys.init
.Sys.init
then callsMain.main
, and enters an infinite loop.Hardware platform convention:
- We request that the code you see here will be stored in the
target computer's instruction memory beginning in address
0
, for our Hack computer is programmed to start executing the program beginning with address0
. - So we first set
SP = 256
. By doing this, it will determined that the stack should begin in this address in the whole stream. - Then we
call Sys.init
.
- We request that the code you see here will be stored in the
target computer's instruction memory beginning in address
1 | SP = 256 |