lc3 LDR instruction and the value stored

user4046073 picture user4046073 · Sep 16, 2014 · Viewed 24.5k times · Source

I can't figure out why After instruction “LDR R3, R0, 2” is executed, the value stored in R3 is x370C. what does 2 stands for in this instruction? It doesn't look like an immidiate value. I understand that R0 contains x370C at this point. Can someone please help? Many thanks!

.ORIG X3700
 LEA R0, A
 LDI R2, C 
 LDR R3, R0, 2 
 AND R1, R1, #0 
 IN
 ST R0, D 
 JSR  F 
 HALT
 F LD  R1, B
 ADD R1, R1, #1
 BRp F 
 RET

 A .FILL X1234
 B .FILL X370B
 C .FILL X370C
 D .BLKW 2
 E .STRINGZ "ABCD"
 G .FILL X1234
 .END

Answer

gustavodidomenico picture gustavodidomenico · Sep 16, 2014

The second parameter is the offset of the base address that will be loaded.

I started to take some pictures to post here and make a good explanation but I found an interesting lecture video that will explain much better than words and will save a lot of time.

LC3 Instructions - LD, LDR, LDI, LEA

The video is explaining the differences between the load instructions for the LC3, highlighting the differences between them.

In your example:

You have your data:

A .FILL X1234
B .FILL X370B
C .FILL X370C

Running your code:

LEA R0, A      -- R0 has the address of A
LDI R2, C      -- R2 has value of which address C has
LDR R3, R0, 2  -- R3 has the value of C 
               -- because R0 has the address of A + 2 positions = C