LC3, Store a Value of a Register to a Memory Location

lc3
David Ayala picture David Ayala · Apr 17, 2015 · Viewed 15.7k times · Source

I'm attempting to write a short LC-3 program that initializes R1=5, R2=16 and computes the sum of R1 and R2 and put the result in memory x4000. The program is supposed to start at x3000. Unfortunately, I have to write it in binary form.

This is what I have so far...

.orig x3000__________; Program starts at x3000

0101 001 001 1 00000 ;R1 <- R1 AND x0000

0001 001 001 1 00101 ;R1 <- R1 + x0005

0101 010 010 1 00000 ;R2 <- R2 AND x0000

0001 010 010 1 01000 ;R2 <- R2 + x0008

0001 010 010 1 01000 ;R2 <- R2 + x0008

0001 011 010 0 00 001 ;R3 <- R2 + R1

//This last step is where I'm struggling... I was thinking of using ST, and I figured PCOFFSET9 to be 994, but I can't represent that using 8 bits... so how else would I do this? Is my code inefficient?

0011 011

Answer

Chris M picture Chris M · Apr 23, 2015

The ST command is limited to only 511 (I believe) from its current location in memory. For something like this you will need to use the STI command (Store Indirect) The sample code below will help explain how to use STI.

.orig x3000

    AND R1, R1, #0         ; Clear R1
    ADD R1, R1, #5         ; Store 5 into R1
    AND R2, R2, #0         ; Clear R2
    ADD R2, R2, #8         ; Store 8 into R2
    ADD R3, R2, R1         ; R3 = R2 + R1
    STI R3, STORE_x4000    ; Store the value of R3 into mem[x4000]

    HALT                   ; TRAP x25 end the program

; Variables
STORE_x4000 .FILL x4000

.END 

You will need to make the appropriate conversions to binary, but if you plug the code into the LC-3 simulator it will give you the binary representation.