0

The question I need answered is, why is there a comparison failure at line 9 but not at line 8?

2nd question:

How 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   @R2
1   M=0
2   @R0
3   D=M
4   @STEP
5   D;JNE
6   @END
7   0;JMP
8   @R2
9   D=M
10  @R1
11  D=D+M
12  @R2
13  M=D
14  @R0
15  D=M-1
16  M=D
17  @STEP
18  D;JGT
19  @END
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  |
  • Line 8 and 9 do not involve a compare instruction. For negative numbers, a simple approach would be to make copies of the inputs that are positive, then once the multiply is done, check the two inputs to see if the product should be made negative. – rcgldr Mar 26 '19 at 00:05
  • Continuing, if both numbers are negative, you can use the positive copies. If only one of the numbers is negative, then add the negative number to the result and decrement from the positive number. – rcgldr Mar 26 '19 at 01:00

0 Answers0