The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: turbo_nutter on September 23, 2005, 12:25:13 AM

Title: pop's vs direct stack references
Post by: turbo_nutter on September 23, 2005, 12:25:13 AM
I have quite a simple question - why is it that when a function is being called we PUSH signature attributes onto the stack
THEN the function being called, instead of POP'ing the data off the stack, it uses direct stack pointer instructions - eg

push somecrap
call afunction

afunction:
.....(obligatory stack instructions)
mov eax,dword ptr ss:[esp+somcrapSTACKLOCATION]
...

Title: Re: pop's vs direct stack references
Post by: Ratch on September 23, 2005, 03:05:54 AM
turbo_nutter,
     The simple answer is that the last item PUSHed is the return address.  That will be the first thing POPed, and unless you save it, you will be unable to return to where the subroutine was called.  Also a stack read can be done any number of times, but a POPed value has to be saved if you want to use it more than once.  Ratch
Title: Re: pop's vs direct stack references
Post by: hutch-- on September 23, 2005, 03:20:17 AM
nutter,

As long as the function you call balances the stack on exit or with C the stack is balanced by the caller, what you push before your call can be popped after the stack cleanup. With STDCALL you normall use RET NUMBER to balace the stack where with a C call you do an ADD ESP, number after the call has returned.
Title: Re: pop's vs direct stack references
Post by: turbo_nutter on September 23, 2005, 05:44:13 AM
nice one! thanks - makes things a lot more clear!