A processor does not read words. It reads numbers from memory, one instruction after another, and each number tells it what to do next: add these two registers, compare this value with zero, jump back twelve bytes. Those numbers are machine code. Assembly language is the same program written so a person can read and write it: one line for each instruction, a short name (the mnemonic) for the operation, names for the registers, labels instead of addresses. The assembler is the program that turns the text back into the numbers. This article starts at the bytes and works outwards, with every opcode and encoding taken from the architecture manuals read on 23 November 2026, and it builds on two pieces already here: byte order, because instruction words are stored in one, and how a WebAssembly tool works, because WebAssembly is a different thing that borrowed the name (the section near the end says exactly how). The companion post, The bytes under everything, tells the stories. After reading, you can encode and decode a handful of instructions by hand on three architectures, compute a branch offset on each, run an assembler's two passes on paper, read a disassembly and match each modern defence to the attack it answers.

In short: machine code is the bit pattern the processor executes; assembly language is a one-for-one textual form of it; the assembler translates the text, resolves labels into addresses with a symbol table, and leaves the rest to a linker and a loader. Three architectures show three philosophies: packs instructions into 1 to 15 bytes with prefixes and a ModR/M byte, and -V use fixed 32-bit words with fields at fixed places. The same eight-line function is 16 bytes on x86-64 and 28 on the other two.

What machine code is

A program in memory is a sequence of bytes, and the processor's instruction set architecture (ISA) is the contract that says what each pattern means. The Intel 64 and IA-32 Architectures Software Developer's Manual (SDM) describes its format as "optional instruction prefixes (in any order), primary opcode bytes (up to three bytes), an addressing-form specifier (if required) consisting of the ModR/M byte and sometimes the SIB (Scale-Index-Base) byte, a displacement (if required), and an immediate data field (if required)" (Intel SDM, Vol. 2A, p. 2-1). An instruction can therefore be one byte long or many; the SDM lists "exceeding the instruction length limit of 15 bytes" among the conditions that raise a general-protection exception (Vol. 3A, p. 7-44).

Arm's 64-bit instruction set takes the opposite route. The Arm Architecture Reference Manual states that "A64 instructions have a fixed length of 32 bits and are always " (Arm ARM, DDI 0487 M.d, section B2.9.2, p. B2-316), and the same manual describes Arm as a reduced instruction set computer (RISC) architecture with a large uniform register file, a load/store design in which data-processing operations work only on registers, and simple addressing modes (p. A1-41). RISC-V, whose specification says it was "originally designed to support computer architecture research and education" (RISC-V Unprivileged ISA, version 20250508, p. 12), also uses 32-bit base instructions, and its manual adds a detail that matters for anyone reading a memory dump: "Instructions are stored in memory as a sequence of 16-bit little-endian parcels, regardless of memory system endianness" (p. 19).

So the first fact of machine code is that it has a byte order. The AArch64 instruction word D2800001, which the next sections build by hand, sits in memory as the four bytes 01 00 80 D2, lowest address first. The Intel SDM states the same rule for the multi-byte values inside an x86 instruction: "All words, doublewords, and quadwords are given with the low-order byte first" (Vol. 2A, p. 3-2), and it calls Intel 64 processors "little endian" machines (Vol. 1, p. 1-6). The byte order article explains why that convention exists; here it is simply the reason a hex dump of code looks backwards.

Assembly language: one line, one instruction

Intel's manual defines the notation it uses throughout, and the definition doubles as the best short description of an assembly language: "When instructions are represented symbolically, a subset of the IA-32 assembly language is used. In this subset, an instruction has the following format: label: mnemonic argument1, argument2, argument3", where "A label is an identifier which is followed by a colon" and "A mnemonic is a reserved name for a class of instruction opcodes which have the same function"; the operands are optional, from zero to three, and "When two operands are present in an arithmetic or logical instruction, the right operand is the source and the left operand is the destination" (Vol. 1, p. 1-7).

IBM explained the same idea to its customers more than sixty years earlier. The manual of the IBM 1401 Symbolic Programming Systems (SPS), copyrighted 1959, 1960 and 1962, says that "Symbolic programming, the use of mnemonic characters to write a program, has been developed to facilitate computer programming", and it shows a payroll fragment in both forms: the symbolic lines ZA CURWTX ACCUM, A TOTMDN ACCUM, ZA GROSS NETPAY, S ACCUM NETPAY, and the machine-language version with numeric addresses. The program written in symbols "(the source program) requires translation into an actual machine-language program (the object program)", done by "a processor", and "In general, the translation is 'one-for-one.' That is, for each instruction written in symbolic form, one machine-language instruction is produced. The processor, sometimes called an assembly system, generally utilizes the machine for which the symbolic program is written" (IBM, C24-1480-0, p. 5).

That one-for-one relation is what separates assembly from the languages a compiler translates. A line of C may become a dozen instructions; a line of assembly becomes one, and a disassembler can usually turn the bytes back into the same line. Two words are worth keeping apart from the start. The assembly language is the notation; the assembler is the program that translates it. In Brazil the two have long shared one word: the magazine called the language itself "Assembler" in 1982, and the Portuguese word for the tool is montador (the Brazilian section below has the sources).

The vocabulary: opcodes, operands, registers, flags and addressing modes

Opcode. The part of an instruction that says which operation to perform. On x86 it is one to three bytes, sometimes completed by three bits of the ModR/M byte; the SDM writes /digit when "the reg field contains the digit that provides an extension to the instruction's opcode" and /r when "the ModR/M byte of the instruction contains a register operand and an r/m operand" (Vol. 2A, p. 3-2). On AArch64 and RISC-V the opcode is a set of fixed bit fields inside the 32-bit word: RISC-V puts a 7-bit opcode in bits 6 to 0 and refines it with funct3 and funct7 (RISC-V, chapter 35, p. 609).

Operands. What the operation works on: registers, an immediate value carried inside the instruction, or a memory location. The encodings say which kind is allowed where.

Registers. The processor's own small, fast storage, named in the instruction. In 64-bit mode an x86-64 processor has 16 general-purpose registers (Intel SDM, Vol. 1, p. 3-12), numbered 0 to 7 in the 3-bit fields of the ModR/M byte (RAX 000, RCX 001, RDX 010, RBX 011, RSP 100, RBP 101, RSI 110, RDI 111; Vol. 2A, Table 2-2, p. 2-6), with an extra bit in the REX prefix reaching the other eight. AArch64 has "31 64-bit general-purpose registers, of which X30 is used as the procedure link register" (Arm ARM, p. A1-44), and register number 31 in an operand field means either the stack pointer or a zero register, depending on the instruction (p. B1-209). RISC-V has 32 x registers, and "Register x0 is hardwired with all bits equal to 0" (RISC-V, p. 23). The has "two sets of six general purpose registers which may be used individually as either 8-bit registers or as 16-bit register pairs" besides its accumulators and flag registers (Zilog, Z80 CPU User Manual UM008005-0205, p. 1), and the has one accumulator and two index registers, X and Y.

