[A83] Re: push - pop


[Prev][Next][Index][Thread]

[A83] Re: push - pop





On Sat, 15 Dec 2001, SUCKER from the old days wrote:

> So pushing and popping should only be done this way:
>
> push af
>   push bc
>    push de
>     push hl
>
>     pop hl
>    pop de
>   pop bc
> pop af
>
> Right??
>
> I thaught that someway the z80 remembered the pushes of
> all the register pairs seperately.
>
> (now I really have to take a look at some programs of mine!)

That is the kind of method you should use if you want to have each of the
register's original values restored after the intervening code.

But since the stack neither knows nor cares which register a value came
from, you could just as well use something like

push hl
push de
<<code that does something or other>>
pop hl
pop de

and the pop instruction will process the stack the same way it always
does, thus giving the result that HL now contains the value initially in
DE and DE now contains the value initially in HL.  If the later routine
needs the values in different registers than they were in before, this
will certainly be more efficient than popping them into what they were in
before and then exchanging.

And of course don't forget that call/ret uses the same stack as well.  If
you want to (for example) return from two levels of subroutines, you could
actually pop the return address (into any register you want) and then do a
ret, which would return to the next return address (that in the function
that called the function that called the current one) assuming nothing
else was pushed on the stack.

There are even stranger ways to use the stack than that.  Since the stack
is not a magical structure, but instead just memory accessed by simple use
of a pointer, it is not limited to just restoring values into the
registers they were in before.





References: