3

In hack assembly language in the CPU simulator Add4.asm, the test keeps failing at line 2. I have tried various forms to fix it but can't figure it out. How can I set it to 0 or if that's not the issue, how else could I fix my code? ADD4 Hack Assembly Language Help

Whenever I run my .asm file, I get a comparison failure at line 2. Please help me resolve this issue. Here is my .asm code, followed by ADD4.tst. The line numbers for Add4.asm are clearly annotated.

Add4 adds four numbers (found in RAM[0], …, RAM[3] or R0, …, R3 equivalently) and stores result in RAM[0] (a.k.a. R0). Your program must finish in 30 cycles or fewer

Add4.asm:

1. @1
2. D=M
3. @2
4. D=D+M
5. @3
6. D=D+M
7. @4
8. D=D+M
9. @5
10. D=D+M
11. @0
12. M=D

Add4.tst:

load Add4.asm,
output-file Add4.out,
compare-to Add4.cmp,
output-list RAM[0]%D2.6.2;
set RAM[0] 1,
set RAM[1] 2,
set RAM[2] 3,
set RAM[3] 4,
repeat 30
{ ticktock; }
output;
set PC 0,
set RAM[0] 0,
set RAM[1] 0,
set RAM[2] 0,
set RAM[3] 0,
repeat 30
{ ticktock; }
output;
set PC 0,
set RAM[0] -10,
set RAM[1] 5,
set RAM[2] 100,
set RAM[3] 9,
repeat 30
{ ticktock; }
output;

add4.cmp:

1.|  RAM[0]  |
2.|      10  |
3.|       0  |
4.|     104  |

2 Answers2

0

Without knowing what Add4 is supposed to do, it is hard to give you meaningful feedback. In addition to providing the Add4.cmp file as @jknotek suggests, you should also define the task it is trying to accomplish.

However, one thing that does jump out is that your test setup is initializing memory addresses 0-3, and you are accessing addresses 1-5 and storing into address 0.

Also, for clarity it is usually best to refer to memory locations 0-15 by the @Rn predefined symbols.

MadOverlord
  • 1,034
  • 6
  • 11
0

You're mistakenly using @1, @2, etc., to reference values stored in the RAM. These are actually the literal numbers 1, 2, etc., so your program will always add the same numbers, thus giving you the comparison failure.

Instead, you must prefix the numbers with an R if you're referring to the register, as follows:

@R0
D=M
@R1
D=D+M
@R2
D=D+M
@R3
D=D+M
@R0
M=D
jknotek
  • 1,778
  • 2
  • 15
  • 23