The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Vineel Kumar Reddy Kovvuri on May 09, 2007, 03:45:04 PM

Title: Why wsprintf is not clearing the stack (popping the arguments)
Post by: Vineel Kumar Reddy Kovvuri on May 09, 2007, 03:45:04 PM



Hi Everybody

I am new to assembly and wanted to learn it.
plz help me

why wsprintf is not clearing the arguments on the stack (not popping the arguments)after the function call

where as some of win32 API functions like WriteFile is clearing the stack.

How can i know which function clears the stack and which does not ?

plz reply to this post

thanks in advance

:'(



Title: Re: Why wsprintf is not clearing the stack (popping the arguments)
Post by: BogdanOntanu on May 09, 2007, 04:22:31 PM
AFAIK from Win32API only wsprintf is using C calling conventions. All other API is using STDCALL and this means each function does clear it's own stack before exit.

wsprintf uses C calling convention because the number of parameters is unknown to the function at runtime since  it depends on the format string contents.

So in order to void crashing if a parameter is missing by mistake the stack has to be cleared by the caller. Hopefully the caler knows how many parameters have been pushed and will clear exactly that much no matter of the format string contents... this is the reason behind this...
Title: Re: Why wsprintf is not clearing the stack (popping the arguments)
Post by: Vineel Kumar Reddy Kovvuri on May 09, 2007, 04:48:54 PM


thank you for your reply sir


now i clearly understood the idea behind wsprintf why it is not clearing the stack .

thanks alot

Title: Re: Why wsprintf is not clearing the stack (popping the arguments)
Post by: Vortex on May 09, 2007, 05:06:57 PM
vineel,

Using Ollydbg, try to observe the state of the stack before and after a call to wsprintf, this will help you to understand.
Title: Re: Why wsprintf is not clearing the stack (popping the arguments)
Post by: GregL on May 09, 2007, 05:15:06 PM
vineel,

Just so you know, if you use PROTO C and INVOKE the assembler takes care of balancing the stack for C functions.

Title: Re: Why wsprintf is not clearing the stack (popping the arguments)
Post by: Vineel Kumar Reddy Kovvuri on May 10, 2007, 01:30:48 AM


Thank you Vortex

I used olly and observed the stack .It is crystal clear thanks once again

Thank you Greg

yaa using INVOKE i observed it in olly . The disassembled code automatically erased the stack
by using "add esp 0c "    thank you for your suggestion





Title: Re: Why wsprintf is not clearing the stack (popping the arguments)
Post by: Vortex on May 10, 2007, 10:04:20 AM
Hi vineel,

You are welcome. Ollydbg is one of the best tools for asm coders.