Skip to content
systemcoding.dev
Assembly

6502 Addressing Modes: Zero Page, Indexed and Indirect

Learn the 6502 addressing modes: immediate, zero page, absolute, indexed with X and Y, indirect indexed and indexed indirect, with traced examples.

5 min read
Summarize in AI
Flat illustration of an assembly listing with addresses and mnemonics, with the Arm, GNU and Intel logos.

A 6502 instruction has two halves: what to do and where the data is. LDA is the first half. The addressing mode is the second, and there are thirteen of them. Programmers coming from high-level languages find them the strangest part of the chip, because they combine operations that a compiler would treat as separate steps: add an offset to a base, read a pointer from memory, load through it. Once you see each as a small recipe, they stop being a list to memorize. This post covers every mode with the notation of the ca65 assembler, a traced example for each family, and the two indirect modes that people confuse for years.

Immediate, zero page and absolute

The three simplest modes name the operand directly:

ModeSyntaxOperandBytesCycles for LDA
ImmediateLDA #$05the value 522
Zero pageLDA $05the byte at $000523
AbsoluteLDA $1234the byte at $123434

The cycle counts come from the MOS Technology programming manual. They explain the zero page. Zero-page instructions have a one-byte address, so they are one byte shorter and one cycle faster, which is why a 6502 program keeps loop counters, pointers and frequently used variables in $00 to $FF. Treat the zero page as the 6502's register file: there are only three registers, so 256 fast bytes are the closest thing to more of them.

modes1.sasm
        LDA #$2A            ; A = $2A
        STA $10             ; zero page: [$0010] = $2A
        STA $0300           ; absolute:  [$0300] = $2A
        LDA $10             ; A = [$0010] = $2A

The assembler picks the shorter form on its own: STA $10 assembles to 85 10 and STA $0010 to the same thing, while STA $0300 assembles to 8D 00 03. Sometimes you want to force the long form, and ca65 offers a: and z: prefixes for that. Mixing up # and the bare form is the commonest beginner error: LDA $2A loads from address $2A, not the value.

Indexed: base plus X or Y

Add ,X or ,Y to a zero-page or absolute address and the CPU adds the register to the address before reading. That gives array access.

ModeSyntaxEffective address
Zero page, XLDA $10,X($10 + X) AND $FF
Absolute, XLDA $1234,X$1234 + X
Absolute, YLDA $1234,Y$1234 + Y
Zero page, YLDX $10,Y($10 + Y) AND $FF (only LDX and STX)

The zero-page version wraps inside page zero. LDA $FF,X with X = 2 reads address $0001, not $0101, because the addition happens on one byte. The absolute version carries into the high byte, so LDA $12FF,X with X = 1 reads $1300. That crossing costs one extra cycle on loads, which cycle-counting code takes into account.

A table copied into reverse order shows X and Y indexing two places at once. X counts up through the source while Y counts down through the destination:

reverse.sasm
        .org $0200
start:  LDX #$00            ; X counts up through the source
        LDY #$03            ; Y counts down through the destination
loop:   LDA src,X
        STA dst,Y
        INX
        DEY
        BPL loop            ; stop when Y goes negative
        BRK

src:    .byte 1, 2, 3, 4
dst:    .res 4

After the loop, the destination holds the source in reverse:

Text
dst = 04 03 02 01   X = $04   Y = $FF

Y ends at $FF because the last DEY took it from 0 to −1, which set N and made BPL fall through. Two index registers running in opposite directions is a natural fit for the 6502's design. A Z80 would use LDIR or LDDR for a plain copy, but reversing needs the same two-index thinking on both.

Indirect indexed: pointer plus Y

This is the mode to learn first, because it is how a 6502 walks a buffer whose address is not known at assembly time. The syntax is LDA (zp),Y. Read it from the inside out: the zero-page address zp and the one after it hold a 16-bit pointer, low byte first. The CPU reads that pointer and then adds Y to it. The result is the effective address.

indy.sasm
ptr     = $20

        LDA #$00
        STA ptr             ; low byte of the pointer
        LDA #$30
        STA ptr+1           ; high byte: ptr now holds $3000

        LDY #$05
        LDA (ptr),Y         ; A = [$3000 + 5] = [$3005]
        LDA #$FF
        STA (ptr),Y         ; [$3005] = $FF

The pointer lives in zero page, so it must be there: this mode accepts only a zero-page operand. That limit is the reason programs reserve a few zero-page pairs for pointers. And because Y is 8 bits, one pointer plus Y reaches 256 bytes. To walk further, add 1 to the pointer's high byte when Y wraps.

indy2.sasm
        LDY #$00
copy:   LDA (src),Y         ; src and dst are zero-page pointers
        STA (dst),Y
        INY
        BNE copy            ; 256 bytes
        INC src+1           ; next page of the source
        INC dst+1           ; next page of the destination

One copy of a 256-byte page. Wrap this in another loop and a 6502 copies any block, one page at a time.

Indexed indirect: pointer table plus X

The other indirect mode, LDA (zp,X), does the addition first. X is added to the zero-page address, wrapping, and the 16-bit pointer is read from there. It is a lookup in a table of pointers:

inx.sasm
        .org $0200
        LDX #$02            ; entry number 1 (each pointer is 2 bytes)
        LDA ($40,X)         ; read the pointer at $42/$43, then load from it

; zero page: $40 = $00 $10    pointer 0 -> $1000
;            $42 = $00 $20    pointer 1 -> $2000

With X = 2 the CPU reads the pointer at $42 and $43, which is $2000, and loads the byte at $2000. The X must be a multiple of two to land on a pointer. This mode is far less common than (zp),Y in compiled code and hand-written code, but it is the right tool for a dispatch table of buffers or of device addresses.

The two modes are easy to confuse because the parentheses sit in different places. The memory aid is that the register is applied where it appears. In (zp),Y the Y is outside the parentheses: dereference first, then add. In (zp,X) the X is inside: add first, then dereference.

NotationOrderTypical use
(zp),Yread pointer, then add Ywalk a buffer or string
(zp,X)add X, then read pointerpick a pointer from a table

The other modes

Three more complete the set. Implied mode names no operand, as in INX, CLC or RTS, because the register or flag is part of the opcode. Accumulator mode applies a shift to A, as in ASL A, and ca65 also accepts the bare ASL. Relative mode is used by branches: the operand is a signed offset from the following instruction. And JMP ($1234), indirect jump, reads a 16-bit address from $1234 and $1235 and jumps to it. That last one has a famous bug on the NMOS chip when the pointer sits at the end of a page, which a later post in this cluster covers.

Choosing a mode

Pick the mode from the data. A constant is immediate. A single variable is zero page if it is hot and absolute otherwise. A fixed array uses absolute with X or Y. A buffer whose address is chosen at run time uses (zp),Y. A table of device handles uses (zp,X). If you find yourself recomputing a pointer every iteration, you probably want (zp),Y and a pointer you update once.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.