Clarification on relationship between Bootstrap/WriteInit(), Sys.vm, and Main.vm

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Clarification on relationship between Bootstrap/WriteInit(), Sys.vm, and Main.vm

peterxu422
I wrote the writeCall() function and was about to test it on FibonacciElement. But I realized that I did not still quite understand how the bootstrap/writeInit(), Sys.vm, and Main.vm were supposed to work together. Earlier during Project7 and the beginning of Proj08, I was also confused about how to handle the programs with multiple files onto one .asm file but I thought I would deal with it later when the time came.

What confused me was that I initially thought in order to handle multiple files, you would need to jump from file to file and parse it accordingly into the .asm file as you read a .vm file and came across a function in another file. I thought this was tedious and difficult to keep track which is what confused me whether my thinking was correct. But then I think I realized what's really going on and I would like it if someone could confirm or correct me.

We parse each .vm file entirely into the .asm file until it is completely read without jumping into another .vm file in the middle. So the asm code for Sys.vm can be written below Main.vm even though we're supposed to do the bootstrap with Sys.vm first. Since each function is labeled uniquely, the hack machine will jump to the appropriate location in the asm file to execute the code. Therefore, it does not matter which order we parse the files as long as we have the bootstrap code in the beginning, which will then likely call Main.main (or in the case of FibonacciElement Main.fibonacci) and then the Hack machine will jump to the correct location in the asm file to execute Main.main/fibonacci.

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?
Reply | Threaded
Open this post in threaded view
|

Re: Clarification on relationship between Bootstrap/WriteInit(), Sys.vm, and Main.vm

cadet1620
Administrator
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