Problems, compiling, assembling and running the exercises for chapter 11
I've been working through the TECS book in my free time for a couple of months. I've really enjoyed it so far and finished the exercises for chapter 11 last week.
The first thing I wanted to do with my fancy new compiler was - of course! - to compile the output VM files down to assembly and then run those on the CPUEmulator. I've re-visited the assembler project, optimized it so that the compiled examples will fit into ROM and... I still can't get the programs to work.
The compiler works correctly. All the tests for the assembler pass, so I'm assuming I haven't broken anything whilst optimizing it, but I am totally stuck debugging it. Can anyone here tell me if:
1) It should be possible to do this at this stage in the book, which would indicate a problem with my assembler.
2) If so, does anyone have any tips on debugging the assembler beyond the supplied test scripts?
If it helps at all it looks like a call to Array.new() returns 0 in the init code for the OS, rather than some memory address on the heap. This gets saved to the pointer segment and, in turn, eventually causes the SP to be overwritten. D'oh!
Re: Problems, compiling, assembling and running the exercises for chapter 11
Yes, at the end of project 11 you should be able to compile a Jack program all the way to a working .hack file. The supplied programs should run the same way on the CPU simulator as they do on the VM simulator.
The first thing to try is loading your VM translator's .asm output into the CPU simulator and see if it crashes the same way. If it does not crash, assemble the .asm with the supplied assembler and compare its output to your assembler's output.
If loading the .asm output from the VM translator crashes, then you've got a subtle problem in either the compiler of the VM translator.
I found that it helped immensely in debugging to add source line comments in the output of my compiler and VM translator. The .asm output looks like
/// 34: let leftWall = AleftWall;
// push argument 2
[more code deleted]
// pop this 10
[more code deleted]
/// 35: let rightWall = ArightWall - 6; // -6 for ball size
(The // # comments are the vm file line numbers.)
I'll be happy to work with you via email to get this debugged. Let me know what you find out from the above tests. (More > Reply to author)