liveBook · Manning

Pha instruction

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

The PHA instruction pushes the current value of the 6502 accumulator onto the processor stack.

PHA (Push A) copies the current value of the 6502 accumulator onto the processor stack. It provides a quick way to preserve A while other work changes it. section 4.6

Use PHA as part of a save-and-restore sequence:

  • Execute PHA to place A on the next stack position. section 4.6
  • Perform calculations or transfers that may overwrite A. section 4.6
  • Execute PLA later to remove the top stack value and restore it to A. section 4.6
  • Restore values in reverse order when several registers share the stack. section 5.13

Preserving a value during calculation

In this sequence, LDA loads 20 into A, PHA preserves it, and later instructions replace A with another calculation. PLA retrieves the original 20; STA stores the calculated value separately. section 4.6

Listing 5.18 Start of NMI routine (example.s)
.segment  "CODE"
.proc nmi
   ; save registers
pha
txa
pha
tya
pha

The NMI listing shows PHA saving A directly, then saving X and Y after transferring each value into A. The matching return sequence restores them in reverse order. section 5.13

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

How deep it goes
Explains the push operation, paired PLA restoration, short instruction size, and register-saving sequence without descending into stack-pointer internals.
Examples
Demonstrates PHA in an arithmetic sequence and an NMI routine that saves A, X, and Y.
What it assumes
Assumes familiarity with the 6502 accumulator and stack, while introducing PHA alongside the related PLA, PHP, and PLP commands.
What you can do after
Apply it in 6502 assembly to preserve A across calculations or protect registers inside an NMI routine.
How it connects
Connects PHA with the processor stack, accumulator, registers, PLA, and NMI routines.

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.