opcode

term

programming

Kept in this browser · My desk

The part of a machine instruction that says which operation to perform; its mnemonic is the name assembly language gives it.

Reading an opcode table is the first skill of hand assembly and of disassembly. Opcodes are not universal: the byte 78 is MOV A,B in Intel's 8080 manual and LD A,B in Zilog's Z80 manual, the same machine operation under two companies' names; and on x86 one opcode byte can stand for several operations, told apart by three bits of the following ModR/M byte.

What it is. An opcode (operation code) is the field of a machine instruction that selects the operation; the rest of the instruction names the operands. Assembly language gives each opcode a mnemonic, and the processor's manual is, at bottom, a table from bit patterns to operations. The idea is not limited to silicon: every virtual machine, from the Java Virtual Machine (JVM) to CPython and WebAssembly, defines its own opcodes.

On x86. The Intel Software Developer's Manual (SDM) lists one to three primary opcode bytes per instruction and writes the rest of the encoding as notation: /digit means "the reg field contains the digit that provides an extension to the instruction's opcode", so FF /1 is DEC while FF /0 is INC; /r means the ModR/M byte holds a register operand and an r/m operand; +rd means the low three bits of the opcode byte name the register, as in B8+rd for MOV r32, imm32. The manual also defines a mnemonic as "a reserved name for a class of instruction opcodes which have the same function", which is why 74 is listed as both JE and JZ.

A decode, byte by byte. The x86-64 bytes 48 01 F8 show the parts working together. 48 is a REX prefix, 0100WRXB with W = 1, which selects 64-bit operands; 01 is the opcode, listed as REX.W + 01 /r, ADD r/m64, r64; F8 is the ModR/M byte, 11 111 000: mod 11 means both operands are registers, reg 111 names RDI and r/m 000 names RAX. The instruction is add rax, rdi, and the opcode byte alone said only "add a register to a register or memory operand"; the prefix chose the width and the ModR/M byte chose the operands. Opcodes that do not fit in one byte begin with the escape byte 0F, which is why so many later x86 instructions, UD2 among them, start with it.

Opcodes with a special job. Some x86 opcodes exist for tools rather than arithmetic. 90 is NOP, the "One byte no-operation instruction" (Vol. 2B, p. 4-159). CC is INT3, which the manual describes as "Generate breakpoint trap" (Vol. 2A, p. 3-466); being one byte long, it can replace the first byte of any instruction. And 0F 0B is UD2, one of the instructions defined only to "Raise invalid opcode exception", so that a program can mark a place execution must never reach. Every byte value that is not a valid opcode in the current mode raises the same exception, which is how a processor reacts to data it was pointed at by mistake.

On fixed-length machines. RISC-V puts a 7-bit opcode field in bits 6 to 0 of every 32-bit instruction and refines it with funct3 and funct7: ADD and SUB share opcode 0110011 and funct3 000 and differ only in funct7. AArch64 has no single opcode field; each encoding in the Arm manual is a pattern of fixed bits around the operand fields.

On 8-bit machines. The Z80 manual gives ADD A, r as 10 000 r and LD r, r' as 01 r r'; Intel's 8080 manual gives MOV r1,r2 as 01DDDSSS; the 6502 manual tabulates each addressing mode with its own opcode, LDA immediate A9, LDA zero page A5. The same byte can carry two vendors' names: 78 is MOV A,B in Intel's 8080 manual and LD A,B in Zilog's Z80 manual, one machine operation under two mnemonics. One lesson from the Z80 manual is that tables beat single pages: its XOR page prints OR's codes, while its opcode table gives XOR A correctly as AF.

The first mnemonics. On the EDSAC of 1949 the opcode was the five most significant bits of a 17-bit order, written as a letter. David Wheeler's notes say Maurice Wilkes devised the orders and Wheeler himself suggested the letters, trying to make them "as mnemonic as possible", with odd exceptions: M could not be used for multiply, and J was not used for jump "because the operation was then thought of as 'transfer of control'". The habit of naming each opcode with a short memorable word starts there.

In virtual machines. The Java Virtual Machine Specification has a whole chapter, Opcode Mnemonics by Opcode, mapping each byte to its instruction: 0x00 is nop, 0x60 iadd; 0xca (breakpoint) and 0xfe and 0xff (impdep1 and impdep2) are reserved, and "Opcode value 186 was not used prior to Java SE 7". WebAssembly says plainly that "Each opcode is represented by a single byte", with 0x00 for unreachable, 0x01 for nop and 0x6A for i32.add, and admits that the byte codes "are historical and do not follow a consistent pattern". CPython's opcodes, by contrast, are an implementation detail: its dis documentation warns that no guarantees are made that bytecode "will not be added, removed, or changed between versions of Python", and since Python 3.6 every instruction takes 2 bytes.

What goes wrong. The same byte means different things in different modes (40H to 4FH are INC and DEC in 32-bit x86 code and REX prefixes in 64-bit code), so an opcode table is only valid with its mode. Manuals contain errors, vendors rename each other's instructions, and a virtual machine's opcode numbers can change between releases. Disassemblers that start in the middle of an instruction read operand bytes as opcodes and produce a plausible but false listing, the property return-oriented programming exploits.

Neighbours. The opcode lives inside machine code, its mnemonic in assembly language, its meaning in the instruction set architecture, and its operands usually in registers. Brazilian practice says opcode or código de operação.

Also known as: operation code, op code, código de operação, instruction opcode

Sources

← Back to O Back to the glossary