liveBook · Manning

Zero-page indirect-X addressing mode

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

Zero-page indirect-X addressing mode adds the X register to a zero-page offset before reading the resulting 16-bit indirect address.

Zero-page indirect-X addressing mode uses X to choose where a two-byte address is stored in zero-page memory. Add X to the instruction’s 8-bit zero-page offset first; read the low byte and then the high byte from that resulting zero-page location; combine them into an absolute address; then use that address for the load, store, or arithmetic operation. section 3.3.1 section 4.1.1

Its assembly form places the zero-page address and X inside rounded brackets: `LDA (<zero-page address>,X)`. The brackets mean that zero-page memory supplies an address rather than the final data location. Unlike indirect addressing indexed by Y, X changes the location of the pointer before the two-byte address is read. section 3.3.1 section 4.1.1

Resolve `($02,X)` in small steps:

  • Load X with `$03`. Adding it to the base offset `$02` selects zero-page location `$05`. section 3.3.1
  • Read the adjacent bytes at `$05` and `$06`. With `$00` first and `$03` second, they form address `$0300` in least-significant-byte-first order. section 3.3.1
  • Use `$0300` as the final location. `LDA ($02,X)` loads its value into A; `STA ($02,X)` writes A’s value there. section 3.3.1 section 3.3.2

Load, store, and add through the selected pointer

Set zero-page `$05` to `$00` and `$06` to `$03`, so they point to `$0300`. With `LDX #$03`, the operand `($02,X)` selects that pointer. If `$0300` contains `$EE`, `LDA ($02,X)` leaves A as `$EE`. With A already `$EE`, `STA ($02,X)` writes `$EE` to `$0300`. The same address lookup works for `ADC ($01,X)`: when X makes the pointer begin at `$02`, a `$12` at `$0300` is added to A’s `$10`, producing `$22`. section 3.3.1 section 3.3.2 section 4.1.1

Figure 3.7 Loading data into a register

Follow the diagram to separate the two address calculations: base offset plus X chooses the zero-page pointer, then that pointer chooses the final memory location. section 3.3.1

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

How deep it goes
Works through pointer-byte setup, X offset calculation, final-address resolution, and register results without covering later game-code use.
Examples
Demonstrates pointers at `$05`–`$06` to `$0300`, X-driven selection from `$02`, and an ADC result of `$22`.
What it assumes
Builds from zero-page offsets, X, registers, and two-byte addresses while explicitly showing least-significant-byte-first pointer storage.
What you can do after
Apply it with LDA, STA, and ADC by laying out a two-byte zero-page pointer and selecting it through X.
How it connects
Connects zero-page memory, X register, absolute memory, LDA, STA, ADC, and indirect indexed addressing.

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.