🔄 6502 Stack Pointer Visualization

Understanding Stack Operations and Pointer Management

Stack Memory ($0100 - $01FF)

Stack Pointer (SP)
Stack Data
Active Region

Current Status

Stack Pointer initialized to $01FF. Ready for operations.

Stack Pointer: $01FF

Operations: 0 push, 0 pull

About the 6502 Stack

The 6502 stack is 256 bytes located at memory addresses $0100-$01FF. The Stack Pointer (SP) register points to the next available location.

Stack Pointer Initialization

The stack pointer starting address is not predetermined. You must initialize it:

reset:
  ldx #$ff  ; Load X with $FF
  txs       ; Transfer X to Stack Pointer
  ; Stack now at $01FF
This initializes the stack pointer to $01FF, the top of the stack memory page.

Push Operation (PHA)

The PHA instruction pushes the accumulator onto the stack:

lda #$38   ; Load value into A
pha        ; Push A to stack
; SP decrements after write
When you push, the value is written to the address pointed to by SP, then SP is decremented. The stack grows downward from $01FF to $0100.

Pull Operation (PLA)

The PLA instruction pulls from the stack into the accumulator:

pla        ; Pull from stack to A
; SP increments before read
When you pull, SP is incremented first, then the value at that address is read into the accumulator.

Stack Overflow Warning

The 6502 stack is only 256 bytes! If you push more than 256 values without pulling, you'll get a stack overflow and overwrite important data.

Important for JSR/RTS: The JSR (Jump to Subroutine) instruction pushes the return address onto the stack. The RTS (Return from Subroutine) pulls it back. This is why the stack needs RAM to work properly!

Real-World Example

In the Ben Eater 6502 project, subroutines like lcd_instruction use JSR to call and RTS to return. Without RAM at $0100-$01FF, these instructions will fail because there's nowhere to store the return address!