Name: Ma Qiyang No.: 20135234
(* Original works reproduced please specify the source *)
(Study course: "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000)
Experimental requirements:
Experimental content:
(The following commands apply to the lab building 64-bit Linux virtual machine environment, 32-bit Linux environments may be slightly different) using gcc –S –o main.s main.c -m32
commands to compile the assembly code,
The code is as follows (to prevent the similarity of others, change the code):
int G (int x) {return x + 1;} int f (int x) {return g (x);} int main (void) {return F (6" + 1;}
experimental process and:
1. Enter the lab building, enter the code folder,
Create mqy.c file using VI
2. Complete code Write, save exit
3. Use Gcc-s-o mqy.s mqy.c-m32 to compile mqy.c into assembly code MQY.S and use
vi open MQY.S
4. will be "." The beginning of the line is deleted, get clean assembly code
Experimental Analysis: Stack changes (the orange underline of the code represents the current EIP execution position)
1. Start with EIP pointing at 19 lines, EBP esp in 0 position (01234 on the right is set for convenient analysis)
2.PUSHL%ebp, the EBP value stack
3.movl%esp,%ebp, Assign ESP value to EBP
4.subl $4,%esp, move esp down one grid
5.movl $33, (%ESP), save 33 at the point where ESP refers to
6.call F, call the F function, and the EIP 24 press stack, at which point the EIP points to the F function
7.pushl%ebp, and the EBP value is stacked at this time
8.
movl %esp,%ebp,将esp值赋给ebp
9.subl $4,%esp,esp向下移一格
10.movl 8(%ebp),%eax,将ebp向上两格装的值赋给eax
11.movl %eax,(%esp),将eax的值赋给esp此时指向的格
12.call g,调用g函数,eip 16压栈,此时eip指向g函数
13.
pushl %ebp,将此时ebp值压栈
14.movl %esp,%ebp,将esp值赋给ebp
15.movl 8(%ebp),%eax,将ebp上两格的装的值赋给eax
16.addl $5,%eax,给eax加5
17.popl %ebp,弹栈,此时ebp赋值为栈中所存的ebp 4,esp缩进退一格
18.ret,弹栈,此时eip赋值为栈中所存eip 16,esp缩进一格
19.leave(leave指令为如下步骤)
故先 movl %ebp,%esp,将ebp赋值给esp
然后弹栈,此时ebp指向弹栈的ebp 1,esp缩进一格
20.ret,弹栈,此时eip指向弹栈的eip 24.
21.addl $1,%eax,将eax加1
22.leave操作(
先 movl %ebp,%esp,将ebp赋值给esp
)
然后弹栈,
此时ebp指向弹栈的ebp 0,esp缩进一格
By compiling a simple C program, analyze the assembly code to understand how the computer works