[A83] Re: returning to OS in call routines


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

[A83] Re: returning to OS in call routines



Well, there are two things you could do in this situation, both require some knowledge on how "call" works.

when you call a routine, the current address is pushed on the stack, and it jumps to where you called.  then, when it hits a "ret" it pops that value and jumps back to there + however many bytes to go to the next instruction, i forget how many.  if you know this, you can do one or two things to exit from a call:

1) save the stack when the program starts.  when you want to exit, jump to an exit label and restore that stack

2) you can pop the value put on by the call into any register and then ret.  for nested routines, you'd have to do it more than one time...

call foo
...
foo:
...
;want to exit?
  pop hl
  ret    ;will exit... for nested calls, do more than one pop


you can use this information about call with push/pop to your advantage.  for instance, i see many people's code call a routine and then do a ret:

call routine1
ret

that can be shortened to:
jp routine1

because the ret at the end of routine1 will jump back to where you want

hope this helps
-joel