Re: A82: push, pop


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

Re: A82: push, pop




In a message dated 98-09-22 20:30:11 EDT, you write:

> I am still trying to learn assembly and I've found a lot of program that 
>  uses the push-pop command, what does it do exactly?

the PUSH command "push"'s the specified 16-bit register's contents to the top
of the stack, and the POP command, likewise, "pop"'s the specified 16-bit
register's contents from the top of the stack.  This is used to save registers
if you want to keep them constant but have a snippet of program that changes
the register.
The stack is LIFO, or Last In First Out, so remember if you have multiple
push's and pop's to reverse the order when the pops occur:

push bc
push hl
...
code that modifies bc and/or hl
...
pop hl
pop bc

Also, the stack doesn't care what register was pushed when, so you can push
one register and pop another one back, effectively transferring the contents
of one register to the other:

push hl
...
code that modifies hl
...
pop de

this is perfectly legal and sometimes very useful since there are no 16-bit
load commands.  However, if you just want to do a 16-bit load, it is better to
do each register separately:

;this is bad:
push hl
pop de

;this is good:
ld d, h
ld e, l

One last thing to remember is that you can only push and pop _16-BIT_
registers, so an instruction such as push a or pop d would be illegal
I hope that wasn't too long...

~Adamman