优秀的编程知识分享平台

网站首页 > 技术文章 正文

C++|从汇编代码角度理解类和对象提供的命名空间、地址偏移

nanyue 2024-08-26 17:56:59 技术文章 5 ℃

假设对某一个小问题的代码有两个写法A,B,其中A使用较简单的语法,需要写较多的代码;B使用较抽象的语法,只需要写较少的代码。B使用的语法是对A使用的语法的抽象,称为语法糖,其中的对应关系由编译器来实现,如:

int arr[5] = {1,2,3};
arr[2] = 33; 
*(arr+2) = 33; 
// 可以理解为前一种写法是后一种写法的语法糖

我们知道,从机器语言到汇编语言到高级语言,实现了逐层抽象,中间的翻译工作由翻译程序汇编器和编译器来实现。高层抽象可以理解为低层抽象的语法糖。

一定程度上,面向对象可以理解为面向过程的语法糖,或者说,两者只是组织代码的方式发生了变化而已,数据可以聚合到结构体或类,而函数以结构体为参数,或函数隐含一个this指针做参数,指向类对象(当然,这里是就抽象和封装而言,类还提供更多的功能,如RAII,继承,多态,RTTI等):

#include <iostream>
using namespace std;

// 角色c1 长方形接口定义者,可放到头文件,使用文件来区分模块
struct CRect{
    int lenth;
    int width;
};
int area(struct CRect *cr);

// 角色c2 长方形接口实现者,可以放到实现文件
int area(struct CRect *cr){
    return cr->lenth * cr->width;
}

// 角色c3 长方形接口使用者,可以放到使用文件
int calc()
{
    struct CRect cr;
    cr.lenth = 0x20;
    cr.width = 0x10;
    int ar = area(&cr);
    return ar;
}

// 角色cpp1 长方形接口定义者,可放到头文件,使用文件和类来区分模块
class CppRect{
public:
    int lenth;
    int width;
    int area();
};

// 角色cpp2 长方形接口实现者,可以放到实现文件
int CppRect::area(){
    return lenth * width;
}

// 角色cpp3 长方形接口使用者,可以放到使用文件
int calc2()
{
    CppRect cr;
    cr.lenth = 0x20;
    cr.width = 0x10;
    int ar = cr.area();
    return ar;
}

int main()
{
    printf("using struct: rect's area is %d\n", calc());
    printf("using class: rect's area is %d\n", calc2());
    while(1);
    return 0;
}
/*
using struct: rect's area is 512
using class: rect's area is 512
*/

从以上实例可见,数据的聚合没有区别,都是由结构体变量或类对象提供一个基地址,而数据成员提供一个相对于基地址的偏移。处理结构体的全局函数以结构体变量或指针为参数,而类的成员函数的调用则是以类对象来修饰,提供一个隐含的this指针做参数,指向类对象。当然,类为类成员函数提供了一个命名空间。

从下面汇编代码可见,两者没有太大的区别:

19:       struct CRect cr;
20:       cr.lenth = 0x20;
004015D8   mov         dword ptr [ebp-8],20h
21:       cr.width = 0x10;
004015DF   mov         dword ptr [ebp-4],10h
22:       int ar = area(&cr);
004015E6   lea         eax,[ebp-8] // 全局函数参数地址放到寄存器
004015E9   push        eax    
004015EA   call        @ILT+650(area) (0040128f)  // 全局函数调用
004015EF   add         esp,4
004015F2   mov         dword ptr [ebp-0Ch],eax
23:       return ar;
004015F5   mov         eax,dword ptr [ebp-0Ch]
24:   }

42:       CppRect cr;
43:       cr.lenth = 0x20;
00401678   mov         dword ptr [ebp-8],20h
44:       cr.width = 0x10;
0040167F   mov         dword ptr [ebp-4],10h
45:       int ar = cr.area();
00401686   lea         ecx,[ebp-8]  // 成员函数this指针放到寄存器
00401689   call        @ILT+75(CppRect::area) (00401050) // 成员函数调用
0040168E   mov         dword ptr [ebp-0Ch],eax
46:       return ar;
00401691   mov         eax,dword ptr [ebp-0Ch]
47:   }

如果结构体变量或类对象定义在全局区,汇编后,其实也看不到什么对象的影子了:

49:   struct CRect g_rect;
50:   void test(){
004016E0   push        ebp
004016E1   mov         ebp,esp
004016E3   sub         esp,40h
004016E6   push        ebx
004016E7   push        esi
004016E8   push        edi
004016E9   lea         edi,[ebp-40h]
004016EC   mov         ecx,10h
004016F1   mov         eax,0CCCCCCCCh
004016F6   rep stos    dword ptr [edi]
51:       g_rect.length = 0x20;
004016F8   mov         dword ptr [g_rect (0047cde8)],20h  // 全局区结构体数据成员赋值
52:       g_rect.width = 0x10;
00401702   mov         dword ptr [g_rect+4 (0047cdec)],10h
53:       printf("using struct to defile a globle:%d\n",area(&g_rect));
0040170C   push        offset g_rect (0047cde8)
00401711   call        @ILT+660(area) (00401299)
00401716   add         esp,4
00401719   push        eax
0040171A   push        offset string "using struct to defile a globle:"... (0046f01c)
0040171F   call        printf (00420860)
00401724   add         esp,8
54:   }
// ……
 56:   CppRect g_cppRect;
57:   void test2(){
00401750   push        ebp
00401751   mov         ebp,esp
00401753   sub         esp,40h
00401756   push        ebx
00401757   push        esi
00401758   push        edi
00401759   lea         edi,[ebp-40h]
0040175C   mov         ecx,10h
00401761   mov         eax,0CCCCCCCCh
00401766   rep stos    dword ptr [edi]
58:       g_cppRect.length = 0x20;
00401768   mov         dword ptr [g_cppRect (0047cdf0)],20h // 全局区对象数据成员赋值
59:       g_cppRect.width = 0x10;
00401772   mov         dword ptr [g_cppRect+4 (0047cdf4)],10h
60:       printf("using class to defile a globle:%d\n",g_cppRect.area());
0040177C   mov         ecx,offset g_cppRect (0047cdf0)
00401781   call        @ILT+75(CppRect::area) (00401050)
00401786   push        eax
00401787   push        offset string "using class to defile a globle:%"... (0046f048)
0040178C   call        printf (00420860)
00401791   add         esp,8
61:   }
004017A6   int         3

以下代码可见类对象相对于类成员的基地址及命名空间功能:

#include <iostream>
using namespace std;

class Person{
public:
    int m_id;
    int m_age;
    int m_height;
    void display(){  //类名给成员函数提供了一个命名空间
        cout<<m_id<<","
            <<m_age<<","
            <<m_height<<endl;
    }
};

void test(){
    Person person;
    person.m_id = 10;
    person.m_age = 20;
    person.m_height = 30;
    person.display();
    Person *p = (Person*)&person.m_age; // p提供了一个新的偏移基准
    p->m_id = 40; // 此处的成员只提供基于p到m_id的偏移:p+sizeof(int)*2
    p->m_age = 50;// 此处的成员只提供基于p到m_id的偏移:p+sizeof(int)*3
    cout<<person.m_id<<","
        <<person.m_age<<","
        <<person.m_height<<endl;
    person.display();
    p->display(); // 编译器通过类对象名给函数提供了一个隐含的this指针参数
    // this指针等于p的值,其成员变量的地址是相对于p处的偏移
}

void display(){  // 并没有命名冲突
    ;
}

void test2(){  // 不使用实例来也可以调用成员函数
    cout<<"test2:\n";
    Person person;
    person.m_id = 10;
    person.m_age = 20;
    person.m_height = 30;
    typedef void (*funcP)();
    funcP fp = NULL;
    void (Person::*fpp)() = &Person::display; // 成员函数指针
    memcpy(&fp,&fpp,sizeof(fpp));
    __asm lea ecx, person; // x86编译器将this指针存放在寄存器ecx中
    fp();               // 不通过实例person间接调用Person::diaplay()
    (person.*fpp)();    // 通过函数指针调用Person::diaplay()
}

int main()
{
    
    test();
    test2();
    while(1);
    return 0;
}
/*
10,20,30
10,40,50
10,40,50
40,50,1245000
*/

从以上可知,通过对象指针访问数据成员时,数据成员名提供的是一个相对于基地址的偏移的功能。成员函数定义时,类名给成员函数提供了一个命名空间,类对象调用成员函数时,编译器通过类对象名给函数提供了一个隐含的this指针参数。

-End-

Tags:

最近发表
标签列表