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

 1 // TestCall.cpp : Defines the entry point for the console application.
 2  //
 3  
 4  #include "stdafx.h"
 5  #include <windows.h>
 6  #include <stdio.h>
 7  
 8  
 9  int __stdcall test_stdcall(char para1, char para2)
10  {
11      para1 = para2;
12      return 0;
13  
14  }
15  
16  int __cdecl test_cdecl(char para, ...)
17  {
18      char    p = '\n';
19  
20      va_list marker;
21  
22      va_start( marker, para );
23  
24      while( p != '\0' )
25      {
26          p = va_arg( marker, char);
27          printf("%c\n", p);
28  
29      }
30  
31      va_end( marker );
32  
33      return 0;
34  
35  }
36  
37  int pascal test_pascal(char para1, char para2)
38  {
39      return 0;
40  
41  }
42  
43  int __fastcall test_fastcall(char para1, char para2, char para3, char para4)
44  {
45      para1 = (char)1;
46      para2 = (char)2;
47      para3 = (char)3;
48      para4 = (char)4;
49  
50      return 0;
51  
52  }
53  __declspec(naked) void __stdcall test_naked(char para1, char para2)
54  {
55      __asm
56      {
57          push    ebp
58          mov     ebp, esp
59  
60          push    eax
61  
62          mov     al,byte ptr [ebp + 0Ch]
63          xchg    byte ptr [ebp + 8],al
64          
65          pop     eax
66          pop     ebp
67          ret     8
68      }
69  
70  
71  
72  //    return ;
73  
74  }
75  
76  
77  int main(int argc, char* argv[])
78  {
79      test_stdcall( 'a', 'b' );
80  
81      test_cdecl( 'c','d','e','f','g' ,'h' ,'\0');
82  
83      test_pascal( 'e', 'f' );
84  
85      test_fastcall( 'g', 'h', 'i', 'j' );
86  
87      test_naked( 'k', 'l');
88  
89      return 0;
90  }
91  

vc自身的调试信息:

 1 76:   {
 2  004011C0   push        ebp
 3  004011C1   mov         ebp,esp
 4  004011C3   sub         esp,40h
 5  004011C6   push        ebx
 6  004011C7   push        esi
 7  004011C8   push        edi
 8  004011C9   lea         edi,[ebp-40h]
 9  004011CC   mov         ecx,10h
10  004011D1   mov         eax,0CCCCCCCCh
11  004011D6   rep stos    dword ptr [edi]
12  77:       test_stdcall( 'a', 'b' );
13  004011D8   push        62h                     ;push 'b'
14  004011DA   push        61h                     ;push 'a'
15  004011DC   call        @ILT+15(test_stdcall) (00401014)
16  78:
17  79:       test_cdecl( 'c','d','e','f','g' ,'h' ,'\0');
18  004011E1   push        0
19  004011E3   push        68h
20  004011E5   push        67h
21  004011E7   push        66h
22  004011E9   push        65h
23  004011EB   push        64h
24  004011ED   push        63h
25  004011EF   call        @ILT+0(test_cdecl) (00401005)
26  004011F4   add         esp,1Ch            ;由调用函数负责平衡堆栈
27  80:
28  81:       test_pascal( 'e', 'f' );
29  004011F7   push        66h
30  004011F9   push        65h
31  004011FB   call        @ILT+10(test_pascal) (0040100f)
32  82:
33  83:       test_fastcall( 'g', 'h', 'i', 'j' );
34  00401200   push        6Ah
35  00401202   push        69h
36  00401204   mov         dl,68h
37  00401206   mov         cl,67h
38  00401208   call        @ILT+5(test_fastcall) (0040100a)
39  84:
40  85:       test_naked( 'k', 'l');
41  0040120D   push        6Ch
42  0040120F   push        6Bh
43  00401211   call        @ILT+25(test_naked) (0040101e)
44  86:
45  87:       return 0;
46  00401216   xor         eax,eax
47  88:   }
48  00401218   pop         edi
49  00401219   pop         esi
50  0040121A   pop         ebx
51  0040121B   add         esp,40h
52  0040121E   cmp         ebp,esp
53  00401220   call        __chkesp (004012d0)
54  00401225   mov         esp,ebp
55  00401227   pop         ebp
56  00401228   ret

怎么老感觉和书上的有差别呢???

原文地址:https://www.cnblogs.com/tk091/p/2712908.html