Bootstrap

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

Bootstrap

HighSchoolerWhoAsksHowTooMuch
I just finished project 8.

Why is it that we set the stack pointer to 261 instead of 256?

Thank you!

Adam
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap

WBahn
Administrator
Where are you seeing that you set the stack pointer to 261? I don't recall seeing that in the text (I only have 1st Ed.)

There are two ways to go about the bootstrap code. The first is to be very minimalist at a high level and set the stack pointer to 256 and then execute a call to Sys.init. But this invokes all of the instructions and time overhead of executing a function call. The other way is to be minimalist at a low level and recognize that you know what the first call is going to be, so figure out what the machine state will be after that call takes place and just put the machine into that state. After that call happens, the stack pointer will be at 261 since Sys.init takes no arguments, thus only the five basic items are pushed onto the stack.
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap

HighSchoolerWhoAsksHowTooMuch
Are you saying that because we know that LCL, ARG, THIS, and THAT will all be 0, we can just put the stack pointer at 261 instead of putting it at 256 and pushing 0 to the stack 5 times?

Below is my bootstrap code:

@261
D=A
@SP
M=D
@Sys.init
0;JMP
(MyFileName.Sys.Sys.init$ret0)


Should I be setting ARG and LCL or can I wait for that to happen at the next function call?

Also, should my VM translator ensure that for a 1 file program, the "Sys.init" function is called first even if it is not the first function?
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap

WBahn
Administrator
Remember that the values of LCL, ARG, THIS, and THAT that are stored in the stack frame are used to restore those pointers when the function called returns. However, the Sys.init() function should never return. Once Main.main() returns control back to Sys.init(), it should call Sys.halt() -- or just go into an infinite loop directly. Therefore it doesn't matter what values exist in its stack frame because they will never get used.

I'm not sure what you mean by what your VM translator program should do with a 1-file program. Unless your file is names Sys.vm, there will not be a Sys.init() to call!

The project allows you to build your translator incrementally so that you can test it before you implement the bootstrap code. This is possible because the test scripts were written specifically under the assumption that your translator isn't yet doing the initialization, so the necessary actions are taken care of by the script itself.

The only time that being the first function matters is when there is no bootstrap code. At that point, the first thing in the .asm file is the definition for the first function. But the hack doesn't know a function from anything else, it just starts executing the code it sees. This is really akin to invoking undefined behavior, but we know it's going to do it, so we leverage that knowledge to make our implementation and testing strategy easier.


Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap

HighSchoolerWhoAsksHowTooMuch
I get it! Thank you!
Reply | Threaded
Open this post in threaded view
|

Re: Bootstrap

pogoda
you should always initialize sp to 256. the problem wuth your code is that instead of calling Sys.init you jump to it - and I believe the tests expect you to call it, which will push 5 garbage values to the stack - positioning sp at 261. it happened to me too, very frustrating