If there are two register A and B in microcontroller, I use PUSH A, PUSH B and then use POP A, POP B, what will happen? I thinked the content of B will give to A, and the content of A will give to B.Is it right?Another thing, if SP is the stack pointer, it can point to the middle of the stack not the top of it. and then use POP instruction will get the content of SP that point to. What I understanding is right or wrong? My operation may be something crazy, but I just want to know the stack deeply.
My understanding of stack, is it right?
Aug 14, 2005
12 Replies
formatting link
Yes. That form of register swap would take four data memory operations if your stacks are in memory and your stack pointer is in a register. It is one way to swap A and B and if there is no free register to move A to X and B to A and X to B and no exchangeAB instruction.
You can manually move a stack pointer to the middle of stack but you will be discarding half of the stack contents by doing it. If you have N items on the stack and you adjust your pointer by N/2 items then you have either added N/2 items to the stack that just happened to be resident in that memory that you are using as a stack or you have removed N/2 items from the stack.
That is correct.
Your understanding is correct. Adding or subtracting an offset to or from a stack pointer is a common way to add or remove a stack frame for a number of items residing temporarily on a stack.
Best Wishes
Op 14 Aug 2005 08:40:34 -0700 schreef snipped-for-privacy@ultratechnology.com:
Not always, some processors do, others point to the next available free position on the stack.
Coos
CHForth, 16 bit DOS applications
http://home.hccnet.nl/j.j.haak/forth.html
The instructions can adjust the pointer before a push or before a pop. If it adjusts the pointer after a PUSH so that the pointer will point to the next free location, then the POP instruction would have to adjust the pointer back to the last value before the POP. I think it has to work that way otherwise a POP would not give you back what you PUSHed.
It is true that the pointer could be adjusted up or down or before or after a PUSH or a POP. But one has to be before and the other after. I think it has to work that way.
Best Wishes
If you only PUSH and POP, ( or converse) for stack access, this is correct for all uC. There is a subtle detail that can matter with more complex stack usage : Some uC Pre INC/DEC, and some Post INC.DEC
You can change the SP anytime ( at your peril, of course :)
If you load the SP into an index register [to get a MOV @SP ] then the pre/post inc/dec detail matters.
ie is the SP pointing to the last PUSH, or the Next PUSH ?
-jg
Are there really processors that point to the next location? If that were true the top of stack contents would not easily be accesible and the stack pointer allways points to garbage. Maybe you could post an example.
The first example which occurs to me is the venerable 6502. Its stack pointer is normally initialized to $FF (with the stack fixed on page 1, so this is location $01FF).
The PUSH operation stores a value at the location pointed to by the stack pointer, then decrements the stack pointer.
The POP operation increments the stack pointer then reads the value at that location.
This is the same behaviour as the stack pointer in the 6800 (and derivatives), except that they have a 16-bit stack pointer instead of an
8-bit one.
David Empson
dempson@actrix.gen.nz
A better way to do this having neither a swap-instruction nor a free register would be:
eor a,b eor b,a eor a,b
This way it=B4s only three register-register operations instead of four memory operations.
Op 14 Aug 2005 21:53:53 -0700 schreef snipped-for-privacy@aol.com:
Cosmac 1802.
Coos
CHForth, 16 bit DOS applications
http://home.hccnet.nl/j.j.haak/forth.html
Or you can do like the ARM and have both flavors as a programmer option. A good mind-boggling combination is an upward growing stack with post-increment for pushing and pre-decrement for popping.
Except on the Moto 680X family where the TSX/TXS instructions make the adjustment for you.
Blimey - that brings back memories. I had an eval board (hex keypad, hex display) for the 1802 circa '80 or so.... usual moonlander-in-hex-programming shenanigans...
Steve
formatting link
Op Tue, 16 Aug 2005 00:56:40 +0100 schreef Steve at fivetrees:
I haven't fired it up since I bought my ZX Spectrum in 1984. It was my first computer and soon ran Forth on it, hence my knowledge of stacks ;-)
The push (STXD) was postdecrement and pointed to a free space on the X-stack, handy if you wanted to do arithmetic while in a subroutine. The pop (LDA) was postincrement and pointed afterwards to a previous value. You had to use INC and DEC everytime. Sloppy of RCA, but the SCAL and SRET of the 1806 cured that. But this one was as my memory goes not available at that time.
Coos
CHForth, 16 bit DOS applications
http://home.hccnet.nl/j.j.haak/forth.html
Join the Discussion
Have something to add? Share your thoughts — no account required.
Didn't find your answer?
Ask the community — no account required