从汇编代码学习C++语言1—类对象构造函数与析构函数
2012-12-18 23:42
585 查看
从汇编代码学习C++语言1—类对象构造函数与析构函数
类对象必须经历初始化与销毁两个过程,类的构造函数与析构函数担当此重任。本文作为从汇编代码学习C++语言系列的第一篇,来分析类对象如何构造与销毁。
示例代码1:
类对象必须经历初始化与销毁两个过程,类的构造函数与析构函数担当此重任。本文作为从汇编代码学习C++语言系列的第一篇,来分析类对象如何构造与销毁。
示例代码1:
class Object { public: int code1; int code2; }; int main(int argc, char *argv[]) { int i = 0; Object c; //func(1, c, 2); return 0; }示例代码1没有实现构造函数与析构函数,汇编代码如下:
Disassembly of section .text: 00000000 <_main>: 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 83 e4 f0 and $0xfffffff0,%esp //Memory Alignment 6: 83 ec 10 sub $0x10,%esp //Assign stack Memory, 16Bytes 9: e8 00 00 00 00 call e <_main+0xe> e: c7 44 24 0c 00 00 00 movl $0x0,0xc(%esp) //Assign 0 to Memory Address(esp + 12Bytes) 15: 00 16: b8 00 00 00 00 mov $0x0,%eax //Summary: code1 and code2 is not assigned value. 1b: c9 leave 1c: c3 ret 1d: 90 nop 1e: 90 nop 1f: 90 nop示例代码2:
class Object { public: Object(){} ~Object(){} int code1; int code2; }; int main(int argc, char *argv[]) { int i = 0; Object c; //func(1, c, 2); return 0; }示例代码2实现构造函数与析构函数,汇编代码如下:
Disassembly of section .text: 00000000 <_main>: 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 53 push %ebx 4: 83 e4 f0 and $0xfffffff0,%esp 7: 83 ec 20 sub $0x20,%esp //Assign Stack Memory,32Bytes a: e8 00 00 00 00 call f <_main+0xf> f: c7 44 24 1c 00 00 00 movl $0x0,0x1c(%esp) //Assign Zero to Memory Address(esp + 28Bytes) 16: 00 17: 8d 44 24 14 lea 0x14(%esp),%eax //Assign Memory Address(esp + 20Bytes) to eax 1b: 89 04 24 mov %eax,(%esp) //Assign eax to esp, so now esp will point to esp + 20Bytes 1e: e8 00 00 00 00 call 23 <_main+0x23> //Call Object constructor function (ObjectC1Ev) 23: bb 00 00 00 00 mov $0x0,%ebx 28: 8d 44 24 14 lea 0x14(%esp),%eax //Assign Memory Address(esp + 20Bytes) to eax
2c: 89 04 24 mov %eax,(%esp) //Move eax to esp 2f: e8 00 00 00 00 call 34 <_main+0x34> //Call Object destructor function (ObjectD1Ev) 34: 89 d8 mov %ebx,%eax 36: 8b 5d fc mov -0x4(%ebp),%ebx // 39: c9 leave 3a: c3 ret 3b: 90 nop Disassembly of section .text$_ZN6ObjectC1Ev: 00000000 <__ZN6ObjectC1Ev>: //do nothing, just do stack balance 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 5d pop %ebp 4: c3 ret 5: 90 nop 6: 90 nop 7: 90 nop Disassembly of section .text$_ZN6ObjectD1Ev: 00000000 <__ZN6ObjectD1Ev>: //do nothing, just do stack balance 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 5d pop %ebp 4: c3 ret 5: 90 nop 6: 90 nop 7: 90 nopSample Code 3, let constructor and destructor do something:
class Object { public: Object() { code1 = 10; code2 = 5; } ~Object() { code1 = 0; code2 = 0; } int code1; int code2; }; int main(int argc, char *argv[]) { int i = 0; Object c; //func(1, c, 2); return 0; }And assembly codes list following:
Disassembly of section .text: 00000000 <_main>: 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 53 push %ebx 4: 83 e4 f0 and $0xfffffff0,%esp // see above 7: 83 ec 20 sub $0x20,%esp a: e8 00 00 00 00 call f <_main+0xf> f: c7 44 24 1c 00 00 00 movl $0x0,0x1c(%esp) 16: 00 17: 8d 44 24 14 lea 0x14(%esp),%eax 1b: 89 04 24 mov %eax,(%esp) 1e: e8 00 00 00 00 call 23 <_main+0x23> 23: bb 00 00 00 00 mov $0x0,%ebx 28: 8d 44 24 14 lea 0x14(%esp),%eax 2c: 89 04 24 mov %eax,(%esp) 2f: e8 00 00 00 00 call 34 <_main+0x34> 34: 89 d8 mov %ebx,%eax 36: 8b 5d fc mov -0x4(%ebp),%ebx 39: c9 leave 3a: c3 ret 3b: 90 nop Disassembly of section .text$_ZN6ObjectC1Ev: 00000000 <__ZN6ObjectC1Ev>: 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 8b 45 08 mov 0x8(%ebp),%eax // 6: c7 00 0a 00 00 00 movl $0xa,(%eax) //Assign 10 to ebp + 8 c: 8b 45 08 mov 0x8(%ebp),%eax f: c7 40 04 0a 00 00 00 movl $0x5,0x4(%eax) //Assign 5 to (ebp + 8) + 4,
//note stack memory assigns from high to low.
//So ebp+8 is address of Object c. 16: 5d pop %ebp 17: c3 ret Disassembly of section .text$_ZN6ObjectD1Ev: 00000000 <__ZN6ObjectD1Ev>: 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 8b 45 08 mov 0x8(%ebp),%eax 6: c7 00 00 00 00 00 movl $0x0,(%eax) c: 8b 45 08 mov 0x8(%ebp),%eax f: c7 40 04 00 00 00 00 movl $0x0,0x4(%eax) 16: 5d pop %ebp 17: c3 ret
相关文章推荐
- 从汇编代码学习C++语言2—类复制构造函数
- (原创)c#学习笔记08--面向对象编程简介01--面向对象编程的含义03--对象的生命周期--构造函数和析构函数
- C++ 学习总结(十)——C++类与对象,及构造函数,析构函数,拷贝构造函数
- 黑马程序员——黑马学习日志之五 面向对象,类,函数,匿名对象,封装,构造函数,this,代码块
- 面向对象的程序设计-学习笔记-23-派生类的构造函数和析构函数
- C++初学初练类与对象构造函数与析构函数之人员信息录入简单代码
- C++学习(构造函数与析构函数、对象数组与对象指针)
- 【学习笔记04】java面向对象-构造函数、构造代码块
- C++学习笔记(六)-- 类和对象 构造函数和析构函数 const成员函数 this指针 对象数组 堆栈管理变量
- 面向对象的程序设计学习笔记-构造函数和析构函数
- 【C++面向对象学习笔记】-构造函数和析构函数
- php面向对象学习笔记01-面向对象介绍、构造函数、析构函数
- 【学习笔记】JavaScript中,构造函数、原型对象和实例的关系
- 《深度探索c++ 对象模型》有感之构造函数和析构函数不能调用虚函数
- my学习OC--类的构造函数和析构函数
- 将构造函数,析构函数声明为私有和保护的,那么对象如何创建?
- 追逐自己的梦想----------辅助制作第三十三课:向仓库存入N个物品对象汇编实现代码
- cocos2d-x学习笔记--创建对象、构造函数及初始化
- 【零起点学通C++】构造函数与默认构造函数的代码及析构函数的相关笔记
- Android SO逆向-对象的构造函数与析构函数