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.
Two other extensions are shared with binary formats. A .z80 file is
usually a ZX Spectrum snapshot, so the viewer takes it as a snapshot when it has the
structure of one and as Z80 assembly when it is text. A .a file is usually
an ar archive, a static library, and is read as assembly source only when
it does not start with the archive signature !<arch> and is text.
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 //.
The viewer colours a comment by the processor and assembler it decides the text is
written for. ;, // and /* */ are comments in every
reading, and so is a line that starts with # unless it is a preprocessor
directive (#include, #define and the like). A #
after an instruction starts a comment only for the GNU assembler on x86 (when the text
has AT&T registers or GNU directives such as .globl, so NASM and MASM
text is not affected), RISC-V, MIPS and PowerPC, and not on the rest of a preprocessor
line. An @ starts a comment only for 32-bit Arm (text that uses the
registers r0 to r15 and none of the AArch64 ones), where
# marks an immediate and stays part of the instruction. The same text read
for another processor or for none keeps those characters plain.
; 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
Not described
Known, not yet covered
- Reference: only x86 is described, in its Intel and AT&T syntaxes. The viewer names ten processor families (x86, 68000, ARM, Z80, 6502, MIPS, SPARC, RISC-V, PowerPC and WebAssembly), but this page has no section for the syntax, registers or directives of any family except x86.
- Reference: there is no list of directives or pseudo-instructions for NASM, MASM or the GNU assembler, and no description of macros, conditional assembly, local labels, sections or segments beyond the two examples.
- Viewer: the comment character after an instruction is decided only for the processors named in the Structure section. The GNU assembler’s
#comments of other targets, the!of SPARC, the|and*of 68000 and a text that does not clearly name its processor are not coloured as comments. - Viewer: registers come from a fixed list: x86 names,
x0–x31,w0–w31,r0–r15and a few others. The Z80 pairs (hl,bc), 68000d0–d7, MIPS names such as$t0and$sp, andr16upwards are left plain. - Viewer: there is no table of instructions. The first word of a line is coloured as an instruction whatever it is, so a misspelt mnemonic is coloured the same as a real one; a name in an operand, such as the target of
jne .L1, is left plain. - Viewer: the directive words are a fixed list plus any word starting with a dot. Directives of other assemblers (and macros defined in the file) are coloured as instructions. A text is named for a processor only when it clearly reads as one of the ten families; other processors (AVR, PIC, 8051, SuperH and so on) are shown as plain “Assembly language source file”, coloured the same way.
- Viewer: only the first 1 MiB of a file is read and coloured; the viewer says so in its problems list.
Also used for
- AIX big archive – IBM AIX’s archive with linked members and 32-bit and 64-bit symbol tables.
- AIX small archive – The older AIX archive: linked members and one symbol table, in narrower fields.
- BSD and macOS static library – The ar archive format of macOS and the BSDs.
- GNU thin archive – An archive that lists its members but leaves their data in other files.
- Static library archive – Unix static libraries: object files in an ar archive with a symbol index.
- System V and GNU static library – The ar archive format of Linux and most Unix systems.
- ZX Spectrum snapshot – ZX Spectrum snapshot: CPU registers and RAM pages, compressed.
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