Flags. Single bits the processor sets after an operation so that a later instruction can test them. On x86, the zero flag "ZF (bit 6)" is "Set if the result is zero; cleared otherwise" (Intel SDM, Vol. 1, p. 3-16); DEC leaves the carry flag alone and sets "The OF, SF, ZF, AF, and PF flags ... according to the result" (Vol. 2A, p. 3-262). AArch64 has condition flags too, but only some instructions touch them: SUBS "updates the condition flags based on the result", while CBZ "does not affect condition flags" (Arm ARM, pp. C6-2837 and C6-1943). RISC-V has no flags register at all for integer comparisons: its branch instructions compare two registers and jump in a single step, as the listings below show.

Addressing modes. The ways an operand can name its value: in a register, as an immediate, at an address built from a register and a displacement, or relative to the program counter. The SDM's Table 2-2 enumerates every 32-bit form the ModR/M byte can express, from [EAX] to [EDI]+disp32 and the eight plain registers (Vol. 2A, p. 2-6). The 6502 has a "zero page" mode that reaches the first 256 bytes of memory with a one-byte address, and its manual lists the instructions that allow it (LDA, STX, and others; MOS Technology, MCS6500 Microcomputer Family Programming Manual, January 1976, p. 63).

Worked example 1: one x86-64 instruction, encoded by hand

Take add rax, rdi: add the value in RDI to RAX. The SDM's ADD page lists the row REX.W + 01 /r ADD r/m64, r64 Add r64 to r/m64 (Vol. 2A, p. 3-14). Reading it:

  1. REX.W is a prefix. The REX byte has the bit pattern 0100WRXB, and W = 1 means "64 Bit Operand Size" (Vol. 2A, Table 2-4, p. 2-9). With R, X and B at 0 the byte is 0100 1000, that is 48.
  2. 01 is the opcode byte.
  3. /r says a ModR/M byte follows, holding a register operand in its reg field and a register-or-memory operand in its r/m field. The ModR/M byte is mod (2 bits), reg (3 bits), r/m (3 bits) (Figure 2-1, p. 2-1).
  4. The destination is the r/m64 operand, RAX, so r/m = 000; the source is the r64 operand, RDI, so reg = 111; both are registers, so mod = 11.
  5. 11 111 000 in binary is F8. Table 2-2 confirms it: in the row for EAX/AX (mod 11, r/m 000) and the column for EDI (reg 111) the value is F8.

The instruction is three bytes: 48 01 F8.

Now xor eax, eax, the usual way to clear a register. The row is 31 /r XOR r/m32, r32 (Vol. 2D, p. 6-40). Both operands are EAX: mod 11, reg 000, r/m 000, so the ModR/M byte is C0 and the instruction is 31 C0. It names the 32-bit register, yet it clears all 64 bits, because in 64-bit mode "32-bit operands generate a 32-bit result, zero-extended to a 64-bit result in the destination general-purpose register" (Vol. 1, p. 3-13). That one sentence of the manual is why compilers emit the two-byte form instead of a three-byte one with REX.W.

Worked example 2: the same function on three architectures

The function adds the numbers from n down to 1 and returns the sum, a loop small enough to encode by hand and large enough to need a label, a conditional branch, a register that accumulates and a return. In C:

unsigned long sum(unsigned long n) {   // n arrives in the first argument register
    unsigned long s = 0;               // the running total
    while (n != 0) { s += n; n--; }    // add n, count down, stop at zero
    return s;                          // the result leaves in the return register
}

Where n arrives and where the result must be left is not decided by the processor but by the calling convention of the platform's application binary interface (ABI). On Linux and other System V systems, integer arguments go in "the next available register of the sequence RDI, RSI, RDX, RCX, r8 and r9" and integer results come back in RAX (x86-64 psABI, low-level-sys-info.tex); Microsoft's x64 convention passes the first four integer arguments in "RCX, RDX, R8, and R9" instead (Microsoft Learn, x64 calling convention). The same processor, two conventions: the instructions below follow System V. On AArch64, "The first eight registers, r0-r7, are used to pass argument values into a subroutine and to return result values from a function" (Arm, Procedure Call Standard for the Arm 64-bit Architecture (AAPCS64)); on RISC-V, x10 to x17 are the argument registers a0 to a7, "the first two of which are also used to return values", and x1 is ra, the return address (RISC-V ABIs Specification, calling convention).

x86-64

; x86-64, Intel syntax, System V: n in RDI, result in RAX
sum:                    ; the function's label
        xor eax, eax    ; 31 C0     clear RAX (the 32-bit result is zero-extended)
        test rdi, rdi   ; 48 85 FF  AND RDI with itself, set ZF if n is zero
        jz done         ; 74 08     n == 0: skip the loop
loop:                   ; the loop's label
        add rax, rdi    ; 48 01 F8  s += n
        dec rdi         ; 48 FF CF  n -= 1, sets ZF when n reaches zero
        jnz loop        ; 75 F8     not zero yet: go back
done:                   ; the exit label
        ret             ; C3        return to the caller, result in RAX

Every byte comes from a row of the SDM: 31 /r XOR (Vol. 2D, p. 6-40); REX.W + 85 /r TEST r/m64, r64, which does "AND r64 with r/m64; set SF, ZF, PF according to result" (Vol. 2B, p. 4-721); 74 cb JZ rel8 and 75 cb JNZ rel8 (Vol. 2A, p. 3-499); REX.W + 01 /r ADD (p. 3-14); REX.W + FF /1 DEC r/m64 (p. 3-262), where /1 puts 001 in the reg field, so with RDI in r/m the ModR/M byte is 11 001 111 = CF; and C3 RET, "Near return to calling procedure" (Vol. 2B, p. 4-569). The total is 16 bytes.

The branch offsets, by hand. The SDM says that for a conditional jump "the target instruction is specified with a relative offset (a signed offset relative to the current value of the instruction pointer in the EIP register)", that it "is generally specified as a label in assembly code, but at the machine code level, it is encoded as a signed, 8-bit or 32-bit immediate value, which is added to the instruction pointer" (Vol. 2A, p. 3-502). When the jump executes, the instruction pointer already points past it. Lay the bytes out: xor at 0x00 (2 bytes), test at 0x02 (3), jz at 0x05 (2), loop: at 0x07, add at 0x07 (3), dec at 0x0A (3), jnz at 0x0D (2), done: at 0x0F, ret at 0x0F.

  • jz done: the next instruction starts at 0x07 and the target is 0x0F, so the offset is 0x0F − 0x07 = +8, encoded 08.
  • jnz loop: the next instruction starts at 0x0F and the target is 0x07, so the offset is 0x07 − 0x0F = −8. In 8-bit two's complement, −8 is 256 − 8 = 248 = F8.

