It is currently Sat Dec 16, 2017 12:33 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: How to use the stack?
PostPosted: Tue Apr 04, 2017 9:05 am 
Offline

Joined: Mon Jan 30, 2017 5:20 pm
Posts: 303
Location: Colorado USA
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?


Top
 Profile  
 
PostPosted: Tue Apr 04, 2017 9:12 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19348
Location: NE Indiana, USA (NTSC)
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?


Top
 Profile  
 
PostPosted: Tue Apr 04, 2017 9:13 am 
Offline

Joined: Mon Jan 30, 2017 5:20 pm
Posts: 303
Location: Colorado USA
Because like I said, I'm using a BEQ after a comparison.


Top
 Profile  
 
PostPosted: Tue Apr 04, 2017 9:17 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19348
Location: NE Indiana, USA (NTSC)
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?


Top
 Profile  
 
PostPosted: Tue Apr 04, 2017 9:43 am 
Offline

Joined: Sun Mar 27, 2011 10:49 am
Posts: 219
Location: NYC
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".


Top
 Profile  
 
PostPosted: Tue Apr 04, 2017 9:43 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10165
Location: Rio de Janeiro - Brazil
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.


Top
 Profile  
 
PostPosted: Tue Apr 04, 2017 9:44 am 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5899
Location: Canada
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:
   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.


Top
 Profile  
 
PostPosted: Tue Apr 04, 2017 1:27 pm 
Offline

Joined: Wed Nov 30, 2016 4:45 pm
Posts: 93
Location: Southern California
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group