几种基本汇编指令详解

来源:互联网 发布:淘宝运费模板设置实例 编辑:程序博客网 时间:2024/05/18 03:08

转自:http://blog.csdn.net/luoyhang003/article/details/46786591

几种基本汇编指令详解

常见寄存器

寄存器16位32位64位累加寄存器AXEAXRAX基址寄存器BXEBXRBX计数寄存器CXECXRCX数据寄存器DXEDXRDX堆栈基指针BPEBPRBP变址寄存器SIESIRSI堆栈顶指针SPESPRSP指令寄存器IPEIPRIP

汇编指令

mov

  • movb(8位)、movw(16位)、movl(32位)、movq(64位)

  • 寄存器寻址: 

    movl %eax, %edx 

    eax -> edx

  • 立即数寻址: 

    movl $0x123, %edx 

    数字->寄存器

  • 直接寻址: 

    movl 0x123, %edx 

    直接访问内存地址数据,edx = *(int32_t *)0x123;

  • 间接寻址: 

    movl (%ebx), %edx 

    %ebx 是个内存地址,(%ebx)指的是该地址中的数据,edx = *(int32_t*)ebx;

  • 变址寻址: 

    movl 4(%ebx), %edx 

    edx = *(int32_t*)(ebx+4);

push & pull

堆栈数据结构简介

作用:

  • 程序调用框架
  • 传递参数
  • 保存返回地址
  • 提供局部变量
  • ……

结构:

image

  • 相关寄存器: esp, ebp

  • 相关操作: pop, push

    //建立被调用者函数的堆栈框架pushl %ebpmovl %esp, %ebp//拆除框架movl %ebp, %esppopl %ebpret
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

push:压栈

  • push %eax 

    相当于:

    subl $4, %esp//栈顶指针减4movl %eax, (%esp)//%eax -> esp 地址
    • 1
    • 2
    • 3
    • 4
    • 5

pop:出栈

  • pop %eax 

    相当于:

    movl (%esp), %eaxaddl %4, %esp//栈顶指针加4
    • 1
    • 2
    • 3
    • 4

call&ret

call

  • call 0x12345 

    相当于:

    pushl %eipmovl $0x12345, %eip//当前地址压栈,存入新地址
    • 1
    • 2
    • 3
    • 4

ret

  • 相当于:

    popl %eip//栈 -> eip
    • 1
    • 2
    • 3

enter&leave

enter

    push %ebp    movl %esp, %ebp    //将堆栈置空(栈上重堆)
  • 1
  • 2
  • 3
  • 4

leave

    movl %ebp, %esp    popl %ebp    //将堆栈置空(撤销堆栈)
  • 1
  • 2
  • 3
  • 4

例子:分析一段汇编代码

    pushl $8   ①    movl %esp, %ebp     ②    subl $4, %esp  ③    movl $8, (%esp)        ④
  • 1
  • 2
  • 3
  • 4
  • 5

image

image

image

image

image

版权声明:本文为博主原创文章,未经博主允许不得转载。(文章来源:http://blog.luoyuanhang.com)
  • 本文已收录于以下专栏:
  • Linux内核学习
原创粉丝点击