Mult.asm keeps failing

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

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
|

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
|

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
|

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
|

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
|

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
|

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
|

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
|

Re: Mult.asm keeps failing

nicolaf
That was the problem indeed. Thank you very much for the useful tip!
Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm keeps failing

Wenhao Yang
In reply to this post by cadet1620
i don't know why we have to set RAM[2] to be 0, we can use variable product to hold the computation value, then assign it to RAM[2], but if so, it alters a  compile error
Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm keeps failing

cadet1620
Administrator
Wenhao Yang wrote
i don't know why we have to set RAM[2] to be 0, we can use variable product to hold the computation value, then assign it to RAM[2], but if so, it alters a  compile error
"compile" error?

Do you mean comparison error? If so, are you initializing your 'product' variable to 0?

If not a comparison error, wheat is the error message?

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

Re: Mult.asm keeps failing

Wenhao Yang
oh, it shows comparison error, we have set variable product to 0
Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm keeps failing

cadet1620
Administrator
Alas, we all commit typos.  I notice "wheat error" in my post...

Please email me your Mult.asm and I'll be happy to take a look at it for you.

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

Re: Mult.asm keeps failing

Mrinal
Even after initializing the RAM to Zero. Mine does not pass.
Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm keeps failing

cadet1620
Administrator
Mrinal wrote
Even after initializing the RAM to Zero. Mine does not pass.
Another common error is that you summation loop is written with the exit test at the bottom of the loop. This causes the results to be incorrect when multiplying by 0.

You can look at the Output and Compare views after the failure and see what your value and the expected value are.

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

Re: Mult.asm keeps failing

Mrinal
Thank you Mark. I was not taking the Mulitply by 0 into account
Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm keeps failing

SRIRAM
In reply to this post by cadet1620
my code keeps on failing
@R2
        M=0

       
        @R0
        D=M

        @END
        D;JEQ

        @R1
        D=M

        @END
        D;JEQ

       
        D=0

(LOOP)
       
        @R2
        D=M

        @R0
        D=D+M

       
        @R2
        M=D


        @R1
        M=M-1
        D=M

        @LOOP
        D;JNE

(END)
        @END
        0;JMP

Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm keeps failing

WBahn
Administrator
How is it failing?

What is it doing, specifically, that it shouldn't be doing?

What is it not doing, specifically, that it should be doing?

What have you done to try to track down the problem?