Void return values from OS functions

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

Void return values from OS functions

JavaJack
Per page 235:
"When translating a do sub statement where sub is a void method or function, the caller of the corresponding VM function must pop (and ignore) the returned value"

I'm just beginning to piece together enough functions to compile Seven. Main.main calls Output.printInt, which returns void and should thus pop the 0 afterwards.

I, as a human, know it returns void by looking at page 192 of the book. But how could my program know this? Is it expected that every student rig up "include files" for all the OS functions?
Reply | Threaded
Open this post in threaded view
|

Re: Void return values from OS functions

cadet1620
Administrator
All functions, regardless of declared return type, push exactly one word on the stack before returning.

Convention is for void functions to push 0. Other functions push an integer or a pointer to a class object.

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

Re: Void return values from OS functions

JavaJack
Still, you'd need to know when (and when not) to pop the voids' zeroes to avoid leaving garbage on the stack, unless I'm misunderstanding something.
Reply | Threaded
Open this post in threaded view
|

Re: Void return values from OS functions

cadet1620
Administrator
Every function always pushes one word onto the stack before returning.
Every call always pops one word.

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

Re: Void return values from OS functions

JavaJack
Sounds like I'm trying to outsmart myself, then. I will ponder on this.
Reply | Threaded
Open this post in threaded view
|

Re: Void return values from OS functions

JavaJack
My compiler now outputs working .vms for all the supplied samples from Chapter 11, so I guess I eventually got it right :)
Reply | Threaded
Open this post in threaded view
|

Re: Void return values from OS functions

cadet1620
Administrator
JavaJack wrote
My compiler now outputs working .vms for all the supplied samples from Chapter 11, so I guess I eventually got it right :)
Congratulations!

Have you tried compiling and running your game? It was an awesome moment when I could compile and VM translate my game and run it in the CPU simulator.

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

Re: Void return values from OS functions

JavaJack
I haven't written games/demos of my own (yet - I was saving that until after I complete chapter 12), but Pong was playable. (Actually it was TOO fast in the VMEmulator, until I added some delays in the .jack code)
Reply | Threaded
Open this post in threaded view
|

Re: Void return values from OS functions

Albert G
Just curious - how did you implement those delays in the Jack code ?

I have the same issue: the Pong game quits before I even get a chance to move the bat! I guess our machines are just  a bit too powerful. :-)

Albert
Reply | Threaded
Open this post in threaded view
|

Re: Void return values from OS functions

cadet1620
Administrator
Albert G wrote
Just curious - how did you implement those delays in the Jack code ?

I have the same issue: the Pong game quits before I even get a chance to move the bat! I guess our machines are just  a bit too powerful. :-)

Albert
Write a function
    function void delay (int ms)
that has nested while loops. The outer loop decrements 'ms'. The inner loop repeats a constant number of times. The constant depends on the speed of your system.

--Mark