6502 Addressing Modes, the Atari Way
The 6502 has few registers but a rich set of ways to name a memory location, and choosing the right one is most of the craft of writing fast Atari code. This guide goes through each addressing mode with an example from typical Atari work: writing to the screen through SAVMSC, looking up row addresses, stepping through tables and jumping through vectors. It also covers the costs in bytes and cycles, and the page-zero locations you can safely use as pointers.
What you'll learn: the 13 addressing modes, what each costs, when an index crossing a page boundary adds a cycle, which page-zero bytes are free under BASIC and under your own program, and Atari-specific traps such as false reads from hardware registers.
What you need: MADS (or any 6502 assembler) and an emulator. If you're completely new to assembly, start with Getting Started with 6502 Assembly on the Atari 8-bit.
1. The modes at a glance
Cycle counts are for reads such as LDA. "+1" means one more cycle when the indexed address crosses into the next page.
| Mode | Syntax | Bytes | Cycles | Typical Atari use |
|---|---|---|---|---|
| Implied | INX, CLC, RTS |
1 | 2+ | Counters, flags |
| Accumulator | ASL A, ROR |
1 | 2 | Shifting bits for graphics |
| Immediate | LDA #$94 |
2 | 2 | Constants, colour values |
| Zero page | LDA $58 |
2 | 3 | OS variables (SAVMSC), your fast variables |
| Zero page,X | LDA $80,X |
2 | 4 | Small arrays and pointer tables in page zero |
| Zero page,Y | LDX $80,Y |
2 | 4 | Only with LDX and STX |
| Absolute | STA $02C8 |
3 | 4 | Shadow and hardware registers |
| Absolute,X | LDA table,X |
3 | 4 (+1) | Lookup tables, row addresses |
| Absolute,Y | LDA text,Y |
3 | 4 (+1) | Copying strings and data |
| Indexed indirect | LDA ($80,X) |
2 | 6 | Picking one of several pointers |
| Indirect indexed | LDA ($CB),Y |
2 | 5 (+1) | Screen and buffer access |
| Relative | BNE loop |
2 | 2, 3 taken, 4 taken across a page | All branches |
| Indirect | JMP ($0A) |
3 | 5 | Jump through OS vectors and jump tables |
Other instructions follow the same pattern with their own base costs. For example, JMP absolute is 3 cycles, JSR 6, and INC takes 5 (zero page), 6 (absolute or zero page,X) and 7 (absolute,X).
Stores are different. A read can guess the address and try again only if the index carried into the next page. A write can't guess, because a wrong write would corrupt memory. So STA abs,X, STA abs,Y and STA (zp),Y always take the extra cycle: 5, 5 and 6 cycles. Read-modify-write instructions such as INC abs,X also always pay it. Some printed tables list these stores at 4 and 5 cycles. The longer figures are the right ones.
These are bare CPU cycles. On an Atari, ANTIC also takes memory cycles away from the 6502 for screen and character fetches, so the actual time depends on the graphics mode. Use the counts to compare alternatives, not to time a loop to the microsecond.
2. Page zero: where your pointers live
Zero-page instructions are a byte shorter and a cycle faster, and the two indirect modes only work through page zero. That makes the 256 bytes at $00–$FF the most valuable memory on the machine, and most of it is already taken:
- $00–$7F belong to the operating system.
- $80–$CA are Atari BASIC's pointers and work area, when BASIC is running.
- $CB–$D1 (203–209) are not used by BASIC or the OS. These seven bytes are the standard place for a USR routine's pointers.
- $D2–$FF are the floating-point package's work area (FR0 at $D4, FR1 at $E0, INBUFF at $F3, FLPTR at $FC), used only while FP routines run.
In a stand-alone machine-language program, with no BASIC or other cartridge, the whole of $80–$FF is yours, and $D4–$FF too if you never call the math pack. Other environments claim their own areas. The Assembler Editor cartridge, for example, uses $80–$B0, so check the memory map of anything that stays resident.
3. The modes in Atari context
Immediate and absolute. LDA #$C4 : STA $02C6 loads a constant and stores it at a fixed address, here the COLOR2 shadow register. Absolute addressing reaches any of the 64K, so it is what you use for page 2 and 3 shadows and for the hardware registers at $D000–$D4FF.
Zero page. The OS keeps many of its variables in page zero, so you read them with 2-byte instructions. LDA $58 and LDA $59 give the address of screen memory (SAVMSC). LDA $14 reads the low byte of the frame counter RTCLOK.
Indirect indexed, (zp),Y. This is the Atari programmer's workhorse. Copy SAVMSC into two free page-zero bytes and you have a pointer to the screen. Y then indexes up to 255 bytes past it:
lda $58 ; SAVMSC low
sta $CB
lda $59 ; SAVMSC high
sta $CC
ldy #39
loop lda #$21 ; screen code for "A"
sta ($CB),y ; 6 cycles every time (it's a store)
dey
bpl loop ; fill the first text line
To move to the next text line, add 40 to the pointer. To move to the next page of a buffer, increment the pointer's high byte. Display lists, player-missile memory, disk buffers and character sets are all reached the same way.
Absolute,X and absolute,Y. Lookup tables. A table of row start addresses, split into low-byte and high-byte halves, turns "where does row X start?" into two 4-cycle loads instead of a multiplication. This split-table layout is the standard 6502 idiom, because an index register can only step one byte at a time.
Zero page,X. A small array in page zero, such as several pointers side by side or per-player positions. Note that the address wraps within page zero: LDA $F0,X with X = $20 reads $0010, not $0110.
Indexed indirect, (zp,X). X picks one of several pointers stored together in page zero, and the CPU reads from wherever that pointer points. There is no Y offset, so you step through the data by incrementing the pointer itself. This mode is rarer than (zp),Y but neat for handling several streams at once, such as music voices or several strings being printed.
Relative. Branches hold a signed offset of −128 to +127 bytes, counted from the instruction after the branch. A taken branch costs 3 cycles, or 4 if the target is in a different page from that next instruction. In a tight loop, keep the branch and its target in the same page.
Indirect, JMP (abs). Jumping through a vector. The OS vectors in page zero and page two, such as DOSVEC ($0A) or VVBLKD ($0224), are tables of addresses used this way, and so is a jump table indexed by a command number. On the original NMOS 6502 there is a well-known bug: if the vector's low byte sits at $xxFF, the high byte is fetched from $xx00 of the same page. Never place a vector across a page boundary.
4. A demo that uses them all
This program draws seven lines on the GRAPHICS 0 screen. Each one is drawn with the mode it describes: a row-address table built with absolute,X, a title copied with absolute,Y into (zp),Y, six strings read through a page-zero pointer array with (zp,X) while zero page,X steps the pointers, and an indirect JMP at the end.
; addressing modes demo - mads modes.asm -o:modes.xex
SAVMSC = $58 ; OS pointer to screen memory
COLOR2 = $02C6 ; text background shadow
SCR = $CB ; (zp),Y screen pointer, $CB/$CC
ROW = $CD ; current row number
PTRS = $80 ; six string pointers, $80-$8B
NSTR = 6
org $2000
main lda #$00 ; immediate
sta COLOR2 ; absolute: black background
; --- absolute,X: build a table of the 24 row addresses
lda SAVMSC ; zero page
ldy SAVMSC+1
ldx #0
mk sta rowlo,x ; absolute,X store
pha
tya
sta rowhi,x
pla
clc
adc #40
bcc mk1 ; relative
iny ; implied
mk1 inx
cpx #24
bne mk
; --- absolute,Y source, (zp),Y destination: the title on row 0
ldx #0
jsr setrow
ldy #title_len-1
tl lda title,y ; absolute,Y
sta (SCR),y ; (zp),Y
dey
bpl tl
; --- copy the pointer table into page zero (absolute,X -> zero page,X)
ldx #NSTR*2-1
cp lda ptrinit,x
sta PTRS,x ; zero page,X
dex
bpl cp
; --- print each string through its pointer with (zp,X)
lda #2
sta ROW
ldx #0 ; X = 0, 2, 4 ... selects the pointer
str txa
pha
ldx ROW
jsr setrow
pla
tax
ldy #0
ch lda (PTRS,x) ; (zp,X): byte at the pointer selected by X
cmp #$FF ; end marker
beq next
sta (SCR),y
inc PTRS,x ; zero page,X read-modify-write
bne ch1
inc PTRS+1,x
ch1 iny
bne ch
next inc ROW ; zero page RMW
inc ROW
inx
inx
cpx #NSTR*2
bne str
jmp (vector) ; indirect: the only JMP (abs) in the program
setrow lda rowlo,x ; SCR = address of row X
sta SCR
lda rowhi,x
sta SCR+1
rts
done jmp done
vector dta a(done)
title dta d'6502 ADDRESSING MODES ON THE ATARI'* ; inverse video
title_len = *-title
s1 dta d'(ZP,X) READ THESE LETTERS',$FF
s2 dta d'(ZP),Y WROTE THEM TO THE SCREEN',$FF
s3 dta d'ZP,X STEPPED THE POINTERS',$FF
s4 dta d'ABS,X BUILT THE ROW TABLE',$FF
s5 dta d'ABS,Y COPIED THE TITLE',$FF
s6 dta d'(ABS) JMP (VECTOR) ENDS HERE',$FF
ptrinit dta a(s1),a(s2),a(s3),a(s4),a(s5),a(s6)
rowlo .ds 24
rowhi .ds 24
run main
Notes:
d'...'in MADS produces screen codes rather than ATASCII, and the trailing*sets bit 7 for inverse video. Screen code 0 is a space, which is why the strings end with $FF and not 0.- The program uses $80–$8B and $CB–$CD, which is fine for a stand-alone XEX. In a USR routine you would keep to $CB–$D1.
- After the copy loop,
PTRS,xpoints into the strings and is modified as they print. Run the program again and it reloads the pointers fromptrinit, so it always starts clean.
5. Atari-specific traps
- False reads from indexed modes. When an index crosses a page, the 6502 first reads from the wrong address (the right low byte with the old high byte) and then repeats the read. Indexed stores and read-modify-write instructions always make this extra access. Normally it's harmless, but reading the PIA data registers ($D300–$D3FF) clears pending interrupts, and some bank-switched cartridges switch banks on any access to $D500–$D5FF. Use absolute addressing for those registers, not
LDA $D5FF,X-style tricks. - zp,Y exists only for LDX and STX. Writing
LDA $80,Ymakes the assembler quietly use absolute,Y instead: one byte longer, and it no longer wraps within page zero. - Index registers are 8-bit.
LDA table,Xcan only reach 256 bytes. For bigger data, use (zp),Y and increment the pointer's high byte. - Indexing doesn't touch the flags. The address addition in absolute,X neither uses nor sets the carry flag, and it isn't affected by decimal mode.
Troubleshooting
- Writes appear one line too low or wrap strangely: the pointer's high byte wasn't incremented when adding 40 crossed a page.
- Program works, then corrupts BASIC or the math pack: you used page-zero bytes outside $CB–$D1 while BASIC was in memory.
- A tight loop is slower than expected: a table or branch target straddles a page boundary. Align hot tables with
.alignin MADS. - JMP (vector) goes to a strange place: the vector's two bytes sit at $xxFF/$xx00.
Further reading
- Mark Chasin, Assembly Language Programming for the Atari Computers: chapter 5 explains the addressing modes, and the appendix gives cycles and bytes per mode for every instruction.
- Altirra Hardware Reference Manual, chapter 3 (CPU): page crossing, dummy cycles, branch timing and the indirect JMP bug.
- Mapping the Atari by Ian Chadwick: page zero, locations 0–255, including the free bytes at 203–209.
- De Re Atari (Atari, 1982): the operating system chapter for the vectors you will jump through.
- On this site: Getting Started with 6502 Assembly on the Atari 8-bit, Calling Machine Language from Atari BASIC with USR and Atari Memory Map Highlights.
Credits
- Picture: The demo program: every line was drawn using the addressing mode it names. — Program authors; screenshot by the site
- Mark Chasin, Assembly Language Programming for the Atari Computers — Appendix 1 (instruction set, cycles and bytes per mode) — Mark Chasin; AtariArchives.org
- Altirra Hardware Reference Manual, chapter 3 (CPU): dummy cycles, page crossing, branch timing, zp,Y promotion, JMP (abs) bug — Avery Lee (Altirra)
- Mapping the Atari (revised) — page zero usage (OS 0-127, BASIC 128-202, free 203-209, FP 210-255), SAVMSC 88-89 — AtariArchives.org
