Why "call" Sys.init?

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

Why "call" Sys.init?

VonLoewe
I've finished project 8 and I'm left with one doubt: what's the point of calling Sys.init as opposed to just jumping straight to it? The way I understand it, Sys.init is supposed to end in an infinite loop and never return, so there's no need to store a return address. None of the other segment pointers are initialized at this point, so there's no need to save them either. The only consequence is that our program begins running at stack address 261 instead of 256, meaning we're completely wasting 5 registers.

My code passed all the tests by simply pushing 5 zero's, because the cmp files expected the program to begin at stack 261, but other than that it's just useless code.

I haven't looked at the next chapter yet. Am I missing something or is this addressed later on?
Reply | Threaded
Open this post in threaded view
|

Re: Why "call" Sys.init?

ivant
As far as I remember you don't really need to call it and just is sufficient. I think the authors specified it that way to be more consistent with the rest of the system. And it's only in the tests. You can add a switch to your compiler, which can instead use jump.
Reply | Threaded
Open this post in threaded view
|

Re: Why "call" Sys.init?

WBahn
Administrator
In reply to this post by VonLoewe
It's a tradeoff. The advantage is making the VM implementation very simple and generic, the disadvantage is some wasted memory. Notice that you could keep the clean implementation and not waste the stack space by simply initializing the stack pointer to 251. This would initially stomp on the upper end of the static segment, but it's unlikely that it would ever get used and, even if it did, this wouldn't cause a problem.

But unless the loss of those five spaces becomes an issue, the advantages of having a clean and readily understandable VM implementation probably outweighs the utility of doing what it takes to get them back.