machine code

term

programming

Kept 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

← Back to M Back to the glossary