출처:http://redstack.net/blog/2008/01/16/x86-calling-conventions/
x86 calling conventions
This is the first article of a (I hope) long series of articles about ‘The Basics: What everyone should know about’
The calling convention defines the way a function or a piece of code should arrange data before calling a function, and what to do after. It responds to questions like “In which order should I pass the arguments ?”, “Should I clean something ?”, “Where is the result ?”, …
There is a lot of different calling conventions. Here are the 3 I see the most of the time:
- cdecl
- stdcall
- fastcall
cdecl convention
The cdecl convention is the default one used when working with a C compiler like GCC or MSVC. To use the cdecl scheme for a function, you can use this syntax (GCC):
GCC will produce the following code when calling a cdecl function with 4 arguments :
As you can see, arguments are pushed into the stack in right to left order, and it’s up to the caller to remove the arguments from the stack (Here this is done by add esp, 0x10
). The result of the function is stored in the EAX register.
stdcall convention
The stdcall convention is the one used by Win32 APIs. It’s also the easyest to use when writing ASM code, in my opinion. A function can be declared as a stdcall function in C with this syntax (GCC):
GCC will produce the following code when calling a stdcall function with 4 arguments :
As for the cdecl calling style, arguments are pushed from right to left, but in stdcall mode, the caller doesn’t have to clean the arguments from the stack after calling the function. A stdcallfunction removes arguments from the stack before returning. This is done by using the ret n
instruction most of the time.
Like for cdecl, result is in EAX.
fastcall convention
The fastcall convention is not standardized, but we will watch the way GCC and MSVC handle it. A function can be declared as afastcall function in C with this syntax (GCC):
GCC will produce the following code when calling a stdcall function with 4 arguments :
As you can see, not all the arguments are pushed into the stack. The first two arguments are passed via the ECX, for the first argument, and EDX, for the second argument. The remaining arguments are pushed into the stack from right to left. The called function has to pop the arguments from the stack before returning, like for stdcall.
The result is, as usual, in EAX
'프로그래밍' 카테고리의 다른 글
[일반][프로그래밍]비대칭경계 (0) | 2013.10.26 |
---|---|
pair coding 를 하기 위한 자세 (0) | 2012.04.27 |
콘솔상 동작 상태 애니메이션 (0) | 2012.04.12 |
면접 때, 이 정도는 1분안에 할 수 있어야 된다. (1) | 2012.04.08 |
TLS, Thread-Local Storage (0) | 2012.02.20 |