Author: Sandy Original works reproduced please indicate the source
"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000 "
Experimental environment: C+linux64 bit (32-bit system may result in different)
In accordance with the academic integrity of the terms, I guarantee that the answer for my original, all the references to the external materials have been marked by provenance.
———————————-Everyone, I'm a split line ————————————————
C source code is as follows
/********************
main.c
*********************/
int g (int x)
{return
x + 2;
}
int f (int x)
{return
g (x);
}
int main (void)
{return
F (7) + 1;
}
Compile the above C program into assembly code with the following command:
Gcc–s–o main.s Main.c-m32
The contents of the resulting assembly code MAIN.S are as follows:
. File "Main.c". Text. Globl g. Type G, @function g:. LFB0: Cfi_startproc pushl%ebp cfi_def_cfa_offset 8 Cfi_offset 5,-8 movl%esp,%EBP. cfi_ Def_cfa_register 5 MOVL 8 (%EBP)%eax Addl $%eax popl%EBP 5 Cfi_restore 4, 4 ret. Cfi_endproc. LFE0:. Size g,.-G. globl F. Type F, @function f:. LFB1: Cfi_startproc pushl%ebp cfi_def_cfa_offset 8 Cfi_offset 5,-8 movl%esp,%EBP. cfi_ Def_cfa_register 5 Subl $%esp movl 8 (%EBP),%eax movl%eax, (%ESP) call G leave. CF I_restore 5 CFI_DEF_CFA 4, 4 ret. Cfi_endproc. LFE1:. Size F,. F. globl Main. Type Main, @function main:. LFB2: Cfi_startproc pushl%ebp cfi_def_cfa_offset 8 Cfi_offset 5,-8 movl%esp,%EBP. cfi_ Def_cfa_register 5 Subl $%esp movl $, (%ESP) call F ADDL $,%eax leave Cfi_restore 5 CFI_DEF_CFA 4, 4 ret. Cfi_endproc. LFE2:. Size main,.-main. Ident "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2".
Gnu-stack, "", @progbits
Which with "." The beginning code is the next link to use, in order to facilitate reading assembly code can be deleted, the resulting "pure" assembly code is as follows:
G:
pushl %ebp
movl %esp,%ebp
movl 8 (%EBP),%eax
addl $,%eax popl%EBP
ret
f:
pushl %ebp
movl %esp,%EBP
subl $,%esp
movl 8 (%EBP), %eax
movl %eax, (%ESP)
call g
leave
ret
main:
pushl
%EBP %esp,%EBP
subl $,%esp
movl $, (%ESP)
call F
addl $,%eax
Leave
RET
——————————————— Everyone, I'm still a split-line —————————————————
Now analyze the execution process of the assembly code and the stack
First, the meanings of each register are shown in the following illustration:
Common assembly statements have the following meanings:
*
Note: Picture screenshot from the course provided courseware, if you want to learn more about register related knowledge, please download courseware online course, the course address at the top
*
The following is a graphical analysis of the assembly code:
The dynamic flowchart is as follows:
If you don't see it clearly, there's a single picture:
The final execution of the return assembly code allows the program to end running.
*
In accordance with the academic integrity of the terms, I guarantee that this blog for my original, all references to external materials have been made a mark of provenance.
*