Grasping Writer: Li Pengju No.: 20132201
(* Original works reproduced please specify the source *)
(Study course: "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000)
Experimental section (the following command is available for lab building 64-bit Linux virtual machine environment, 32-bit Linux environment may be slightly different)
Compile the assembly code using the gcc–s–o main.s main.c-m32 command.
The code is as follows:
int g (int x) { return8;} int f (int x) { return g (x);} int Main (void) { return f (2
The experimental process and:
1.进入实验楼,进入Code文件夹,单机右键直接建立 lipengju.c文件
2.打开文件并将相应代码写入,保存并退出
3.
使用gcc -S -o lipengju.s lipengju.c -m32 将lipengju.c编译成汇编代码lipengju.s
4.打开lipengju.s文件,会看到机器转换之后的汇编代码
5.将以"."开头的行删去,得到纯净的汇编代码
< Span class= "Hljs-title" > Experimental analysis: Stack changes
1.
开始时eip指向18行,ebp esp都在堆栈0的位置
2.执行eip处的指令,pushl %ebp,将ebp值压栈
3.eip执行至19行,
movl %esp,%ebp,将esp值赋给ebp
4.eip执行至20行,
subl $4,%esp,将esp向下移一格
5.movl $24,(%esp),将24存入此时esp所指的位置
6.
call f,调用f函数,将eip 23压栈,此时eip指向f函数
7.pushl %ebp,将此时ebp值压栈
8.
movl %esp,%ebp,将esp值赋给ebp
9.subl $4,%esp,esp向下移一格
10.movl 8(%ebp),%eax,将ebp向上两格装的值赋给eax,所以此时eax=24
11.movl %eax,(%esp),将eax的值赋给esp此时指向的格
12.call g,调用g函数,eip 15压栈,此时eip指向g函数
13.
pushl %ebp,将此时ebp值压栈
14.movl %esp,%ebp,将esp值赋给ebp
15.movl 8(%ebp),%eax,将ebp上两格的装的值赋给eax,此时eax=33
16.addl $11,%eax,给eax加11,加上之后,eax=44
17.popl %ebp,弹栈,此时ebp赋值为栈中所存的ebp 4,esp缩进退一格
18.ret,弹栈,此时eip赋值为栈中所存eip 15,esp缩进一格
19.leave(leave指令为如下步骤)
MOVL%ebp,%esp
POPL%EBP
So this time the stack pointer changes to
20.ret,弹栈,此时eip指向弹栈的eip 23
21.addl $1,%eax,将eax加1,此时eax为45
22.leave操作(
先 movl %ebp,%esp,将ebp赋值给esp
)
This is all the stack changes in the C language process.
Experiment Experience:
The most important experience of this experiment is to feel the difference between the Linux system assembler statement and the assembler statement in Windows, but the analysis of the stack changes is basically the same, but understanding these processes and step to understand these steps and draw them is different, Through this analysis, I learned more about the changes of the Linux assembly language.
And through this experiment to understand the use of the experimental building, know how to build a C file in Linux, and compile a well-compiled C language code into assembly language, in short, this is a good start for my future Linux learning.
Experiment--disassembly of a simple C program (Li Pengju)