AArch64

// AArch64, AAPCS64: n in X0, result in X0
sum:                        // the function's label
        mov x1, #0          // D2800001  MOVZ: X1 = 0, the running total
        cbz x0, done        // B4000080  n == 0: skip the loop (flags untouched)
loop:                       // the loop's label
        add x1, x1, x0      // 8B000021  s += n
        subs x0, x0, #1     // F1000400  n -= 1 and set the flags
        b.ne loop           // 54FFFFC1  not equal to zero: go back
done:                       // the exit label
        mov x0, x1          // AA0103E0  ORR X0, XZR, X1: copy s to the result
        ret                 // D65F03C0  branch to the address in X30

The Arm ARM prints each encoding as a diagram of bit fields, and every word above is those fields filled in:

  • MOVZ (C6.2.285, p. C6-2453): sf 10 100101 hw imm16 Rd. With sf = 1 (64-bit), hw = 00, imm16 = 0 and Rd = 1: 1 10 100101 00 0000000000000000 00001 = D2800001. The assembler accepts mov x1, #0 because MOVZ "is used by the alias MOV (wide immediate)".
  • CBZ (C6.2.78, p. C6-1943): sf 011010 0 imm19 Rt, with the label's offset "encoded as imm19 times 4" from the address of the CBZ itself.
  • ADD, shifted register (C6.2.6, p. C6-1815): sf 0 0 01011 shift 0 Rm imm6 Rn Rd; for add x1, x1, x0, Rm = 0, Rn = 1, Rd = 1: 8B000021.
  • SUBS, immediate (C6.2.464, p. C6-2837): sf 1 1 100010 sh imm12 Rn Rd; imm12 = 1, Rn = Rd = 0: F1000400.
  • B.cond (C6.2.34, p. C6-1862): 01010100 imm19 0 cond, where NE is 0001 in the table of conditions on the same page.
  • MOV, register (C6.2.282, p. C6-2447), an alias of ORR with the zero register as the first source: AA0103E0 for mov x0, x1.
  • RET (C6.2.338, p. C6-2562): 1101011001011111000000 Rn 00000, where Rn "Defaults to X30 if absent": D65F03C0.

The branch offsets, by hand. Every instruction is 4 bytes: mov at 0x00, cbz at 0x04, loop: and add at 0x08, subs at 0x0C, b.ne at 0x10, done: and mov at 0x14, ret at 0x18. AArch64 measures from the branch instruction itself, not from the next one.

  • cbz x0, done: 0x14 − 0x04 = +16 bytes = 4 words, so imm19 = 4. Placed at bits 23 to 5: 4 << 5 = 0x80. The word is B4000000 | 0x80 | Rt=0 = B4000080.
  • b.ne loop: 0x08 − 0x10 = −8 bytes = −2 words. In 19-bit two's complement, −2 is 0x7FFFE; shifted left by 5 it is 0xFFFFC0. With NE = 1: 54000000 | FFFFC0 | 1 = 54FFFFC1.

The total is 7 words, 28 bytes, stored little-endian: the first word, D2800001, is the bytes 01 00 80 D2 in memory.

RISC-V (RV64I)

# RISC-V RV64I, base encodings: n in a0 (x10), result in a0
sum:                        # the function's label
        li a1, 0            # 00000593  ADDI a1, zero, 0: the running total
        beqz a0, done       # 00050863  BEQ a0, zero: n == 0, skip the loop
loop:                       # the loop's label
        add a1, a1, a0      # 00A585B3  s += n
        addi a0, a0, -1     # FFF50513  n -= 1
        bnez a0, loop       # FE051CE3  BNE a0, zero: not zero yet, go back
done:                       # the exit label
        mv a0, a1           # 00058513  ADDI a0, a1, 0: copy s to the result
        ret                 # 00008067  JALR zero, 0(ra): return

Four of the eight lines are pseudo-instructions, shorthand that the assembler expands into a real instruction. The RISC-V Assembly Programmer's Manual lists them: mv rd, rs is addi rd, rs, 0; beqz rs, offset is beq rs, x0, offset; bnez is bne rs, x0, offset; ret is jalr x0, 0(x1); and nop is addi x0, x0, 0 (RISC-V Assembly Programmer's Manual). The ISA manual confirms two of them from its side: "ADDI rd, rs1, 0 is used to implement the MV rd, rs1 assembler pseudoinstruction" and "NOP is encoded as ADDI x0, x0, 0" (RISC-V, pp. 28 and 30).

The encodings are the rows of the RV32I listing (chapter 35, p. 609), which RV64I uses at 64 bits: imm[11:0] rs1 000 rd 0010011 ADDI, 0000000 rs2 rs1 000 rd 0110011 ADD, imm[12|10:5] rs2 rs1 000 imm[4:1|11] 1100011 BEQ (BNE has 001 in funct3), imm[11:0] rs1 000 rd 1100111 JALR.

The branch offsets, by hand. RISC-V also measures from the branch itself, in bytes, and stores a 13-bit signed offset whose bit 0 is always zero, scattered across the word so that the register fields stay in the same place in every format. Take bnez a0, loop at 0x10 with the target at 0x08: the offset is −8.

  1. −8 in 13 bits is 1 1111 1111 1000: imm[12] = 1, imm[11] = 1, imm[10:5] = 111111, imm[4:1] = 1100.
  2. Place the pieces: bit 31 = imm[12] = 1; bits 30 to 25 = imm[10:5] = 111111; bits 24 to 20 = rs2 = x0 = 00000; bits 19 to 15 = rs1 = x10 = 01010; bits 14 to 12 = funct3 = 001 (BNE); bits 11 to 8 = imm[4:1] = 1100; bit 7 = imm[11] = 1; bits 6 to 0 = 1100011.
  3. Written out: 1111 1110 0000 0101 0001 1100 1110 0011 = FE051CE3.

The forward branch beqz a0, done at 0x04 jumps +16 to 0x14: imm[4:1] = 1000 and everything else zero, giving 00050863. The total is again 7 words, 28 bytes.

The three side by side, and a check

Stepx86-64 bytesAArch64 wordRISC-V word
total = 031 C0D280000100000593
n == 0? skip48 85 FF + 74 08B400008000050863
total += n48 01 F88B00002100A585B3
n -= 148 FF CFF1000400FFF50513
loop while n != 075 F854FFFFC1FE051CE3
result register(already in RAX)AA0103E000058513
returnC3D65F03C000008067
size16 bytes, 7 instructions28 bytes, 7 instructions28 bytes, 7 instructions

