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
最近的回應