TExcelTAD
Home › x86-64 Assembly › NASM Syntax

NASM Syntax

Assembly source is line-oriented: one instruction (or directive) per line. This lesson covers the shape of a line, how NASM treats names and case, how to tell it the size of a memory operand, and how NASM's Intel syntax differs from the AT&T syntax you will see in some tools.

The shape of a line

Every line has up to four parts, all optional:

nasm
label:    instruction   operands        ; comment
loop_top: add           rax, [rbx + 8]  ; add the next element
  • A label names the address of the following instruction or data. The colon is recommended.
  • The instruction (or mnemonic) is the operation: mov, add, jmp, … or a directive such as db or section.
  • Operands are separated by commas. In Intel syntax the destination comes first: mov rax, rbx copies rbx into rax.
  • A comment starts with ; and runs to the end of the line.

Operand types

Kind Example Meaning
register rax, cl, r9d a CPU register
immediate 42, 'A', msg_len, 3 * 8 a constant encoded in the instruction (NASM evaluates the expression)
memory [counter], [rbx + rcx*8] the bytes at an address

Most two-operand instructions allow reg, reg, reg, imm, reg, mem, mem, reg and mem, imm — but never mem, mem.

Case and names

Instruction names, register names and directives are case-insensitive, so MOV RAX, 1 and mov rax, 1 are the same. Labels and constants are case-sensitive: Total and total are different symbols. Lower case is the usual style.

nasm · try it yourself
section .data
Total:  dq 100
total:  dq 7                ; a different label!

section .text
global _start
_start:
    MOV RDI, [Total]        ; upper-case instruction and register: fine
    call print_uint
    mov rdi, [total]
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
100
7

Size specifiers

When a register is involved, NASM knows the operand size from the register: mov [count], eax writes 4 bytes. When there is only memory and a constant, the size is ambiguous and you must say it with byte, word, dword or qword:

nasm · try it yourself
section .bss
counter: resq 1

section .text
global _start
_start:
    mov [counter], 5        ; 1, 2, 4 or 8 bytes?
    mov eax, 60
    xor edi, edi
    syscall
<source>:7: error: operation size not specified

The fixed version writes 8 bytes. (resq 1 reserves one zero-filled quadword; see the next lesson.)

nasm · try it yourself
section .bss
counter: resq 1

section .text
global _start
_start:
    mov qword [counter], 5
    add qword [counter], 10
    mov rdi, [counter]      ; size known from rdi
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
15

A sneaky pitfall: a lone word is a label

NASM allows labels without a colon. That means a misspelled instruction with no operands is silently accepted as a label! Here sycall (missing an s) becomes a label, the real exit never happens, and the program falls off the end of its code:

nasm · try it yourself
section .text
global _start
_start:
    mov eax, 60
    xor edi, edi
    sycall                  ; typo: this defines a label "sycall"
Program terminated with signal SIGSEGV (11)

Tip: Always write labels with a colon. When assembling locally, nasm -w+orphan-labels warns about labels without one, which catches this mistake.

Intel vs AT&T syntax

NASM uses Intel syntax. The GNU tools (gcc -S, objdump without -M intel, gdb by default) use AT&T syntax, so you will meet both:

Intel (NASM) AT&T (GNU as)
Operand order mov rax, rbx (dest, src) movq %rbx, %rax (src, dest)
Registers rax %rax
Immediates 5 $5
Memory [rbx + rcx*8 + 16] 16(%rbx,%rcx,8)
Size from operands or qword suffix: b w l q (movq)
Comments ; #

Use objdump -M intel and gdb's set disassembly-flavor intel to see Intel syntax everywhere.

Other syntax details

  • Character and string constants: 'A', "text" and `with\nescapes` (backquotes understand C-style escapes such as \n and \t).
  • Expressions are evaluated by NASM at assembly time: mov eax, 60 * 60 * 24 assembles mov eax, 86400.
  • Line continuation: a backslash at the end of a line joins it with the next one.
  • Local labels start with a dot (.loop) and belong to the previous normal label. They get their own lesson later.

Test yourself

In NASM's Intel syntax, what does sub rax, rbx do?

  1. rbx = rbx - rax
  2. rax = rax - rbx
  3. rax = rbx - rax
  4. It compares rax and rbx

Why does mov [counter], 5 fail to assemble?

  1. Immediates can't be stored to memory
  2. The operand size is ambiguous; write mov qword [counter], 5 (or byte/word/dword)
  3. counter must be in a register
  4. 5 is too large

Which of these is AT&T syntax?

  1. mov rax, [rbx+8]
  2. movq 8(%rbx), %rax
  3. mov qword [rbx], 1
  4. lea rdi, [rel msg]