Re: LZ: New method didnt work either


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

Re: LZ: New method didnt work either



Josh Franta wrote:
> 
> 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 FrantaYou must remember that you can only pop and push register pairs.  I liked 
 your first command(ex (sp),hl).  I never knew it existed.  But because 
you must do pairs, the last routine wouldn't work.  It would be something 
like this:
	pop hl		;this pops previous pc into hl, sp is set back
	ex (sp),hl	;this takes the hl off and puts the pc back on.
This would keep the previous pc on the stack, but the byte between it 
owuld be taken off into hl.
wow...easier than I thought it would be.
<pre>
-- 
Compliments of:
_-_-_-_-_-_-_-_
  Alan Bailey
  mailto:bailala@mw.sisna.com
  IRC:Abalone
  Web:http://www.mw.sisna.com/users/bailala/home.htm
</pre>


References: