计算机是如何工作之简单的程序分析

tgut 原创作品转载出处 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000

编写main.c如下小程序


然后

gcc -S -o main.s main.c -m32

然后

vi main.s

看到如下代码(删除一些不相关的代码后)


下面让我们分析一下这些汇编代码:


ebp(main)
6
eip23
ebp(f)
6
eip15
ebp(g)
 
     pushl %ebp

      move$6,(%esp)

      call f

      pushl %ebp; mov %esp, %ebp;sub $4,  %esp

      movl %eax,(%esp);

      call g;

       pushl %ebp;movl %esp,%ebp;            /*eax=6

        add $2, %eax;                                  /*eax=6+2=8        

 

注释:ebp(main),ebp(f),ebp(g)分别是各个函数的栈底指针。

 
下面给出一些指令的宏定义:

enter:

push %ebp;

movl %esb,%ebp;

leave:

movl %ebp,%esp;

popl %ebp;

call:

push eip

mov(函数入口地址) eip

ret:

pop eip;

addl $4,%esp ;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值