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