Hand assembly is easy to get wrong, so the three listings were also assembled on 23 November 2026 by the (GNU's Not , as the GNU Project expands it) binutils 2.45 for each architecture, through the public interface of Compiler Explorer. Every byte matched. The disassembler printed je for jz and jne for jnz (two mnemonics for the same opcode, as the SDM's Jcc table shows: 74 cb is listed as both JE and JZ), printed mov x1, #0x0 for the MOVZ, and, for RISC-V, showed a relocation of type R_RISCV_BRANCH next to each branch: the assembler had left a note for the linker even though the target was in the same file. The linker section below explains why that note exists.

CISC and RISC, seen through the bytes

The labels complex instruction set computer (CISC) and RISC are usually argued about in terms of speed. The bytes above show the differences that are not in dispute.

  • Length. x86-64 instructions here are 1, 2 or 3 bytes, and the format allows up to 15; AArch64 and RISC-V instructions are all 4 bytes. A decoder for the fixed formats knows where every instruction starts; an x86 decoder knows only by decoding.
  • Where operands may be. Arm's manual lists the load/store rule as a defining RISC feature: data-processing operations "only operate on register contents, not directly on memory contents" (p. A1-41). x86's ADD has forms whose destination is a memory location (the r/m in ADD r/m64, r64).
  • How a decision is made. x86 sets flags in one instruction and tests them in the next (dec, then jnz); AArch64 can do the same (subs, then b.ne) or test a register directly (cbz); RISC-V always compares registers inside the branch.
  • Density. For this function x86-64 needs 16 bytes and the other two 28. That is one function, not a benchmark, and the RISC-V specification itself reports the opposite result at scale for its compressed extension: "Typically, 50%-60% of the RISC-V instructions in a program can be replaced with RVC instructions, resulting in a 25%-30% code-size reduction" (p. 160), and the same chapter recalls that the compressed RISC instruction sets of the 1980s and later "reduced code size relative to their starting points by about 25-30%, yielding code that was significantly smaller than 80x86", which "surprised some, as their intuition was that the variable-length CISC ISA should be smaller" (p. 161). Compilers for 64-bit RISC-V commonly use that compressed extension; the listing above was assembled without it on purpose, to show the base encodings.

The 8-bit home computers: Z80 and 6502

The same loop shrinks on the 8-bit processors that put assembly in front of a generation, and their manuals make the encodings visible in a way the 64-bit manuals no longer do. The two processors have glossary entries, Zilog Z80 and MOS 6502, and the machines built on them are in Microcomputers by category and generation.

Z80

; Z80: n in register B (1 to 255), result in A
sum:            ; the routine's label
        XOR A   ; AF      A = 0 (the accumulator XORed with itself)
loop:           ; the loop's label
        ADD A,B ; 80      A = A + B
        DJNZ loop ; 10 FD decrement B, jump back if B is not zero
        RET     ; C9      return to the caller

Five bytes. The Zilog manual gives ADD A, r as 10 000 r with B = 000 (p. 140), so ADD A,B is 1000 0000 = 80. RET is C9 (p. 260). DJNZ is the Z80's own loop instruction: "The B register is decremented, and if a non zero value remains, the value of the displacement e is added to the Program Counter (PC)", the op code is 00010000 followed by e-2, and "The jump is measured from the address of the instruction Op Code and has a range of -126 to +129 bytes. The assembler automatically adjusts for the twice incremented PC" (p. 253). Here DJNZ is at address 2 and loop at address 1, so e = −1 and the byte stored is e − 2 = −3 = FD. That sentence about the assembler adjusting for the program counter is the whole reason hand assemblers of the 1980s kept a pencil next to the opcode table.

The manual also shows why the opcode table, not a single page, is the reference to trust. Its XOR s page (p. 156) prints the encodings of the OR instruction, labelled "OR r", "OR n", "OR (HL)", with OR's bit pattern. The 8-bit arithmetic and logic table of the same manual gives the XOR row as AF A8 A9 AA AB AC AD AE for the registers A, B, C, D, E, H, L and (HL) (p. 60), and the manual's own example program uses XOR A ;CLEAR ACCUMULATOR (p. 35). XOR A is therefore AF. Manuals have errata; two pages of the same manual that agree with each other beat one that does not.

The Z80 was designed to run the Intel 8080's programs, and the two manuals show the same byte under different names. Intel's 8080/8085 Assembly Language Programming Manual encodes MOV r1,r2 as 01DDDSSS, with B = 000 and the accumulator = 111 (Intel, 9800301C, November 1978, pp. A-1 to A-3), so Intel's MOV A,B is 01 111 000 = 78. Zilog encodes LD r, r' as 01 r r' with A = 111 and B = 000 (Zilog, p. 81), so Zilog's LD A,B is also 78. One byte, two mnemonics, two companies. The same holds for RET (1100 1001 = C9 in both manuals) and for Intel's XRA r (10101SSS), whose XRA A is the same AF as Zilog's XOR A.

The Z80's production story ended while this article's sources were being written: The Register reported on 22 April 2024 that Zilog had issued an end-of-life notice blaming its wafer foundry, that orders would be accepted until 14 June 2024, that the chip had debuted in 1976 with about 8,500 transistors, and that Zilog still appeared to make the eZ80 (The Register, 22 April 2024).

6502

; 6502: n in X (1 to 255), result in A, $FB is a free zero-page byte
sum:            ; the routine's label
        LDA #$00 ; A9 00  A = 0
loop:           ; the loop's label
        STX $FB  ; 86 FB  copy X to memory: the 6502 cannot add X to A directly
        CLC      ; 18     clear the carry, because ADC always adds it
        ADC $FB  ; 65 FB  A = A + memory[$FB] + carry
        DEX      ; CA     X = X - 1, sets the zero flag when X reaches 0
        BNE loop ; D0 F8  not zero: go back
        RTS      ; 60     return to the caller

Eleven bytes, and the extra lines are the lesson: the 6502 has an accumulator architecture, so the only addition is "Add memory to accumulator with carry", and the loop counter has to pass through memory to be added. The opcodes come from the instruction summary of the MOS programming manual: LDA # immediate A9 (p. B-17), STX zero page 86 (p. B-26), CLC 18 (p. B-9), ADC zero page 65 (p. B-3), DEX CA (p. B-13), BNE D0 (p. B-7) and RTS 60 (p. B-23). A branch "is limited to +127 or -128 from the current program counter location" (p. 42), and the manual's worked example shows a branch at 0100 whose offset is added to 0102, the address after the two-byte branch (p. 66). Here BNE sits at 8 and 9, the next instruction is at 10 (0x0A), and loop is at 2: 2 − 10 = −8 = F8.

Hand assembly

