assembler

term

programming

Kept 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

← Back to A Back to the glossary