Compile Square Game vm code to asm

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

Compile Square Game vm code to asm

ywangd
I tried to compile the supplied Square game to assembly code and run it on the CPU Emulator. However, I got an error while running the program in the CPU Emulator.

The error is due to the SP being -1 and the code tries to read RAM[SP]. So somehow the program sets SP to 0 and a subsequent stack pop operation further decrease it to -1. I did found out the error was generated in Memory.alloc. However, I do not know exactly how it happened. And it is rather difficult to debug this large asm code ... So any help or hints are appreciated!

I finished my vmtranslator (written in Python) in project 08 and passed all the tests in 07 and 08. What I have done to compile the Square game is as follows:
1. Copy all the OS vm files into the Square folder.
2. Compile the jack files to vm files with the supplied jack compiler
3. Translate all the vm files to a single asm file with my vmtranslator

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Compile Square Game vm code to asm

Dano
I am just guessing, but maybe you are not generating bootstrap code. Bootstrap code sets the stack pointer and calls Sys.init()
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Compile Square Game vm code to asm

ywangd
Thanks for the reply. But I did generate the bootstrap code to call Sys.init. Otherwise, I wouldn't make pass through project 08's FibonacciElement and StaticsTest. Any other ideas? Thanks!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Compile Square Game vm code to asm

cadet1620
Administrator
In reply to this post by ywangd
The first thing that I'd check is the size of the generated assembly code.  The first time I tried using my entire tool chain to build an app (as opposed to just a piece of test code) I found that I was not generating efficient enough code for the app to fit in 32K.

To aid debugging, I have my compiler include the source lines as comments in the generated VM code. My VM translator includes the VM source in the ASM output (Jack code ends up //// commented) and my assembler can generate a list file that displays the ROM address and source. This way I can have my ASM list file open in an editor and find my way around in the simulator.

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Compile Square Game vm code to asm

ywangd
In reply to this post by ywangd
Thanks for all the replies.

I have been able to identify the bug and fixed it. The compiled Square.asm and Pong.asm code can now be ran on the supplied CPUEmulator.

The bug is caused by the LCL segment not setting properly for functions with local variables.
In a function, I should just push N * Zeros onto the stack, where N is equal to the number of local variables. However, I pop the Zeros out after the push and assign them to the local variables. So I did something like the following code
for i=0, n-1
    push 0
    pop local i

I thought I was doing "initialize the local variables to zero" ...... But this causes SP being overlap with LCL. So they overwrite each other.

All the functions in chapter 08's tests do not use any local variables. That is why my vmtranslator passed all the tests. So I would like suggest to add at least one test for function with local variables.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Compile Square Game vm code to asm

cadet1620
Administrator
It's great that you were able to find your bug, and even better that you posted what you found!

I'll look into writing a local variable test as a part of project 8.

--Mark
Loading...