0

Hi everyone and sorry for the silly question. Let's say that "main" calls an "add" function (pseudocode):

main { add(5,10) }

The stack would be something similar:

...main...
|  10   |
|  05   |
|  RET  |
|  EBP  |
...add...

The stack frame of "add" starts from EBP or include also 10, 5 (args) and RET? In a CDECL calling convention the caller (main) is in charge of cleaning the stack after the "add" function returns. I suppose clean means "remove" (by increasing esp) the arguments from the stack, not the local vars of the "add" function, right?

Thanks a lot

  • 1
    locals are cleaned by the called function arguments are cleaned up by the callee in __cdecl – blabb Feb 2 '16 at 4:23
  • "arguments are cleaned up by the callee in __cdecl" you mean by the caller? – Claudio Feb 2 '16 at 21:42
  • add(5,10) add(int x , int y ) {a,b,c,d ...x ,y} abcd will be cleaned by add() and 5 ,10 will be cleaned by the parent after returning from the call the function that calls in your case main is the callee or if you prefer the caller – blabb Feb 5 '16 at 19:33
1

The short answer is sometimes. Depending on the calling convention and the compiler, some arguments may be stored in registers rather than on the stack. See this wikipedia article: https://en.wikipedia.org/wiki/X86_calling_conventions#Microsoft_x64_calling_convention

To be a little more clear, if the caller pushes an argument on the stack, the caller is typically responsible for cleaning it up. According to the C specification, a function does not know how many arguments are being passed in to the function (consider functions like printf() and friends) The code that knows how many arguments are actually on the stack and how to deal with them is the calling function. All this is convention, however. C makes very few requirements for how this all actually ties together on any given physical machine.

In practical code, you will usually see sub ESP, in the calling function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.