C程序反汇编代码分析

来源:互联网 发布:A.join() js 编辑:程序博客网 时间:2024/05/22 03:16


程序代码与反汇编过程如图:


一下为C程序文件first.c:

//The first programint g(int x){    return x + 2;}int f(int x){    return g(x);}int main(void){    return f(4) - 1;}

使用的编译命令如下:


编辑first.s文件,删除已"."开头的所有链接信息,留下的汇编代码如下:

g:pushl%ebpmovl%esp, %ebpmovl8(%ebp), %eaxaddl$2, %eaxpopl%ebpretf:pushl%ebpmovl%esp, %ebpsubl$4, %espmovl8(%ebp), %eaxmovl%eax, (%esp)callgleaveretmain:pushl%ebpmovl%esp, %ebpsubl$4, %espmovl$4, (%esp)callfsubl$1, %eaxleaveret

对以上汇编代码的分析栈如下图所画:

   


    

  

如图以上图片为整个函数调用过程中栈的变化,小结如下:

1)系统为每个函数开辟一段栈空间,由于每个函数的栈具体有多少内容未知,且栈的大小不一,故设置栈顶和栈底指针

2)每当有新函数被调用,则修改栈底指针,并保存在新的函数栈中

3)将参数传入对应栈空间,用eax保存函数的返回值。

4)使用递归的方法计算结果,最终结果保存在eax寄存器中。

5)代码翻译:

      call   g

call gpush %eipmovl f, %eip

      ret

ret pop %eip

      leave

leavemovl  %ebp  %esppopl  %ebp

真实姓名(万辉) + 原创作品 + 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000



0 0