“Expression Expected” error on line 1. How to fix

2019-08-17 18:29发布

问题:

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-- }

回答1:

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.