没想到《linux debugging:使用反汇编理解c++程序函数调用栈》发表了收到了大家的欢迎。但是有网友留言说不熟悉汇编,因此本书列了汇编的基础语法。这些对于我们平时的调试应该是够用了。
本科时候大家学的基本上都是intel的8086汇编语言,微软采用的就是这种格式的汇编。gcc采用的是at&t的汇编格式, 也叫gas格式(gnu asembler gnu汇编器)。
1、寄存器命名不同
at&t
intel
说明
%eax
eax
intel的不带百分号
2、操作数顺序不同
movl %eax, %ebx
mov ebx, eax
intel的目的操作数在前,源操作数在后;at&t相反
3、常数/立即数的格式不同
movl $_value,%ebx
mov eax,_value
intel的立即数前面不带$符号
movl $0xd00d,%ebx
mov ebx,0xd00d
规则同样适用于16进制的立即数
4、操作数长度标识
movw %ax,%bx
mov bx,ax
intel的汇编中, 操作数的长度并不通过指令符号来标识。
at&t的格式中, 每个操作都有一个字符后缀, 表明操作数的大小. 例如:mov指令有三种形式:
movb 传送字节
movw 传送字
movl 传送双字
如果没有指定操作数长度的话,编译器将按照目标操作数的长度来设置。比如指令“mov %ax, %bx”,由于目标操作数bx的长度为word,那么编译器将把此指令等同于“movw %ax, %bx”。
5、寻址方式
imm32(basepointer,
indexpointer,
indexscale)
[basepointer + indexpointer*indexscale + imm32)
两种寻址的实际结果都应该是
imm32 + basepointer + indexpointer*indexscale
例如: 下面是一些寻址的例子:
mov 4(%ebp), %eax
mov eax, [ebp + 4]
基址寻址(base pointer addressing mode),用于访问结构体成员比较方便,例如一个结构体的基地址保存在<code>eax</code>寄存器中,其中一个成员在结构体内的偏移量是4字节,要把这个成员读上来就可以用这条指令
<code>data_items(,%edi,4)</code>
[data_items+edi*4
变址寻址(indexed addressing mode),访问数组
movl $addr, %eax
mov eax, addr
直接寻址(direct addressing mode)
<code>movl (%eax), %ebx</code>
mov ebx, [eax]
间接寻址(indirect addressing mode),把<code>eax</code>寄存器的值看作地址,把内存中这个地址处的32位数传送到<code>ebx</code>寄存器
mov $12, %eax
mov eax, 12
立即数寻址(immediate mode)
寄存器寻址(register addressing mode
6.跳转方式不同
at&t 汇编格式中,绝对转移和调用指令(jump/call)的操作数前要加上'*'作为前缀,而在 intel 格式中则不需要。
jmp *%eax
jmp %eax
用寄存器%eax中的值作为跳转目标
jmp *(%eax)
jmp (%eax)
以%eax中的值作为读入的地址, 从存储器中读出跳转目标
通过求一个数组的最大数,来进一步学习at&t的语法
[cpp]
view plaincopy
#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
汇编程序中以<code>.</code>开头的名称并不是指令的助记符,不会被翻译成机器指令,而是给汇编器一些特殊指示,称为汇编指示(assembler directive)或伪操作(pseudo-operation),由于它不是真正的指令所以加个“伪”字。<code>.section</code>指示把代码划分成若干个段(section),程序被操作系统加载执行时,每个段被加载到不同的地址,操作系统对不同的页面设置不同的读、写、执行权限。<code>.data</code>段保存程序的数据,是可读可写的,相当于c++程序的全局变量。
<code>.text</code>段保存代码,是只读和可执行的,后面那些指令都属于<code>.text</code>段。
<code>.long</code>指示声明一组数,每个数占32;.quad类似,占64位;.byte是8位;.word 是16位。<code>.ascii</code>,例如<code>.ascii "hello world"</code>,声明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