維尼的蜂巢

__stdcall ,__cdecl的區別 再次探討

Posted by: kevinlin on: 六月 17, 2008

前面寫了這篇,感覺很亂,再來深入探討一次

看了這篇後,我覺得用組合語言來敘述差異似乎更加的清楚明白

__cdecl 之前說 由呼叫者來調整堆疊

//函式 foo(int arg1, int arg2, int arg3)
push     arg3
push    arg2
push    arg1
call       foo
add      sp, 12         //相當於 pop; pop; pop

__stdcall 之前說 由被呼叫者調整堆疊,這樣的做法可以節省程式的大小

//函式 foo(int arg1, int arg2, int arg3)
push    arg3
push    arg2
push    arg1
call      foo
//stack 的清除工作會在 foo 函式內完成

上面兩個差異MSDN有告訴我們:
The __cdecl calling convention creates larger executables than __stdcall, because it requires each function call to include stack cleanup code. The following list shows the implementation of this calling convention.

__fastcall 會使用暫存器來傳遞參數 (如 ecx, edx) 以加速程式的運作

//函式 foo(int arg1, int arg2, int arg3)
push    arg3
mov     edx, arg2
mov     ecx, arg1
call      foo

通常__cdecl 都會被當成預設的,可以省略它,除非我們用了/Gz (stdcall) 或 /Gr (fastcall) ,/Gd就是強制給__cdecl
Linker 參數
__cdecl : cl /Gd
__stdcall : cl /Gz
__fastcall : cl / Gr

留言

Add to Google

這是一個月曆

六月 2008
W
« 五月   七月 »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

最近的回應

simon on 留言板
亞當 on 玩WII不用放光碟
北方鹰 on error LNK2005 operator new(uns…
kevinlin on 留言板
小鬼 on 留言板