assembler
termKept in this browser · My desk
The program that translates assembly language into machine code: it encodes each instruction, keeps a symbol table so labels become addresses, obeys directives and expands macros, and usually writes an object file for a linker.
Every compiler that emits machine code either contains an assembler or calls one, so the assembler sits under almost all software. Its one hard problem is the label used before it is defined, which David Salomon calls the future symbol problem; the classic answers are the one-pass and two-pass designs. On x86, where a branch can be short or long, choosing sizes can force more passes, which is why NASM documents a multipass mode.
What it is. An assembler reads a program written in assembly language and writes machine code: it encodes each instruction from its mnemonic and operands, keeps a symbol table so that labels become addresses, obeys directives and expands macros, and usually leaves an object file for a linker. David Salomon's Assemblers and Loaders (1993) also names its relatives: a macro assembler supports macros; a cross-assembler "runs on one computer and assembles programs for another", usually written in a higher-level language and run on a large machine for a small one; a meta-assembler handles many instruction sets; and a disassembler is, in a sense, the opposite, translating machine code back into source.
How it works. Salomon describes the core: a location counter that holds "the address into which the current instruction will eventually be loaded" and a symbol table with each symbol's name, value and type. A label defines a symbol with the current value of the counter; an operand uses it.
Two passes, or one. A two-pass assembler "performs two passes over the source file": the first collects every label and its value, assembling nothing; the second reads the lines again and encodes them using the table. A one-pass assembler reads the source once and fills the gaps later, and in its classic form loads the code straight into memory instead of writing a relocatable object file. The GNU assembler says it "is designed to assemble a source program in one pass of the source file"; the Netwide Assembler's (NASM's) default -Ox mode performs "Multipass optimization" to minimize branch offsets, because an x86 conditional jump has an 8-bit and a 32-bit form and pass one must guess before the distance is known.
When the passes disagree. Salomon calls the worst general error a "phase error between passes": pass one makes an assumption that turns out, in pass two, to be wrong, and the whole program has to be reassembled. Simple architectures never produce one. The Intel 80x86, with variable-size instructions, several offset sizes and segmented memory, produces them easily; his example is an instruction that refers to a variable declared in a data segment placed after the code, so that pass one guesses the variable's segment, sizes the instruction accordingly, and pass two finds the guess wrong.
Where it came from. On the EDSAC at Cambridge an order was written as a function letter, a decimal address and F or D for the length. David Wheeler recalled that the letters were made "as mnemonic as possible", that the 17-bit order held five bits of function, a spare bit, ten bits of address and a length bit, and that the orders on the paper tape were "mixed with directives which affected where the orders were placed and how they were modified, and where the program was started": directives are as old as assemblers. On the EDSAC in 1949 the first assembler and the first loader were the same 31-instruction initial orders. Salomon describes the IBM 650's SOAP as perhaps the first assembler to do symbolic assembly in the conventional way, with the extra duty of placing each instruction on the magnetic drum so that it arrived under the read head just as its predecessor finished; IBM's 1401 manual of 1959 to 1962 calls its translator a processor "sometimes called an assembly system".
What it adds. Directives (pseudo-ops) place data, align code and export symbols; macros define new pseudo-instructions; local labels spare the programmer from inventing a name for every loop. Salomon's history credits the IBM Autocoder as apparently the first assembler with macros and UNISAP (1958) as the first with local labels. The assembler can also hide arithmetic: the Zilog manual notes that for DJNZ "The assembler automatically adjusts for the twice incremented PC", PC being the program counter.
The assemblers in use. In the GNU assembler, as, every directive has a name that begins "with a period", and .macro and .endm define macros, the manual's example being a macro sum with the default arguments from=0 and to=5. NASM describes itself as an 80x86 and x86-64 assembler "designed for portability and modularity", released under the 2-clause Berkeley Software Distribution (BSD) licence (copyright 1996 to 2025); nasm -f elf myfile.asm writes an Executable and Linkable Format (ELF) object file, and -l writes a listing with the hex codes beside the source. Microsoft's Macro Assembler (MASM) runs as ML and, for x64, ml64.exe, with a macro language that has looping, arithmetic and string processing. One x86 instruction is spelled differently by different assemblers: in the AT&T syntax that GNU as documents beside Intel's, immediates take $ and registers %, so Intel's push 4 is AT&T's pushl $4, and the operand order is reversed.
After it. The output is usually an object file with relocations, which the linker combines with others (GNU ld "combines a number of object and archive files, relocates their data and ties up symbol references") and a loader places in memory. The System V application binary interface (ABI) defines relocation as "the process of connecting symbolic references with symbolic definitions" and stores each one as an entry with an offset, an information word naming the symbol and the relocation type and, in the Rela form, an explicit addend (Elf32_Rel, Elf32_Rela, Elf64_Rel, Elf64_Rela).
What goes wrong. Most assembler bugs are the programmer's: a branch assembled short that later needs to be long, operands in the wrong order after switching syntax, or a byte copied from a faulty manual. Zilog's own Z80 manual prints OR's codes on its XOR page, while its opcode table gives XOR A correctly as AF; an assembler encodes from tables checked by use, which is why the Learn article verified its hand-computed bytes against GNU's assemblers through Compiler Explorer on 23 November 2026.
Neighbours. Assembly language is the notation, the assembler the translator, the linker and loader what follow it, the disassembler the reverse, and a compiler sits above it, emitting assembly or object code. Brazilian practice says montador or, very often, simply assembler.
Also known as: assembler program, montador, two-pass assembler, one-pass assembler, macro assembler
Sources
- David Salomon, Assemblers and Loaders, Ellis Horwood, 1993: location counter and symbol table (pp. 17-18); the future symbol problem, one-pass and two-pass assemblers, relocatable object files (pp. 19-20); EDSAC Initial Orders, SOAP, UASAP, Autocoder macros and UNISAP local labels (pp. 7-8) (read 2026-11-23)
- IBM, IBM 1401 Symbolic Programming Systems SPS-1 and SPS-2, C24-1480-0, 1959 to 1962: symbolic programming with mnemonic characters; source and object programs; one-for-one translation by a processor 'sometimes called an assembly system' (p. 5) (read 2026-11-23)
- University of Cambridge Computer Laboratory, EDSAC statistics: initial orders of 31 instructions on uniselectors that assembled programs in symbolic form from paper tape; the second release with relocation in 41 words (read 2026-11-23)
- GNU Binutils, Using as, The GNU Assembler: designed to assemble a source program in one pass (read 2026-11-23)
- NASM manual, section 2.1.24: multipass optimization that minimizes branch offsets and signed immediate bytes (read 2026-11-23)
- GNU Binutils, ld manual, Overview: ld combines object and archive files, relocates their data and ties up symbol references (read 2026-11-23)
- Zilog, Z80 Family CPU User Manual UM008005-0205: register set (p. 1); LD r, r' is 01 r r' (p. 81); ADD A, r is 10 000 r (p. 140); DJNZ is 10 followed by e-2, and the assembler adjusts for the incremented PC (p. 253); the XOR page prints OR's codes while the opcode table gives XOR A as AF (pp. 60 and 156) (read 2026-11-23)
- Compiler Explorer, godbolt.org public API with GNU binutils 2.45: the Learn article's three listings assembled to the hand-computed bytes (read 2026-11-23)
- University of Cambridge Computer Laboratory, David Wheeler, The EDSAC Order Code: Explanatory Notes (page last updated August 2004): order letters made 'as mnemonic as possible'; a 17-bit order of five function bits, a spare bit, ten address bits and a length bit; written as function letter, decimal address and F or D; orders mixed with directives that placed and modified them and set the start; the order code of 1949 (read 2026-11-23)
- NASM, The Netwide Assembler, version 3.02, chapter 1: an 80x86 and x86-64 assembler designed for portability and modularity; 2-clause BSD licence, copyright 1996-2025 the NASM Authors (read 2026-11-23)
- NASM manual, section 2.1, NASM Command-Line Syntax: nasm -f <format> <filename> [-o <output>]; nasm -f elf myfile.asm writes a 32-bit ELF object file; -l writes a listing with the hex codes on the left of the source (read 2026-11-23)
- GNU Binutils, Using as, chapter 7, Assembler Directives: every directive name begins with a period and is case-insensitive for most targets (read 2026-11-23)
- GNU Binutils, Using as, section 7.66, .macro: .macro and .endm define macros that generate assembly output; the example macro sum with from=0 and to=5 (read 2026-11-23)
- GNU Binutils, Using as, i386 Syntax (AT&T versus Intel): AT&T immediates take $ and registers %, Intel push 4 is AT&T pushl $4, operand order reversed (read 2026-11-23)
- Microsoft Learn, Microsoft Macro Assembler reference: ML and ML64 command-line options, MASM for x64 (ml64.exe), a macro language with looping, arithmetic and string processing (read 2026-11-23)
- System V Application Binary Interface (gABI), chapter 4, Relocation (SCO copy): relocation is 'the process of connecting symbolic references with symbolic definitions'; Elf32_Rel, Elf32_Rela, Elf64_Rel and Elf64_Rela with r_offset, r_info and r_addend (read 2026-11-23)