Help with bootstrap and call Sys.init

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

Help with bootstrap and call Sys.init

cyclogenesis
I've read nearly every thread discussing the bootstrap code and for some reason I am just not getting it.
I will use this one as reference: http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/Bootstrap-Code-vs-Initialization-td4028658.html

The bootstrap code(starting at ROM[0]) should do this:

Set SP to 256
call Sys.init

Fair enough. SP is easy.
Next, let's call Sys.init. Well this is just like calling any other function right?
We have to abide by these rules:

    push return-address // (Using the label declared below)
    push LCL // Save LCL of the calling function
    push ARG // Save ARG of the calling function
    push THIS // Save THIS of the calling function
    push THAT // Save THAT of the calling function
    ARG = SP-n-5 // Reposition ARG (n = number of args.)
    LCL = SP // Reposition LCL
    goto f // Transfer control
(return-address) // Declare a label for the return-address

So the return address is generated by the assembler. In my case, it maps to 48. But we don't care. Sys.init should never return(right?).

So RAM[256]  = 48.

Is this alright?
If I run the VM emulator, the stack looks much differently than mine.
The VM emulator does not show call Sys.init, only funciton Sys.init.
So if we observe the state of RAM before funciton Sys.init runs, I notice that their RAM[SP] = 256.
That's fine. But their RAM[256] = 0 while mine is RAM[256]  = 48. That's different than what I have.

At this point, I am thinking maybe it doesn't matter and the VM emulator defaults the return address to 0.

Let's move on. Next let's push LCL, ARG, THIS, THAT, ARG=SP-n-5, LCL=SP
My RAM looks like this now:

0  261
1  261
2  256
3  0
4  0

Now goto Sys.init.
At this point, my RAM should match the VM emulator. But it does not. The VM emulator(before function Sys.init runs) is:

0  256
1  0
2  0
3  0
4  0

Apparently, call sys.init does not behave like a normal call.

So am I missing some type of initialization? I noticed in the thread I linked to, he said he initialized some values. But, the book doesn't mention that. The book says initialize SP to 256 and then call Sys.init.
Does call Sys.init not act like a normal call?

I am sure I am missing something dumb. Thanks for the advice.
Reply | Threaded
Open this post in threaded view
|

Re: Help with bootstrap and call Sys.init

WBahn
Administrator
As you noted, Sys.init() should never return, so the details of what happens should it return really don't matter and are therefore not clearly laid out. Some people take a more purist approach and make an actual call to Sys.init including having a stack frame that stores all of the state information for when it returns. Others note that this is just wasting stack space and so let the bottom stack frame actually be for what Sys.init() actually requires instead of the phantom function that called it.

The VM Emulator is emulating the VM commands and not assembler instructions, so there isn't a strong mapping between executing a command and what it appears to show happing in RAM -- especially when built-in functions are being used. It really shouldn't show physical RAM addresses at all -- but they are helpful in visualizing what is basically happening under the hood. But take them with a grain of salt.
Reply | Threaded
Open this post in threaded view
|

Re: Help with bootstrap and call Sys.init

cyclogenesis
I accidentally emailed you. Sorry about that. I was on mobile.
Uh if you don't get that, I'll repeat what I said here...
Reply | Threaded
Open this post in threaded view
|

Re: Help with bootstrap and call Sys.init

WBahn
Administrator
Go ahead and repeat it here -- I don't check the account linked to this site very often.
Reply | Threaded
Open this post in threaded view
|

Re: Help with bootstrap and call Sys.init

cyclogenesis
Yeah sorry about that.

So a normal call Sys.init should work right?
I'm getting stuck in a loop in the CPU emulator and I thought it was due to me setting up the stack frame incorrectly.

I read some other threads where the commenters were telling the individual that their mistake was jumping to Sys.init rather than calling it. So that tells me I can't simply jump to it.

Now I am thinking that my bug is due to me implementing writeCall() incorrectly.
Reply | Threaded
Open this post in threaded view
|

Re: Help with bootstrap and call Sys.init

cyclogenesis
Well I passed FibonacciElement. It appears the Sys.init call wasn't the issue.
I implemented If-Goto with Greater than instead of Not Equal and that was causing the issue.

I did this even after reading this thread http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/Why-logical-commands-return-0xFFFF-as-true-td4029419.html

Oh well. Moving on.
Reply | Threaded
Open this post in threaded view
|

Re: Help with bootstrap and call Sys.init

WBahn
Administrator
Glad to hear you figured it out. As with many things in life, we can easily get ourselves so focused on what we think the problem is that we overlook the real problem for quite some time. The good news is that every time we do it we learn a little bit more so that we don't do it as bad in the future.
Reply | Threaded
Open this post in threaded view
|

Re: Help with bootstrap and call Sys.init

cyclogenesis
Some of us(me) are slower learners than the rest
But, I think I might want to go into embedded after going through this course.
It sounds more preferable than learning REST, or node.js right now.