[159]disch_explains_stack_basics.txt
// This is a conversation I had with a friend of mine, Disch. He is a // well known ROM hacker, and I thought this conversation we had could // help others understand how the Stack Pointer and Program Counter // work. [23:39] *** Now talking to Disch [23:39] Disch is ~disch_@dpc67143199073.direcpc.com Disch [23:39] Disch on @#rom-hacking @#rom-hacking-ops [23:39] Disch using discworld.esper.net Fabricati deim, pvnc. [23:39] Disch has identified for this nick [23:39] <Spliff> Disch, I am having issues with the Stack Pointer and Program Counter [23:40] <Spliff> I know you're not 24/7 encyclopedia, but I need some help here... [23:40] <Spliff>…
Read moreRead less
// This is a conversation I had with a friend of mine, Disch. He is a // well known ROM hacker, and I thought this conversation we had could // help others understand how the Stack Pointer and Program Counter // work. [23:39] *** Now talking to Disch [23:39] Disch is ~disch_@dpc67143199073.direcpc.com Disch [23:39] Disch on @#rom-hacking @#rom-hacking-ops [23:39] Disch using discworld.esper.net Fabricati deim, pvnc. [23:39] Disch has identified for this nick [23:39] <Spliff> Disch, I am having issues with the Stack Pointer and Program Counter [23:40] <Spliff> I know you're not 24/7 encyclopedia, but I need some help here... [23:40] <Spliff> How do they work? [23:40] <Spliff> What do they do? [23:40] <Disch> The Program Counter just tells where the CPU is executing from [23:40] <Disch> like if the PC is at $8000 ... then the next instruction to be executed comes from $8000 [23:41] <Disch> JMP $C123 <--- all that does is set the PC to $C123 ... so that it's the next instruction executed [23:41] <Disch> The Stack Pointer just tells where the top of the stack is [23:41] <Spliff> What is the stack? [23:41] <Disch> okay [23:41] <Disch> imagine a stack of plates [23:42] <Disch> or [23:42] <Disch> before it's a stack [23:42] <Disch> say I just put one plate on the table [23:42] <Disch> and another plate on top of it [23:42] <Disch> the second plate is now the top of the stack... and in order to take off any plates below it, I need to take off that top plate first [23:43] <Disch> it's kind of hard to explain... [23:43] <Disch> but okay [23:43] <Disch> you know about PHA and PLA? [23:43] <Spliff> I haven't began to delve into ALL the mnemonics yet [23:43] <Spliff> I only know maybe 10 offhand at most [23:43] <Disch> well I can run through them [23:43] <Spliff> Maybe 20 [23:44] <Disch> PHA "pushes" a value onto the stack (like adding another plate to the stack) [23:44] <Spliff> Onto the top? [23:44] <Disch> PLA "pulls" a value off the stack (like taking a plate off) [23:44] <Disch> so here's an example: [23:44] <Disch> LDA #$00 ; stack: empty [23:45] <Disch> PHA ; stack: [top] $00 [bottom] [23:45] <Disch> LDA #$65 [23:45] <Disch> PHA ; stack: [top] $65 $00 [bottom] [23:45] <Disch> LDA #$23 [23:45] <Disch> PHA ; stack: [top] $23 $65 $00 [bottom] [23:46] <Disch> LDA #$00 [23:46] <Disch> PLA ; A is now $23, stack: [top] $65 $00 [bottom] [23:46] <Disch> PLA ; A is now $65 stack: [top] $00 [bottom] [23:46] <Disch> make sense? [23:47] <Spliff> Yeah, but whats the deal with the second $00? [23:47] <Spliff> I get the rest, by the way [23:47] <Disch> just to show that PLA changes A [23:48] <Spliff> Oh [23:48] <Disch> so anyway [23:48] <Disch> the stack exists on the $01xx page [23:48] <Disch> where the Stack Pointer (SP) determines the next place to be the top of the stack [23:48] <Spliff> $100 - $1FF? [23:48] <Disch> yeah [23:49] <Disch> now the stack is actually backwards kinda [23:49] <Disch> $01FF is the normal "bottom" of the stack [23:49] <Disch> so when the stack is empty... SP is $FF [23:49] <Disch> when a value is pushed... it gets written to $0100+SP then SP is decremented [23:50] <Disch> when a value is pulled, SP gets incremented... then the value returned is from $0100+SP [23:50] <Disch> am I making sense? lol [23:50] <Spliff> Yeah, it made sense when I remembered to read "$01FF is the bottom" [23:51] <Spliff> Until then, I was kinda confused [23:51] <Disch> heh [23:51] <Disch> so any more Q's? ^^ [23:52] <Spliff> So when we push from $FF, incrementing by $01, we are now at $FE? [23:52] <Disch> that's decrementing.. no incrementing ;P but yeah that's right [23:53] <Disch> LDA #$00 ; if SP is $FF here... [23:53] <Spliff> Ah, decrementing [23:53] <Disch> PHA ; $00 gets written to $01FF here, SP is now $FE [23:53] <Spliff> But we're pushing [23:53] <Spliff> And pulling brings it back to FF [23:53] <Disch> right [23:53] <Spliff> Sweet [23:53] <Spliff> Now what can that do for a game? [23:53] <Disch> well [23:53] <Spliff> Pushing and Pulling? [23:53] <Disch> the stack is mostly used with JSR/RTS [23:54] <Spliff> Bit shifts? [23:54] <Disch> JSR is like JMP... only it "saves" the PC by pushing it to the stack [23:54] <Disch> RTS pulls that back from the stack [23:54] <Disch> so like [23:54] <Disch> say a game has a subroutine to print some text [23:55] <Disch> at address $C654 [23:55] <Disch> $C654: [23:55] <Disch> LDA blah [23:55] <Disch> STA blah [23:55] <Disch> ; more crap to print text [23:55] <Disch> RTS [23:55] <Disch> then elsewhere in the game [23:55] <Disch> say you want to print some text... move the player... then print some more text [23:56] <Disch> say you're at $8000 or something [23:56] <Disch> $8000: [23:56] <Disch> JSR $C654 ; jump to draw text routine [23:56] <Disch> ; when the draw text routine hits that RTS, execution will continue from here [23:56] <Disch> JSR Move_player [23:56] <Disch> JSR $C654 [23:56] <Disch> ; to print more text [23:57] <Disch> make sense? [23:57] <Spliff> I didn't know Move_player was a valid syntax...I always thought it was Hex numbers after a Mnemonic [23:58] <Disch> well that was just an example [23:58] <Disch> (if you're using an assembler you can use labels and stuff -- but whatever) [23:58] <Disch> put a number there for Move_player then ;P [23:59] <Spliff> Lol [23:59] <Spliff> hmmm... [00:00] <Spliff> So JSR jumps right to C654> [00:00] <Spliff> ?* [00:00] <Disch> yeah [00:00] <Disch> but [00:00] <Disch> it also pushes the return address ($8003) to the stack [00:00] <Spliff> Ahhh [00:00] <Disch> so when the game hits RTS.. it'll pull the address from the stack and jump to it [00:01] <Spliff> "I'm gonna jump to C654 now, but first I'm gonna make sure the the of the Stack is set to where I am now first" [00:01] <Spliff> Right? [00:01] <Disch> actually... technically it would push $8002 ;P [00:01] <Disch> yeah [00:01] <Spliff> Aight :) [00:01] <Disch> RTS is "go back to wherever called JSR last" [00:01] <Spliff> Ahh :) [00:02] <Spliff> "Now I've visited C654 and done my business, now lets go back to where the top of the Stack tells us to"? [00:02] <Disch> yep [00:02] <Spliff> :D [00:03] <Spliff> This convo should be placed on Romhacking.net :p [00:03] <Disch> haw [00:03] <Spliff> "Understanding the Stack Pointer" [00:03] <Disch> feel free to submit it or whatever
Retro Replay Retro Gaming History, Reviews & Emulation