Assembly Directives Reference: GAS vs NASM vs MASM
Compare the three major x86/x64 assembler syntaxes side by side: GAS (GNU Assembler), NASM (Netwide Assembler), and MASM (Microsoft Macro Assembler).
This assembly directives reference compares the three major x86/x64 assembler syntaxes side by side: GAS (GNU Assembler, AT&T syntax), NASM (Netwide Assembler, Intel syntax), and MASM (Microsoft Macro Assembler, Intel syntax). Use it to translate between toolchains when reading examples, porting code, or switching platforms.
Interactive Lab: Assembly Directives Quick Reference
Search and filter 22 essential assembler directives across GAS, NASM, and MASM. Expand any directive to compare syntax, see its purpose, and view real usage examples.
1. Section Control
Directives that define the memory segments where code and data will reside.
| Purpose | GAS (GNU) | NASM | MASM | Description |
|---|---|---|---|---|
| Code section | .text | section .text | .code | Defines the executable code section. |
| Initialized data | .data | section .data | .data | Defines the section for initialized variables. |
| Uninitialized data | .bss | section .bss | .data? | Defines the section for uninitialized variables. |
| Read-only data | .section .rodata | section .rodata | .const | Defines the section for constants/read-only data. |
| Custom section | .section .mydata,"aw" | section .mydata | _mydata SEGMENT | Defines a user-specified custom section. |
2. Symbol & Scope
Directives that control the visibility and scope of symbols for the linker.
| Purpose | GAS (GNU) | NASM | MASM | Description |
|---|---|---|---|---|
| Global symbol | .global _start | global _start | PUBLIC _start | Exports a symbol to make it visible to the linker. |
| External symbol | .extern printf | extern printf | EXTERN printf:PROC | Imports a symbol defined in another file. |
| Weak symbol | .weak my_func | global my_func:function weak | — | Defines a symbol that can be overridden at link time. |
| Local label | .L0: | .local: | @@: | Defines a label with scope limited to the current block. |
| Entry point | .global _start | global _start | END main | Marks the starting point of the program. |
3. Data Definition Directives
Directives used to allocate memory and initialize data of various sizes.
| Purpose | GAS (GNU) | NASM | MASM | Description |
|---|---|---|---|---|
| Define byte | .byte 0x41 | db 0x41 | DB 41h | Defines an 8-bit value. |
| Define word | .word 0x1234 | dw 0x1234 | DW 1234h | Defines a 16-bit value. |
| Define dword | .long 42 | dd 42 | DD 42 | Defines a 32-bit value. |
| Define qword | .quad 42 | dq 42 | DQ 42 | Defines a 64-bit value. |
| Define string | .ascii "hello" | db "hello" | DB "hello" | Defines a character string. |
| Null-term string | .asciz "hello" | db "hello", 0 | DB "hello", 0 | Defines a string with a trailing null byte. |
| Define float | .float 3.14 | dd 3.14 | REAL4 3.14 | Defines a 32-bit floating point value. |
| Define double | .double 3.14 | dq 3.14 | REAL8 3.14 | Defines a 64-bit floating point value. |
| Reserve bytes | .skip 64 | resb 64 | DB 64 DUP(?) | Reserves uninitialized space in bytes. |
| Reserve words | .skip 128 | resw 64 | DW 64 DUP(?) | Reserves uninitialized space in words. |
| Reserve dwords | .skip 256 | resd 64 | DD 64 DUP(?) | Reserves uninitialized space in double words. |
4. Alignment & Spacing
Directives used to align instructions/data for performance or to pad sections.
| Purpose | GAS (GNU) | NASM | MASM | Description |
|---|---|---|---|---|
| Align to N | .align 16 | align 16 | ALIGN 16 | Aligns the next instruction/data to a byte boundary. |
| Align with fill | .align 16, 0x90 | align 16, nop | ALIGN 16 | Aligns to boundary and fills gap with specific byte. |
| Pad with bytes | .fill 8, 1, 0x90 | times 8 nop | DB 8 DUP(90h) | Inserts a specific number of padding bytes. |
| Repeat | .rept 4 / .endr | times 4 nop | REPT 4 / ENDM | Repeats a block of code or an instruction. |
5. Macros & Conditional Assembly
Directives used for basic metaprogramming and selective code generation.
| Purpose | GAS (GNU) | NASM | MASM | Description |
|---|---|---|---|---|
| Define macro | .macro name arg1 | %macro name 1 | name MACRO arg1 | Starts a macro definition. |
| End macro | .endm | %endmacro | ENDM | Ends a macro definition. |
| Macro param | \arg1 | %1 | arg1 | References a parameter within a macro. |
| If defined | .ifdef SYMBOL | %ifdef SYMBOL | IFDEF SYMBOL | Conditional assembly if a symbol is defined. |
| If equal | .if expr == 1 | %if expr == 1 | IF expr EQ 1 | Conditional assembly based on an expression. |
| Else | .else | %else | ELSE | Alternative branch for conditional assembly. |
| End if | .endif | %endif | ENDIF | Ends a conditional assembly block. |
| Define constant | .equ NAME, 42 | NAME equ 42 | NAME EQU 42 | Assigns a constant value to a symbol. |
6. External Files
Directives used to modularize source code across multiple files.
| Purpose | GAS (GNU) | NASM | MASM | Description |
|---|---|---|---|---|
| Include file | .include "file.inc" | %include "file.inc" | INCLUDE file.inc | Includes the contents of an external source file. |
Key Syntax Differences
Beyond directives, the three assemblers differ in how they express instructions and operands:
| Feature | GAS (AT&T) | NASM (Intel) | MASM (Intel) |
|---|---|---|---|
| Operand Order | Source, Destination | Destination, Source | Destination, Source |
| Register Prefix | Requires % (e.g., %rax) | None (e.g., rax) | None (e.g., rax) |
| Immediate Prefix | Requires $ (e.g., $42) | None (e.g., 42) | None (e.g., 42) |
| Memory Ref | offset(base, index, scale) | [base + index*scale + offset] | [base + index*scale + offset] |
| Size Suffixes | Suffixes like q, l, w, b | Keywords like qword, dword | Keywords like QWORD PTR |
| Comments | # | ; | ; |
| Hex Format | 0x1234 | 0x1234 or 1234h | 1234h |
Example Comparison
; Move the 64-bit value at [rbp-8] into rax
GAS: movq -8(%rbp), %rax # source, destination
NASM: mov rax, qword [rbp-8] ; destination, source
MASM: mov rax, QWORD PTR [rbp-8] ; destination, source
Related Resources
Deep Dives
- Assembly Hello World: Cross-Platform Syscall Tutorial
- Windows Assembly Toolchain: MASM Guide
- x86-64 vs ARM64 Instruction Map
Cheat Sheets
Official Manuals
- GNU Assembler: GAS Manual
- NASM: NASM Documentation
- MASM: MASM Reference