How to use the stack?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

How to use the stack?

Post by DementedPurple »

So I want to use the stack so that I can return to where I started after a BEQ. I know that it keeps the address of a command when you're ready to return. But what does pushing registers to the stacks do? Would it copy the value in the stack into the register? How would PHA work? The accumulator is an 8-Bit register while the stack is 16-Bit. Does it write the value in the accumulator to the stack? That would make sense, but you wouldn't be able to access the entire ROM because of the 8-Bit limitations, unless you pushed twice. If you did that though, why would you have to pull it? How would you tell the stack to keep the address? Would you just use an RTS to return after your done?
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: How to use the stack?

Post by tepples »

The answer for what you're asking to do at a low level is what we call the RTS Trick.

But could you explain why you can't just JSR to the code that you're going to call?
DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

Re: How to use the stack?

Post by DementedPurple »

Because like I said, I'm using a BEQ after a comparison.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: How to use the stack?

Post by tepples »

Could you show short source code or pseudocode that includes said "BEQ after a comparison" so that others reading this can better understand your problem?
adam_smasher
Posts: 271
Joined: Sun Mar 27, 2011 10:49 am
Location: Victoria, BC

Re: How to use the stack?

Post by adam_smasher »

While tepples' concern (that you're doing something unnecessarily complicated) is valid, it also seems to me like you don't really understand how the stack works (and forgive me if you do and I'm just misunderstanding you!), so let's tackle that.

First of all, the stack register S on the 6502 is technically 8-bit, not 16-bit. It points to an address in memory, but that address is always in the "one-page", addresses $0100 to $01FF. If S contains, say, $E0, then the "top of the stack" is at memory address $01E0.

When you push something on the stack, that means that you're writing something to the memory address pointed to by S. So suppose A contains, I dunno, $88, and S as above is $E0. Again, in this case the top of the stack is $01E0. If you do PHA (PusH Accumulator), what happens is:
  1. The value of A, $88, gets written to the top of the stack, $01E0
  2. S gets decremented to $DF, so the top of the stack is now at $01DF
This is what makes the stack useful - as long as you're careful to use it properly and never overflow it, it always provides you with a safe place to temporarily keep stuff in memory.

Similarly, if you then used PLA (PuL Accumulator), what would happen is the opposite:
  1. S gets incremented to $E0, so the top of the stack is now at $01DF
  2. The value at the top of the stack, $88, gets read into A
Now, as an exercise, think about what might happen if you don't properly match each push with a corresponding pull. That's what I mean by "use it properly".
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How to use the stack?

Post by tokumaru »

You normally don't need any stack tricks in the middle of common logic, so I bet your problem can be solved in a simpler way. Please show us some code illustrating the problem so we can point you in the right direction.
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: How to use the stack?

Post by rainwarrior »

DementedPurple wrote:So I want to use the stack so that I can return to where I started after a BEQ. I know that it keeps the address of a command when you're ready to return. But what does pushing registers to the stacks do? Would it copy the value in the stack into the register? How would PHA work? The accumulator is an 8-Bit register while the stack is 16-Bit. Does it write the value in the accumulator to the stack? That would make sense, but you wouldn't be able to access the entire ROM because of the 8-Bit limitations, unless you pushed twice. If you did that though, why would you have to pull it? How would you tell the stack to keep the address? Would you just use an RTS to return after your done?
Just invert the BEQ test to be BNE, its opposite, and make that jump over a JSR:

Code: Select all

	bne :+
		jsr subroutine
	:

...

subroutine:
	; do stuff
	rts ; return to jsr it was called from
JSR uses the stack to remember where it was run, and will return there at the next RTS. There's no need to use PHA directly for this purpose, in this case.
Garth
Posts: 246
Joined: Wed Nov 30, 2016 4:45 pm
Location: Southern California
Contact:

Re: How to use the stack?

Post by Garth »

I have a treatise on 6502 stacks (plural, not just the page-1 hardware stack) at http://wilsonminesco.com/stacks/ . For your situation, you'll find a lot of help even if you only read the first page or two. Hopefully you'll get enthused and keep going though. It has 19 chapters plus appendices. There is so much that can be done 6502 stacks that most enthusiasts have never thought of.
http://WilsonMinesCo.com/ lots of 6502 resources
Post Reply