How does LEA instruction store address of A?

2019-01-29 12:49发布

问题:

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

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?

回答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.