Document directory
- The AT&T or GAS Assembly Syntax
- REGISTERS
- LITERAL VALUES
- MEMORY ADDRESSING
- OPERAND SIZES
- CONTROL TRANSFER INSTRUCTIONS
AT&T syntax (1)
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 Compiler Er (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 policer syntax.
The AT&T or GAS Assembly Syntax
Like any other extends er, 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 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 offsets and the scale shocould not be prefixed by $. Few more examples with their equivalent NASM syntax ..
GAS memory operandNASM 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 shoshould be prefixed by a *. For example ..
GAS syntaxNASM syntax---------------------IMMEDIATEjmp$100, $100jmp 100:100ljmp$100, $100jmp 100:100call$100, $100call 100:100lcall$100, $100call 100:100ABSOLUTEjmp100jmp 100call100call 100INDIRECTjmp*100jmp near [100]call*100call near [100]jmp*(%eax)jmp near [eax]call*(%ebx)call near [ebx]ljmp*100jmp far [100]lcall*100call far [100]ljmp*(%eax)jmp far [eax]lcall*(%ebx)call far [ebx]RETURNretretnlretretflret $0x100retf 0x100
Thats it for now.
Reference: http://www.cnblogs.com/huqingyu/archive/2005/03/04/113195.html