The Complete Guide to NASM, GAS and x86 Assembler Syntax
A full guide to x86 assembler syntax: Intel and AT&T operand order, NASM and GNU as directives, addressing modes, sections, macros, linking and porting.

Two programs can contain the same machine code and look nothing alike. mov eax, [rbx + 8] and movl 8(%rbx), %eax assemble to the same four bytes, and a newcomer who has learned one spelling meets the other in the first disassembly, the first compiler listing or the first kernel source file. This guide teaches both spellings side by side. It covers the two assemblers most people use on Linux, NASM and the GNU assembler, called GAS or just as, and the syntax rules that decide how a line of source becomes bytes: operand order, sizes, addressing, directives, sections, symbols, macros and the build commands. Every example targets x86-64 Linux with NASM 2.16.03 and GNU binutils 2.42, and the examples are written and read, not run: each is followed by the output it produces. If you are choosing between the assemblers, the later part of the cluster compares them; if you already know one syntax, the translation tables in this guide are the fastest way to the other.
What an assembler does, and what it does not
An assembler translates mnemonics into machine code and records where symbols live. It does not choose instructions, allocate registers or optimize. When you write add rax, rbx, you get an add and nothing else. That one-to-one relationship is the reason assembly is educational and also the reason it is tedious: every decision a compiler would make is yours.
The assembler's output is an object file, a relocatable file in ELF format on Linux, that contains sections of bytes, a symbol table and a list of relocations, which are places where an address must be filled in later. A linker, usually GNU ld or the gcc driver, combines object files, resolves symbols and writes an executable. The two steps are separate commands even though gcc prog.s hides both:
nasm -f elf64 prog.asm -o prog.o
ld prog.o -o progEverything in the next sections is about the source text that goes into the first command.
Two syntaxes: Intel and AT&T
x86 assembly has two syntactic families, and the difference is cosmetic but absolute.
Intel syntax puts the destination first: mov rax, rbx copies RBX into RAX. Registers have no prefix, immediates have no prefix, and memory operands are wrapped in square brackets with an explicit size when the size is ambiguous: mov dword [rbx], 5. NASM, MASM, FASM and the Intel manuals all use it.
AT&T syntax puts the source first: movq %rbx, %rax copies RBX into RAX. Registers carry a % prefix, immediates a $ prefix, and the mnemonic carries a size suffix, b, w, l or q for 8, 16, 32 and 64 bits. Memory operands are written displacement(base, index, scale). GAS uses it by default, and so do GCC's -S output, the Linux kernel's assembly and objdump unless told otherwise.
Here is the same instruction sequence in both, with the machine code the assembler produces for each line:
bits 64
default rel
section .text
mov rax, 60 ; 48 c7 c0 3c 00 00 00
mov edi, 7 ; bf 07 00 00 00
mov ecx, [rbx + rsi*4 + 8] ; 8b 4c b3 08
lea rdx, [rax + rax*2] ; 48 8d 14 40
add qword [rbp - 8], 1 ; 48 83 45 f8 01 .text
movq $60, %rax # 48 c7 c0 3c 00 00 00
movl $7, %edi # bf 07 00 00 00
movl 8(%rbx,%rsi,4), %ecx # 8b 4c b3 08
leaq (%rax,%rax,2), %rdx # 48 8d 14 40
addq $1, -8(%rbp) # 48 83 45 f8 01The two files assemble to the same bytes because the CPU only ever sees the encoding. The syntax is a human interface. The encodings above follow the instruction formats in the Intel Software Developer's Manual, and you can confirm each one by assembling the file and running objdump -d. Both dialects are easier to read once you can see which parts are the same.
The same program in both assemblers
A complete program shows the whole path. This one writes a line to standard output and exits with status 0, using Linux system calls directly with no C library. The syscall numbers on x86-64 are 1 for write and 60 for exit, and arguments travel in RDI, RSI and RDX, as the system call convention post in this cluster spells out.
NASM:
bits 64
default rel
global _start
section .data
msg: db "hello, assembler", 10
len equ $ - msg
section .text
_start:
mov eax, 1 ; sys_write
mov edi, 1 ; fd = stdout
lea rsi, [msg] ; buffer
mov edx, len ; count
syscall
mov eax, 60 ; sys_exit
xor edi, edi ; status = 0
syscallGAS in AT&T syntax:
.globl _start
.data
msg:
.ascii "hello, assembler\n"
.set len, . - msg
.text
_start:
movl $1, %eax # sys_write
movl $1, %edi # fd = stdout
leaq msg(%rip), %rsi # buffer
movl $len, %edx # count
syscall
movl $60, %eax # sys_exit
xorl %edi, %edi # status = 0
syscallBuild and run either one:
nasm -f elf64 hello.asm -o hello.o && ld hello.o -o hello && ./hello
as hello.s -o hello.o && ld hello.o -o hello && ./hellohello, assemblerEvery construct in these programs is explained in the sections that follow: the directive that makes a symbol visible to the linker, the section switches, the data definitions, the length computed from two addresses, and the two forms of addressing.
Registers and operand sizes
The 16 general-purpose registers of x86-64 have names for each width. RAX is the 64-bit register, EAX its low 32 bits, AX its low 16 bits, and AL and AH the two bytes of AX. For RSI, RDI, RBP and RSP the byte forms are SIL, DIL, BPL and SPL; for R8 to R15 the forms are R8D, R8W and R8B. One rule matters more than any other: writing a 32-bit register clears the upper 32 bits of the 64-bit register, while writing 8-bit or 16-bit registers leaves the rest unchanged. That is why xor edi, edi zeroes all of RDI in a shorter encoding than xor rdi, rdi.
How each assembler expresses an operand size:
| Width | NASM keyword | GAS suffix | Registers (rax example) |
|---|---|---|---|
| 8 bits | byte | b | al |
| 16 bits | word | w | ax |
| 32 bits | dword | l | eax |
| 64 bits | qword | q | rax |
| 128 bits | oword | none | xmm0 |
A size is needed whenever no register operand pins it down. mov [rbx], 5 is ambiguous, because the 5 could be stored as a byte, a word, a dword or a qword. NASM rejects it with operation size not specified, and wants mov dword [rbx], 5. GAS in AT&T mode wants the suffix: movl $5, (%rbx). When a register is present, the register decides: mov [rbx], eax is 32 bits in NASM and movl %eax, (%rbx) or even mov %eax, (%rbx) in GAS, because GAS can infer the suffix from the register.
Immediates have their own size limits. Most instructions accept at most a 32-bit immediate, sign-extended to 64 bits when the operation is 64-bit. The single exception is mov r64, imm64, which NASM writes mov rax, 0x1122334455667788 and GAS writes movabsq $0x1122334455667788, %rax. Writing movq $0x1122334455667788, %rax in GAS produces an error because movq with an immediate takes only a sign-extended 32-bit value, and that surprise costs every newcomer one afternoon.
Memory operands and addressing modes
x86-64 addressing is one formula: base + index * scale + displacement, where the base and index are 64-bit registers, the scale is 1, 2, 4 or 8, and the displacement is a signed 32-bit number. Any part can be omitted. The two syntaxes spell the same formula differently.
| Meaning | NASM | GAS AT&T |
|---|---|---|
| Value at address in RBX | [rbx] | (%rbx) |
| With a displacement | [rbx + 8] | 8(%rbx) |
| Base and index | [rbx + rcx] | (%rbx,%rcx) |
| Scaled index | [rbx + rcx*4] | (%rbx,%rcx,4) |
| All parts | [rbx + rcx*4 + 8] | 8(%rbx,%rcx,4) |
| Absolute symbol | [table] | table |
| Symbol, RIP-relative | [rel table] | table(%rip) |
| Index only | [rcx*8 + table] | table(,%rcx,8) |
| Segment override | [fs:0x28] | %fs:0x28 |
The most common source of confusion is the difference between an address and the value at it. In NASM, mov rax, table loads the address of table as an immediate, while mov rax, [table] loads the eight bytes stored there. The brackets are the dereference. In AT&T syntax it is the opposite shape: movq $table, %rax loads the address, and movq table, %rax loads the contents. Dropping the $ is a classic mistake that assembles without complaint and reads from the symbol.
lea, load effective address, evaluates the addressing formula without touching memory. It is the only instruction where brackets in NASM do not mean a memory access, and it doubles as a three-operand add: lea rdx, [rax + rax*2] computes 3 times RAX in one instruction, which is what compilers use for multiplication by 3, 5 and 9.
On x86-64, position-independent code addresses data relative to the instruction pointer. NASM spells that [rel symbol], or default rel at the top of the file to make it the default. GAS spells it symbol(%rip), with the %rip as the base. A non-RIP-relative absolute address such as mov eax, [table] produces a 32-bit absolute relocation that a position-independent executable cannot hold, which is the subject of the relocation error post in this cluster.
Sections and what goes in them
A section is a named block of bytes with attributes. The linker concatenates same-named sections from all objects and maps them into memory. Four names cover almost every program.
| Section | Contents | Permissions | NASM | GAS |
|---|---|---|---|---|
.text | machine code | read, execute | section .text | .text |
.data | initialized writable data | read, write | section .data | .data |
.rodata | constants and strings | read | section .rodata | .section .rodata |
.bss | zero-initialized data, no bytes in the file | read, write | section .bss | .bss |
Putting code in .text and data in the others is not a style point. The linker maps .text as non-writable and the data sections as non-executable, so a program that stores to its code or runs its data faults. .bss takes no space in the file: its size is recorded and the loader maps zeroed pages, which is why large buffers belong there and not in .data.
In NASM you reserve .bss space with the res family, and in GAS with .skip, .space or .comm:
section .bss
buffer: resb 4096 ; 4096 bytes
count: resq 1 ; one 64-bit slot .bss
buffer: .skip 4096
count: .skip 8Both define two labels in the zero-initialized section, and buffer and count occupy 4104 bytes of address space but zero bytes of the file.
Defining data
Data directives place bytes at the current location in the current section. The mapping between the two assemblers is mechanical.
| Size | NASM | GAS | Example (NASM / GAS) |
|---|---|---|---|
| 1 byte | db | .byte | db 0x41 / .byte 0x41 |
| 2 bytes | dw | .word or .short | dw 1000 / .word 1000 |
| 4 bytes | dd | .long or .int | dd 100000 / .long 100000 |
| 8 bytes | dq | .quad | dq 1 / .quad 1 |
| String | db "text" | .ascii "text" | db "hi", 0 / .asciz "hi" |
| Repeat | times 8 db 0 | .fill 8, 1, 0 | |
| Align | align 16 | .balign 16 or .p2align 4 |
Strings are where the dialects differ in small, annoying ways. NASM strings in double or single quotes are raw bytes and there are no escape sequences inside double quotes, so a newline is written as a separate number: db "line", 10. Backquoted strings, ` line\n , do process C escapes. GAS strings are always C-style: .ascii "line\n" contains the newline, and .asciz appends a terminating zero byte, where NASM needs db "line", 10, 0`.
Little-endian layout applies to everything. dd 0x11223344 stores the bytes 44 33 22 11. The same value in GAS, .long 0x11223344, stores the same four bytes.
Labels, symbols and constants
A label is a name for an address. In both assemblers it is an identifier followed by a colon: msg:. In NASM the colon is optional and a label in .text is a position, while in GAS the colon is required.
Constants are different from labels. They name a number and take no space.
STDOUT equ 1
SYS_WRITE equ 1
len equ $ - msg ; $ is the current address
mask equ 0xff00 .set STDOUT, 1
.equ SYS_WRITE, 1
len = . - msg # . is the current address$ in NASM and . in GAS both mean the current location. Subtracting a label from $ right after the data gives its length, which is the idiom in the hello program. The subtraction is evaluated by the assembler to a constant because both operands are in the same section, so len becomes the number 17 in the output. $$ in NASM is the start of the current section.
Symbols are local to the object file unless you export them. global _start in NASM and .globl _start in GAS mark the symbol visible to the linker, and extern printf in NASM, with .extern in GAS optional, declares a symbol defined elsewhere. Forgetting global _start makes ld warn cannot find entry symbol _start; defaulting to ... and produce a binary that crashes. Linkage is the subject of the post on global, extern and .globl in this cluster.
Directives that shape the output
Beyond sections and data, a handful of directives appear in almost every file.
| Purpose | NASM | GAS |
|---|---|---|
| 64-bit mode | bits 64 | implicit for -64; .code64 |
| RIP-relative default | default rel | not needed; write (%rip) |
| Alignment | align 16 | .p2align 4 |
| Include a file | %include "x.inc" | .include "x.inc" |
| Symbol type | not needed | .type f, @function |
| Symbol size | not needed | .size f, . - f |
| Mark stack non-executable | section .note.GNU-stack noalloc noexec nowrite progbits | .section .note.GNU-stack,"",@progbits |
The last row deserves a comment. Binutils 2.39 and later warn when an object lacks a .note.GNU-stack section, because the linker would otherwise mark the whole program's stack executable. Add the line to every file that ends up in a binary you care about.
.type and .size give the symbol table the information that nm and debuggers use to show function boundaries. GCC emits them for every function, and in a GAS file you should too. NASM writes the type and size only when you ask with global name:function name.end-name style annotations, which most NASM code omits.
Instructions that look different between the two
Most mnemonics are the same in both dialects once the suffix is added. A few are not, and each has tripped up at least one porting effort.
| Intel | AT&T | Note |
|---|---|---|
movzx eax, byte [rbx] | movzbl (%rbx), %eax | zero-extending load |
movsx rax, dword [rbx] | movslq (%rbx), %rax | sign-extending load |
movsxd rax, ecx | movslq %ecx, %rax | register to register |
cdqe | cltq | sign-extend EAX into RAX |
cqo | cqto | sign-extend RAX into RDX:RAX |
call [rax] | call *(%rax) | indirect call needs * |
jmp [table + rax*8] | jmp *table(,%rax,8) | indirect jump needs * |
rep movsb | rep movsb | same in both |
ret | ret or retq | same |
The * on indirect branches is AT&T's way of saying that the operand is a pointer to the target and not the target. Omitting it assembles a direct branch to the symbol, which is a silent bug: call (%rax) is not valid, but jmp table jumps to the table's bytes as code.
Comments, numbers and layout rules
Comments are a semicolon to end of line in NASM and a hash in GAS on x86. GAS also accepts C comments /* ... */ anywhere, and, with .S files preprocessed by the C preprocessor, // too. A single # at the start of a line in a .S file is a preprocessor directive, so #include and #define work there, and comments in such files are safest written with /* */ or //. The comment forms matter because a stray hash at the start of a GAS line in a .S file is read by the preprocessor, not the assembler.
Numeric literals follow similar rules: 0x1F and 1Fh in NASM, 0x1F in GAS; 0b1010 in both for recent versions, with 1010b also in NASM; octal 0o17 in NASM and 017 in GAS, where a leading zero means octal, which has surprised everyone who wrote 010 meaning ten. Character constants are 'A' in both; NASM allows multi-character constants like 'ABCD' packed little-endian into a number, and GAS does not.
Instruction layout is free-form in both: one statement per line, optional label, optional whitespace. GAS allows several statements on one line separated by ;, which in NASM starts a comment, so a line copied from one to the other can lose half of its content without an error.
Macros and conditional assembly
Both assemblers have a macro processor, and both are used constantly in real code.
NASM's %macro takes a name and a parameter count, and refers to arguments as %1, %2:
%macro sys_write 3
mov eax, 1
mov edi, %1
lea rsi, [%2]
mov edx, %3
syscall
%endmacro
sys_write 1, msg, lenThe call at the bottom expands in place to the five instructions above it with 1, msg and len substituted. %define creates a single-line text substitution, %assign an evaluated numeric one, and %if, %else and %endif select code at assembly time.
GAS's .macro names its parameters, and refers to them with a backslash:
.macro sys_write fd, buf, count
movl $1, %eax
movl $\fd, %edi
leaq \buf(%rip), %rsi
movl $\count, %edx
syscall
.endm
sys_write 1, msg, lenConditionals use .if, .else and .endif, and a .S file can use the C preprocessor instead, which most large projects do because it composes with header files shared with C. A later post in this cluster covers NASM macros and the preprocessor in detail.
Local labels
Large functions need labels for loops and branches that must not collide with any other function. NASM treats any label that begins with a dot as local to the preceding non-local label: .loop after strlen: is really strlen.loop. GAS offers numeric local labels, 1:, 2:, referenced as 1b for the nearest one backward and 1f for the nearest forward, and the .L prefix, for labels that the assembler keeps out of the symbol table entirely.
strlen:
xor eax, eax
.next:
cmp byte [rdi + rax], 0
je .done
inc rax
jmp .next
.done:
retstrlen:
xorl %eax, %eax
1:
cmpb $0, (%rdi,%rax)
je 2f
incq %rax
jmp 1b
2:
retBoth compute the length of a NUL-terminated string, leaving it in RAX. Numeric labels are the idiom in the kernel and in macros, where a macro that defines its own labels must not collide with the surrounding code, and each expansion of a macro can reuse 1: safely.
Working with the build tools
The commands you will type constantly are short. For NASM:
nasm -f elf64 -g -F dwarf prog.asm -o prog.o
nasm -f elf64 -l prog.lst prog.asm
ld prog.o -o prog-f elf64 selects the object format, -g -F dwarf adds debug information so GDB shows source lines, and -l writes a listing. For GAS:
as -g --64 prog.s -o prog.o
as -al prog.s
ld prog.o -o prog-g adds debug info, and -al prints a listing to the terminal. For a program that calls the C library, link with gcc, which supplies the start-up code and the dynamic linker setup, and write main instead of _start:
gcc prog.o -o progThe choice between _start and main has real consequences. With _start you own the process from the first instruction: no C runtime, no argc in a register, the stack holding argc, argv and envp directly. With main, glibc's start code has already run, the stack is aligned, and returning from main calls exit. A post in this cluster compares the two.
Disassembly closes the loop. objdump -d -M intel prog.o shows Intel syntax and objdump -d prog.o shows AT&T, so you can read your own object file in either dialect:
0000000000000000 <_start>:
0: b8 01 00 00 00 mov eax,0x1
5: bf 01 00 00 00 mov edi,0x1
a: 48 8d 35 00 00 00 00 lea rsi,[rip+0x0]
11: ba 11 00 00 00 mov edx,0x11
16: 0f 05 syscall
18: b8 3c 00 00 00 mov eax,0x3c
1d: 31 ff xor edi,edi
1f: 0f 05 syscallThe lea has a zero displacement in the object because the linker fills in the distance to msg through a relocation, and readelf -r hello.o lists it as R_X86_64_PC32 .data - 4. The immediate 0x11 is 17, the length of the message including its newline.
A larger example: counting characters
A realistic function exercises most of the syntax at once. This one counts how many times a byte occurs in a buffer, with the signature size_t count_byte(const char *p, size_t n, int c) under the System V ABI: pointer in RDI, length in RSI, byte in EDX, result in RAX.
bits 64
default rel
global count_byte
section .text
count_byte:
xor eax, eax ; count = 0
test rsi, rsi
jz .done
.loop:
cmp byte [rdi], dl ; compare *p with c (low byte of edx)
jne .skip
inc rax
.skip:
inc rdi
dec rsi
jnz .loop
.done:
ret
section .note.GNU-stack noalloc noexec nowrite progbitsThe same function in GAS:
.text
.globl count_byte
.type count_byte, @function
count_byte:
xorl %eax, %eax # count = 0
testq %rsi, %rsi
jz .Ldone
.Lloop:
cmpb %dl, (%rdi) # compare *p with c
jne .Lskip
incq %rax
.Lskip:
incq %rdi
decq %rsi
jnz .Lloop
.Ldone:
ret
.size count_byte, . - count_byte
.section .note.GNU-stack,"",@progbitsA C test calls it with a known buffer:
#include <stdio.h>
#include <string.h>
unsigned long count_byte(const char *p, unsigned long n, int c);
int main(void)
{
const char *s = "banana bandana";
printf("%lu\n", count_byte(s, strlen(s), 'a'));
return 0;
}6Count the letters: "banana" has three, "bandana" has three, six in all. Note the reversed operand order in the compare: cmp byte [rdi], dl in Intel compares the memory byte with DL, and cmpb %dl, (%rdi) in AT&T lists the operands in the opposite order, which is the same comparison. For cmp and sub, the order matters for the flags and for conditional jumps, and mixing up the order when translating is the most common porting bug.
Reading compiler output in both syntaxes
You will read assembler more than you write it, and the most common source is the compiler. gcc -S produces AT&T syntax by default, and gcc -S -masm=intel produces Intel syntax that NASM can almost accept. The flag -fverbose-asm annotates each line with the source expression it came from, which is the quickest way to learn what a construct compiles to. A one-line function shows the whole shape:
long add(long a, long b) { return a + b; } .file "add.c"
.text
.globl add
.type add, @function
add:
leaq (%rdi,%rsi), %rax
ret
.size add, .-add
.ident "GCC: (GNU) 14.2.0"
.section .note.GNU-stack,"",@progbitsGCC 14 at -O2 emits leaq (%rdi,%rsi), %rax for an addition of two registers, using the addressing formula as a three-operand add. Everything around it is directives: .type and .size for the symbol table, .ident for the compiler string, and the stack note. When you read a listing, ignore the directives first and read the instructions, then come back to the directives to see what they declare.
How the assembler chooses an encoding
The same mnemonic can map to several encodings, and the assembler picks one by rules you should know, because they explain the byte differences you see between listings. Take mov eax, 1. There is a five-byte form, b8 01 00 00 00, that carries the register in the opcode and a 32-bit immediate, and a longer six or seven byte form through the ModRM byte. Both assemblers choose the shortest. For mov rax, 1, the choice is between a 10-byte movabs with a 64-bit immediate, 48 b8 ..., and a 7-byte mov r/m64, imm32 with sign extension, 48 c7 c0 01 00 00 00. NASM and GAS both emit the seven-byte form, and a source that wants the shortest zeroing writes mov eax, 1, which is five bytes and also clears the upper half.
Jumps work the same way. A conditional jump has a two-byte form with an 8-bit displacement, reaching 127 bytes forward or 128 back, and a six-byte near form with a 32-bit displacement. GAS and NASM start with the short form and widen it when the target is too far, a process called relaxation. NASM's -O option controls how many passes it makes: since NASM 2.09 the default is -Ox, which iterates until the sizes settle. When you need a particular form, NASM accepts jmp short label and jmp near label, and GAS picks the form itself and widens a jump automatically whenever the short form cannot reach.
Two details commonly puzzle readers of listings. First, xor eax, eax assembles to two bytes, 31 c0, while mov eax, 0 takes five, and compilers prefer the xor even though it modifies flags. Second, lea with a 32-bit destination, as in lea eax, [rdi + rsi], encodes the same as with a 64-bit destination plus an address-size prefix and is rarely worth writing. If a listing shows an unexpected 66 or 67 prefix byte, the assembler is applying an operand-size or address-size override, and the cause is almost always a 16-bit register or a 32-bit address register in the source.
Flags and conditional jumps
Most arithmetic sets the flags register, and conditional jumps read it. The mnemonics are the same in both syntaxes, which makes this one of the easier parts to port. The pairs to remember are je and jne for equal and not equal, jl, jle, jg, jge for signed comparisons, jb, jbe, ja, jae for unsigned comparisons, and js and jns for the sign flag. jz and jnz are aliases of je and jne.
The one place the dialects differ is the operand order of the instruction that sets the flags. A comparison is a subtraction whose result is discarded, and the jump reads the flags from first - second in Intel order. In NASM, cmp rax, rbx followed by jl target jumps when RAX is less than RBX. In AT&T the operands are reversed, so the same test is written cmpq %rbx, %rax, and a reader who forgets the swap will invert every conditional in the program:
cmp rax, rbx ; flags from rax - rbx
jl .rax_smaller ; signed: taken when rax < rbx cmpq %rbx, %rax # flags from rax - rbx
jl .Lrax_smaller # signed: taken when rax < rbxThe rule that works in both: write the comparison in words, "is RAX less than RBX", then place the operands so that RAX is the left operand in Intel and the right operand in AT&T. For test, the operands commute, so order does not matter, which is why test rax, rax and testq %rax, %rax both check for zero without ambiguity.
A worked example: reversing a string in place
A longer example uses data, a loop, two pointers and the length idiom together. The program reverses a string in the writable data section and prints it, using only the syntax from this guide. NASM version:
bits 64
default rel
global _start
section .data
text: db "stressed", 10
len equ $ - text - 1 ; exclude the newline
section .text
_start:
lea rsi, [text] ; left pointer
lea rdi, [text + len - 1] ; right pointer
.swap:
cmp rsi, rdi
jae .print
mov al, [rsi]
mov bl, [rdi]
mov [rsi], bl
mov [rdi], al
inc rsi
dec rdi
jmp .swap
.print:
mov eax, 1 ; sys_write
mov edi, 1 ; stdout
lea rsi, [text]
mov edx, len + 1 ; include the newline
syscall
mov eax, 60 ; sys_exit
xor edi, edi
syscall
section .note.GNU-stack noalloc noexec nowrite progbitsnasm -f elf64 reverse.asm -o reverse.o && ld reverse.o -o reverse && ./reversedessertsThe word "stressed" has eight letters, and reversing it gives "desserts". Several syntax points show up at once. len equ $ - text - 1 is evaluated at assembly time to 8. The expression [text + len - 1] is a constant address computation inside the brackets, resolved by the assembler to a displacement. The byte registers AL and BL carry single characters, and the size of each mov is fixed by them with no keyword needed. The labels .swap and .print are local to _start. The AT&T translation of the loop body shows the differences concretely:
.Lswap:
cmpq %rdi, %rsi
jae .Lprint
movb (%rsi), %al
movb (%rdi), %bl
movb %bl, (%rsi)
movb %al, (%rdi)
incq %rsi
decq %rdi
jmp .LswapThe cmp operands are swapped to keep the same meaning, each mov has its b suffix and its operands reversed, and the labels use the .L prefix, so none of them reaches the symbol table.
Debugging in either syntax
GDB prints disassembly in AT&T syntax by default, and one command changes it for the session: set disassembly-flavor intel. Put it in ~/.gdbinit if you prefer Intel. With debug information assembled in (nasm -g -F dwarf or as -g), layout asm shows the instructions with the current one highlighted, stepi executes one instruction, and info registers rax rbx rsi rdi prints the four registers used above. After each stepi through the reversal loop you can watch RSI climb and RDI fall until they cross, which makes the loop's termination condition concrete.
Printing memory uses the same formats as in the stack frame post: x/9cb &text shows nine bytes as characters, x/s &text shows the string, and p/x $eflags shows the flags register after a comparison. Because GDB reads the symbol table, text and len are available as names whether you wrote them in NASM or GAS, which is another reason to give data labels meaningful names.
Exercises that teach the syntax
Work through these with the manuals open: the NASM documentation at https://www.nasm.us/doc/ for the Intel side and the GNU as manual at https://sourceware.org/binutils/docs/as/ for the AT&T side. Both are primary sources, and both are short enough to search rather than read from the front.
Reading about syntax helps less than translating a few lines yourself. Four short exercises cover the traps in this guide.
First, take the count_byte function above and rewrite it with the pointer advanced by lea and the length counted down with dec, in both syntaxes, and check that the object files disassemble to the same instructions. Second, write a function max3 that returns the largest of three signed values in RDI, RSI and RDX using cmp and cmovg, and verify the operand order with a test where the first argument is largest. Third, write a data table of eight dq values and a loop that sums them using [table + rcx*8] in NASM and table(,%rcx,8) in GAS, noticing that the NASM form needs default rel removed or an explicit abs to assemble the absolute address. Fourth, take any C file, compile it with gcc -O1 -S and again with -masm=intel, and read the two outputs side by side until each instruction looks as natural in one dialect as in the other.
When an exercise fails to assemble, read the first error only and fix it before looking at the rest, since assemblers often report one real mistake and several follow-on complaints. Each exercise forces a different part of the syntax: sizes and suffixes in the first, operand order in the second, addressing modes in the third, and the dialect translation itself in the fourth.
Switching dialects without rewriting
GAS can read Intel syntax with a directive: .intel_syntax noprefix at the top of a file makes it accept mov rax, [rbx + 8] with no % prefixes, and .att_syntax prefix switches back. NASM has no AT&T mode. This is how many projects write Intel-syntax assembly for GCC's toolchain, and it is what the GCC inline assembly option -masm=intel relies on. One difference remains even in Intel mode: GAS treats a bare symbol as a memory reference, so mov rax, msg loads the contents of msg, and mov rax, offset msg loads its address. NASM is the other way around, and that single semantic difference is the largest trap when moving code between them.
The converse tool is objdump -M intel, which prints disassembly in Intel syntax, and -M intel-mnemonic for finer control. For converting a whole file, the practical path is to write the new version by hand against the tables in this guide, assemble both, and compare the object files byte for byte with cmp or objdump -d. If the bytes match, the port is right.
Common errors and what they mean
Error messages repeat. The ones you will meet in your first week:
| Message | Assembler | Cause |
|---|---|---|
operation size not specified | NASM | memory and immediate with no size keyword |
invalid combination of opcode and operands | NASM | wrong operand sizes, for example mov eax, rbx |
symbol 'x' undefined | NASM | missing extern, or a typo |
no such instruction: 'mov' followed by odd text | GAS | a comment character or Intel syntax read as AT&T |
operand type mismatch for 'mov' | GAS | movq with a 64-bit immediate, or a size mismatch |
ambiguous operand size for 'mov' | GAS | mov $5, (%rbx) without a suffix |
cannot find entry symbol _start | ld | missing global _start or wrong name |
relocation R_X86_64_32 ... PIE | ld | absolute address in a PIE; use RIP-relative |
When an error is unclear, assemble with a listing and look at the line the assembler cites. NASM's -l shows the bytes it generated next to each source line, and a line that produced no bytes was treated as a comment or as nothing at all.
Choosing between them
The choice rarely matters for correctness and often matters for the project. Use NASM when you want the clearest Intel syntax, a strong macro system, and a standalone tool that also targets Windows and macOS object formats. Use GAS when the code lives in a GCC or kernel tree, must be preprocessed with C headers, or has to share symbols and CFI with compiler output. The post comparing NASM, GAS, MASM and FASM puts the same question as a table, and the post on porting NASM to GAS goes line by line through one program.
What you should not do is mix syntaxes inside one file or one team without a reason. Pick one for a project, write it down in the build instructions, and keep the translation tables from this guide nearby for the days you read the other kind.
A checklist for your first assembly file
Before you assemble a new source file, run through the points where beginners lose the most time:
Does the file declare its mode and addressing default? In NASM that is
bits 64anddefault rel.Is the entry symbol exported, and does the linker command name it?
global _startor.globl _start, andld -eif it has a different name.Does every memory operand with an immediate carry a size?
dword [rbx]or thelsuffix.Is every address either RIP-relative or deliberately absolute with a linker mode that allows it?
Does the file end with the non-executable stack note?
After assembling, did you disassemble with
objdump -dand compare the instructions with what you meant?
That last step is the habit that separates people who write assembly from people who write something that happened to assemble. Reading your own object file in the other syntax is also the fastest way to learn it.


