Page 1 of 1
Initializing the stack
Posted: Thu Oct 13, 2011 2:41 am
by picccca
As far as I know the stack pointer points to the first free space in the stack. So when the stack pointer is set up like this:
the first free space is in RAM address $00FF or is the stack always in page 1? that would mean the first free space is in $01FF. This also gets me thinking of the way the stack pointer works. is the pointer incremented/decremented first and the byte copied second or the other way around? and which one is it? is the pointer incremented or decremented when pushing a byte?
Posted: Thu Oct 13, 2011 3:51 am
by thefox
Stack is always at $100-$1FF. When you initialize it to $FF, it points to $1FF. When a value is pushed on stack, the byte goes to $1FF, and the stack pointer gets decreased to $FE (= $1FE).
If you're not using the memory at $100-$1FF for anything else, it really doesn't matter what value you initialize the stack pointer to, as it'll wrap around. Some games use only some of the top bytes of the stack, like $1F0-1FF for stack and use the lower part for other purposes, like a PPU update buffer.
Posted: Thu Oct 13, 2011 4:01 am
by picccca
Ok, got it.
Posted: Thu Oct 13, 2011 6:44 am
by tokumaru
thefox wrote:If you're not using the memory at $100-$1FF for anything else, it really doesn't matter what value you initialize the stack pointer to, as it'll wrap around.
Unless you do something this to access the stack, because this addressing mode doesn't wrap around:
If you're gonna do this, or use only a portion of page 1 for the stack, you should initialize the stack pointer properly.