Reverse a very interesting small program

Source: Internet
Author: User

Question: Use the C ++ class to cleverly describe the relationship between boys and girls ......

Author: A1Pass
Time:
Source: Hacker antivirus (http://bbs.hackav.com)
Source: Http://bbs.hackav.com
Note: For reprinting, please attach this group of information; otherwise, infringement is required!

 

Note: The article code can be adapted according to "high quality C ++ programming guide" and thanks to stuman's article for providing ideas. (This article assumes that the error in the original stuman document has been corrected, but it is not certain, I hope C ++ cool people will not hesitate to give us some advice and make great efforts to shoot bricks ).

Tile address: html "> http://bbs.hackav.com/thread-1026-1-1.html

# Include <iostream>
Using namespace std;

// Male
Class boy
{
Public: // open your mind
Void LoveYou ()
{
Cout <"Jessica, I love you, marry me! "<Endl;
}
Void KissYou ()
{
Cout <"Jessica, you are too sexy, I want to kiss you..." <endl;
}
};

// God says that a woman is made of a man's ribs, so a woman is derived from a man ......
Class girl: public boy
{
Public: // She is also open-minded ...... I didn't see anything -_-!
Void LoveYou ()
{
Cout <"I love you too... But I am too shy to say..." <endl;
}
Void KissYou ()
{
Cout <"You do what you want..." <endl;
}
};

Void main (void)
{
Boy A1Pass; // a boy named A1Pass ^_^
Girl Jessica; // a girl named Jessica

Boy * the_A1Pass_home = & Jessica;
Girl * the_policica_home = & Jessica;

//////////////////////////////////////// //
// ------ What are they doing? Please write the result ---------

The_A1Pass_home-> LoveYou ();
The_policica_home-> LoveYou ();

The_A1Pass_home-> KissYou ();
The_policica_home-> KissYou ();

//////////////////////////////////////// //
}

Syntax analysis:
Through "girl * the_policica_home = & Jessica" and "boy * the_A1Pass_home = & Jessica", we can conclude that the two pointers point to the same address,
Therefore, the member functions pointed to in the following calls must also be in the same class. Therefore, the output result should be two repeated statements, but is it true?
We can give a concrete description of this. A girl-type "Jessica's home" points to an object called Jessica. A boy-type "A1Pass's home" points to an object called Jessica.
We can now understand that "Jessica with separation skills has lived in a girl's home and a boy's home respectively ".
Now, I want to call the methods in "girl's house" and "boy's house" respectively. Now, assume that the boy's house has a "method of playing game machines ", in the girl's house, there is a "Method for playing with dolls ",
If I call a method of "boy's house", will the boy's house change his original "game machine" method by having a girl called Jessica at home? Certainly not!
Therefore, I did the test in the following two methods, and the program running results are exactly the same.
Boy * the_A1Pass_home = & A1Pass;
Girl * the_policica_home = (girl *) & A1Pass;
And
Boy * the_A1Pass_home = NULL;
Girl * the_policica_home = NULL;
Therefore, the correct result should be:
Jessica, I love you, marry me!
I love you too... But I am too shy to say...
Jessica, you are too sexy, I want to kiss you...
You do what you want ..

Reverse Analysis:
Note: This reverse analysis is mainly used to help beginners establish this idea and read the assembly code. Therefore, there are a lot of comments and the explanations are very detailed! Forget to stop Daniel, cainiao ......

Explanation:
Lines and 10 are typical features of the VC DeBug edition. It opens up a large space for each function and fills it with 0xCC to facilitate debugging.
Lines 11-12 obtain the address from the stack and assign it to another space in the stack. The stack structure cannot be difficult to see, this is the process of getting the address of the variable Jessica and assigning it to "girl * the_policica_home.
Line 13-14 is obviously "boy * the_A1Pass_home = & Jessica ".
Lines 15-16 and lines 15 of the Pressure stack ECX will be used when calling the member function. Here we don't have to worry about that much. Lines 16 call boy: Love you.
Line 17-22, same as above, omitted.
Line 23-28 is also a typical feature compiled by VC DeBug. It will verify whether the stack is balanced at the end of the function. Otherwise, a prompt will pop up.
Line 29 restores the stack status saved when entering this function.
Row 30 is the EBP register that stores the original state of the stack.
Line 31 is the return statement in the Assembly, similar to the return Statement of C/C ++.

Through the disassembly instruction above, we can see that, in the source code
Boy A1Pass;
Girl Jessica;
It is not actually reflected, but by default, it gives them a total of 8 bytes of space, and the content of the program execution is always 0 xcccccccccc after initialization. On the other hand, we use class type pointers to call member functions.
It is not expressed as a pointer, but as a direct Call address. This indicates that our Code has already been processed between compiled binary executable files by the compiler and bound to the member function address to be called.

Beginners can learn two knowledge points in this article. One is the importance and general meaning of disassembly, the other is C ++ function calls, which are divided into pre-compilation binding and post-compilation dynamic calls.

I hope you will like another article on popular science and technology after two months.

Related Article

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.