Learn about methods called C + + from the assembly

Source: Internet
Author: User

SRC (Debug vesion by VS2008 SP1 on XP SP3)

#include<iostream>using namespace std;class SimpleClass{public:    int x;    void HelloWorld(){        printf("hello,mao");    }};int main(int argc,char **argv){    SimpleClass myclass;    myclass.HelloWorld();    return 0;}

Post-disassembly structure

---c:\vc6\myprojects\vsclasstest\vsclasstest\bingome.cpp----------------------1: #include <iostream> 2: using namespace Std; 3:4: Class simpleclass{5:public:6: int x; 7:void HelloWorld () {00411440 push ebp 00411441 mov ebp,esp 0041 1443 Sub esp,0cch 00411449 push ebx 0041144A push esi 0041144B push EDI 0041144C push ecx 0041144D Lea edi,[ebp- 0CCH] 00411453 mov ecx,33h 00411458 mov eax,0cccccccch 0041145D rep stos dword ptr Es:[edi]//The local variable space is all initialized to 0XCC (0x33* 4=CC) (store to string addr edi by dword,repeat time was Ecx,filled-word is eax.) 0041145F pop ecx 00411460 mov dword ptr [EBP-8],ECX//ebp-8 is a variable that holds the address of the current class instance, so where does our x go? --A variable in a class is not a local variable in a function, and a method is not related to a class member. 8:printf ("Hello,mao"); 00411463 mov esi,esp 00411465 push offset string "Hello,mao" (415800h) 0041146A call Dwo Rd PTR [__imp__printf (4182BCH)]//Get address from Import table, import table IAT at 4182BCh 00411470 add esp,4 00411473 cmp esi,esp 00411475 call @ILT + 310 (__RTC_CHECKESP) (41113Bh) 9:}0041147a popEDI 0041147B pop esi 0041147C pop ebx 0041147D add esp,0cch 00411483 cmp ebp,esp 00411485 call @ILT +310 (__RTC_CHEC   KESP) (41113Bh) 0041148A mov esp,ebp 0041148C pop ebp 0041148D ret

Main function Section

17:     SimpleClass myclass;18:     myclass.HelloWorld(); 004113CE  lea ecx,[myclass]  004113D1  callSimpleClass::HelloWorld (41114Ah) 19:     return 0; 004113D6  

As you can see, when you execute a method in a class, the address () of the instantiated class is taken out, that ecx= myclass is, the compiler has introduced a hidden variable when the internal method of the class is implemented 实例化的类指针 .

A variable in a class is not a variable in a class method, and a variable in a class method access class is passed in by ECX as a hidden parameter, so there is no space in the class method to hold the class member.

Learn about methods called C + + from the assembly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.