Call and Return as global functions (and LT,GT,and EQ too)

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

Call and Return as global functions (and LT,GT,and EQ too)

virote328
After going through the project 8 I noticed that the call and return codes added a LOT to the total generated code.  So My biggest wonder was whether or not "call" and "return" could be used as functions.

I had a feeling that there would be and after much thought I was able to implement such a routine.

Below are some hints as to how I was able to accomplish this feat.

Call_function
The call_function needs to know three things.  The return address, the number of arguments, and the line number of the function that will be called.  every time the vm command requests a call, I store the above 3 things in a temporary variable, then jump to the call_function.  The call_function will do stuff depending on what is stored in the three temporary variables.


return_function
The return function is much easier then the Call_function.  Since everything you need to know is saved in the stack, you can simply make an unconditional jump to the return_function.


Also, as a side comment...it may be well worth the effort to make GT,LT, and EQ into functions as well.


==========

I'm quite sure many people had to go through something like this therefore I didn't go into much detail.  But in the event that this method is ground breaking....I'd be more than happy to elaborate.
Reply | Threaded
Open this post in threaded view
|

Re: Call and Return as global functions (and LT,GT,and EQ too)

ybakos
Very interesting!
Reply | Threaded
Open this post in threaded view
|

Re: Call and Return as global functions (and LT,GT,and EQ too)

krakerjak
In reply to this post by virote328
I agree, this is a good idea.

Cadet1620 made a post here
http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/Generated-code-size-tp1201814p3242894.html

Analyzing frequency of all VM commands in the Hack OS
If you are trying to minimize your code, your best choices are the high usage ones that your implementation will translate using lots of hack assembly instructions.
Reply | Threaded
Open this post in threaded view
|

Re: Call and Return as global functions (and LT,GT,and EQ too)

krakerjak
I plan to optimize my translations once I get past the compiler chapters.
Im currently almost done chapter 9.

I tested translating the supplied OS files and then assembling.
To my shock I have around 54k lines of hack assembly, and about 50k of hack instructions.
As it seems many people found out, the 32k instructions limit for the ROM requires you to do some optimization if you want to be able to test your work in the simulator....and in my case I will have to since as it stands I con't even fit the OS into ROM.
Reply | Threaded
Open this post in threaded view
|

Re: Call and Return as global functions (and LT,GT,and EQ too)

virote328
Yeah, it DOES eat up a lot of ROM space....which makes me think that everyone needs to optimize if they want to continue beyond chapter 9.


I recently optimized the call, return, and lt,gt,and eq, and even the push pop routines.  The "square" game took up 31564 lines of hack and became about 26000. after optimizing it.

After seeing the thread from cadet, it seems like there is more that can be done to optimize.

perhaps the next step is to somehow "merge" all the push/pops into one big thing
Reply | Threaded
Open this post in threaded view
|

Re: Call and Return as global functions (and LT,GT,and EQ too)

cadet1620
Administrator
virote328 wrote
Yeah, it DOES eat up a lot of ROM space....which makes me think that everyone needs to optimize if they want to continue beyond chapter 9.
You don't need to optimize your VM code generation to complete the book because your compiler and OS are tested using the VM emulator.

That said, however, it is an amazing feeling to compile your game and the OS you wrote, run it through your VM translator and assembler, and see it run on the CPU emulator!

--Mark