peterxu422 wrote
...
I also initially thought when I read the API for writeInit(), which said that the function must set SP=256 and call Sys.init, that I would have to parse the Sys.vm file first. But with the explanation I just gave, it makes sense to me that that is not necessary and that as long as I have 'call Sys.init' in the beginning, I can have my translation of Sys.vm anywhere in my asm file.
Is this correct?
P.S. Why must Sys.vm have an infinite loop below its call to Main.fibonacci or Main.main?
Yes that is correct. The assembly code for Sys.vm can be anywhere in the asm file. The bootstrap is the only thing that must be at a fixed location in ROM, and it must be at address 0 since that is the address where the CPU starts execution after reset.
The real Sys.vm (in the tools/os directory) Calls Sys.halt() after calling Main.main(). Sys.halt() contains an infinite loop. There are some test specific versions of Sys.vm like that in FibonacciElement that call a function directly.
In either case, what normally happens is that Main.main() (or Main.fibonacci() ) simply returns when it is done and it is the OS's responsibility to halt processing. Since there is no HALT instruction in the Hack computer, an infinite loop serves that purpose.
Note that I found it handy to add a '-n' command line option to my VM translator that skips the bootstrap call to Sys.init(). This lets me translate and test the earlier programs that don't need (in fact will fail with) the bootstrap code.
--Mark