0

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?

committedandroider
  • 8,711
  • 14
  • 71
  • 126

1 Answers1

1

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
Chris M
  • 651
  • 6
  • 10
  • Ahh I see. I agree what you said. I tried experimenting a bit by changing ASCII .FILL x0001 to ASCII .FILL x0000 and adding a HALT(shown above). I still got the same error though. Shouldn't x0000 or decimal value 0 work as the immediate value value(falls in range -16 to 15)? – committedandroider May 02 '15 at 15:56
  • Did you see the line of code where we are LDing the ASCII value into R1, and then we're ADDing the registers together? – Chris M May 02 '15 at 16:38
  • I get that but when you use immediate mode, you don't need to load the value. The immediate value would be 0 here. So then R2 would store the contents of R2 + 0? – committedandroider May 02 '15 at 16:50
  • yeah that's right. You're using correctly in your code above. – Chris M May 02 '15 at 17:11
  • Do you get why I am still getting that error though with the changed code above? – committedandroider May 02 '15 at 17:12
  • I just assembled and ran the modified code I gave above and it didn't give me any errors. I'm not sure why exactly you're getting that error. – Chris M May 02 '15 at 17:14
  • Yeah I just assembled too and got that error. Can you explain why you need HALT before declaring your variables? From http://en.wikipedia.org/wiki/TRAP_%28processor_instruction%29 , I saw that HALT will stop the program. Why don't you want Lc3 to execute those lines? In Java doesn't a line declaring a variable like int x = 23 get executed by the machine? – committedandroider May 02 '15 at 17:21
  • The LC-3's unique in the fact that variables, strings, commands, binary values, ect... are all stored in the LC-3's memory as a hex value. That means the LC-3 has no idea what's a command and what's a variable, it's up to the user to make sure all of the data gets used correctly. If the LC-3's PC ever reaches "ASCII .FILL x0001" it will think it's actually "BR 0" which is not a correct use of the BR command. – Chris M May 02 '15 at 17:27
  • Ah I see variable declaration needs to be separated from commands with HALT. Can you take a look at this one? http://stackoverflow.com/questions/30005154/what-does-stripping-off-the-ascii-template-mean – committedandroider May 02 '15 at 17:33