Introduction
Assembly language is a text form of a processor’s machine code, with one line for each instruction. An assembler turns it into an object file or ELF object, which a linker makes into a program. There is no single assembly language. Each processor family has its own instructions, and each assembler has its own syntax and directives.
The extension mostly says which tools were used. .asm is the usual name
for NASM, MASM and other DOS and Windows assemblers, .nasm marks source
for NASM, and .s is the Unix convention, used by the GNU assembler and the
output of C compilers. GCC and Clang pass a .S file, with a capital, through
the C preprocessor first.
Structure
Assembly source is plain text, one statement per line: an optional label ending in a colon, an instruction or directive, its operands and a comment. Directives instruct the assembler rather than the processor, and set sections, define data and export symbols.
On x86 there are two main syntaxes. Intel syntax, used by NASM and
MASM, puts the destination first and leaves registers and numbers unmarked. MASM and
NASM differ in their directives and in details such as memory operands, which MASM may
write as dword ptr [eax] where NASM writes dword [eax].
AT&T syntax, the default of the GNU assembler on x86, puts the
destination last, prefixes registers with % and immediate values with
$, and adds a size suffix to the instruction, as in movq. The
GNU assembler also accepts Intel syntax after .intel_syntax noprefix.
Comments start with ; in NASM and MASM. In the GNU assembler the comment
character depends on the processor: # on x86 and @ on 32-bit
Arm. On AArch64 it uses //.
; NASM, Intel syntax: exit with status 0 on 64-bit Linux
section .text
global _start
_start:
mov rax, 60 ; system call number for exit
xor rdi, rdi ; status 0
syscall
# GNU assembler, AT&T syntax: the same program
.text
.globl _start
_start:
movq $60, %rax # system call number for exit
xorq %rdi, %rdi # status 0
syscall
Links
Software
- NASM – free x86 assembler using Intel syntax (Windows, macOS, Linux)
- GNU Binutils – includes
as, the GNU assembler (Linux, macOS, Windows) - MASM – the Microsoft Macro Assembler, included with Visual Studio (Windows)
- flat assembler – free x86 assembler (Windows, Linux, DOS)
Developers
Reference
- NASM manual – the assembler’s syntax and directives
- Using as – the GNU assembler manual
- Microsoft Macro Assembler reference – MASM’s directives and syntax
- Intel 64 and IA-32 Software Developer’s Manuals – the instruction set reference