Re: LZ: New method didnt work either


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

Re: LZ: New method didnt work either



C.J. Oster wrote:
> 
> When you pop the registers, then it sets the registers to the values that
> they were set to before the call was made.  Understand?


That's not totally correct. The way it really works is that when you
make a call, the PC (program counter) get's set to the address of the
new instruction you want to execute, and the next instruction (that the
PC would normally contain if there was no call) get's pushed on the
stack. You can push and pop within the routine, as long as when the PC
hits the *ret* op code, the stack pointer points at the address that our
*call* put on the stack. For example:


This is incorrect:
-----------------------------
	PUSH H		;H gets put on stack
	CALL ORDER	;PC contents (next instruction) gets put on stack
	...		;more code
ORDER:
	POP H		;H is address of next instruction (after routine ends)
	...		;more code
	RET		;PC now contains what was H ???PROGRAM CRASH???
-----------------------------


If I had put a pop after my *CALL ORDER* (as you suggest), it would have
never gotten there.
You could use the *EX (SP),HL* command to make this particular example
work, or you could increment the stack pointer (INC SP), pop h (POP H),
then decrement the stack pointer so it pointed to the correct address
(DEC SP). I think I've gotten this right... somebody let me know if I
made a mistake somewhere...


Josh Franta


References: