0

The question I need answered is, why is there a comparison failure at line 9 but not at line 8? I am new to Hack assembly language and am still learning the basics but am confused as to why the CPU Emulator would pass line 8 but not line 9.

2nd question:

How can I could I fix this to support the multiplication of negative values?

The program multiplies R0 and R1 and stores the result in R2. (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)

Mult.asm:

0   @2
1   M=0
2   @0
3   D=M
4   @8
5   D;JNE
6   @19
7   0;JMP
8   @2
9   D=M
10  @1
11  D=D+M
12  @2
13  M=D
14  @0
15  D=M-1
16  M=D
17  @8
18  D;JGT
19  @19
20  0;JMP

Mult.cmp:

|  RAM[0]  |  RAM[1]  |  RAM[2]  |
|       0  |       0  |       0  |
|       1  |       0  |       0  |
|       0  |       2  |       0  |
|       3  |       1  |       3  |
|       2  |       4  |       8  |
|       6  |       7  |      42  |
|       6  |      -7  |     -42  |
|      -6  |       7  |     -42  |

1 Answers1

1

If memory serves, what that error message is saying is that you are not passing the test in line 9 of Mult.cmp.

So, assuming that the test script in Mult.tst is correct, the problem is that your program is failing when the first number (R0) is initially negative.

In lines 14-18, you are decrementing R0 and jumping back to line 8 if it is >0. This will immediately fail because R0 starts out negative!

You need to have a little extra code at the start to check for this case and make the appropriate adjustments.

Also, a style suggestion. When referring to a memory location, use symbolic notation, ie: @R1 instead of @1, and in particular, use the (LABEL) notation to specify jump targets. This will make your code much easier to read and modify.

Good luck!

MadOverlord
  • 1,034
  • 6
  • 11
  • Thank you. How could I fix it to support multiplication of negative numbers? – David Shatzkamer Mar 25 '19 at 20:19
  • @DavidShatzkamer, it would not be appropriate for anyone to write your code for you. The whole point of Nand2Tetris is for you to learn stuff. You might want to ask other students in your class, or research "signed multiplication" on your own. – MadOverlord Mar 25 '19 at 21:31