0

I am getting the "In line 1, expression expected" error and I am not sure why.

I am using the CPU Emulator from nand2tetris. I tried changing line 1 to 5, but it did not solve the problem. I just do not get what is the problem in the first place.

@j
D=5;
@i;
M=1;
@5
@i
D=M
D=D-A;
@END
D;JGT
@j
@1
M=M-A
@i
@1
M=M+A
@LOOP
0;JMP

What I am trying to recreate is this loop: J=5 for(i=1; i<5; i++) { j-- }

Lili V.
  • 51
  • 1
  • 11

1 Answers1

2

There are several issues that pop out at first glance.

First, D=5 is not a valid Hack operation. If you want to load 5 into D, you have to first load it into A and then move to D:

@5
D=A

Second, ; is the jump delimiter, and should be followed by a jump condition (such as JEQ, or JMP for an unconditional jump). You have several lines (including line 1) where you have a ; but no jump condition. These should be removed.

Finally, you should probably review the book pages on the Hack assembly language syntax to make sure you are clear on how it works. In particular, in the above code, you haven't specified your jump targets like END and LOOP. This is done with the (LABEL) construct.

MadOverlord
  • 1,034
  • 6
  • 11
  • Thank you. I added the label construct, and change my code with your suggestions. But i still get the same error, but now on line 2, which is D=A... I don't understand what this error means :/ `@5 D=A D=5 @i M=1 (LOOP) @5 @i D=M D=D-A @END D;JGT @j @1 M=M-A @i @1 M=M+A @LOOP 0;JMP (END)` – Lili V. May 28 '19 at 21:17
  • I went over the code once more. I changed it. Now i get that same error but on line 13... Which is when I call @j again... Since I took off @j in the beginning, how do I refer to that same value again later? `@5 D=A @i M=1 (LOOP) @5 @i D=M D=D-A @END D;JGT @j @1 M=M-A @i @1 M=M+A @LOOP 0;JMP (END)` – Lili V. May 28 '19 at 21:20
  • First, it would be helpful if you indented your code samples with 4 spaces so that it displays as a preformatted code block – MadOverlord May 29 '19 at 22:18
  • Second, M=M-A is not a valid CPU operation in hack. You should review the slides in https://docs.wixstatic.com/ugd/56440f_65a2d8eef0ed4e0ea2471030206269b5.pdf to refresh your memory of what operations you can perform. Finally, I think you are trying to perform j = j - 1. There is a much easier way to do this. – MadOverlord May 29 '19 at 22:27
  • I am probably completely stupid but I read over and over the hack assembly language instructions, and I still can't make it work. I can't figure out how to make the loop happen. So far I have: `@5 D=A @j M=D @1 D=A @i M=D` which makes the error disappear. But that's it. (In comments, it doesn't allow me to put my code line by line, sorry). – Lili V. May 30 '19 at 03:12