Linux Debugging(二): 熟悉AT&T汇编语言
来源:互联网 发布:opengl 平移矩阵 编辑:程序博客网 时间:2024/05/06 03:38
没想到《Linux Debugging:使用反汇编理解C++程序函数调用栈》发表了收到了大家的欢迎。但是有网友留言说不熟悉汇编,因此本书列了汇编的基础语法。这些对于我们平时的调试应该是够用了。
1 AT&T与Intel汇编语法对比
本科时候大家学的基本上都是Intel的8086汇编语言,微软采用的就是这种格式的汇编。GCC采用的是AT&T的汇编格式, 也叫GAS格式(Gnu ASembler GNU汇编器)。
1、寄存器命名不同
AT&TIntel说明%eaxeaxIntel的不带百分号
2、操作数顺序不同
3、常数/立即数的格式不同
4、操作数长度标识
AT&T的格式中, 每个操作都有一个字符后缀, 表明操作数的大小. 例如:mov指令有三种形式:
movb 传送字节
movw 传送字
movl 传送双字
如果没有指定操作数长度的话,编译器将按照目标操作数的长度来设置。比如指令“mov %ax, %bx”,由于目标操作数bx的长度为word,那么编译器将把此指令等同于“movw %ax, %bx”。
5、寻址方式
AT&TIntel说明imm32(basepointer,indexpointer,
indexscale)[basepointer + indexpointer*indexscale + imm32)
两种寻址的实际结果都应该是
imm32 + basepointer + indexpointer*indexscale例如: 下面是一些寻址的例子:AT&TIntel说明mov 4(%ebp), %eaxmov eax, [ebp + 4]基址寻址(Base Pointer Addressing Mode),用于访问结构体成员比较方便,例如一个结构体的基地址保存在eax
寄存器中,其中一个成员在结构体内的偏移量是4字节,要把这个成员读上来就可以用这条指令data_items(,%edi,4)
[data_items+edi*4变址寻址(Indexed Addressing Mode),访问数组movl $addr, %eaxmov eax, addr直接寻址(Direct Addressing Mode)movl (%eax), %ebx
mov ebx, [eax]间接寻址(Indirect Addressing Mode),把eax
寄存器的值看作地址,把内存中这个地址处的32位数传送到ebx
寄存器mov $12, %eaxmov eax, 12立即数寻址(Immediate Mode)mov $12, %eaxmov eax, 12寄存器寻址(Register Addressing Mode6.跳转方式不同
AT&T 汇编格式中,绝对转移和调用指令(jump/call)的操作数前要加上'*'作为前缀,而在 Intel 格式中则不需要。
AT&TIntel说明jmp *%eaxjmp %eax用寄存器%eax中的值作为跳转目标jmp *(%eax)jmp (%eax)以%eax中的值作为读入的地址, 从存储器中读出跳转目标2 求一个数组最大数
通过求一个数组的最大数,来进一步学习AT&T的语法
- #PURPOSE: This program finds the maximum number of a
- # set of data items.
- #
- #VARIABLES: The registers have the following uses:
- #
- # %edi - Holds the index of the data item being examined
- # %ebx - Largest data item found
- # %eax - Current data item
- #
- # The following memory locations are used:
- #
- # data_items - contains the item data. A 0 is used
- # to terminate the data
- #
- .section .data #全局变量
- data_items: #These are the data items
- .long 3,67,34,222,45,75,54,34,44,33,22,11,66,0
- .section .text
- .globl _start
- _start:
- movl $0, %edi # move 0 into the index register
- movl data_items(,%edi,4), %eax # load the first byte of data
- movl %eax, %ebx # since this is the first item, %eax is
- # the biggest
- start_loop: # start loop
- cmpl $0, %eax # check to see if we've hit the end
- je loop_exit
- incl %edi # load next value
- movl data_items(,%edi,4), %eax
- cmpl %ebx, %eax # compare values
- jle start_loop # jump to loop beginning if the new
- # one isn't bigger
- movl %eax, %ebx # move the value as the largest
- jmp start_loop # jump to loop beginning
- loop_exit:
- # %ebx is the status code for the _exit system call
- # and it already has the maximum number
- movl $1, %eax #1 is the _exit() syscall
- 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
- Linux Debugging(二): 熟悉AT&T汇编语言
- 杂记之Linux汇编语言(AT&T语法)(二)
- at&t linux汇编语言
- AT&T 汇编语言学习笔记二
- 杂记之Linux汇编语言(AT&T语法)(一)
- Linux AT&T 汇编语言开发指南(转载)
- linux下的C语言开发(AT&T 汇编语言)
- linux下的C语言开发(AT&T 汇编语言)
- linux下的C语言开发(AT&T 汇编语言)
- linux下的C语言开发(AT&T 汇编语言)
- Linux AT&T 汇编语言开发指南
- Linux 汇编语言开发指南(AT&T)
- Linux AT&T 汇编语言开发指南
- AT&T汇编语言语法(一)(转)
- AT&T(内联)汇编语言使用
- AT&T汇编语言语法
- at&t汇编语言
- AT&T汇编语言语法
- 傻瓜式IOS发布教程(一)iTunes Connect创建应用以及内购
- Spring源码解读——Spring容器初始化 1
- vs2012调试发布到iis的站点
- 函数指针的几种用法
- nutch2.1在windows平台上使用eclipsedebug 存储在mysql的搭建过程
- Linux Debugging(二): 熟悉AT&T汇编语言
- 前端开发工程师
- HTML 中有用的字符实体
- 修改beacon帧结构(3)帧结构修改示例【Linux内核-OpenWRT】
- HDU1242 Rescue-BFS,优先队列
- POJ1208(模拟)
- ZOJ Monthly, December 2013
- Flume-ng配置
- u-boot移植第三弹——移植2013.10u-boot到RealARM210 cortex-A8开发板(支持moviNAND_Fusing_Tool_v2.0)