anon
anon

Reputation: 641

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)?

Upvotes: 7

Views: 3976

Answers (2)

sylvanaar
sylvanaar

Reputation: 8216

It was right there on the wikipedia page above the _cdecl header

In these conventions the caller cleans the arguments from the stack, which allows for variable argument lists, eg. printf().

Upvotes: 7

sbi
sbi

Reputation: 224079

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.

Upvotes: 19

Related Questions