By compiling a simple C program, analyze the assembly code to understand how the computer works

Source: Internet
Author: User

C Language code:

int g (int x)

{

return x + 5;

}

int f (int x)

{

return g (x);

}

int main (void)

{

Return F (5) + 1;

}

Disassembly code:

G:

PUSHL%EBP to stack the value of EBP while ESP moves down 4 bytes

MOVL%esp,%ebp EBP also points to the location where ESP refers

MOVL 8 (%EBP),%eax the EBP to address 8, which moves up 8 bytes, the content in the stack is 8, and 8 is assigned to EAX

Addl $%eax eax = 8+3 = 11

POPL%EBP Ebp points to the position in the stack where the value points, while ESP moves up to 4 bytes

RET esp moves up 4 bytes, while the EIP points to the position of the value in the stack

F:

PUSHL%EBP to stack the value of EBP while ESP moves down 4 bytes

MOVL%esp,%ebp EBP also points to the location where ESP refers

Subl $4,%esp esp moves down 4 bytes

MOVL 8 (%EBP),%eax the EBP to address 8, which moves up 8 bytes, the content in the stack is 8, and 8 is assigned to EAX

Movl%eax, (%ESP) put 8 in the location where ESP points

Call G presses the value of the EIP (the next bar of the call instruction) into the location where ESP points, while the ESP moves down 4 bytes, and the EIP points to G

Leave EBP points to the location where the ESP points, Ebp points to the position of the value in the stack, and ESP moves up to 4 bytes

RET esp moves up 4 bytes, while the EIP points to the position of the value in the stack

Main

PUSHL%EBP to stack the value of EBP (0) while ESP moves down 4 bytes

MOVL%esp,%ebp EBP also points to the location where ESP refers

Subl $4,%esp esp moves down 4 bytes

MOVL $, (%ESP) put 5 in the location where ESP points

Call F presses the value of the EIP into the location where ESP points, while the ESP moves down 4 bytes, and the EIP points to F

Addl $,%eax eax = 11 + 1 = 12

Leave EBP points to the location where the ESP points, Ebp points to the position of the value in the stack, and ESP moves up to 4 bytes

The value of the EIP before the RET main function (determined by the operating system)

Summary: The calculation of the work of the Register address and the value of the register is pressed into the stack, through the Register pointer changes and operation of the value of the operation of the calculation, resulting in the results.

Wang Xue Cheng

"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

By compiling a simple C program, analyze the assembly code to understand how the computer works

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.