My understanding of stack, is it right?

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
     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.


Re: My understanding of stack, is it right?

Quoted text here. Click to load it



http://en.wikipedia.org/wiki/Stack_ (programming)

Re: My understanding of stack, is it right?
Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

That is correct.

Quoted text here. Click to load it

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


Re: My understanding of stack, is it right?
Op 14 Aug 2005 08:40:34 -0700 schreef snipped-for-privacy@ultratechnology.com:

Quoted text here. Click to load it

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

Re: My understanding of stack, is it right?
Quoted text here. Click to load it

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


Re: My understanding of stack, is it right?
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.


Re: My understanding of stack, is it right?

Quoted text here. Click to load it

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
snipped-for-privacy@actrix.gen.nz

Re: My understanding of stack, is it right?
Op 14 Aug 2005 21:53:53 -0700 schreef snipped-for-privacy@aol.com:

Quoted text here. Click to load it

Cosmac 1802.

Coos
--
CHForth, 16 bit DOS applications
http://home.hccnet.nl/j.j.haak/forth.html

Re: My understanding of stack, is it right?
Quoted text here. Click to load it

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...

<sigh>

Steve
http://www.fivetrees.com



Re: My understanding of stack, is it right?
Op Tue, 16 Aug 2005 00:56:40 +0100 schreef Steve at fivetrees:

Quoted text here. Click to load it

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

Re: My understanding of stack, is it right?
Quoted text here. Click to load it

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 itB4%s only three register-register operations instead of four
memory operations.


Re: My understanding of stack, is it right?
Quoted text here. Click to load it

  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



Re: My understanding of stack, is it right?
Quoted text here. Click to load it

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.

Quoted text here. Click to load it

Except on the Moto 680X family where the TSX/TXS instructions
make the adjustment for you.

Quoted text here. Click to load it

Site Timeline