LDX and LDY – Load Index Registers

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?

  • LDX loads a value into the X register
  • LDY loads 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 ModeExampleOpcodeBytesCycles
ImmediateLDX #$10$A222
Zero PageLDX $00$A623
Zero Page, YLDX $10,Y$B624
AbsoluteLDX $1234$AE34
Absolute, YLDX $1234,Y$BE34 (+1)

LDY

Addressing ModeExampleOpcodeBytesCycles
ImmediateLDY #$10$A022
Zero PageLDY $00$A423
Zero Page, XLDY $10,X$B424
AbsoluteLDY $1234$AC34
Absolute, XLDY $1234,X$BC34 (+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 JSR routines 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,Xinvalid!) — LDX only supports Zero Page, Y and Absolute, Y
  • Assuming LDX and LDY behave exactly like LDA — 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
  • LDX uses Y as an index for zero-page; LDY uses 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.


Leave a Reply

Your email address will not be published. Required fields are marked *