6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 22, 2024 6:43 pm

All times are UTC




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Sun Aug 07, 2022 9:35 pm 
Offline

Joined: Sun Oct 03, 2021 2:17 am
Posts: 114
Why is pulling from the stack on the 65816 always one cycle slower than pushing that same register to the stack?

I tend to think of these operations as pseudo-inverses of each other. Pushing and then pulling, or pulling and then pushing, generally restores the stack to the state it was before, but only pushing and then pulling keeps the state of the register the same; pulling and then pushing a register discards its old value.

But these operations still seem quite symmetric - what is the extra cycle spent doing during a pull?


Top
 Profile  
Reply with quote  
PostPosted: Sun Aug 07, 2022 10:23 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
jeffythedragonslayer wrote:
what is the extra cycle spent doing during a pull?
It's the increment of the Stack Pointer. As you know, S points to the next available location -- not to the top item. So, to pull the top item, S must be incremented before the item is read. This consumes one cycle.

(For a push, the decrement of S happens after the write has occurred... and the decrement can be overlapped with the fetch of the next opcode, making it appear to consume zero cycles.)

-- Jeff

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 08, 2022 3:18 am 
Offline

Joined: Thu Jan 21, 2016 7:33 pm
Posts: 282
Location: Placerville, CA
That explains a bit; always wondered about that myself.


Top
 Profile  
Reply with quote  
PostPosted: Sat Aug 20, 2022 6:08 am 
Offline

Joined: Sun Oct 03, 2021 2:17 am
Posts: 114
Very fascinating. I would be interested in seeing what parts of the processor can be engaged simultaneously during a single clock cycle. Since do a lot of adjacent pulls is so slow, I'm finding it useful to create stack unwinding macros that increment the stack pointer back to where it was at the beginning of the subroutine.


Top
 Profile  
Reply with quote  
PostPosted: Sun Sep 04, 2022 7:47 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
jeffythedragonslayer wrote:
I'm finding it useful to create stack unwinding macros that increment the stack pointer back to where it was at the beginning of the subroutine.


Are you talking about exception handling in assembly language? Something like this?
Code:
   \ let a subroutine in the nesting
   \ return to this subroutine's caller
   \ if there is an error

LABEL SOMEROUTINE
   ...
   TSX
   STX ERR_RETURN
   JSR SOMEWHERE
   ...

LABEL SOMEWHERE
   ...
   JSR SOMEWHERE_ELSE
   ...

LABEL SOMEWHERE_ELSE
   ...
   BCC NOERROR
   \ oops, there was an error
   LDX ERR_RETURN
   TXS
LABEL NOERROR
   RTS
   
   \ The main routine.
   ...
   JSR SOMEROUTINE
   ...



Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 06, 2022 6:42 am 
Offline

Joined: Sun Oct 03, 2021 2:17 am
Posts: 114
Not really - I just wanted to speed up restoring the stack pointer.


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 06, 2022 7:14 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
You can use TSX and then save X somewhere, so then you can use TXS to restore the previous stack pointer.

Possibly you can even store the previous stack pointer on the stack, after having put the things you want on there.


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 06, 2022 4:22 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8505
Location: Midwestern USA
BigEd wrote:
Possibly you can even store the previous stack pointer on the stack, after having put the things you want on there.

Yes, but if you've changed the stack pointer how would you get back the old one that was written to the stack, a stack whose location was changed after saving the entry stack pointer?

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 06, 2022 4:32 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
I think it's fairly normal, isn't it, to have a stack frame and to push the previous value?


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 06, 2022 5:19 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8505
Location: Midwestern USA
BigEd wrote:
I think it's fairly normal, isn't it, to have a stack frame and to push the previous value?

Push the previous value to where?

If I arbitrarily change the stack pointer after pushing something, I no longer have the original stack pointer to tell me where to go to pull what I pushed. The only way in which one could use the stack to store the current stack pointer before changing it would be to treat the stack as memory, not a stack. That can be readily done with the 65C816 and some stack pointer arithmetic, but isn’t nearly as simple with the 65(C)02.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 06, 2022 5:39 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
Well, one of us is confused!


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 06, 2022 8:30 pm 
Offline

Joined: Sun Oct 03, 2021 2:17 am
Posts: 114
I don't arbitrarily change the stack pointer. I use the stack for automatic variables like in C, and I can tell at edit-time how many bytes I have allocated, so stack unwinding becomes simply incrementing the stack pointer up that many bytes.


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 06, 2022 9:12 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
Had you considered saving and restoring it with TSX and TXS?


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 06, 2022 10:17 pm 
Offline

Joined: Sun Oct 03, 2021 2:17 am
Posts: 114
Yes that is how one of my stack unwinding macros works.


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 07, 2022 1:30 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8505
Location: Midwestern USA
jeffythedragonslayer wrote:
Yes that is how one of my stack unwinding macros works.

That's adequate, as long as the function expects a fixed number of elements in the stack frame, and no recursion is involved. Get either one of those into the picture and you will need a more advanced means of processing the stack.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 21 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: