05-汇编补充&面向对象

《C++文章汇总》
上一篇介绍了引用和汇编《04-Reference、引用和Const》,本文介绍汇编指令补充和对象。

1.寄存器命令,imm表示立即数,mov 两遍不能同时放内存地址,只能一边寄存器,一边内存地址

image

2.引用数组的另一种写法 int * const &ref = arr;

int main(int argc, const char * argv[]) {
    //数组名arr其实是数组的地址,也是数组首元素的地址
    //数组名arr可以看做是指向数组首元素的指针(int *)
    int arr[] = {1,2,3};
    int (&ref)[3] = arr;
    ref[1] = 0;
    int * const &ref0 = arr;
    ref0[1] = 0;
    return 0;
}

3.面向对象

◼ C++中可以使用struct、class来定义一个类
◼ struct和class的区别
struct的默认成员权限是public
class的默认成员权限是private

#include 
using namespace std;
struct Car {
    //成员变量
    int m_price;
    //成员函数
    void run(){
        cout << "run():" << m_price << endl;
    }
};
class Person {
    int age;
    void run(){
        cout << "run():" << age << endl;
    }
};
int main(int argc, const char * argv[]) {
    Car car;
    car.m_price = 10;
    car.run();
    Person person;
    person.age = 10;//私有的访问不了
    person.run();//私有的访问不了
    return 0;
}
图片.png
  • 通过指针间接访问person对象
#include 
using namespace std;
struct Car {
    //成员变量
    int m_price;
    //成员函数
    void run(){
        cout << "Car run():" << m_price << endl;
    }
};
struct Person {
    int age;
    void run(){
        cout << "Person run():" << age << endl;
    }
};
int main(int argc, const char * argv[]) {
    Car car;
    car.m_price = 10;
    car.run();
    Person person;
//    person.age = 10;
//    person.run();
    //通过指针间接访问person对象
    Person *p = &person;
    p->age = 30;
    p->run();
    return 0;
}

◼ 每个人都可以有自己的编程规范,没有统一的标准,没有标准答案,没有最好的编程规范
◼ 变量名规范参考
全局变量:g_
成员变量:m_
静态变量:s_
常量:c_
使用驼峰标识

4.对象的内存布局

image
#include "main.hpp"
#include 
using namespace std;

struct Person{
    int m_id;
    int m_age;
    int m_height;
};
int main(){
    Person person;
    person.m_id = 1;
    person.m_age = 2;
    person.m_height = 3;
    cout << "person = " << &person << endl;
    cout << "m_id = " << &person.m_id << endl;
    cout << "m_age = " << &person.m_age << endl;
    cout << "m_height = " << &person.m_height << endl;
    getchar();
    return 0;
}
//输出
person = 0x7ffeefbff4d0
m_id = 0x7ffeefbff4d0
m_age = 0x7ffeefbff4d4
m_height = 0x7ffeefbff4d8

5.this,在函数调用时默认传入调用者的地址给this指针,this是指针,必须用this->m_age访问成员变量,不能用this.m_age,点.左边只能放对象。

#include "main.hpp"
#include 
using namespace std;
struct Person {
    //this指针存储着函数调用者的地址
    //this指向了函数调用者
    int m_age;
    void run(){
        cout << "Person::run " << this->m_age << endl;
    }
};
int main(){
    
    Person person1;
    person1.m_age = 1;
    person1.run();
    
    Person person2;
    person2.m_age = 2;
    person2.run();
    
    return 0;
}
//输出
Person::run 1
Person::run 2

汇编分析this

#include "main.hpp"
#include 
using namespace std;
struct Person {
    //this指针存储着函数调用者的地址
    //this指向了函数调用者
    int m_age;
    void run(){
//        cout << "Person::run " << this->m_age << endl;
        this->m_age = 3;
        //m_age = 3;this省略了,查看汇编和this->m_age是一样的
        //汇编
        /*
         //ecx中存放的是person1对象的地址
         //ebp-8是this指针的地址,存放person对象的地址
         //ebp-8存放的地址值赋值给eax寄存器
         //将3放入eax寄存器中存放的地址值的内存空间
         mov dword ptr [ebp-8],ecx
         mov eax,dword ptr [ebp-8]
         mov dword ptr [eax],3
         */
    }
};
int main(){
    
    Person person1;
    person1.m_age = 1;
    person1.run();
    
    Person person2;
    person2.m_age = 2;
    person2.run();
    return 0;
}

原理:如何利用指针间接访问所指向对象的成员变量?
1.从指针中取出对象的地址
2.利用对象的地址+成员变量的偏移量计算出成员变量的地址
3.根据成员变量的地址访问成员变量的存储空间

int main(){
    /*
    Person person1;
    person1.m_age = 1;
    person1.run();
    
    Person person2;
    person2.m_age = 2;
    person2.run();
     */
    
    Person person;
    person.m_id = 10;
    person.m_age = 20;
    person.m_height = 30;
    person.display();
    /*
    dword ptr [ebp-14h],0Ah
    dword ptr [ebp-10h],14h
    dword ptr [ebp-0Ch],1Eh
     */
    Person *p = &person;
    p->m_id = 10;
    p->m_age = 10;
    p->m_height = 10;
    p->display();
    /*
     //Person *p = &person
     //ebp-14h是person对象的地址
     lea eax,[ebp-14h]
     //ebp-20h是指针变量p的地址,存放对象person的地址
     mov dword ptr [ebp-20h],eax
     //eax存储的是person对象的地址,也就是person.m_id的地址
     //p->m_id = 10;
     mov eax,dword ptr [ebp-20h]
     mov dword ptr [eax],0Ah
     
     //p->m_age = 10;
     mov eax,dword ptr [ebp-20h]
     mov dword ptr [eax+4],0Ah
     
     //p->m_height = 10
     mov eax,dword ptr [ebp-20h]
     mov dword ptr [eax+8],0Ah
     
     mov eax,dword ptr [ebp-20h]
     call 003B141F
     */
    return 0;
}

通过对象直接访问成员变量生成一句汇编代码,通过指针访问成员变量生成两句汇编代码,直接访问效率固然高些,有些地方只能用指针,比如堆空间只能通过地址值去访问堆空间的数据

思考题:Person *p = (Person *)&person.m_age;window下输出10,40,50

Person person;
person.m_id = 10;
person.m_age = 20;
person.m_height = 30;
person.display();
    
Person *p = (Person *)&person.m_age;
//eax == &person.m_age == &person + 4
//mov eax,dword ptr [p]
//mov dword ptr [eax+0],40
//mov dword ptr [&person+4+0],40
p->m_id = 40;
//mov dword ptr [eax + 4],50
//mov dword ptr [&person + 4 + 4],50
p->m_age = 50;
person.display();
//输出
id = 10, age = 20, height = 30
id = 10, age = 40, height = 50

此时若用指针变量p->display()调用方法,结果为40,50,0,windows平台为40,50,-858993460(0xcccccccc),为什么会是cccccccc填充而不是00000000呢?新开辟的栈空间里面存满了cccccccc汇编指令,表示中断:interrupt,cc->int3:起到断点的作用,函数的栈空间不小心被当成代码来执行也很安全,全时断点

struct Person {
    int m_id;
    int m_age;
    int m_height;
    void display(){
        //eax == &person.m_age == &person+4
        //mov eax,dword ptr [this]
        //[eax]、[eax+4]、[eax+8]
        //[&person+4]、[&person+4+4]、[&person+4+8]
        //40           50            0xcccccccc
        cout << "id = " << m_id << ", age = " << m_age << ", height = " << m_height << endl;
    }
};
int main(){ 
    Person person;
    person.m_id = 10;
    person.m_age = 20;
    person.m_height = 30;
    person.display();
    Person *p = (Person *)&person.m_age;
    p->m_id = 40;
    p->m_age = 50;
    p->display();
}
//输出
id = 10, age = 20, height = 30
id = 40, age = 50, height = 0

6.内存空间布局

◼ 每个应用都有自己独立的内存空间,其内存空间一般都有以下几大区域
代码段(代码区)
✓ 用于存放代码
数据段(全局区)
✓ 用于存放全局变量等
栈空间
✓ 每调用一个函数就会给它分配一段连续的栈空间,等函数调用完毕后会自动回收这段栈空间
✓ 自动分配和回收
堆空间
✓ 需要主动去申请和释放


image

调用函数,执行函数代码,其实就是CPU在访问代码区的内存(指令),调用函数的时候,需要分配额外的存储空间栈空间来存储函数内部的局部变量,为什么不在代码区开辟空间存储函数内部的局部变量呢,因为代码区是只读的,不能改
函数代码存储在代码区
局部变量存储在栈空间

你可能感兴趣的:(05-汇编补充&面向对象)