几个重要的寄存器

eip - 用于存放当前所执行的指令地址

esp - 栈(顶)指针寄存器

ebp - 基址(栈底)指针寄存器

简单的C程序

 int g(int x)
 {
   ;
 }

 int f(int x)
 {
   return g(x);
 }

 int main(void)
 {
   ) + ;
 }

汇编代码分析

 g:
  pushl %ebp
  movl %esp, %ebp
 ;下面两条指令的含义是:将传入的参数7和10相加并保存在寄存器eax中
  movl (%ebp), %eax
  addl $, %eax
  popl %ebp
  ret
 f:
  pushl %ebp
  movl %esp, %ebp
  subl $, %esp
 ;下面两句指令的含义是:将参数7保存到函数f的栈帧中
  movl (%ebp), %eax
  movl %eax, (%esp)
  call g
  leave
  ret

 main:
  pushl %ebp
  movl %esp, %ebp
  subl $, %esp
  movl $, (%esp)
  call f
  addl $, %eax
  leave
  ret

针对上面的汇编代码,我们有如下的图例分析

说明:

  • 在执行call指令时,eip的所指向的指令是addl $5, %eax

  • call 指令等价于,将eip中的地址压栈保存,然后将函数f第一条指令(pushl %ebp)的地址放到eip中。

  • pushl、popl、leave、ret和call指令的等价指令如下
    pushl %eax ;将eax中的值压栈保存
    <=>
    subl $4, %esp
    movl %eax, (%esp)
    popl %eax      
       <=>    
    movl (%esp), %eax
    addl $4, %esp
    call 0x12345
    <=>
    pushl %eip(*)
    movl $0x12345, %eip(*)
    ret ; 将栈顶值弹出放到eip中,此时eip中的值便是将要执行的指令的地址
    <=>
    popl %eip(*)
    leave ;恢复所调用程序的堆栈
    < =>
    movl %ebp, %esp
    popl %ebp
    enter ;与leave指令的操作相反
    <=>
    pushl %ebp
    movl %esp, %ebp

一个简单C程序的汇编代码分析的更多相关文章

  1. 《Linux内核分析》week1作业-分析一个简单c语言的汇编代码

    1.C语言源码 #include <stdio.h> int g(int x){ ; } int f(int x){ return g(x); } int main(){ )+; } 2. ...

  2. 简单C程序生成的汇编代码分析

    首先给出完整的C代码: int g(int x) { ; } int f(int x) { return g(x); } int main(void) { )+; } 使用命令:gcc –S –o h ...

  3. Linux内核分析—完成一个简单的时间片轮转多道程序内核代码

    ---恢复内容开始--- 20135125陈智威 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-10 ...

  4. 20135202闫佳歆--week2 一个简单的时间片轮转多道程序内核代码及分析

    一个简单的时间片轮转多道程序内核代码及分析 所用代码为课程配套git库中下载得到的. 一.进程的启动 /*出自mymain.c*/ /* start process 0 by task[0] */ p ...

  5. Arachnid包含一个简单的HTML剖析器能够分析包含HTML内容的输入流

    Arachnid是一个基于Java的web spider框架.它包含一个简单的HTML剖析器能够分析包含HTML内容的输入流.通过实现Arachnid的子类就能够开发一个简单的Web spiders并 ...

  6. 关于SIGSLOT的一个简单的程序

    废话少说直接看代码即可,这只是一个简单的程序,可以帮我们简单地明白SIGSLOT是怎么回事.至于深入研究自己去百度吧. #include "sigslot.h" using nam ...

  7. start_kernel之前的汇编代码分析

    start_kernel之前的汇编代码分析 Boot中执行下面两句话之后,进入uclinux内核. theKernel = (void (*)(int, int, unsigned int))((ui ...

  8. python定义的一个简单的shell函数的代码

    把写代码过程中经常用到的一些代码段做个记录,如下代码段是关于python定义的一个简单的shell函数的代码. pipe = subprocess.Popen(cmd, stdout=subproce ...

  9. STM32F103 ucLinux开发之二(内核启动汇编代码分析)

    start_kernel之前的汇编代码分析 Boot中执行下面两句话之后,进入uclinux内核. theKernel = (void (*)(int, int, unsigned int))((ui ...

随机推荐

  1. po 和 mo 的互相转换

    反编译 mo 文件成 po 文件 msgunfmt test.mo -o test.po 编码 po 文件为 mo 文件 msgfmt -o test.mo test.po 记着备用.

  2. 文件夹Tab Ctrl

    http://blog.163.com/gz_ricky/blog/static/1820491182011061180897/ 转载 Tab Ctrl Tab属性页控件可以在一个窗口中添加不同的页面 ...

  3. iOS 摇一摇的实现-备用

    - (void)viewDidLoad { [super viewDidLoad]; [[UIApplication sharedApplication] setApplicationSupports ...

  4. PowerShell控制台输出符号+函数参数类型指定+文本内容读取

    There are several ways: Write-Host: Write directly to the console, not included in function/cmdlet o ...

  5. python调用shell, shell 引用python

    python 调用 shell get_line_num="wc -l as_uniq_info | awk '{print $1}'" ###get the lines of & ...

  6. 【转】文件同步软件FreeFileSync

    原文网址:http://imcn.me/html/y2012/9855.html FreeFileSync 是一款开源的文件夹比较和同步工具,可用于 Win 和 Lin 平台,最近发布了 5.0 版本 ...

  7. HDOJ(HDU) 1562 Guess the number(水题,枚举就行)

    Problem Description Happy new year to everybody! Now, I want you to guess a minimum number x betwwn ...

  8. HDU_2056——相交矩形的面积

    Problem Description Given two rectangles and the coordinates of two points on the diagonals of each ...

  9. SQL - 配置SQLServer 使其可以远程访问

    环境: SQL Server2008 R2 SQL Server Management Studio 今天测试部署项目的时候,发现不能远程访问SQL Server.具体情形就是在Management ...

  10. MySQL日期函数

    1.已知出生日期,求年龄 SELECT '1992-04-10' as birthday, curdate(), ( YEAR (curdate()) - YEAR ('1992-04-10')-1 ...