Archive for ‘assembly’

August 17, 2008

at&t vs intel assembly syntax

Although intel is the standard assembly syntax on the x86 platform and is generally thought to be nicer than at&t there is still good reason to learn at&t as the gcc compiler emits code in this syntax.

Apart from a few aesthetic and operand-ordering differences the main obstacle to learning at&t is the arcane indirect addressing syntax. What in intel is an easily decipherable instruction to save the address of an element in an array:

lea eax, [my_array + edi*4]

In at&t looks like a syntax error:

leal my_array(,%edi,4), %eax

The above examples are specific instances of an address; the general form for an address in intel is:

 [base+index*scale+disp] 

And in at&t:

 disp(base,index,scale) 

Note how the at&t syntax looks alot like a C function-call whereas the intel syntax looks more like a simple mathematical calculation.

Each element in the general address form is optional.

In our array example earlier we made use of the ‘disp’ and ‘index’ and ‘scale’ elements, but did not need the ‘base’ element. In fact it was because we did not need ‘base’ that the resulting address in at&t looked like a syntax error – a comma right next to a bracket – but this is how we tell the at&t assembler that ‘base’ is not being used.

Intel syntax is alot more straight-forward than at&t in this respect, if we wish to omit an element, we just leave it out of the address calculation altogether, no need to tell the assembler we are leaving it out. (this is one advantage of the mathematical expression syntax versus at&t’s function-call style syntax).

In conclusion, although the at&t addressing syntax is a bit tricky and counter intuitive, once it is mastered the remaining diferences between at&t and intel are minimal.  However in my opinion at&t has one significant advantage over intel in its ordering of operands. For example the at&t movl instruction expects the source operand followed by the destination:

 movl %esp, %ebp 

Which, to me at least, is more intuitive than the intel destination/source order:

 mov ebp, esp 

But, once again, these differences are minimal and ultimately come down to personal preference.