C + + code disassembly after stack register EBP and ESP

Source: Internet
Author: User

Recently in the analysis of a process crash serious problem, some of the process analysis needs to have a clear understanding of EBP, ESP, for ebp and esp believe that everyone is familiar with, but in order to make this article self-system, I would like to explain.

ebp-- Stack Bottom pointer

esp-- stack top pointer

, the simplified code call process is as follows:

void Layer02 ()

{

int b = 2;

}

void Layer01 ()

{

int a = 1;

Layer02 ();

}

So how did EBP and ESP change during the execution of the function? The following is the disassembly code :

void Layer02 ()

{

00413700 Push EBP

00413701 mov Ebp,esp

00413703 Sub Esp,0cch

00413709 push EBX

0041370A push ESI

0041370B Push EDI

0041370C Lea EDI,[EBP-0CCH]

00413712 mov ecx,33h

00413717 mov eax,0cccccccch

0041371C Rep STOs dword ptr Es:[edi]

int b = 2;

0041371E mov dword ptr [b],2

}

00413725 Pop EDI

00413726 pop ESI

00413727 pop ebx

00413728 mov esp,ebp

0041372A Pop EBP

0041372B ret

We see that the function call begins with the following two lines of code:

00413700 Push EBP

00413701 mov Ebp,esp

Execute the following code before returning:

00413728 mov esp,ebp

0041372A Pop EBP

0041372B ret

So what do these lines of code mean? First, the above shows:

The beginning of two lines of code means that the EBP1 stack , and then the current stack top esp1 as a function call at the bottom of the stack, so the following statements are executed:

00413701 mov Ebp,esp

So what does it mean to return a few statements before returning?

I think we have guessed that when the function call executes, we are going to perform the opposite process :

00413728 mov esp,ebp

Restore stack top pointer

0041372A Pop EBP

Restore the stack bottom pointer

0041372B ret

Return to the instruction before the function call to continue execution. To Be continued ...

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.