The best thing to do would be to read the LC3 ISA and pay special attention to the LDR
instruction.
Here is an example program to help get you started.
You need to know and keep track of two things: (1) the address of the array and (2) the length of the array/index as you iterate. For (1), I chose to use a filled value; for (2) the length of the array is defined in the instruction itself. I AND R1, R1, 0
to clear the register and add 10
. We could just as easily have a filled value LENGTH
equal to 10
and LD R1 LENGTH
.
The loop begins by decrementing R1
and checking to see if it is negative. If R1
is negative, then the loop exits. We decremented length first, so you can think of the "length" (value in R1
) as an index. So, if the index < 0, then the loop exits.
The LDR R0, R2, 0
instruction gets the value at the address in register R2
offset by 0
and puts it in R0
. Presumably you would want to do something with the data after this line.
Before the unconditional branch to LOOP
, we increment the address of the array to point at the next element.
.orig x3000
LD R2, ARRAY ; R2 = x6000
AND R1, R1, 0 ; R1 = 0 (clear before add)
ADD R1, R1, 10 ; R1 = 10 (length of array)
LOOP ADD R1, R1, -1 ; R1--
BRn DONE ; if R1 < 0 then halt, else...
LDR R0, R2, 0 ; R0 <-- mem[R2 + 0]
ADD R2, R2, 1 ; R2++
BR LOOP ; loop
DONE HALT ; halt
ARRAY .fill x6000
.orig x6000
.fill -20
.fill 14
.fill 7
.fill 0
.fill -3
.fill 11
.fill 9
.fill -9
.fill 2
.fill -5
.end