Search code examples
assemblycpu-registerscpu-architecturemachine-codelc3

Why am I getting an "expected register or immediate value" error?


This is my Lc3 Assembly code

.ORIG x3000
AND R0,R0, #0
AND R2,R2, #0
ADD R2,R2, #7
JSR SUB
ADD R2,R2, ASCII
ADD R0,R2,#0
TRAP x21
SUB   ADD R2,R2,#9
ADD R7,R7,#1
RET
HALT
ASCII .FILL x0000
.END

When I try to assembly the code, I got these errors enter image description here

I know that pass 1 refers to the first step in the assembly process which is to add variables and what values they are holding to the symbol table. I know that step 2 is to substitute values using the symbol table. (Using Lc3 Assembly as a reference)

Can anyone explain why the assembler is trying to substitute in the first pass and causing the error? Shouldn't pass 1 be recognizing ASCII as a variable that holds x0001 and not attempting to substitute it?


Solution

  • You're getting this error because the LC3's state machine only has two versions of the ADD command.

    • ADD R1, R2, R3
    • ADD R1, R2, #7

    You can see that we can add registers together or we can use ADD immediate. ADD immediate is where we use the last operand as a whole value between -16 and 15.


    That means to get your code segment to work you will need to load your variable into a register first.

    .ORIG x3000
    AND R0,R0, #0
    AND R2,R2, #0
    ADD R2,R2, #7
    JSR SUB
    
    
    LD R1, ASCII    ; load the value stored in ASCII into R1
    ADD R2, R2, R1  ; R2 = R2 + R1
    
    
    ADD R0,R2,#0
    TRAP x21
    SUB   ADD R2,R2,#9
    ADD R7,R7,#1
    RET
    
    HALT    ; Remember to add this at the end of your running code
            ; or else the LC3 will execute the values stored in your
            ; variables 
    
    ASCII .FILL x0001
    .END