Node:Syntax,
Next:Converting ASM,
Previous:Converting,
Up:Converting
Q: I have some code written in assembly which compiles under
MASM and TASM, but GCC gives me a long list of error
messages.
A: The GNU Assembler (as.exe
), or Gas
, called by GCC
accepts the AT&T syntax, which is different from the Intel
syntax. Notable differences between the two syntaxes are:
$
; Intel immediate operands
are undelimited (Intel push 4
is AT&T pushl $4
).
%
; Intel register operands
are undelimited. AT&T absolute (as opposed to PC-relative)
jump
/call
operands are prefixed by *
; they are
undelimited in Intel syntax.
add eax, 4
is addl $4, %eax
in
AT&T syntax.
The source, dest
convention is maintained for compatibility with
previous Unix assemblers, so that GCC won't care about the assembler with
which it is configured, as some of GCC installations (on systems other
than MS-DOS) don't use GNU Binutils.
b
,
w
, and l
specify byte (8-bit), word (16-bit), and long
(32-bit) memory references. Intel syntax accomplishes this by prefixing
memory operands (not the opcodes themselves) with `byte
ptr'
, `word ptr'
, and `dword ptr'.
Thus, Intel
mov al, byte ptr FOO
is movb FOO, %al
in AT&T
syntax.
lcall/ljmp $SECTION, $OFFSET
in AT&T syntax; the Intel syntax
is call/jmp far SECTION:OFFSET.
Also, the far return
instruction is lret $STACK-ADJUST
in AT&T syntax; Intel syntax
is ret far STACK-ADJUST.
SECTION:[BASE + INDEX*SCALE + DISP]
is translated into the AT&T syntax
SECTION:DISP(BASE, INDEX, SCALE)
Examples:
Intel: [ebp - 4] AT&T: -4(%ebp) Intel: [foo + eax*4] AT&T: foo(,%eax,4) Intel: [foo] AT&T: foo(,1) Intel: gs:foo AT&T: %gs:foo
For a complete description of the differences,
see GNU assembler documentation. If you don't
read this FAQ with an Info browser, download GNU Binutils,
unzip the files named as.iN
(where N
is a digit) from
it, then type at the DOS prompt:
info as machine i386
You will see a menu of Gas
features specific to x86 architecture.
A user guide for inline assembly was written by Brennan Underwood; it describes how to use inline assembly programming with DJGPP and includes a tutorial on the AT&T assembly syntax. Check out the DJGPP inline assembly tutorial.
Another useful tutorial about writing separate assembly-language modules for DJGPP was written by George Foot and is available from George's home page.
The DJGPP User's Guide also has a tutorial on writing assembly-language code. One of the sections there describes the CPU architecture, which is geared towards assembly-language programming.
Yet another tutorial on the subject of inline assembly is available at
<http://www.castle.net/~avly/djasm.html
>.
Many people who used Intel syntax and then converted to the AT&T style say that they like the AT&T variant more. However, if you prefer to stick with the Intel syntax, download and install NASM, which is a free portable assembler. It is compatible with DJGPP and accepts a syntax which is much more similar to the Intel style. A guide for using NASM with DJGPP was written by Matthew Mastracci and is available from Matthew's Web page.
Note that Binutils maintainers are working on adding an option to Gas which will cause it accept the Intel syntax as well, so it is most probable that beginning with Binutils 2.10, Gas will have this feature.