Zero-page memory is the first 256 bytes of 6502 RAM. A location there uses a one-byte address rather than the two-byte address required elsewhere in the 64K address space, so zero-page instructions execute faster than normal memory instructions. It fills the gap between the 6502’s few, fastest registers and its larger, slower RAM. section 3.2 section 3.2.4
Treat zero page as a compact area for program variables. It holds data that must persist outside A, X, and Y registers, while remaining efficient to load and store. On the NES, zero-page declarations reserve RAM only: code belongs in ROM, not in this RAM area. section 3.3.2 section 5.6
Choose the addressing form by the location and operation needed:
- Load from a zero-page location by omitting # before an address from $00 through $FF. For example, lda $05 reads the byte stored at $05; lda #$05 instead loads the literal value $05. section 3.3.1
- Store a register value with sta, stx, or sty and a zero-page destination, such as sta $06, stx $07, or sty $08. section 3.3.2
- Use X or Y indexing where supported to calculate a zero-page location, such as lda $01,X or sta $01,X. section 3.3.1 section 3.3.2
- Use two zero-page bytes as an address when indirect indexed addressing is needed, such as lda ($05),Y or sta ($05),Y. section 3.3.1 section 3.3.2
Reserve game variables
Place variables in a ZEROPAGE segment with .res. A one-byte nmi_ready flag can control whether rendering proceeds, gamepad can hold controller input, and d_x and d_y can store ball velocity. These labels reserve RAM locations that later instructions can access with zero-page forms. section 5.6 section 5.2.2
Listing 5.6 Our zero-page memory allocations (example.s)
;*****************************************************************
; 6502 Zero Page Memory (256 bytes)
;*****************************************************************
.segment "ZEROPAGE"
nmi_ready: .res 1 #1
#1
gamepad: .res 1 #2
d_x: .res 1 #3
d_y: .res 1 #4
This allocation listing shows labeled one-byte variables reserved in the ZEROPAGE segment. section 5.6
