Page 1 of 1
Questions On The Stack
Posted: Wed Sep 11, 2013 9:23 pm
by James_Parsons
When a
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
What is in Y 1 or 2
Re: Questions On The Stack
Posted: Wed Sep 11, 2013 9:33 pm
by enso
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: Select all
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.
Re: Questions On The Stack
Posted: Wed Sep 11, 2013 9:42 pm
by enso
BTW, there are a few great 6502 instruction reference sites. Here is one:
http://homepage.ntlworld.com/cyborgsyst ... 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).
Re: Questions On The Stack
Posted: Wed Sep 11, 2013 9:56 pm
by GARTHWILSON
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.
Re: Questions On The Stack
Posted: Sun Sep 15, 2013 9:52 am
by White Flame
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.
Re: Questions On The Stack
Posted: Sun Sep 15, 2013 1:45 pm
by GARTHWILSON
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.
Re: Questions On The Stack
Posted: Sun Sep 15, 2013 4:07 pm
by BigDumbDinosaur
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.
Re: Questions On The Stack
Posted: Tue Sep 24, 2013 10:27 pm
by White Flame
Whoops! Of course you guys are right. I googled that when I posted to make sure, and ended up reading the info completely wrong!
