文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>汇编角度看函数重载

汇编角度看函数重载

时间:2010-09-24  来源:老钟古

由汇编代码我们可以清楚的看到,C++中的函数重载对于非对象的重载基本上是一样的,只是在调用的时候采用不同的名称实现(VC++ 6.0实现)

程序的代码:

代码 #include <iostream>
using namespace std;


void output( int x );
void output( float x );

void output( int x ) {
    cout << "output int " << x << endl;
}

void output( float x ) {
    cout << "output float " << x << endl;
}


int main() {

    int x = 1;
    float y = 1.0;
    output( x );
    output( y );
    output( 1 );
    //output( 0.5 );
    output( int( 0.5 ) );
    output( float( 0.5 ) );

    return 0;
}

 

 

两个函数重载的代码:

代码  1 25:   void output( int x ) {
 2 004015A0   push        ebp
 3 004015A1   mov         ebp,esp
 4 004015A3   sub         esp,40h
 5 004015A6   push        ebx
 6 004015A7   push        esi
 7 004015A8   push        edi
 8 004015A9   lea         edi,[ebp-40h]
 9 004015AC   mov         ecx,10h
10 004015B1   mov         eax,0CCCCCCCCh
11 004015B6   rep stos    dword ptr [edi]
12 26:       cout << "output int " << x << endl;
13 004015B8   push        offset @ILT+200(std::endl) (004010cd)
14 004015BD   mov         eax,dword ptr [ebp+8]
15 004015C0   push        eax
16 004015C1   push        offset string "output int " (0046c01c)
17 004015C6   push        offset std::cout (004777f0)
18 004015CB   call        @ILT+655(std::operator<<) (00401294)
19 004015D0   add         esp,8
20 004015D3   mov         ecx,eax
21 004015D5   call        @ILT+255(std::basic_ostream<char,std::char_traits<char> >::operator<<) (00401104)
22 004015DA   mov         ecx,eax
23 004015DC   call        @ILT+490(std::basic_ostream<char,std::char_traits<char> >::operator<<) (004011ef)
24 27:   }
25 004015E1   pop         edi
26 004015E2   pop         esi
27 004015E3   pop         ebx
28 004015E4   add         esp,40h
29 004015E7   cmp         ebp,esp
30 004015E9   call        __chkesp (00420d20)
31 004015EE   mov         esp,ebp
32 004015F0   pop         ebp
33 004015F1   ret
34 
35 
36 
37 29:   void output( float x ) {
38 00401610   push        ebp
39 00401611   mov         ebp,esp
40 00401613   sub         esp,40h
41 00401616   push        ebx
42 00401617   push        esi
43 00401618   push        edi
44 00401619   lea         edi,[ebp-40h]
45 0040161C   mov         ecx,10h
46 00401621   mov         eax,0CCCCCCCCh
47 00401626   rep stos    dword ptr [edi]
48 30:       cout << "output float " << x << endl;
49 00401628   push        offset @ILT+200(std::endl) (004010cd)
50 0040162D   mov         eax,dword ptr [ebp+8]
51 00401630   push        eax
52 00401631   push        offset string "output float " (0046c02c)
53 00401636   push        offset std::cout (004777f0)
54 0040163B   call        @ILT+655(std::operator<<) (00401294)
55 00401640   add         esp,8
56 00401643   mov         ecx,eax
57 00401645   call        @ILT+305(std::basic_ostream<char,std::char_traits<char> >::operator<<) (00401136)
58 0040164A   mov         ecx,eax
59 0040164C   call        @ILT+490(std::basic_ostream<char,std::char_traits<char> >::operator<<) (004011ef)
60 31:   }
61 00401651   pop         edi
62 00401652   pop         esi
63 00401653   pop         ebx
64 00401654   add         esp,40h
65 00401657   cmp         ebp,esp
66 00401659   call        __chkesp (00420d20)
67 0040165E   mov         esp,ebp
68 00401660   pop         ebp
69 00401661   ret
70 
71 
72 

 

 

函数在进行调用的时候的不同名称:


 

代码 38:       int x = 1;
00401698   mov         dword ptr [ebp-4],1
39:       float y = 1.0;
0040169F   mov         dword ptr [ebp-8],3F800000h
40:       output( x );
004016A6   mov         eax,dword ptr [ebp-4]
004016A9   push        eax
004016AA   call        @ILT+260(output) (00401109)
004016AF   add         esp,4
41:       output( y );
004016B2   mov         ecx,dword ptr [ebp-8]
004016B5   push        ecx
004016B6   call        @ILT+165(output) (004010aa)
004016BB   add         esp,4
42:       output( 1 );
004016BE   push        1
004016C0   call        @ILT+260(output) (00401109)
004016C5   add         esp,4
43:       //output( 0.5 );
44:       output( int( 0.5 ) );
004016C8   push        0
004016CA   call        @ILT+260(output) (00401109)
004016CF   add         esp,4
45:       output( float( 0.5 ) );
004016D2   push        3F000000h
004016D7   call        @ILT+165(output) (004010aa)
004016DC   add         esp,4

 

 

相关阅读 更多 +
排行榜 更多 +
三角符文第一章下载

三角符文第一章下载

角色扮演 下载
嘀嘀动画官方正版下载

嘀嘀动画官方正版下载

趣味娱乐 下载
像素世界僵尸危机安卓版

像素世界僵尸危机安卓版

飞行射击 下载