views:

207

answers:

4

Hi I wish to know which one is responsible for cleanup of the stack

suppose you have a function fun lets say like this :-

var = fun(int x, int y, float z, char x);

when fun will get called it will go into the stack along with the parameters then when the function returns who is responsible for cleanup of the stack is it the function it self or the "var" which will hold the return value.

One more thing can anyone explain the concepts of calling conventions?

Let me know if my question is not clear to you.

+4  A: 

calling convention refers to who is doing the cleanup of the stack; caller or callee.

Calling conventions can differ in:

  • where parameters and return values are placed (in registers; on the call stack; a mix of both)
  • the order in which parameters are passed (or parts of a single parameter)
  • how the task of setting up and cleaning up a function call is divided between the caller and the callee.
  • which registers that may be directly used by the callee may sometimes also be included

Architectures almost always have more than one possible calling convention.

Mitch Wheat
+1  A: 

Here's a good bit of reading.

Michael Foukarakis
A: 

By the time that line is complete var will hold the value returned by fun() and any memory on the stack used by fun will be gone: "push", "pop" all tidy.

Calling conventions: everything that the compiler organises so that fun can do its work. Consider those parameters x, y, z. What order do they get pushed onto the stack (indeed do they get passed via the stack)? Doesn't matter so long as the caller and callee agree! It's a convention.

djna
+8  A: 

You referred to the answer yourself: calling conventions.

A calling convention is similar to a contract. It decides the following things:

  • Who is responsible to cleanup the parameters.
  • How and in which order the parameters are passed to the called function.
  • Where the return value is stored.

There are many different calling conventions, depending on the platform and the programming environment. Two common calling conventions on the x86 platforms are:

stdcall

The parameters are passed onto the stack from right to left. The called function cleans up the stack.

cdecl

The parameters are passed onto the stack from right to left. The calling function cleans up the stack.

In both cases the return value is in the EAX register (or ST0 for floating point values)

Many programming languages for the x86 platform allow to specify the calling convention, for example:

Delphi

function MyFunc(x: Integer): Integer; stdcall;

Microsoft C/C++

int __stdcall myFunc(int x)

Some usage notes:

When creating a simple application it's rarely necessary to change or to know about the calling convention, but there are two typical cases where you need to concern yourself with calling conventions:

  • When calling external libraries, Win32 API for example: You have to use compatible calling conventions, otherwise the stack might get corrupted.
  • When creating inline assembler code: You have to know in which registers and where on the stack you find the variables.

For further details I recommend these Wikipedia articles:

DR