Thursday, November 12, 2009

A little beyond what we know about varargs in functions

Lemme Start first
Have you ever noticed __cdecl and __stdcall in your code.
The major difference between them is

1) In __cdecl , the Calling function pops the arguments from the stack.So functions declared
using __cdecl can support varargs as the called function knows how many arguments it needs to pass(push to stack) and how many arguments it needs to pop from the stack .Assume funcA,funcB,funC call "funcZ" ,then each of the functions that call funcZ should have the code to pop the stack .So the code size becomes more here.

2) In __stdcall ,the Called function pops its own arguments from the stack.
Other differences:
__cdecl
Argument-passing order --> Right to left
Stack-maintenance responsibility --> Calling function pops the arguments from the stack Name-decoration convention --> Underscore character (_) is prefixed to names
Case-translation convention --> No case translation performed

__stdcall
Argument-passing order --> Right to left.
Argument-passing convention --> By value, unless a pointer or reference type is passed.
Stack-maintenance responsibility --> Called function pops its own arguments from the stack.
Name-decoration convention --> An underscore (_) is prefixed to the name. The name is followed by the at sign (@) followed by the number of bytes (in decimal) in the argument list. Therefore, the function declared as int func( int a, double b ) is decorated as follows:

will update with a small example for the above in next blog.


No comments:

Post a Comment