On these machines many owners assembled by hand: write the mnemonics, look up each opcode in the manual's table, count bytes to place each label, compute each relative branch as above, then type the numbers into memory. The Brazilian section below shows what that looked like in São Paulo in 1983. The rule of thumb that falls out of the three branch conventions above is worth memorizing: x86 and the 6502 count from the end of the branch, AArch64 and RISC-V from its start, and the Z80's DJNZ from its start but with the two bytes subtracted by the assembler.

How an assembler works

David Salomon's Assemblers and Loaders (Ellis Horwood, 1993, now published free by its author) describes the core in a few pages. The assembler keeps a location counter (LC), "a variable, maintained by the assembler, that contains the address into which the current instruction will eventually be loaded", and a symbol table, "an internal, dynamic table that is generated, maintained, and used by the assembler", with "fields for the name, value, and type of the symbol". A label defines a symbol with the current value of the LC; an operand uses it (Salomon, pp. 17 and 18).

The one difficulty is the label used before it is defined, which Salomon calls "the future symbol problem or the problem of unresolved references". It has two classic solutions. A two-pass assembler "performs two passes over the source file. In the first pass it reads the entire source file, looking only for label definitions. All labels are collected, assigned values, and placed in the symbol table in this pass. No instructions are assembled... In the second pass, the instructions are again read and are assembled, using the symbol table" (p. 20). A one-pass assembler reads the source once and patches the gaps later (p. 19).

Worked example 3: the two passes on paper

Run the x86-64 listing through both passes.

Pass 1. The LC starts at 0. The assembler does not encode anything, but it must know each instruction's size to advance the LC, which on x86 means looking at the mnemonic and the operands (Salomon notes that "on most modern microprocessors ... the size is determined by the OpCode and the specific operands used", p. 20).

LineSizeLC beforeSymbol table after
sum:00x00sum = 0x00
xor eax, eax20x00
test rdi, rdi30x02
jz done2 (short form assumed)0x05
loop:00x07loop = 0x07
add rax, rdi30x07
dec rdi30x0A
jnz loop20x0D
done:00x0Fdone = 0x0F
ret10x0F

Pass 2. The assembler reads the lines again, encodes each instruction with the rows of the SDM, and when it meets jz done looks up done (0x0F), subtracts the address of the next instruction (0x07) and emits 74 08; for jnz loop it emits 75 F8. The output is the 16 bytes of the listing.

There is a catch hidden in "short form assumed". JZ has an 8-bit form (74 cb) and a 32-bit form (0F 84 cd, same Jcc table), and pass 1 had to guess the size before it knew the distance. If a target turns out to be more than 127 bytes away, every later address moves. Assemblers handle it in different ways. The Netwide Assembler (NASM) documents an option that iterates: in its default -Ox mode it performs "Multipass optimization. Minimize branch offsets and signed immediate bytes", and it can print "the number of passes actually executed" (NASM manual, section 2.1.24). The GNU assembler says that "Unlike older assemblers, as is designed to assemble a source program in one pass of the source file" (GNU as manual, The GNU Assembler).

Directives and macros

