liveBook · Manning

Ldx instruction

As explained in Classic Game Programming on the NES: Make your own retro video game

The LDX instruction loads an operand into the 6502 X register.

LDX loads a source value into the 6502 X register. Like other load instructions, it begins with LD and names its target register with the final letter. The source can be a direct numerical value or a memory location. section 3.3.1

Choose the operand form by what the value represents:

  • Use an immediate value when the number is written directly in the assembly listing: `LDX #$05` places 5 in X. The `#` means use the number itself. section 3.3.1
  • Omit `#` to read a value from memory. For example, `LDX $05` loads X from zero-page location `$05`. section 3.3.1

Load X from zero-page memory

Store `$10` at `$05`, then run `LDX $05`. X receives `$10`; the operand identifies a memory location rather than the number 5. section 3.3.1

Table 3.7 The available addressing modes (view table figure)

Mode

Description

Immediate

The actual numerical value is specified immediately after the statement.

Zero-page memory

The value is obtained from a location in zero-page memory using an 8-bit offset.

Absolute memory

The value is obtained from anywhere in memory using a 16-bit memory address.

Zero-page index with X or Y

The value is obtained from a location in zero-page memory by adding an 8-bit offset address to the value in either the X or Y index registers to get the final 8-bit offset.

Absolute memory indexed with X or Y

The value is obtained from a location anywhere in memory starting at a specified 16-bit memory address and adding the value in either the X or Y index to determine the final memory address to get the value from.

Zero-page indirect indexed with Y

The value is obtained from a location anywhere in memory starting at the base address stored in the specified 2 bytes of zero-page memory, and then the value in the Y register is added to that address to get the final address.

Zero-page Indexed indirect with X

The value is obtained from a location anywhere in memory starting at the base zero-page offset and adding the value in the X register to get the location in zero-page memory where the final address is specified in 2 bytes of memory.

Use the addressing-mode table to distinguish direct values from zero-page and full-memory locations before selecting a load operand. section 3.3.1

How Classic Game Programming on the NES: Make your own retro video game covers it

How deep it goes
Explains immediate, zero-page, and memory-based loading with syntax, diagrams, and simulator results.
Examples
Demonstrates `LDX #$05` and loading X from zero-page and absolute memory locations.
What it assumes
Teaches load syntax from zero by distinguishing `#` values from memory locations.
What you can do after
Practice/perform it by loading direct values or stored memory values into the X register.
How it connects
Connects LDX with X registers, memory, zero-page memory, addressing modes, LDA, and LDY.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Game Programming on the NES: Make your own retro video game. The section links point to the parts of the book it draws on.