JackCompiler -> Vm2Asm -> Assembler

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

JackCompiler -> Vm2Asm -> Assembler

Diogo
Hello there,

I am a computer science student, going through this amazing book on my own. I am in need of some assistance. Thanks in advance for any one willing to help =)

I've got my Assembler and Vm-2-Asm translator working (wrote those in plain C, both passed all the tests). Following the suggestion given in page 174, I am interested in compiling the supplied Square project from Chapter 9 using the JackCompiler, then further translate the compiled VM code into binary code using my own translator and assembler.

So, this is what I've tried:

0. I put all files from tools/OS into the Square folder (as suggested in page 197);

1. Compiled the Square folder with the JackCompiler;

2. Tested the compiled project at the VmEmulator and all went well;

3. Translated all .vm files into one single Square.asm file using my translator;

4. Tested the Square.asm file with the CPUEmulator and the error "In line 0, ')' expected" came up. Cheking the code there was no reason for ')' at the first line... got confused there!;

5. Just for the sake of it, I went ahead and ran Square.asm through my Assembler to Square.hack;

6.  Tested the Square.hack file with the CPUEmulator and the error "Program too large" came up. Well, I then realized that my Vm2Asm translator did translate all the functions from the OS files as well as the Square projects files so the generated .asm file has near 56K lines (lots of those are comments). The generated .hack file has near 47K lines.

So... that raised some questions in my mind.

q0. Did I do something wrong? In general, I mean...

q1. Is the error "In line 0, ')' expected" related to the fact that the file is way too large?

q2. Can it be that something is wrong with my assembler and/or Vm2Asm translator (please, recall that both passed all supplied tests)?

q3. Or is it something wrong with the whole process described above?

Once again, thanks in advance! By the way, this book is really, really amazing!!!

Best regards,
Diogo
Reply | Threaded
Open this post in threaded view
|

Re: JackCompiler -> Vm2Asm -> Assembler

cadet1620
Administrator
It sounds like you are doing everything right.  You will feel amazing when you finally get your whole tool chain working.

I haven't hear about the "error in line 0". I would not be surprised if it's caused by the .asm file being too long. As an experiment you could try shortening it with a text editor and see what happens.

Sounds like your compiler and VM translator are OK.  One of the things that you've seen is that the code your VM translator generates is much larger than that generated by the supplied VM translator. This happens to most people. When I encountered it, I thought about what VM commands generated huge amounts of code -- it's no secret, it's call and return -- and how I might be able to write an assembly language helper routine that would shorten them. The idea is to load arguments in R13-15 and jump to a routine that your VM translator wrote as part of the bootstrap. You can do the same thing for 'gt', 'eq' and 'lt' which are also a bi big, but there are not so many of them that it makes such a big improvement.

This post shows the number of various VM commands in the OS .vm files.

One thing that I just remembered was a student who was writing in C and ended up with the \0 on the end of some of his strings written to the output file. The Java based tools were not too happy about that!

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

Re: JackCompiler -> Vm2Asm -> Assembler

Diogo
Mark, thanks for your time and attention.

The "In line 0, ')' expected" message went away when I re-translated the project. I went back to the previous version looking for the reasons. Well, I found out that I had somehow accidentally typed a '@' on a label, probably when browsing around with my text editor.

So, I wanted to share this: if you load your translated ASM files in the CPUEmulator and get the error "In line 0, ')' expected", look for your labels, you might have something like:

@symbol
...
(@symbol) <- wrong


Also, your tips on how to start thinking about optimization were great. When I finally realized the whole idea I was like "how come I didn't think about this before?" =)

I followed your benchmark numbers on the post. Well... from 42K HACK instructions now I can translate the whole OS folder to 22544 instructions. Needless to say, my codewriter module became quite big, dealing with lots of specific sequences of VM instructions and all. Optimizing is fun, but I'll leave that for now...

Finally, I was able to tool chain the whole thing... and yes, it felt amazing!

Thanks once again Mark,
Diogo

ps.: although posted on the chapter 9 folder, this was really about chapter 8, sorry about that.
Reply | Threaded
Open this post in threaded view
|

