Opcode syntax: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
mNo edit summary
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(8 intermediate revisions by 6 users not shown)
Line 6:
== Important Details ==
 
There are some substantial differences between the AT&T syntax and the Intel sytnaxsyntax, which a programmer intending to use the GNU tools should be aware of.
Here are a few key things to look for, when moving to AT&T syntax:
 
Line 13:
*'''Escapes:''' Special characters are written as C-style escapes (\n, \", \#, \\, ...).
*'''Comments:''' Either C-style ( /* ... */ ) or shell style (# ...).
*'''Directive syntax:''' Directives begin with a period (".align 4" to align on a doubleword32-bit boundary, ".word 0x1234" is the equivalent of "DW 1234h").
*'''Strings:''' Defined using special directives, .ascii (or .asciz for a zero-terminated string). Example: msg: .ascii "Hello, World!\n"
*'''Current location address:''' Indicated by a period (".", equivalent to Intel syntax "$").
Line 35:
The AT&T syntax format for macros:
 
<syntaxhighlight lang="asm">
<code>
.macro <name> <args>
<operations>
.endm
</syntaxhighlight>
</code>
 
Example:
 
<syntaxhighlight lang="asm">
<pre>
.macro write string
movw string, %si
call printstr
.endm
</syntaxhighlight>
</pre>
 
This would be equivalent to the NASM macro:
 
<syntaxhighlight lang="asm">
<pre>
%macro write 1
mov si, %1
call printstr
%endmacro
</syntaxhighlight>
</pre>
 
Additionally, the cpp and m4[[M4]] macro preprocessors are often used for macro handling.
 
== Converting small snippets of code from Intel syntax to AT&T ==
 
You can use the following script to convert short snippets of code (one liners) from Intel syntax to AT&T syntax:
== Sources ==
<syntaxhighlight lang="bash">#!/bin/bash
set -e
 
# Usage:
[http://www.delorie.com/djgpp/v2faq/faq17_1.html DJGPP AT&T Assembly Tutorial]<br>
#
[http://asm.sourceforge.net//howto/Assembly-HOWTO.html Linux Assembly HOWTO]<br>
# ./inteltoatt [16|32|64] "mov eax, eax \n xor ecx, edx"
[https://savannah.nongnu.org/projects/gas-user/ GAS/AS End User Help Project]<br>
#
[http://www.gnu.org/software/binutils/manual/gas-2.9.1/html_chapter/as_toc.html Using as]<br>
 
[http://savannah.nongnu.org/projects/pgubook/ Programming from the Ground Up]<br>
case "$1" in
16|32|64)
bits="$1"
shift ;;
*)
bits="32" ;;
esac
code="$1"
 
nasm="$(mktemp)"
obj="$(mktemp)"
objdump="$(mktemp)"
 
case "$bits" in
16) m="i8086" ;;
32) m="i386" ;;
64) m="i386:x86-64" ;;
esac
 
echo -e "BITS $bits\n$code" > "$nasm"
 
nasm "$nasm" -o "$obj"
objdump -D -b binary -m $m -Maddr${bits},data${bits} "$obj" > "$objdump"
 
lineno="$(egrep -m 1 -n '<\.data>\:$' "$objdump" | cut -d':' -f1)"
lineno=$((lineno+1))
 
tail -n +$lineno "$objdump"
</syntaxhighlight>
 
== SourcesSee Also ==
=== External Links ===
*[http://www.delorie.com/djgpp/v2faq/faq17_1.html DJGPP AT&T Assembly Tutorial]<br>
*[http://asm.sourceforge.net//howto/Assembly-HOWTO.html Linux Assembly HOWTO]<br>
*[https://savannah.nongnu.org/projects/gas-user/ GAS/AS End User Help Project]<br>
*[httphttps://savannah.nongnu.org/projects/pgubook/ Programming from the Ground Up]<br>
 
[[Category:Assembly]]