Memory & Data

LD, ST & LEA

PC-relative addressing and loading effective addresses.

PC-Relative Addressing

The most common way to access memory in LC-3 is PC-relative: the instruction contains a 9-bit signed offset that's added to the current PC.

LD — Load

LD DR, LABEL loads the value at the labeled memory address into DR.
.ORIG x3000
LD R0, VALUE   ; R0 = memory[address of VALUE] = 42
HALT
VALUE .FILL #42
.END

ST — Store

ST SR, LABEL writes the value in SR to the labeled memory address.
.ORIG x3000
AND R0, R0, #0
ADD R0, R0, #7
ST R0, RESULT    ; memory[RESULT] = 7
HALT
RESULT .FILL #0
.END

LEA — Load Effective Address

LEA DR, LABEL computes the address of the label and stores it in DR. It does NOT access memory — it just calculates the address.

This is crucial for working with strings and arrays:

.ORIG x3000
LEA R0, HELLO    ; R0 = address of HELLO string
PUTS             ; Print the string at address in R0
HALT
HELLO .STRINGZ "Hello, LC-3!"
.END

LEA does not read from memory. LD R0, X loads the value stored at X. LEA R0, X loads the address of X into R0.

When to Use Each

LD — Use when you want the value at a label. Loading a number, a constant, or a single data value. Example: LD R0, COUNT where COUNT .FILL #10 ST — Use when you want to write a register's value to a label. Example: ST R0, RESULT to save your answer LEA — Use when you need the address of something, not its value. Essential for:
  • Strings (PUTS needs the address): LEA R0, MSG then PUTS
  • Setting up a pointer to walk through an array: LEA R1, ARRAY
  • Any time you'll use LDR/STR afterward (you need a base address first)
  • Exercise

    Write a program that loads two values from memory, adds them, stores the result back to memory, and prints "OK". Use labels DATA1, DATA2, and RESULT.

    main.asm
    1
    R0
    x00000
    R1
    x00000
    R2
    x00000
    R3
    x00000
    R4
    x00000
    R5
    x00000
    R6
    x00000
    R7
    x00000
    PCx3000
    CC
    NZP
    Quiz

    What is the difference between `LD R0, X` and `LEA R0, X`?