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