Mult.asm keeps failing

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

Mult.asm keeps failing

Connor_Walsh
Hi!

I have what I believed to be a working asm file for mult. If I manually assign numbers to R0 and R1, I always get the correct answer in R2, but I can never pass the Mult.tst

At the start of my code I set the values of the R0 R1 and R2 to zero to avoid using old values left in ram.

It would always fail when trying to multiply 3 and 1. However if I manually assign 3 and 1 it works perfectly fine. I tried replacing the Mult.tst file with the one in this thread: http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/Mult-tst-does-not-match-Mult-cmp-td4027015.html

And it would pass the 3x1 part fine, but then fail just after it, specifically on the line Set PC 0

Any help or suggestions is appreciated!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mult.asm keeps failing

cadet1620
Administrator
One thing that can cause the Mult test to fail is if your code is slower than expected.

The tests run a fixed number of instructions which is assumed to be enough to get to the infinite loop at the end of your code. Make sure your code ends with
    (HALT)
    @HALT
    0;JMP

You might try increasing the number in the various REPEAT commands in the Mult.tst file.
    set PC 0,
    set RAM[0] 3,   // Set test arguments
    set RAM[1] 1,
    set RAM[2] -1;  // Ensure that program initialized product to 0
    repeat 120 {    // ***** try increasing this number *****
      ticktock;
    }

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

Re: Mult.asm keeps failing

Connor_Walsh
Still getting a failure after trying to increase that number, and my code does have the infinite loop at the end. If somebody would like to look at my code to see if I've made any fatal errors I could email it or temporarily post it here and delete it afterwords

Thanks for the help!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mult.asm keeps failing

cadet1620
Administrator
Connor_Walsh wrote
If somebody would like to look at my code to see if I've made any fatal errors I could email it or temporarily post it here and delete it afterwords
It would be easier to just email it to me directly. Feel free to do so.

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

Re: Mult.asm keeps failing

trevoski
I have the exact same issue, can you shed some light on what the problem was or perhaps I could just e-mail you my code for you to look over.

I realize this thread was posted almost a year ago, I'd really appreciate some help!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mult.asm keeps failing

cadet1620
Administrator
trevoski wrote
I have the exact same issue, can you shed some light on what the problem was or perhaps I could just e-mail you my code for you to look over.

I realize this thread was posted almost a year ago, I'd really appreciate some help!
I don't remember what this particular problem turned out to be.  Feel free to send me your code.

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

Re: Mult.asm keeps failing

nicolaf
sorry to re-up this post after more than one year.

I'm experiencing the exact same problem: everything seems to be working fine in manual simulation and also going through the test, step by step. But at every "set PC 0" in the test script, the CPU emulator prompts a "Comparison Failure" error. Not sure what the solution to this is. Is there anyone who solved this and could share some tips? Thanks!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mult.asm keeps failing

cadet1620
Administrator
At every "set PC 0" in the test script, the CPU emulator prompts a "Comparison Failure" error.
The line highlighted in a test script when it miscompares is the line that follows the command that miscompared, so the actual failure is that your multiply code computed the wrong value.

The most common problem that causes multiply code to work when you step through it, but fail when run by the test, is to forget to initialize the product to 0 before the addition loop.

The CPU emulator starts with RAM initialized to 0, so the first time you test it manually the product starts at 0. The next time you run the program the product starts with whatever was left in RAM from the earlier run.

The test script specifically initials R2 to non-zero to catch this error.

If you can't find your bug, feel free to email me your code.

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

Re: Mult.asm keeps failing

nicolaf
That was the problem indeed. Thank you very much for the useful tip!
Loading...