linux汇编⼯具之GAS(ATT语法)和NASM(Intel语法)⽐较在Linux0.11内核源码中,bootsect.s和setup.s是实模式下运⾏的16位代码程序,采⽤近似Intel的汇编语⾔语法并且需要使⽤Intel8086汇编编译器和连接器as86和Ld86,⽽head.s使⽤GNU的汇编程序格式,并且运⾏在保护模式下,需要GNU的as(gas)进⾏编译,使⽤的是AT&T语法。
Linus使⽤这两种编译器的原因是linus那时的汇编编译器⽆法⽀持16位实模式代码程序编译,在内核2.4.x开始,bootsect.s和head.s程序完全使⽤统⼀的as来编写。关于GNU as的使⽤,可参考GNU汇编器⼿册《Using as-The GNU Assembler》。由此可见,汇编 语法 与编译器 是相互对应的。看来,应该了解⼀下编译原理。。。
DOS下的汇编语⾔编程:
安装了DOS了以后,再下载MASM611编译器,并安装,这样DOS下的汇编环境就搭建好了。MASM使⽤的是Intel语法,在学校⽤的就是这种语法,所以现在对这种语法⽐较熟悉,⼼⾥⾯⽐较容易接受。
Linux下的汇编语⾔编程:
⼀般GNU/Linux系统都会安装好了GNU Assembler,所以就不⽤单独安装了,可以直接使⽤了。GAS使⽤的是AT&T语法。
此外,还有⼀个汇编编译器-NASM,它既可以在Linux中使⽤,也可在Windows中使⽤,它使⽤的语法是Intel语法,与MASM类似。
Intel语法和AT&T语法的区别:
以下是⼀段关于两者区别的描述
CSCI 223 Computer Organisation and Assembly Language
Intel and AT&T Syntax.
Intel and AT&T syntax Assembly language are very different from each other in appearance, and this will lead to confusion when one first comes across AT&T syntax after having learnt Intel syntax first, or vice versa. So lets start with the basics.
Prefixes.
In Intel syntax there are no register prefixes or immed prefixes. In AT&T however registers are prefixed with a '%' and
immed's are prefixed with a '$'. Intel syntax hexadecimal or binary immed data are suffixed with 'h' and 'b' respectively. Also if the first hexadecimal digit is a letter then the value is prefixed by a '0'.
Example:
Intex Syntax
mov
eax,1mov
ebx,0ffhint
80h AT&T Syntax
movl
$1,%eaxmovl
$0xff,%ebxint
$0x80
Direction of Operands.
The direction of the operands in Intel syntax is opposite from that of AT&T syntax. In Intel syntax the first operand is the destination, and the second operand is the source whereas in AT&T syntax the first operand is the source and the second operand is the destination. The advantage of AT&T syntax in this situation is obvious. We read from left to right, we write from left to right, so this way is only natural.
dest,sourcemov
eax,[ecx] AT&T Syntax
instr
source,destmovl
(%ecx),%eax
Memory Operands.
Memory operands as seen above are different also. In Intel syntax the base register is enclosed in '[' and ']' whereas in AT&T syntax it is enclosed in '(' and ')'.
Example:
Intex Syntax
mov
eax,[ebx]mov
eax,[ebx+3] AT&T Syntax
movl
(%ebx),%eaxmovl
3(%ebx),%eax
The AT&T form for instructions involving complex operations is very obscure compared to Intel syntax. The Intel syntax form of these is segreg:[base+index*scale+disp]. The AT&T syntax form is %
gnu编译器segreg:disp(base,index,scale).
Index/scale/disp/segreg are all optional and can simply be left out. Scale, if not specified and index is specified, defaults to 1. Segreg depends on the instruction and whether the app is being run in real mode or pmode. In real mode it depends on the instruction whereas in pmode its unnecessary. Immediate data used should not '$' prefixed in AT&T when used for
scale/disp.
Example:
Intel Syntax
instr
foo,segreg:[base+index*scale+disp]mov
eax,[ebx+20h]add
eax,[ebx+ecx*2hlea
eax,[ebx+ecx]sub
eax,[ebx+ecx*4h-20h] AT&T Syntax
instr
%segreg:disp(base,index,scale),foomovl
0x20(%ebx),%eaxaddl
(%ebx,%ecx,0x2),%eaxleal
(%ebx,%ecx),%eaxsubl
-0x20(%ebx,%ecx,0x4),%eax
As you can see, AT&T is very obscure. [base+index*scale+disp] makes more sense at a glance than disp(base,index,scale).
Suffixes.
As you may have noticed, the AT&T syntax mnemonics have a suffix. The significance of this suffix is that of operand size. 'l' is for long, 'w' is for word, and 'b' is for byte. Intel syntax has similar directives for use with memory operands, i.e. byte ptr, word ptr, dword ptr. "dword" of course corresponding to "long". This is similar to type casting in C but it doesnt seem to be necessary since the size of registers used is the assumed datatype.
al,blmov
ax,bxmov
eax,ebxmov
eax, dword ptr [ebx] AT&T Syntax
movb
%bl,%almovw
%bx,%axmovl
%ebx,%eaxmovl
(%ebx),%eax
官⽹或在线⽂档
NASM(Netwide Assembler)
The Netwide Assembler, NASM, is an 80x86 and x86-64 assembler designed for portability and modularity. It supports a range of object file formats, including Linux and *BSD a.out , ELF , COFF , Mach-O , Microsoft 16-bit OBJ , Win32 and
Win64 . It will also output plain binary files. Its syntax is designed to be simple and easy to understand, similar to Intel's but less complex . It supports all currently known x86 architectural extensions, and has strong support for macros.
The Netwide Assembler grew out of an idea on comp.lang.asm.x86 (or possibly alt.lang.asm - I forget which), which was essentially that there didn't seem to be a good free x86-series assembler around, and that maybe someone ought to write one
GNU Assembler
《Using as, the GNU Assembler 》
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论