6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Sep 20, 2024 12:48 pm

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Questions On The Stack
PostPosted: Wed Sep 11, 2013 9:23 pm 
Offline
User avatar

Joined: Wed Jul 10, 2013 3:13 pm
Posts: 67
When a
Code:
PLY
Is executed, Is Y literally pulled for the stack, or does it just retrieve the last item on the stack and store it in Y
For Example
Code:
PHY  $#01
PHA  $#02

PLY

What is in Y 1 or 2

_________________
JMP $FFD2


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 11, 2013 9:33 pm 
Offline
User avatar

Joined: Sat Sep 29, 2012 10:15 pm
Posts: 899
A quick correction is first necessary.
PHY pushes the current value of whatever is in the Y register; PHA means push Accumulator. So you can't say 'PHY $#01'. PHY and PHA have the operands (Y and A) implied by the opcode. Your code should look like this:

Code:
LDY #$01   ;first, load immediate 1 into Y.  note that # comes first!
PHY        ;now push it
LDA #$02   ;now load immediate 2 into A
PHA        ;now push 2
PLY

Y is now 2; the next value on the stack is 1.

So to answer your question, pulling a value off the stack places it into the register; the value is no longer on the stack.

_________________
In theory, there is no difference between theory and practice. In practice, there is. ...Jan van de Snepscheut


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 11, 2013 9:42 pm 
Offline
User avatar

Joined: Sat Sep 29, 2012 10:15 pm
Posts: 899
BTW, there are a few great 6502 instruction reference sites. Here is one:
http://homepage.ntlworld.com/cyborgsystems/CS_Main/6502/6502.htm#DETAIL

Don't forget that the same stack is used for return addresses. So if you call a subroutine, before you return, the stack should be exactly as it was on entry (usually), or you will return to some random location and crash. If you push something, make sure to pull it off (for preserving registers, for instance).

_________________
In theory, there is no difference between theory and practice. In practice, there is. ...Jan van de Snepscheut


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 11, 2013 9:56 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8510
Location: Southern California
Note however that the linked page, even though it says "Rockwell," is only for the NMOS instructions, and leaves out the extra CMOS ones like PHY and PLY.

As enso alluded, the stack doesn't care where it got its contents. So for example if you do a PHA PHX PHY, in that order, and then do a PLX, X will take on the value that Y had, because Y was pushed last, and the new top-of-stack will now be the value that was originally in X.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Sun Sep 15, 2013 9:52 am 
Offline

Joined: Tue Jul 24, 2012 2:27 am
Posts: 674
All push operations first decrement the SP, then store the value at $0100,SP.

All pull (pop) operations read the value at $0100,SP, then increment the SP.

There are no "peek at the stack without popping" instructions. In order to do that you need to manually transfer the stack pointer to another register and index.

The stack pointer points to the most recently pushed element on the stack, so TSX then LDA $0100,X looks at the head of the stack. LDA $0101,X looks at the 2nd byte on the stack.

_________________
WFDis Interactive 6502 Disassembler
AcheronVM: A Reconfigurable 16-bit Virtual CPU for the 6502 Microprocessor


Top
 Profile  
Reply with quote  
PostPosted: Sun Sep 15, 2013 1:45 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8510
Location: Southern California
Actually when you push something, the byte is stored at the stack pointer S, and then the stack pointer is decremented, so it ends up pointing to the next available location. When you pull something, it's the reverse, so the stack pointer is incremented before the byte is read.

A nice thing added on the 65816 is the stack-relative addressing where you can read for example the fifth byte on the stack without doing TSX, LDA 105,X, or pulling anything off the stack first.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Sun Sep 15, 2013 4:07 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8389
Location: Midwestern USA
White Flame wrote:
All push operations first decrement the SP, then store the value at $0100,SP.

All pull (pop) operations read the value at $0100,SP, then increment the SP.

Pushes post-decrement SP and pulls pre-increment SP.

In native mode, the 65C816's stack can be anywhere in the range $000000-$00FFFF, so the notation of $0100,SP wouldn't apply. The actual address in SP is used. As Garth noted, the stack can be treated as indexed RAM with any of the very useful ...,S instructions. I make extensive use of that capability in my POC unit's firmware.

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


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 24, 2013 10:27 pm 
Offline

Joined: Tue Jul 24, 2012 2:27 am
Posts: 674
Whoops! Of course you guys are right. I googled that when I posted to make sure, and ended up reading the info completely wrong! :oops:

_________________
WFDis Interactive 6502 Disassembler
AcheronVM: A Reconfigurable 16-bit Virtual CPU for the 6502 Microprocessor


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

All times are UTC


Who is online

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