0

This is based off this question LEA instruction

Here is the code segment I have a question about

.ORIG X3700
 LEA R0, A
 .....
  A .FILL X1234

@Paul R, the answer responder, said that "The origin of the code is x3700, and you have 12 instructions, so the address of A will be x3700 + x0C = x370C. As you guessed, LEA R0,A loads the address of A into R0, so R0 will contain x370C after that first instruction has been executed."

I agree with the first part of what Paul said, his reasoning for why the address of A is x370C. That makes sense.

I am confused about the next part, that "LEA R0, A loads the address of A into R0". This is the slide my reference has on the LEA instruction. Lc3 LEA, 5-23 enter image description here

Unlike the ADD and AND instructions, the LEA instruction has only one mode.(reference specifies both modes for ADD and AND.

From this diagram, the second part of LEA, A should be PCoffset 9. However the value of A is 4660(in decimal) from ,A .FILL X1234, which is beyond the PCoffset 9 range, which is -256 to 255).
Can anyone explain what is going on? Am I using the wrong diagram as a reference? Is there another LEA mode?

Community
  • 1
  • 1
committedandroider
  • 8,711
  • 14
  • 71
  • 126

1 Answers1

1

Anytime you see a PCoffset as an opcode operand

LEA R2, A    ; Loads the memory location of A into R2
             ; LEA, DR, PCoffset9

It's telling you that when your code is assembled it doesn't actually place the label 'A' into your LEA command.


Take the following code:

.ORIG X3700     ; Not a code line in the simulator, only used by the assembler
LEA R0, A       ; Line 1, starting at x3700
HALT            ; Line 2
A .FILL X1234   ; Line 3, located at x3702
.END            ; Not a code line in the simulator, only used by the assembler

Now in the simulator the LEA line actually looks like this:

1110 000 000000001    ; Opcode 1110 is LEA
                      ; 000 is our register R0
                      ; 000000001 gives us how many memory locations away A is

And that's what the offset means. It asks the question "How many blocks of memory is A away from me?" and if our variable 'A' is too many blocks away then we will get an error because we cannot represent that value in the offset.

Chris M
  • 651
  • 6
  • 10
  • Same idea with LD R0, A right? You don't specify the PCOffet9, just what variable you want to load? – committedandroider May 03 '15 at 05:30
  • Would the difference between LDI R1, A and LD R1, A be that LD just straights load up the value of A into R1 while LDI treats A as an address and loads the value at that address into R1? – committedandroider May 03 '15 at 21:00
  • http://stackoverflow.com/questions/29898909/how-to-load-value-of-a-memory-address-in-a-register-in-asm-on-the-lc3 explains more about LDI – Chris M May 03 '15 at 21:27
  • Thanks I thought this link was more clear - http://stackoverflow.com/questions/25868391/lc3-ldr-instruction-and-the-value-stored – committedandroider May 03 '15 at 21:54