Experiment---Disassembly a simple C program (Yeung Kwong)

Source: Internet
Author: User

Disassembly of a simple C program

Writer: Yang Guangxu No.: 20135233

(* 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 Section (the following command is available for 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 are compiled into assembly code,

The code is as follows:
IntG(int x) {return x +5;}IntF(int x) {return g (x);}int main  (void) {return F (33" + 1;} 


experimental process and:
1. Enter the lab building and go to the Code folder

2. Create a yangguang.c file using VI

3. Complete the code write, and use the WQ command to save the exit

4. Using Gcc-s-o yangguang.s yangguang.c-m32 Compile yangguang.c into assembly code YANGGUANG.S

5. Use VI to open YANGGUANG.S

6. Will be "." The beginning of the line is deleted, get the Pure assembly code


Experimental Analysis: Stack changes (the orange underline of the code represents the current EIP execution position)
1. At the beginning, the EIP points to 19 lines, EBP ESP is 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, assigning ESP value to 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缩进一格


综上,就是该程序在执行时,栈的变化的图片分析及步骤说明


总结

这次实验让我明白了,在一个程序运行的过程中,计算机究竟是如何工作的。实际上就是cpu中的ip寄存器,(16位是ip 32位是eip 64位是rip)自加的情况下一条接一条的实行内存中的指令的过程。
本次课程不算困难,但是由于实验报告需要分析每一步的栈的变化,所以画图画的我有一些焦灼,不过能学到知识,我还是很开心的~


Experiment---Disassembly a simple C program (Yeung Kwong)

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.