zoukankan      html  css  js  c++  java
  • Linux Debugging(二): 熟悉AT&T汇编语言

        没想到《Linux Debugging:使用反汇编理解C++程序函数调用栈》发表了收到了大家的欢迎。但是有网友留言说不熟悉汇编,因此本书列了汇编的基础语法。这些对于我们平时的调试应该是够用了。

    1 AT&T与Intel汇编语法对比

        本科时候大家学的基本上都是Intel的8086汇编语言,微软采用的就是这种格式的汇编。GCC采用的是AT&T的汇编格式, 也叫GAS格式(Gnu ASembler GNU汇编器)。

    1、寄存器命名不同

    AT&T Intel 说明
    %eax eax Intel的不带百分号


    2、操作数顺序不同

    AT&T Intel 说明
    movl %eax, %ebx mov ebx, eax Intel的目的操作数在前,源操作数在后;AT&T相反


    3、常数/立即数的格式不同

    AT&T Intel 说明
    movl $_value,%ebx mov eax,_value Intel的立即数前面不带$符号
    movl $0xd00d,%ebx mov ebx,0xd00d 规则同样适用于16进制的立即数


    4、操作数长度标识

    AT&T Intel 说明
    movw %ax,%bx mov bx,ax Intel的汇编中, 操作数的长度并不通过指令符号来标识。
    AT&T的格式中, 每个操作都有一个字符后缀, 表明操作数的大小. 例如:mov指令有三种形式:
    movb  传送字节
    movw  传送字
    movl   传送双字
    如果没有指定操作数长度的话,编译器将按照目标操作数的长度来设置。比如指令“mov %ax, %bx”,由于目标操作数bx的长度为word,那么编译器将把此指令等同于“movw %ax, %bx”。

    5、寻址方式

    AT&T Intel 说明
    imm32(basepointer,
    indexpointer,
    indexscale)
    [basepointer + indexpointer*indexscale + imm32)

    两种寻址的实际结果都应该是

    imm32 + basepointer + indexpointer*indexscale
    例如: 下面是一些寻址的例子:

    AT&T Intel 说明
    mov 4(%ebp), %eax mov eax, [ebp + 4] 基址寻址(Base Pointer Addressing Mode),用于访问结构体成员比较方便,例如一个结构体的基地址保存在eax寄存器中,其中一个成员在结构体内的偏移量是4字节,要把这个成员读上来就可以用这条指令
    data_items(,%edi,4) [data_items+edi*4 变址寻址(Indexed Addressing Mode),访问数组
    movl $addr, %eax mov eax, addr 直接寻址(Direct Addressing Mode)
    movl (%eax), %ebx mov ebx, [eax] 间接寻址(Indirect Addressing Mode),把eax寄存器的值看作地址,把内存中这个地址处的32位数传送到ebx寄存器
    mov $12, %eax
    mov eax, 12 立即数寻址(Immediate Mode)
    mov $12, %eax mov eax, 12 寄存器寻址(Register Addressing Mode

    6.跳转方式不同

    AT&T 汇编格式中,绝对转移和调用指令(jump/call)的操作数前要加上'*'作为前缀,而在 Intel 格式中则不需要。 

    AT&T Intel 说明
    jmp *%eax jmp %eax 用寄存器%eax中的值作为跳转目标
    jmp *(%eax) jmp (%eax) 以%eax中的值作为读入的地址, 从存储器中读出跳转目标

    2 求一个数组最大数

    通过求一个数组的最大数,来进一步学习AT&T的语法

    1. #PURPOSE: This program finds the maximum number of a  
    2. #     set of data items.  
    3. #  
    4. #VARIABLES: The registers have the following uses:  
    5. #  
    6. # %edi - Holds the index of the data item being examined  
    7. # %ebx - Largest data item found  
    8. # %eax - Current data item  
    9. #  
    10. # The following memory locations are used:  
    11. #  
    12. # data_items - contains the item data. A 0 is used  
    13. # to terminate the data  
    14. #  
    15.  .section .data #全局变量  
    16. data_items:         #These are the data items  
    17.  .long 3,67,34,222,45,75,54,34,44,33,22,11,66,0  
    18.   
    19.  .section .text  
    20.  .globl _start  
    21. _start:  
    22.  movl $0, %edi      # move 0 into the index register  
    23.  movl data_items(,%edi,4), %eax # load the first byte of data  
    24.  movl %eax, %ebx    # since this is the first item, %eax is  
    25.             # the biggest  
    26.   
    27. start_loop:         # start loop  
    28.  cmpl $0, %eax      # check to see if we've hit the end  
    29.  je loop_exit  
    30.  incl %edi      # load next value  
    31.  movl data_items(,%edi,4), %eax  
    32.  cmpl %ebx, %eax    # compare values  
    33.  jle start_loop     # jump to loop beginning if the new  
    34.             # one isn't bigger  
    35.  movl %eax, %ebx    # move the value as the largest  
    36.  jmp start_loop     # jump to loop beginning  
    37.   
    38. loop_exit:  
    39.  # %ebx is the status code for the _exit system call  
    40.  # and it already has the maximum number  
    41.  movl $1, %eax      #1 is the _exit() syscall  
    42.  int $0x80  

    汇编程序中以.开头的名称并不是指令的助记符,不会被翻译成机器指令,而是给汇编器一些特殊指示,称为汇编指示(Assembler Directive)或伪操作(Pseudo-operation),由于它不是真正的指令所以加个“”字。.section指示把代码划分成若干个段(Section),程序被操作系统加载执行时,每个段被加载到不同的地址,操作系统对不同的页面设置不同的读、写、执行权限。.data段保存程序的数据,是可读可写的,相当于C++程序的全局变量。

    .text段保存代码,是只读和可执行的,后面那些指令都属于.text段。

    .long指示声明一组数,每个数占32;.quad类似,占64位;.byte是8位;.word 是16位。.ascii,例如.ascii "Hello world",声明11个数,取值为相应字符的ASCII码。

    参考资料:

    1.  最简单的汇编程序

    2. 第二个汇编程序

    3. http://blog.chinaunix.net/uid-27717694-id-3942757.html

    最后复习一下lea命令:

    mov 4(%ebp) %eax #将%ebp+4地址处所存的值,mov到%eax

    leal 4(%ebp) %eax #将%ebp+4的地址值, mov到%eax

    leal 可以被mov取代:

    addl $4, %ebp

    mov. %ebp, %eax


  • 相关阅读:
    MySQL sys Schema 简单介绍-2
    dubbo服务+Spring事务+AOP动态数据源切换 出错
    sql 查询优化
    spring事务-说说Propagation及其实现原理
    Redis 分布式锁
    三、操作符
    二、一切皆是对象
    一、对象导论
    SpringMVC工作原理
    数据库性能优化策略
  • 原文地址:https://www.cnblogs.com/anzhsoft/p/3602969.html
Copyright © 2011-2022 走看看