machine code
termKept in this browser · My desk
The bit patterns a processor fetches and executes directly, laid out in memory as bytes in the order its instruction set architecture defines; assembly language is its human-readable notation.
Everything a computer runs ends as machine code, which is why reverse engineers, exploit writers and defenders all read it. It has a byte order (A64 and RISC-V instruction words are stored little-endian), and on x86 it has no fixed instruction length, so the same bytes read from a different starting point decode as different instructions, a fact that return-oriented programming exploits.
What it is. Machine code is the only language a processor runs: patterns of bits, each instruction an operation (the opcode) with its operands, fetched from memory and executed in the order the program counter dictates. Nothing in the bytes marks them as instructions; they are code because the processor is pointed at them. Everything above it, from assembly language to Python, ends as machine code or as a program written in it that interprets something else.
Formats. Intel's manual defines an x86 instruction as optional prefixes, one to three opcode bytes, an optional ModR/M byte and SIB byte, a displacement and an immediate, with a 15-byte limit. Arm's manual states that "A64 instructions have a fixed length of 32 bits and are always little-endian". RISC-V stores instructions "as a sequence of 16-bit little-endian parcels, regardless of memory system endianness", 32 bits for the base set and 16 for the compressed extension. The earliest machine codes were simpler still: on the EDSAC in 1949 an order was 17 bits, five of them naming the function and ten the store address, as David Wheeler's notes on the order code record.
An example, in three dialects. A loop that sums n down to 1 is 16 bytes of x86-64 (it starts 31 C0 48 85 FF) and seven 32-bit words on AArch64 and on RISC-V; the same loop on a Z80 is five bytes, AF 80 10 FD C9, and on a 6502 eleven, because the 6502 can add only memory to its accumulator. The Learn article computes every byte from the manuals and checked them against GNU's assemblers on 23 November 2026.
Decoding by hand. The Z80 version shows how little separates bytes from meaning. AF is XOR A, which clears the accumulator; 80 is 10 000 000, the pattern 10 000 r of ADD A, r with r = 000, register B; 10 FD is DJNZ with the displacement byte FD, minus 3, counted from the address after the instruction, so the loop returns to the ADD; C9 is RET. On x86-64 the first bytes 31 C0 decode the same way through the manual's tables: 31 /r is XOR r/m32, r32, and the ModR/M byte C0 is 11 000 000, register-direct mode with both fields naming EAX, so the instruction is xor eax, eax.
The container around it. Machine code rarely travels bare. On Linux and most Unix systems it sits in an Executable and Linkable Format (ELF) file, whose first four bytes are the magic number 0x7f, 'E', 'L', 'F'; the header records the byte order (ELFDATA2LSB means two's complement "with the least significant byte occupying the lowest address") and the machine in e_machine: 62 for AMD x86-64, 183 for 64-bit Arm (AArch64), 243 for RISC-V. Running the right bytes on the wrong machine is caught there, before a single instruction executes. Portable formats imitate machine code for a virtual machine instead: a WebAssembly module begins with the 4-byte magic \0asm and a version field (version 1), and a Java class file with 0xCAFEBABE; both hold instructions for an abstract processor, which a runtime then interprets or translates into the real machine code of the host.
Code is also data. In June 1983 a reader of the Brazilian magazine Micro Sistemas published a TK82-C routine as a list of decimal numbers POKEd into a REM line from address 16514 and run with USR 16514; BASIC listed the same bytes as odd characters because it read them, as the magazine put it, "not as Z80 commands but as characters". Hovav Shacham's 2007 paper showed the security side of the same idea: two x86 instructions, f7 c7 07 00 00 00 and 0f 95 45 c3, read from one byte later contain a ret (c3) that an attacker can use.
Keeping data from running. Because a processor will execute any bytes it is pointed at, the classic exploit wrote machine code into a buffer and jumped to it. The answer was to separate writable from executable memory. The PaX project's NOEXEC design states its goal as preventing "the injection and execution of code into a task's address space" and calls the idea a form of least privilege enforcement; Intel's manual describes the execute-disable bit, bit 63 of a paging-structure entry, enabled by IA32_EFER.NXE (bit 11), after which instructions "cannot be fetched" from a page that has it set. The defence moved attackers from injecting machine code to reusing the machine code already present, which is return-oriented programming.
What goes wrong. Reading machine code requires knowing where an instruction starts and in which mode the processor runs: the same x86 bytes decode differently from another offset, and 40H to 4FH are INC and DEC in 32-bit code but REX prefixes in 64-bit code. Code that writes its own instructions has to follow the processor's rules for self-modifying code, which Intel's manual still asks programmers to observe. And a binary built for one byte order or one instruction set is simply garbage on another.
Words. Brazilian practice says "código de máquina" or "linguagem de máquina"; object code usually means machine code still awaiting linking, and native code means machine code for the host processor, as opposed to bytecode.
Also known as: machine language, native code, object code, código de máquina, linguagem de máquina, binary code
Sources
- Intel, Intel 64 and IA-32 Architectures Software Developer's Manual, order 325462-093: instruction format of optional prefixes, one to three opcode bytes, ModR/M, SIB, displacement and immediate (Vol. 2A, p. 2-1); the 15-byte limit (Vol. 3A, p. 7-44); the label: mnemonic argument notation and the definition of a mnemonic as a reserved name for a class of opcodes with the same function (Vol. 1, p. 1-7) (read 2026-11-23)
- Arm, Arm Architecture Reference Manual for A-profile architecture, DDI 0487 M.d: A64 instructions are 32 bits and always little-endian (p. B2-316); RISC features (p. A1-41); 31 general-purpose registers, X30 the link register (p. A1-44); encodings of MOVZ, ADD, SUBS, CBZ, B.cond and RET (chapter C6) (read 2026-11-23)
- RISC-V International, The RISC-V Instruction Set Manual Volume I: Unprivileged Architecture, version 20250508: designed for research and education (p. 12); instructions stored as 16-bit little-endian parcels (p. 19); x0 hardwired to zero (p. 23); the RV32I listing with opcode, funct3 and funct7 fields (p. 609); the compressed extension's 25-30% code-size reduction (p. 160) (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)
- MOS Technology, MCS6500 Microcomputer Family Programming Manual, 6500-50A, January 1976: instruction summary opcodes such as LDA immediate A9, ADC zero page 65, BNE D0 and RTS 60 (pp. B-3 to B-26); branch range of +127 to -128 (p. 42) (read 2026-11-23)
- Micro Sistemas no. 21, June 1983 (Internet Archive text): Z80 machine code POKEd into a REM line from address 16514 and run with USR 16514 on the TK82-C (read 2026-11-23)
- Hovav Shacham, The Geometry of Innocent Flesh on the Bone, full version of the ACM CCS 2007 paper (Cornell course copy): gadgets ending in ret, W-xor-X, the English-without-spaces analogy and the f7 c7 07 00 00 00 / 0f 95 45 c3 example (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)
- System V Application Binary Interface (gABI), chapter 4, ELF Header (SCO copy): the magic number 0x7f 'E' 'L' 'F' in the first four bytes; ELFDATA2LSB, two's complement with the least significant byte at the lowest address; e_machine values EM_X86_64 62, EM_AARCH64 183 and EM_RISCV 243 (read 2026-11-23)
- WebAssembly Core Specification, Binary Format, Modules: a module starts with a preamble of a 4-byte magic number (\0asm) and a version field; the current binary format version is 1 (read 2026-11-23)
- The Java Virtual Machine Specification, Java SE 21, chapter 4, The class File Format: the magic item has the value 0xCAFEBABE; class file verification by type checking (section 4.10) (read 2026-11-23)
- Intel SDM 325462-093, Vol. 3A, section 6.13 (p. 6-30): the execute-disable capability enabled by IA32_EFER.NXE (bit 11); instructions cannot be fetched from a page whose paging-structure entries set the execute-disable bit (bit 63); the guidelines for self-modifying and cross-modifying code (read 2026-11-23)
- PaX Team, NOEXEC design document (noexec.txt): the goal is to prevent the injection and execution of code into a task's address space by separating writable and executable mappings; 'a form of least privilege enforcement' (read 2026-11-23)
- University of Cambridge Computer Laboratory, David Wheeler, The EDSAC Order Code: Explanatory Notes: the 17-bit order of five function bits, a spare bit, ten address bits and a length bit, as the computer had it in 1949 (read 2026-11-23)