Stack Pointer initialized to $01FF. Ready for operations.
Stack Pointer: $01FF
Operations: 0 push, 0 pull
$0100-$01FF.
The Stack Pointer (SP) register points to the next available location.
The stack pointer starting address is not predetermined. You must initialize it:
reset:ldx #$ff ; Load X with $FFtxs ; Transfer X to Stack Pointer$01FF, the top of the stack memory page.
The PHA instruction pushes the accumulator onto the stack:
lda #$38 ; Load value into Apha ; Push A to stackThe PLA instruction pulls from the stack into the accumulator:
pla ; Pull from stack to AThe 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.
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!
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!