您的位置:首页 > 其它

函数调用方式(__cdecl、__stdcall、__fastcall等)

2012-10-06 12:57 537 查看
// TestCall.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>

int __stdcall test_stdcall(char para1, char para2)
{
para1 = para2;
return 0;

}

int __cdecl test_cdecl(char para, ...)
{
char    p = '\n';

va_list marker;

va_start( marker, para );

while( p != '\0' )
{
p = va_arg( marker, char);
printf("%c\n", p);

}

va_end( marker );

return 0;

}

int pascal test_pascal(char para1, char para2)
{
return 0;

}

int __fastcall test_fastcall(char para1, char para2, char para3, char para4)
{
para1 = (char)1;
para2 = (char)2;
para3 = (char)3;
para4 = (char)4;

return 0;

}
__declspec(naked) void __stdcall test_naked(char para1, char para2)
{
__asm
{
push    ebp
mov     ebp, esp

push    eax

mov     al,byte ptr [ebp + 0Ch]
xchg    byte ptr [ebp + 8],al

pop     eax
pop     ebp
ret     8
}

//    return ;

}

int main(int argc, char* argv[])
{
test_stdcall( 'a', 'b' );

test_cdecl( 'c','d','e','f','g' ,'h' ,'\0');

test_pascal( 'e', 'f' );

test_fastcall( 'g', 'h', 'i', 'j' );

test_naked( 'k', 'l');

return 0;
}


vc自身的调试信息:

76:   {
004011C0   push        ebp
004011C1   mov         ebp,esp
004011C3   sub         esp,40h
004011C6   push        ebx
004011C7   push        esi
004011C8   push        edi
004011C9   lea         edi,[ebp-40h]
004011CC   mov         ecx,10h
004011D1   mov         eax,0CCCCCCCCh
004011D6   rep stos    dword ptr [edi]
77:       test_stdcall( 'a', 'b' );
004011D8   push        62h                     ;push 'b'
004011DA   push        61h                     ;push 'a'
004011DC   call        @ILT+15(test_stdcall) (00401014)
78:
79:       test_cdecl( 'c','d','e','f','g' ,'h' ,'\0');
004011E1   push        0
004011E3   push        68h
004011E5   push        67h
004011E7   push        66h
004011E9   push        65h
004011EB   push        64h
004011ED   push        63h
004011EF   call        @ILT+0(test_cdecl) (00401005)
004011F4   add         esp,1Ch            ;由调用函数负责平衡堆栈
80:
81:       test_pascal( 'e', 'f' );
004011F7   push        66h
004011F9   push        65h
004011FB   call        @ILT+10(test_pascal) (0040100f)
82:
83:       test_fastcall( 'g', 'h', 'i', 'j' );
00401200   push        6Ah
00401202   push        69h
00401204   mov         dl,68h
00401206   mov         cl,67h
00401208   call        @ILT+5(test_fastcall) (0040100a)
84:
85:       test_naked( 'k', 'l');
0040120D   push        6Ch
0040120F   push        6Bh
00401211   call        @ILT+25(test_naked) (0040101e)
86:
87:       return 0;
00401216   xor         eax,eax
88:   }
00401218   pop         edi
00401219   pop         esi
0040121A   pop         ebx
0040121B   add         esp,40h
0040121E   cmp         ebp,esp
00401220   call        __chkesp (004012d0)
00401225   mov         esp,ebp
00401227   pop         ebp
00401228   ret


怎么老感觉和书上的有差别呢???
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: