RE: LZ: Me agian Same Problem
[Prev][Next][Index][Thread]
RE: LZ: Me agian Same Problem
On Thu, 26 Sep 1996, Frank wrote:
> can u explain what push and pop do ?
There is a register in the cpu called sp. That stands
for stack pointer. It contains the address of the
hardware stack, which is simply an area of ram that you
can move things to temporarily. Push subtracts 2 from
the stack pointer and moves the contents of the 16 bit
variable you're pushing to the resulting address. Pop
does the reverse. It moves the object back into the
register youre popping, not necessarily the same register,
and adds 2 to the stack pointer.
It's almost as simple as that but theres one more thing to
be aware of. When you do a call, the way the cpu knows
where to return to is that it pushes the program counter (pc)
and then the return pops it back into the pc. The pc is
the register that the cpu uses to know what instruction to
execute next.
The result of this is that if you do a call or a return,
the stack HAS to be where it should be. If you call a
subroutine and then do a push in it, you have to do a
corresponding pop before the ret. If you don't, you'll
almost always clear your memory and you'll often lock up
the computer and have to remove the batteries.
One thing I might be telling you wrong is the name of the
pc. Some cpu's call it the Instruction Pointer or IP. I
don't remember which one the Z80 uses.
Hang in with that thing. You're doing just what you need
to be doing to learn this stuff and you picked a good first
project.
Barry
References: