几种基本汇编指令详解

来源:互联网 发布:自学编程看什么书 编辑:程序博客网 时间:2024/04/30 16:30

几种基本汇编指令详解

常见寄存器

寄存器 16位 32位 64位 累加寄存器 AX EAX RAX 基址寄存器 BX EBX RBX 计数寄存器 CX ECX RCX 数据寄存器 DX EDX RDX 堆栈基指针 BP EBP RBP 变址寄存器 SI ESI RSI 堆栈顶指针 SP ESP RSP 指令寄存器 IP EIP RIP

汇编指令

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

push:压栈

  • push %eax

    相当于:

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

pop:出栈

  • pop %eax

    相当于:

    movl (%esp), %eaxaddl %4, %esp//栈顶指针加4

call&ret

call

  • call 0x12345

    相当于:

    pushl %eipmovl $0x12345, %eip//当前地址压栈,存入新地址

ret

  • 相当于:

    popl %eip//栈 -> eip

enter&leave

enter

    push %ebp    movl %esp, %ebp    //将堆栈置空(栈上重堆)

leave

    movl %ebp, %esp    popl %ebp    //将堆栈置空(撤销堆栈)

例子:分析一段汇编代码

    pushl $8   ①    movl %esp, %ebp     ②    subl $4, %esp  ③    movl $8, (%esp)        ④

image

image

image

image

image

1 0
原创粉丝点击