GDB Debug Assembler Stack Process analysis

Source: Internet
Author: User

First I compile my own code, nothing more than calling a function.

Here I first roughly hand-painted a whole program in the Storage Space assembly code (according to the Assembly Code on P239)

It is then compiled. Go to GDB and get the assembly code for Main.

Set a breakpoint on sum to get the assembly code for sum

First I analyze the assembly code:

According to P238 in the book, &esp and%EBP are initialized first, and I guess this is because the power outage we set up is given to the main function, so only the assembly code that executes main is displayed.

After the stack pointer and frame pointer are initialized, call main is executed, then%ebp into the stack,%EBP save%esp.

Because the code applied three int variables to the computer space.

Then: Assign the variable to the application because 3 int is applied so the offset is 12, corresponding to:

Corresponding

Then enter the SUM function:

%ebp into the stack, save the frame pointer of main, and%esp assigns the%EBP to the current sum function.

Save the result of the operation in%eax, return to the main function, pop%EBP return to the previous frame:

The main function assigns the return value%eax to a space with an offset of 4, corresponding to start allocating space int Z.

The following steps begin the analysis of the code:

The value of the base address%EBP of main function main is:

At this time, Y is assigned a value of 2, corresponding to the opening of the storage space offset plus 8.

When executed: corresponds to z= (x, y), consisting of three assembly instructions, which is involved in the stack, two times into the stack, a total of 8 bytes. So%ESP changed: the stack pointer increases by 8.

Then enter the SUM function

Look at it. The value of the%EBP has changed to the base address of the SUM function

When the operation is completed,%eax saves the value to be returned 3: corresponding 1+2

Stack the frame of the previous function to%EBP, followed by the RET instruction, to end the call of the SUM function:

The storage space where int z resides is the main function base address plus an offset of 4, so the return value is assigned to the storage address.

The leave command ends with a 0 overlay%eax.

Here the code analysis is complete. Here is the corresponding table

Bottom: 0x0bfffefec stack top: 0x08048413

Feelings:

In the study of the fourth Chapter P239 page is detailed, this time GDB analysis is to use GDB debugging to consolidate the knowledge learned through this practice, the stack frame grasp has been quite solid, thank Lou Teacher.

Problem:

When the main function will be assigned to the base site%EBP,%esp immediately reduce the 0x10, which is why, I think about it is not possible to open up variable storage space, I can only think of is to save the current stack frame parameters, because after the two parameters stacked, I hope the teacher gave me advice, thank you teacher.

GDB Debug Assembler Stack Process analysis

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.