Re: A83: Sub-routines


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

Re: A83: Sub-routines




Olle Hedman wrote:
> 
> you could pop the return address for the subroutine from the stack, so when the
> ret comes, the returnaddress of your entire program is on top of the stack, and
> the program quits.. but that only works if you havn't pushed anything before the
> call to the subroutine..
> to get around that, you can, in the beginning of the program, pop the
> returnaddress, save it in a variable, an push it back on..   then if you want to
> quit in a subroutine, you just load the saved address and push it on the stack
> before the ret.

Bad idea.  By doing that, you've lost everything before that on the
stack (memory leak), and that also assumes that whatever code will be
returned to didn't have anything on the stack that it needs (for
example, preserved registers).

A better idea, IMHO, would be to save the value of the stack pointer and
set it back when you want to return (...if this is possible).  Just
realize that no matter how you exit from a sub-routine, since this is in
assembly and the caller could have been doing anything, you are possibly
leaving the machine in an unsafe state.

-- 
John Kugelman.  jkugelman@mgfairfax.rr.com

I believe we can change anything.
I believe in my dream.
    - Joe Satriani


Follow-Ups: References: