Let’s continue the blog series with a combined post about the 6502’s LDX and LDY instructions.
The sibling instructions of LDA.
In the 6502 instruction set, LDX and LDY are the counterparts to LDA. While LDA loads a value into the accumulator, LDX and LDY load values into the index registers X and Y, respectively.
These index registers are used for addressing, iteration, and temporary storage in a wide variety of programming scenarios. Understanding how to load them efficiently is crucial for tight, readable, and performant 6502 code.
What Do LDX and LDY Do?
LDXloads a value into the X registerLDYloads a value into the Y register
Both instructions support several addressing modes and affect two status flags: Zero (Z) and Negative (N).
Supported Addressing Modes
LDX
| Addressing Mode | Example | Opcode | Bytes | Cycles |
|---|---|---|---|---|
| Immediate | LDX #$10 | $A2 | 2 | 2 |
| Zero Page | LDX $00 | $A6 | 2 | 3 |
| Zero Page, Y | LDX $10,Y | $B6 | 2 | 4 |
| Absolute | LDX $1234 | $AE | 3 | 4 |
| Absolute, Y | LDX $1234,Y | $BE | 3 | 4 (+1) |
LDY
| Addressing Mode | Example | Opcode | Bytes | Cycles |
|---|---|---|---|---|
| Immediate | LDY #$10 | $A0 | 2 | 2 |
| Zero Page | LDY $00 | $A4 | 2 | 3 |
| Zero Page, X | LDY $10,X | $B4 | 2 | 4 |
| Absolute | LDY $1234 | $AC | 3 | 4 |
| Absolute, X | LDY $1234,X | $BC | 3 | 4 (+1) |
Note: Indexed Absolute modes can take one extra cycle if a page boundary is crossed.
Flags Affected
Just like LDA, both LDX and LDY affect:
- Zero Flag (Z): Set if the loaded value is
0 - Negative Flag (N): Set if the high bit (bit 7) is set
Other flags remain unchanged.
Practical Examples
Load X Register
LDX #$0A ; Load constant 10 into X
LDX $20 ; Load value from zero page address $0020
LDX $1000,Y ; Load value from $1000 + Y
Load Y Register
LDY #$FF ; Load constant 255 into Y
LDY $D000,X ; Load value from $D000 + X
These instructions are often used for:
- Setting up offsets for indexed memory access
- Loop counters and decrement operations
- Calling
JSRroutines that expect parameters in X or Y - Configuring sprite data or screen memory locations
Typical Usage Patterns
LDX #$00
LOOP:
LDA message,X
BEQ done
STA $0400,X
INX
JMP LOOP
done:
In this example, LDX is used to walk through a string and copy it to screen memory. A classic use case for the X register.
Common Pitfalls
- Using the wrong indexed mode (e.g.,
LDX $10,X→ invalid!) — LDX only supports Zero Page, Y and Absolute, Y - Assuming
LDXandLDYbehave exactly likeLDA— their indexed modes differ - Forgetting that setting flags can affect branching after a load
Summary
While LDA is used to manipulate data, LDX and LDY are your tools for managing control and structure. They’re essential when looping through data, accessing arrays, or working with hardware registers.
Key takeaways:
- Both instructions support immediate, zero page, and absolute modes
LDXuses Y as an index for zero-page;LDYuses X- Flags Z and N are always updated based on the loaded value
Together, LDA, LDX, and LDY form the foundation of memory access on the 6502. Master these three, and you’re well on your way to becoming fluent in low-level 6502 programming.