Re: JackCompiler -> Vm2Asm -> Assembler

Andrew
In case anyone else searches for "In line 0, ')' expected"

I found that it had to do with an illegal character being used for labels. I had allowed '\' to be used as a character in my labels, and it was giving me this error when trying to load the .asm file. Note that this was nowhere near line 0 at all, so I spent a lot of time staring at a very simple line that was perfectly correct.

Message: the message doesn't necessarily indicate an error at line 0, so consider other possibilities. I spent a long time learning that, so I wanted to share in case anyone else has that problem.
Reply | Threaded
Open this post in threaded view
|

Re: JackCompiler -> Vm2Asm -> Assembler

invisible_kid_
I'm getting
*(-15) - Problems while executing the translated BasicLoop.asm program - In line 0, ')' expected
*(-15) - Problems while executing the translated FibonacciSeries.asm program - In line 0, ')' expected
when I submit my VMTranslator for project 8 on Coursera, but I experience no problems when I use it to generate .asm files on my computer and run the generated code on the CPU emulator. Both .asms I generate pass the test scripts when I run them locally (in fact, my VMTranslator generates passing code for all the Project 8 local tests). I've been going over my asm code but I'm at a loss. I have attached my asm code for BasicLoop and FibonacciSeries, perhaps someone can have a look.

Incidently, my translator is coded in Python2.7 and I run it on windows.
BasicLoop.asm
FibonacciSeries.asm
Reply | Threaded
Open this post in threaded view
|

Re: JackCompiler -> Vm2Asm -> Assembler

cadet1620
Administrator
Two related things are probably occurring.

The Coursera grader runs on Linux, so all file paths use '/' as a directory separator rather than Window's '\'.

The grader also runs your translator using paths that are multiple directories deep. For instance
  VMTranslator foo/bar/file.vm
or
  VMTranslator foo/bar/directory

If you are expecting just "file.vm" or "directory", then you are likely to be generating labels that include '/' characters, which freak out the Assembler.
  (foo/bar/file.vm$LOOP_START)

The same problem can occur if you have written you own code to separate the path from the final file or directory name and have hard coded the '\' on your Windows system, then it will not find the '/' separators when running on on Linux.

You will want to use the library supplied path manipulation routines to do directory splitting and combining, because they are platform aware and will automatically use the correct '/' or '\'.

For Python, these routines are in the os.path module. See os.path.join and the various .split functions.
Reply | Threaded
Open this post in threaded view
|

Re: JackCompiler -> Vm2Asm -> Assembler

invisible_kid_
Took a couple of read throughs to see where I was confusing myself, but now I think I get my problem!

Seems that, as my VMTranslator takes file/directory arguments in the Linux style (forward slashes) without complaint, I had naively assumed all would be well and good on the grader side. In somewhat of an oversight, I had been placing my translator prog in the same directory as the single .vm file examples and running it. If I try to process them through a directory tree, I indeed get labels such as:

//C_LABEL LOOP_START
($./BasicLoop/BasicLoop.vm$$LOOP_START)

Which of course, freak out my local CPU emulator. I think this also explains why I only passed module 7 with a score of 80- I recall the grader output complaining about some errant forward slashes, but I kind of just shrugged and moved on. Let that be a warning to others! Fortunately, I think it will just require a couple of lines of code needed to remedy the issue. Funnily enough, the directory handling branch of my Translator has no such problems. Live and learn!
Reply | Threaded
Open this post in threaded view
|

Re: JackCompiler -> Vm2Asm -> Assembler

Moshe
This post was updated on .
In reply to this post by Diogo
When I try to do the same thing, compile the jack programs in projects 09 directory and then use my VM translator to asm and try to run it on the CPU emulator I am getting this error on all the programs. "At line # destination is M but A=24577 is an illegal memory address" and this after it seems like the program was running in an endless loop.
Is anything wrong with my translator?  it passed all the test.
Also all this jack programs do not create a Sys.init function that we were asked to call at the bootstrap when compiled.