Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the caller have to clear the stack in the cdecl calling convention?

From: http://en.wikipedia.org/wiki/X86_calling_conventions

push c
push b
push a
call function_name
add esp, 12 ;Stack clearing
mov x, eax

Why do we need to explicitly add 12 to ESP to clear the stack since the called function should have poped the parameters off the stack therefore restoring the stack pointer...?

Another question:

Theoretically, it would be possible to implement variable parameter functions with the callee taking care of the cleanup right (for instance if you pass the number of arguments on the stack in a register)?

like image 864
anon Avatar asked Oct 08 '09 16:10

anon


People also ask

What is the purpose of the calling convention?

Calling conventions specify how arguments are passed to a function, how return values are passed back out of a function, how the function is called, and how the function manages the stack and its stack frame. In short, the calling convention specifies how a function call in C or C++ is converted into assembly language.

What does cdecl mean in C?

The cdecl (which stands for C declaration) is a calling convention that originates from Microsoft's compiler for the C programming language and is used by many C compilers for the x86 architecture. In cdecl, subroutine arguments are passed on the stack.

What is __ cdecl in C++?

The __cdecl function specifier (C++ only) The __cdecl keyword instructs the compiler to read and write a parameter list by using C linkage conventions. To set the __cdecl calling convention for a function, place the linkage keyword immediately before the function name or at the beginning of the declarator.

What is the difference between Stdcall and cdecl?

In CDECL arguments are pushed onto the stack in revers order, the caller clears the stack and result is returned via processor registry (later I will call it "register A"). In STDCALL there is one difference, the caller doeasn't clear the stack, the calle do. You are asking which one is faster.


1 Answers

Because, with the C calling convention, the called function will not pop the parameters. That's the point of this calling convention.

It allows things like variable arguments.

like image 196
sbi Avatar answered Oct 21 '22 03:10

sbi