浙林龙哥

   :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

For the first timer the AT&T syntax may seem a bit confusing, atleast I felt so. Personally Im a big fan of this syntax and if you ask me it has got its own advantages. It is the syntax understood by the GNU assembler (GAS) and youll have to use this syntax if you inline assembly into C source files which need to be compiled using the GNU C compilers. As far as os development is concerned, for those who work with the GNU Compiler Collection, I blv that a basic knowledge of this syntax is a must. This article is meant only for those who have a basic knowledge of assembly language and preferably some familiarity with the intel and/or NASM assembler syntax.

The AT&T or GAS Assembly Syntax

Like any other assembler, the basic structure of an instruction in GAS is the same. But the difference from the intel syntax starts from the specification of operands for an instruction. For example in the intel syntax, the structure of a data moving instruction is..

 		instruction destination, source

but in the case of GAS, the strucuture is

 		instruction source, destination

which to me makes more sense.

REGISTERS

All register names of the i386+ architecture have to be prefixed by a % sign. Example, %al,%bx, %ds, %cr0 etc. No matter where you use them they must be prefixed by %. For example...

 		mov	%ax,	%bx

which moves the value from register ax to register bx.

LITERAL VALUES

All literal values must be prefixed by a $ sign. For example..

 		mov	$100,	%bx
 		mov	$A,	%al

The first instruction moves the the value 100 into the register ax and the second one movesthe numerical value of the ascii A into the al register. Please note that the below instruction is not valid..

 		mov	%bx,	$100

as it just tries to move the value in register bx to a literal value.

MEMORY ADDRESSING

In GAS, memory is addressed in the following way..

 		
	segment-override:signed-offset(base,index,scale)

For example the GAS equivalent of [es:eax+ebx*2+100] is

		%es:100(%eax,%ebx,2)

Please note that that offsets and the scale should not be prefixed by $. Few more examples with their equivalent NASM syntax..

GAS memory operand			NASM memory operand
------------------			-------------------

100					[100]
%es:100					[es:100]
(%eax)					[eax]
(%eax,%ebx)				[eax+ebx]
(%ecx,%ebx,2)				[ecx+ebx*2]
(,%ebx,2)				[ebx*2]
-10(%eax)				[eax-10]
%ds:-10(%ebp)				[ds:ebp-10]
Example instructions..
 		mov	%ax,	100
 		mov	%eax,	-100(%eax)

The first instruction moves the value in register ax into offset 100 of the data segment register, and the second one moves the value in eax register to [eax-100].

OPERAND SIZES

At times, especially when moving literal values to memory, it becomes neccessary to specify the size of transfer or the operand size. For example the instruction...

 		mov	$10,	100

only specfies that the value 10 to be moved to the memory offset 100, but not the transfer size. In NASM this is done by adding the casting keyword byte/word/dword etc. to any of the operands. In GAS this is done by adding the suffix b/w/l to the instruction. For example ...

 		movb	$10,	%es:(%eax)
moves a byte value 10 to the memory location [ea:eax], whereas..
 		movl	$10,	%es:(%eax)

moves a long value 10 to the same.

A few more examples..

 		movl	$100,	%ebx
 		pushl	%eax
 		popw	%ax

CONTROL TRANSFER INSTRUCTIONS

The jump, call and ret instructions can transfer the control from one part of the code to another. The immediate value jump and call are two operand instructions of the form..

 		jmp	$segment,	$offset
As for absolute jumps, the memory operand should be prefixed by a *.For example..
GAS syntax			NASM syntax
----------			-----------

IMMEDIATE

jmp	$100, $100		jmp  100:100
ljmp	$100, $100		jmp  100:100
call	$100, $100		call 100:100
lcall	$100, $100		call 100:100

ABSOLUTE

jmp	100			jmp  100
call	100			call 100

INDIRECT

jmp	*100			jmp  near [100]
call	*100			call near [100]
jmp	*(%eax)			jmp  near [eax]
call	*(%ebx)			call near [ebx]
ljmp	*100			jmp  far  [100]
lcall	*100			call far  [100]
ljmp	*(%eax)			jmp  far  [eax]
lcall	*(%ebx)			call far  [ebx]

RETURN

ret				retn
lret				retf
lret $0x100			retf 0x100
Thats it for now.
posted on 2005-03-04 20:23  浙林龙哥  阅读(2390)  评论(2编辑  收藏  举报