Expression expected when trying to access segments

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Expression expected when trying to access segments

Odzer
I did not find any previous threads on this in the forum.

I am loading the translated .asm file in the CPU Emulator but there is a "In line 14, Expression expected" error, which happens with both the BasicTest and the PointerTest. In BasicTest.asm, line 14 is @LCL, and in PointerTest it is @THAT.

It seems like it is not possible to access these, even though I also load the .tst script in the emulator. Does anyone know what may be the problem?
Reply | Threaded
Open this post in threaded view
|

Re: Expression expected when trying to access segments

ivant
The problem is probably one or two lines above the reported one. if you still cannot find it, you can post the start of the asm file here so we can take a look.
Reply | Threaded
Open this post in threaded view
|

Re: Expression expected when trying to access segments

Odzer
This post was updated on .
This is the beginning of the file, up to a few lines after line 14 where the error comes up:

@3030
D=A
@SP
M=M+1
A=M-1
M=D

@SP
M=M-1
A=M
D=M
@THIS
A=M+0
M=D

@3040
D=A
@SP
M=M+1
A=M-1
M=D

@SP
M=M-1
A=M
D=M
@THAT
A=M+1
M=D


Also the beginning of BasicTest.asm:

@10
D=A
@SP
M=M+1
A=M-1
M=D

@SP
M=M-1
A=M
D=M
@LCL
A=M+0
M=D
Reply | Threaded
Open this post in threaded view
|

Re: Expression expected when trying to access segments

ivant
A=M+0 is wrong. There is no such instruction.
Reply | Threaded
Open this post in threaded view
|

Re: Expression expected when trying to access segments

Odzer
This is just adding the index to M, which in this case is zero. But I removed it and there is still the same error.
Reply | Threaded
Open this post in threaded view
|

Re: Expression expected when trying to access segments

Odzer
I am probably doing another unsupported operation with M somewhere. I will take a better look and see if it works after that, thanks for the help.
Reply | Threaded
Open this post in threaded view
|

Re: Expression expected when trying to access segments

ivant
Even though it looks like expressions, these are actually names. A=M, A=M+1, M=M-1 are valid assembly command, while A=M+0, M=M-2, A = M (spaces are not allowed) are not. The book/course material specifies the valid ones.

The HACK assembly language is special in this sense. Most others would use commands like LDA, STA, BNZ, etc. The authors chose a more mnemonic approach, but it sometimes confuses people that it's a more complex language than it actually is.
Reply | Threaded
Open this post in threaded view
|

Re: Expression expected when trying to access segments

ivant
Here is an excellent guide to help you with other pitfalls like that.
Reply | Threaded
Open this post in threaded view
|

Re: Expression expected when trying to access segments

Odzer
Thanks, that will be useful for the remaining assignments.
Reply | Threaded
Open this post in threaded view
|

Re: Expression expected when trying to access segments

WBahn
Administrator
In reply to this post by ivant
ivant wrote
Even though it looks like expressions, these are actually names. A=M, A=M+1, M=M-1 are valid assembly command, while A=M+0, M=M-2, A = M (spaces are not allowed) are not. The book/course material specifies the valid ones.
A = M is an allowed instruction since white space is ignored. Similarly A D = M - 1 is acceptable.

Also, the author's emulator will accept both D+M and M+D as the same mnemonic, although that is not strictly compliant with their own language definition.

Somewhat oddly, their emulator strictly enforces the order of the destination registers, meaning what while AM = D+1 is acceptable, MA = D+1 is not. This is particularly unfortunate because the order defined for the assembly language, AMD, is not the same as the order of the bits in the instruction word, ADM.


Reply | Threaded
Open this post in threaded view
|

Re: Expression expected when trying to access segments

ivant
WBahn wrote
A = M is an allowed instruction since white space is ignored. Similarly A D = M - 1 is acceptable.
Thank you, I didn't know that. I don't think I like this feature. I think it obfuscates how assembly works even more.
Reply | Threaded
Open this post in threaded view
|

Re: Expression expected when trying to access segments

WBahn
Administrator
ivant wrote
WBahn wrote
A = M is an allowed instruction since white space is ignored. Similarly A D = M - 1 is acceptable.
Thank you, I didn't know that. I don't think I like this feature. I think it obfuscates how assembly works even more.
I agree. I don't mind allowing whitespace around the mnemonics, but AD and M-1 should be treated as what they are -- mnemonics.