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

怎甘沉沦 提交于 2019-12-25 01:24:26

问题


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.



来源:https://stackoverflow.com/questions/56336813/expression-expected-error-on-line-1-how-to-fix-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!