How to use the stack?
Moderator: Moderators
-
- Posts: 318
- Joined: Mon Jan 30, 2017 5:20 pm
- Location: Colorado USA
How to use the stack?
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?
-
- Posts: 318
- Joined: Mon Jan 30, 2017 5:20 pm
- Location: Colorado USA
Re: How to use the stack?
Because like I said, I'm using a BEQ after a comparison.
Re: How to use the stack?
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?
-
- Posts: 271
- Joined: Sun Mar 27, 2011 10:49 am
- Location: Victoria, BC
Re: How to use the stack?
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:
Similarly, if you then used PLA (PuL Accumulator), what would happen is the opposite:
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:
- The value of A, $88, gets written to the top of the stack, $01E0
- S gets decremented to $DF, so the top of the stack is now at $01DF
Similarly, if you then used PLA (PuL Accumulator), what would happen is the opposite:
- S gets incremented to $E0, so the top of the stack is now at $01DF
- The value at the top of the stack, $88, gets read into A
Re: How to use the stack?
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.
- rainwarrior
- Posts: 8732
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: How to use the stack?
Just invert the BEQ test to be BNE, its opposite, and make that jump over a JSR: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?
Code: Select all
bne :+
jsr subroutine
:
...
subroutine:
; do stuff
rts ; return to jsr it was called from
Re: How to use the stack?
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