Bootstrap code and Sys.init

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

Bootstrap code and Sys.init

Adi
Hi,
I have a question regarding the function write_init.
According to the book this function "writes assembly code that effects the VM initialization, also called bootstrap code. This code must be placed at the begining of the output file." In page 165 in the book it is explained that the meaning of this function is to set SP to 256 and to call Sys.init:
@256
D=A
@0
M=D

I don't understand where Sys.init is defined. I followed the implementation of write_call described in page 163 and my problem it that when i call "call Sys.init" and get to the line goto f, my program gets into infinite loop since Sys.init gets some address and then it jump to this address until it reaches to goto f again.

I would really appreciate your help on the subject and an explanation on when and how i call Sys.init and where it is defined.
Thanks in advanced Adi
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap code and Sys.init

cadet1620
Administrator
Sys.init is defined in System.vm.

You need to copy all the *.vm files from nand2tetris/tools/OS directory into your program's directory so that your VM translator can process all of them into a single .asm.

--Mark
Adi
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap code and Sys.init

Adi
Hi Mark thanks for the replay.

I an not sure i understand you, do i need to compile those .VM files separately? Or just to put them in the project directory?

Thank again
Adi
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap code and Sys.init

cadet1620
Administrator
Sorry for the confusion.  For the tests in project 8 that require it, there are special Sys.vm files:
-rw----        606   1-02-2005   8:01p   projects/08/FunctionCalls/FibonacciElement/Sys.vm
-rw----        621   1-02-2005   8:02p   projects/08/FunctionCalls/StaticsTest/Sys.vm
Your VM translator needs to process all .vm files in the directory into a single .asm file. In the case of FibonacciElement, Main.vm and Sys.vm get translated into FibonacciElement.asm so the @Sys.init/0;JMP generated for the bootstrap call will reference the (Sys.init) label generated for the "function Sys.init 0" in Sys.vm.

--Mark
Adi
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap code and Sys.init

Adi
Ok but what do i do with tests that don't have those .VM file.  For example both Basic Loop and Fibonacci Series works when i remove the call to Sys.init but fail when i leave it.
Just to make sure all the test in Function Calls should work without me having to do anything. For some reason i get a failure message in those 3 files.

Thank you so much for you help
Adi
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap code and Sys.init

cadet1620
Administrator
I added a switch to my VM translator that optionally excludes the call to Sys.init so that I could use it for the earlier tests that did not have Sys.vm.
[c:/TECS/projects/08/FunctionCalls/FibonacciElement]
% hvm
usage: hvm [options] sourceFile.vm
    sourceFile may be a directory in which case all vm files in
    the directory will be processed to sourceFile.asm
options:
    -o[-] turns on[off] optimization.  (Default ON)
    -u[-] turns on[off] unused function deletion.  (Default ON)
    -u+   lists unused functions.
    -d    writes VM commands as comments in .asm file.
    -n    does not write Sys.init call in the bootstrap.
    -v[-] turns on[off] progress messages.  (Default ON)
[c:/TECS/projects/08/FunctionCalls/FibonacciElement]
%

--Mark

Adi
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap code and Sys.init

Adi
Thanks
Adi
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap code and Sys.init

Adi
Mark hi,

Just to make sure, you are suggesting to prompt the user if he wants to call Sys.init or not?
Or i should check if the directory contains a file named Sys.vm and than to call Sys.init?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap code and Sys.init

cadet1620
Administrator
My VM translator runs from the command line so I need to specifically include the -n option for the early test programs that shouldn't have the Sys.init call.

I like your idea of making it automatic based on the presence of Sys.vm. I would suggest a warning message to inform the user that the call has been skipped.

--Mark
Adi
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap code and Sys.init

Adi
Thanks