Not every line becomes an instruction. Directives (GNU calls them "pseudo-ops") tell the assembler where to put things, how to align them, which symbols to export, which bytes of data to emit (GNU as, Assembler Directives). IBM's II (Symbolic Optimal Assembly Program) for the 650 already had them in 1957: BLR (Block Reservation) marked drum addresses as unavailable, BLA (Block Availability) released them, RBR reserved relocatable blocks (Wikipedia, Symbolic Optimal Assembly Program). Macros let a programmer define a new pseudo-instruction that expands into several real ones; GNU's .macro directive "allows you to define macros that generate assembly output" (GNU as, .macro). Local labels avoid inventing a unique name for every small loop: in the RISC-V assembler a numeric label such as 1: is referenced as 1b (backwards) or 1f (forwards) (RISC-V Assembly Programmer's Manual, Labels).

The linker and the loader

An assembler rarely produces the final program. It produces an object file whose addresses start at zero and whose references to other files are still open, and it lists the places that need fixing as relocations. The GNU linker's manual says that "ld combines a number of object and archive files, relocates their data and ties up symbol references" (GNU ld manual, Overview). Salomon puts the distinction between the two kinds of output: a one-pass assembler of the classic kind "does not generate a relocatable object file but rather loads the object code ... directly into memory", while "the most important feature of the two-pass assembler is that it generates a relocatable object file, that is later loaded into memory by a loader" (p. 19). The R_RISCV_BRANCH entries in the cross-check above are those relocations: GNU's RISC-V assembler leaves them even for branches inside one file, so that the linker can still move code (the toolchain's "relaxation" of RISC-V code, which this article does not cover further).

The loader's job is the last step: put the program in memory at its real addresses, apply what remains of the relocations, and jump to it. On the Cambridge EDSAC (Electronic Delay Storage Automatic Calculator), as the history below shows, the first assembler and the first loader were the same 31 words.

A short history

1947: Kathleen Booth's notation

Kathleen Britten, who married Andrew Booth in 1950, spent the summer of 1947 at the Institute for Advanced Study in Princeton with him, and they co-authored two reports there: General considerations in the design of an all-purpose electronic digital computer and Coding for the A.R.C. (MacTutor, Kathleen Booth). The second report opens by explaining that "we have made the code of A.R.C. identical with that projected for the electronic machine" (MacTutor, extracts from the Booth and Britten reports); MacTutor's bracketed gloss expands A.R.C. as Automatic Relay Computer in one place and Automatic relay calculator in another. MacTutor calls her "the first to create assembly language". The Register's obituary of 29 October 2022 says that she "wrote all the software for the ARC2 and SEC machines, in the process inventing what she called Contracted Notation. This language, through evolution and contributions by others, is today known as assembly language" (The Register, 29 October 2022).

What the notation looked like is harder to establish from a copy anyone can read. The Institute's own repository holds a scan of Coding for A.R.C., but it refused automated access on 23 November 2026, and the scan of General considerations hosted in the Association for Computational Linguistics' anthology is an image without text. The General considerations report gives the order to clear the arithmetic register and transfer a value from memory into it as the binary pattern 10011, and writes the same order in contracted notation as M -> cR Officially attributed Hackaday (Steven Dufresne, 21 August 2018), reading the 1947 General considerations report (Hackaday, 21 August 2018); the same author doubted that the contracted notation in that report was the assembly language itself, and placed the assembly language in Coding for A.R.C. instead. Booth is credited with creating the first assembly language Officially attributed MacTutor and The Register, and the credit is not exclusive: the next machine has its own claim.

1949: the EDSAC's initial orders

At the University of Cambridge's Mathematical Laboratory, the EDSAC (the Institute of Electrical and Electronics Engineers () Computer Society's profile of David Wheeler expands its last word as Computer, not Calculator) ran its first logged program on 6 May 1949 (Cambridge Computer Laboratory, EDSAC chronology). Its orders were written as a function letter, a decimal address and F or D for short or long numbers, for example A D, S 4D and T D; David Wheeler recalled that "we did try to make it as mnemonic as possible", and that "J was not used for jump because the operation was then thought of as 'transfer of control'" (Wheeler, EDSAC order code notes). The program that read those letters from paper tape and placed the binary orders in memory was the "initial orders": according to the laboratory's statistics page they "consisted of 31 instructions which were hard-wired on uniselectors, a mechanical read-only memory. These instructions assembled programs in symbolic form from paper tape into the main memory and set them running" (EDSAC statistics), and the laboratory's poster credits Wheeler with writing them in May 1949 (EDSAC poster).

The second version, Initial Orders 2, added relocation ("coordination") for subroutines in 41 words; the statistics page says it "was installed in August 1949", while the chronology says it was "Wired by Oct 22 49". The two pages of the same laboratory disagree, and both are reported here. Wheeler's initial orders were "the first 'assembly language'", and the IEEE gave him its 1985 Computer Pioneer Award "for assembly language programming" Officially attributed IEEE Computer Society, David Wheeler profile (IEEE Computer Society, David Wheeler). Salomon adds that Maurice Wilkes was "the first to propose the use of labels (which he called floating addresses), the first to use an early form of macros (which he called synthetic orders), and the first to develop a subroutine library" (Salomon, p. 7). Wilkes, Wheeler and Stanley Gill described all of it in The Preparation of Programs for an Electronic Digital Computer (1951), the book that the IEEE profile calls the first book for programmers; the term "assembler" is generally attributed to that book, which used it for a program that combined several sections into a single program Officially attributed Wikipedia, Assembly language (Wikipedia, Assembly language).

The 1950s: SOAP, Autocoder and their kin

The IBM 650 kept its program on a rotating magnetic drum, and each instruction carried the address of the next. SOAP placed each instruction so that it would arrive under the read head just as the previous one finished; Salomon calls it "perhaps the first assembler" to do symbolic assembly "in the conventional way" (p. 7). Its name is spelt three ways in the sources: Symbolic Optimal Assembly Program (Wikipedia), Symbolic Optimum Assembly Program (Donald Knuth's manual) and Symbolic Optimizer and Assembly Program (Salomon). Knuth, then at the Case Institute of Technology in Cleveland, wrote in February 1958 that his CASE SOAP III "is a modified version of SOAP II which was originally written by Stan Poley of the Applied Science Division of IBM Service Bureau Corp." (Knuth, CASE SOAP III, February 1958, Computer History Museum copy); Wikipedia places Poley at the IBM Thomas J. Watson Research Center instead. In August 1959 Knuth's SuperSoap promised assembly "considerably faster than ever before", at 140 to 150 cards per minute (Knuth, SuperSoap, August 1959).

Salomon's short history continues: the IBM 704's first assembler, UASAP-1 (United Aircraft Symbolic Assembly Program), was written by Roy Nutt of United Aircraft in 1956 and "did practically nothing but one-to-one translation"; SHARE, the IBM users' organization, distributed a later version; the IBM Autocoder for the 702 and 705, developed by R. Goldfinger, "was apparently the first to use macros"; UNISAP for the UNIVAC I and II (1958, M. E. Conway) "was the first one to use local labels"; and by the late 1950s IBM's SCAT (Symbolic Coder And Translator) for the 709 and 7090 "had all the features of modern assemblers" (Salomon, pp. 7 and 8). The 1401's SPS, quoted at the top of this article, belongs to the same years. The cards these assemblers read, and how a program deck was laid out, are in Punched cards: from the Jacquard loom to the 80-column card.

The 1970s and 1980s: microprocessors and hand assembly

Intel's 8080 manual, MOS Technology's 6500 manuals and Zilog's Z80 manual, all quoted above, were the textbooks of the next wave. The microprocessors were cheap, the assemblers were not always available, and the manuals printed every opcode in tables designed to be read by a person. Magazines printed programs as columns of numbers to be typed in.

The demoscene

The craft of fitting effects into small, fast code outlived the 8-bit machines in the demoscene, whose biggest gathering is called, fittingly, Assembly. The first Assembly was held from 24 to 26 July 1992 in Kauniainen, Finland, organized by the Amiga groups Complex and Rebels and the PC group Future Crew, and it now runs every summer at the Messukeskus in Helsinki (Wikipedia, Assembly (demoparty)). In 2020 Finland's Ministry of Education and Culture added the demoscene to its national inventory of living heritage "as the first branch of digital culture ever" (Aalto University, 7 May 2020); the Art of Coding initiative dates the Finnish decision to April 2020, Aalto's news item to May, and the initiative lists Finland, Germany, Poland, Switzerland, the Netherlands, Sweden, France and Norway as having recognized the demoscene as national cultural heritage by April 2026 (Art of Coding).

The Brazilian angle

Brazil's first home-grown computer was not programmed in anyone else's assembly language. The Patinho Feio, built at the Escola Politécnica of the (USP) as the final project of a graduate course in computer architecture proposed by Glen Langdon Jr., was inaugurated on 24 July 1972, according to the magazine of the Fundação de Amparo à Pesquisa do Estado de São Paulo () (Revista Pesquisa FAPESP, December 2022). It had about 450 integrated circuits and a core memory of 4,096 words of 8 bits, and Antônio Massola, then a master's student, was one of those who wrote its software (Revista Pesquisa FAPESP, June 2002). The machine led to the G-10, built by USP and the Pontifícia Universidade Católica do Rio de Janeiro (PUC-Rio) for the Brazilian Navy, which in turn was the prototype for Cobra's MC 500, the country's first commercial computer (FAPESP, 2022; Jornal da USP, 23 September 2022).

A decade later, under the market reserve, the Brazilian micros of the early 1980s were largely clones of foreign designs, and the magazine Micro Sistemas taught their owners the assembly language. In August 1982 it explained that "many programs are written in machine language or in Assembler, a language that uses symbols or mnemonic codes representing the microcomputer's instruction set" (em linguagem de máquina ou em Assembler: the magazine called the language itself "Assembler"; our translation, as for every quotation from Portuguese in this article), and listed the reasons to use it: a machine without a high-level language, programs that must be small and fast, access to the bits of a word, and "for pure 'fun'", because it gives a fine view of how the microcomputer works (Micro Sistemas no. 11, August 1982, pp. 45 and 46). In February 1983 the magazine opened a twelve-lesson Curso de Assembler (Assembler course) "taking the Z80 central processing unit as its basis", for situations in which a high-level language does not let you handle the microprocessor and its peripherals directly (Micro Sistemas no. 17, February 1983).

The June 1983 issue shows hand assembly in practice on the TK82-C, the Microdigital clone of the Sinclair ZX81 that also appears in the 1984 São Paulo post. A reader who had bought one wrote that his micro's processor "is a Z80, and it does everything", and published a program-renumbering routine as a list of decimal numbers: a loop POKEd them into memory starting at address 16514, inside a REM line at the top of the program, and USR 16514 ran them. He explained what the reader was seeing: the numbers "are nothing more than Z80 machine-code instructions", and when the program is listed the REM line looks full of odd characters because BASIC "reads our list of numbers not as Z80 commands but as characters to be printed" (Micro Sistemas no. 21, June 1983, pp. 66 and 67). That is machine code in its rawest form: the same bytes are instructions to the processor and characters to the BASIC listing, depending only on who reads them.

The law that governs software in Brazil, Lei 9.609 of 1998, does not mention disassembly or decompilation. Its article 6 lists what does not offend the rights of a program's owner, among them "the partial citation of the program, for teaching purposes, provided that the program and the holder of the respective rights are identified" and the integration of a program into an application or operating system when technically indispensable to the user, "provided that it is for the exclusive use of whoever carried it out" (Lei 9.609/1998, art. 6). How far reverse engineering for interoperability is covered is a question for a lawyer, not for this article.

Intel syntax and AT&T syntax

There is no single assembly language for a processor; there is one notation per assembler. On x86 two families dominate, and the GNU assembler manual summarizes the differences: "AT&T immediate operands are preceded by '$'"; "AT&T register operands are preceded by '%'"; "AT&T and Intel syntax use the opposite order for source and destination operands. Intel 'add eax, 4' is 'addl $4, %eax'"; and in AT&T syntax "the size of memory operands is determined from the last character of the instruction mnemonic", with the suffixes b, w, l and q for 8, 16, 32 and 64 bits, where Intel syntax writes byte ptr, word ptr and so on (GNU as, AT&T Syntax versus Intel Syntax). The manual adds that the .intel_syntax directive selects Intel mode and that the source-then-destination order "is maintained for compatibility with previous Unix assemblers".

The instruction encoded by hand above, in both:

add rax, rdi        ; Intel syntax: destination first, no prefixes, size from the register names
addq %rdi, %rax     # AT&T syntax: source first, % before registers, q for a 64-bit operation

Both assemble to 48 01 F8. The Intel SDM writes Intel syntax; the GNU Compiler Collection (GCC) and the GNU tools default to AT&T; NASM, "an 80x86 and x86-64 assembler designed for portability and modularity" (NASM manual, chapter 1), and the Microsoft Macro Assembler (MASM) use Intel-style syntax (Microsoft Learn, Microsoft Macro Assembler reference). Go has a third dialect: its assembler "is based on the input style of the Plan 9 assemblers", and "The most important thing to know about Go's assembler is that it is not a direct representation of the underlying machine" (The Go Programming Language, A Quick Guide to Go's Assembler).

Where assembly still lives

Almost nobody writes whole applications in assembly any more. It survives where a compiler cannot reach, or cannot be trusted to.

  • Boot code. A processor starts in a state that no high-level language expects. 's arch/x86/boot/header.S begins with the warning "We're in real mode using 64k segments", keeps BOOTSEG = 0x07C0, the "original address of boot-sector", and and in the part of its header that comes "from the old boot sector" still carries the field boot_flag: .word 0xAA55, followed by the comment "offset 512, entry point" (Linux, arch/x86/boot/header.S). The boot sector is also where the boot sector viruses of the 1980s, such as Ping-Pong, made their home. How a machine finds its first code over the network is told in the article on network boot.
  • Cryptographic primitives. 's -NI module is written in that generates assembly through the project's perlasm translator (x86_64-xlate.pl); it was "Written by Andy Polyakov ... initially for use in the OpenSSL project" and dual-licensed under the OpenSSL and CRYPTOGAMS licences; its header discusses performance in cycles per byte for each mode (OpenSSL, crypto/aes/asm/aesni-x86_64.pl).
  • Multimedia. FFmpeg's assembly lessons explain that most of its assembly is SIMD (Single Instruction Multiple Data) code and say why the project writes it by hand: it is "very common to get a 10x or more speed improvement from writing assembly code", and intrinsics "are typically around 10-15% slower" Officially attributed FFmpeg, asm-lessons, lesson 1 (FFmpeg, asm-lessons, lesson 1).
  • Inside high-level languages. Rust supports inline assembly "via the asm!, naked_asm!, and global_asm! macros" (The Rust Reference, Inline assembly); Go's assembler guide points to the standard library's runtime and math/big packages for examples of its assembly.
  • Reading what the compiler did. Matt Godbolt open-sourced "a little tool called GCC Explorer" on 22 May 2012, written in a week of spare time at his then-employer DRW; it became Compiler Explorer on 30 April 2014, and ten years after its start it was performing "around 3,000,000 compilations a week" (Godbolt, Happy 10th Birthday Compiler Explorer). Its README describes it as "an interactive compiler exploration website" for more than 30 languages (Compiler Explorer README).
  • History kept running. The source code of the Apollo 11 guidance computer for the command module (Comanche055) and the lunar module (Luminary099), digitized by the Virtual AGC project and the MIT Museum, is public, with lines such as "Assemble revision 055 of AGC program Comanche by NASA" (Apollo-11 repository). The software effort is told in the glossary entry on Margaret Hamilton.

The TIOBE index, which describes itself as "an indicator of the popularity of programming languages" with ratings based on "the number of skilled engineers world-wide, courses and third party vendors" as counted through popular web sites and search engines, placed "Assembly language" 17th in November 2026, with 0.92% (TIOBE Index, November 2026).

WebAssembly is a different thing

WebAssembly borrowed the word and little else. Its home page defines it as "a binary instruction format for a stack-based virtual machine", "designed as a portable compilation target for programming languages" (webassembly.org). It is not the instruction set of any physical processor; a browser or runtime compiles or interprets it. The specification's binary format begins every module with a 4-byte magic number that spells the name, the bytes 0x00 0x61 0x73 0x6D, the string \0asm (WebAssembly Core Specification, Modules). WebAssembly has a text format for people, as assembly does, but the bytes it describes run on a virtual machine with its own sandbox, which is what How a WebAssembly tool works examines.

The security thread

Disassembly and reverse engineering

A disassembler runs the assembler backwards: bytes in, mnemonics out. GNU's objdump does it from the command line (GNU Binutils, objdump); , "a software reverse engineering (SRE) framework created and maintained by the National Security Agency Research Directorate", lists among its capabilities "disassembly, assembly, decompilation, graphing, and scripting" (Ghidra README); its public GitHub repository was created on 1 March 2019, (GitHub's application programming interface (API) record of the repository). Disassembly is how defenders read malware, how researchers find vulnerabilities in programs without source, and how interoperability is achieved with undocumented formats. In the United States, 17 U.S. Code section 1201(f) permits a person who has lawfully obtained the right to use a program to circumvent a protection measure "for the sole purpose of identifying and analyzing those elements of the program that are necessary to achieve interoperability of an independently created computer program with other programs" (17 U.S. Code 1201); the Brazilian text is quoted in the section above.

Why x86 disassembly can disagree with itself

Because x86 instructions have variable length, a disassembler that starts one byte off reads different instructions from the same bytes. Hovav Shacham's 2007 paper makes the point with an analogy, "Intel x86 code is like English written without punctuation or spaces, so that the words all run together", and with an example from the C library he studied (Shacham, The Geometry of Innocent Flesh on the Bone, full version of the CCS 2007 paper):

Read from the intended startRead from one byte later
f7 c7 07 00 00 00 test $0x00000007, %edic7 07 00 00 00 0f movl $0x0f000000, (%edi)
0f 95 45 c3 setnzb -61(%ebp)95 xchg %ebp, %eax
45 inc %ebp
c3 ret

The byte c3 at the end, the operand of one instruction, becomes a RET when decoded from another start. The example is 32-bit code: in 64-bit mode the bytes 40 to 4F are not INC and DEC but REX prefixes, as the SDM puts it, "REX prefixes are a set of 16 opcodes that span one row of the opcode map and occupy entries 40H to 4FH. These opcodes represent valid instructions (INC or DEC) in IA-32 operating modes and in compatibility mode" (Vol. 2A, p. 2-8). The meaning of a byte depends on the mode as well as the offset, which is why a disassembler must be told both.

Shellcode, as a concept

Shellcode is a small piece of machine code that an attacker places in a vulnerable program's memory and causes to run, classically through a buffer overflow. It is written in assembly because it must work with no loader, no libraries and often no zero bytes. This article does not show any; the concept is enough to understand the defences below.

Return-oriented programming, at the idea level

The defence called W⊕X (memory may be writable or executable, never both), enforced in hardware by the NX bit (the execute-disable, or XD, flag of Intel's paging-structure entries; Intel SDM, Vol. 3A, p. 6-2), stops injected code from running. Shacham's paper showed that it does not stop an attacker from reusing code that is already there: short instruction sequences ending in a RET, which he called gadgets, can be chained by writing a sequence of return addresses on the stack, each RET jumping to the next gadget. He found enough of them in the C library to compute anything, and concluded that W⊕X "is much less useful than previously thought". Intel's manual now opens its chapter on Control-flow Enforcement Technology (CET) with the same description: "Return-oriented programming (), and similarly CALL/JMP-oriented programming (COP/JOP), have been the prevalent attack methodologies for stealth exploit writers targeting vulnerabilities in programs" (Intel SDM, Vol. 1, p. 18-1).

The defences in the instruction set

  • Shadow stack (Intel CET). "A shadow stack is a second stack for the program that is used exclusively for control transfer operations"; when it is enabled, "the CALL instruction pushes the return address on both the data and shadow stack. The RET instruction pops the return address from both stacks and compares them", and a mismatch raises a control protection exception (Vol. 1, p. 18-1).
  • Indirect branch tracking (Intel CET). Valid targets of indirect calls and jumps begin with ENDBR64, encoded F3 0F 1E FA (Vol. 2A, p. 3-288), an opcode "selected to be one that is a NOP on legacy machines" (Vol. 1, p. 18-1). The SDM says the instruction "is used to mark valid jump target addresses of indirect calls and jumps in the program", so in code built for indirect branch tracking a disassembly shows those four bytes at such targets.
  • Pointer authentication and branch target identification (Arm). PACIASP "calculates and inserts a Pointer Authentication Code for an instruction address, using a modifier and key A", taking the address from X30, the link register, and the modifier from SP (C6.2.306, p. C6-2495); the Arm ARM encodes it as 1101010100000011001000 CRm op2 11111 with CRm = 0011 and op2 = 001, that is D503233F (p. C6-2496); BTI guards "against the execution of instructions that are not the intended target of a branch" and "Outside of a guarded memory region ... executes as a NOP" (C6.2.50, p. C6-1888).
  • Guarded Control Stack (Arm). The Arm ARM states the threat plainly, "ROP techniques involve changing the return address of procedure calls to cause execution to change to a different path", notes that "Technologies such as Pointer Authentication help to significantly reduce the scope of an attack", and adds the Guarded Control Stack to store return addresses "such that they are immutable" (section D11.1.1, p. D11-7909).
  • And the older ones. Address space layout randomization (ASLR) randomizes where a process's memory regions land, so the gadgets' addresses change from run to run, and stack canaries are there to reveal a stack overwrite before the function returns. The root cause, memory that a program can corrupt, is the subject of Memory safety.

Trust in the toolchain

An assembler, like a compiler, sits between the text you read and the bytes that run. 's lecture, discussed in Reflections on trusting trust, is about exactly that gap. Disassembling the output, or assembling the same source with an independent tool as this article did, is the practical check.

What defenders can do

  • Read the bytes, not just the source. A disassembler on the shipped binary shows what will run, including whether ENDBR64 or PACIASP is present where it should be.
  • Tell the disassembler the mode and the start. The same bytes decode differently in 32-bit and 64-bit mode and from a different offset; a listing made with the wrong assumption is wrong without warning.
  • hand-written assembly where speed or the boot path matters. Cryptographic and multimedia libraries and boot code contain it by design; review it with the manuals open, as their authors did.
  • Know which defence answers which attack. Non-executable memory answers injected code; shadow stacks, pointer authentication and Guarded Control Stacks answer reused code; and canaries raise the cost of both.
  • Cross-check the toolchain. A second assembler or disassembler that agrees byte for byte is cheap evidence that the first one did what it says.

What to remember

  • Machine code is bytes; assembly language is a one-for-one textual form of them; the assembler translates, the disassembler reads back.
  • x86-64 instructions are 1 to 15 bytes, built from prefixes, opcode, ModR/M, SIB, displacement and immediate; AArch64 and RISC-V base instructions are 32-bit words with fields in fixed places, stored little-endian.
  • Branch offsets are counted from different places: from the next instruction on x86 and the 6502, from the branch itself on AArch64 and RISC-V, with the Z80's DJNZ adjusted by the assembler.
  • An assembler needs a location counter and a symbol table; a two-pass assembler collects labels first and encodes second, and variable-length branches can force more passes.
  • The calling convention is part of the ABI, not of the processor: RDI on Linux, RCX on Windows, X0 on AArch64, a0 on RISC-V.
  • Booth's notation (1947) and the EDSAC's initial orders (1949) both have documented claims to be the first assembly language.
  • ROP reuses existing code ending in RET; shadow stacks, pointer authentication, branch target identification and guarded control stacks are the instruction-set answers.

The next step away from the machine, the languages that a compiler or an interpreter turns into instructions like these, is Programming languages: from Plankalkül to Rust.

Sources