An opcode , also op code or operation code , is a number that specifies the number of a machine instruction for a specific processor type. All opcodes together form the instruction set of the processor or the processor family. Each instruction has its own opcode, such as the addition, multiplication, copying of registers , loading and saving of registers from the main memory , input and output, etc. Simple opcodes already represent a machine instruction. Addresses and constants must be added to some of the opcodes or the like follow, which together with the opcode then form a machine instruction.
A short word or an abbreviation, a mnemonic, is assigned to each opcode. The mnemonics result in the assembly languages in which a mnemonic, possibly followed by addresses or constants, is written in a line of a text file. A special program, the assembler , generates machine code by essentially replacing the mnemonics with their respective opcodes.
Most modern processors have a few hundred opcodes.
Older processors often had "undefined" or "illegal" opcodes. Certain opcodes not intended for programming could have all sorts of strange, harmful or, less often, useful effects on the internal circuitry of the processor due to their side effects. Such opcodes were sometimes used in the programming of computer games on home computers and early game consoles to speed up certain operations or to disguise the functionality of copy protection . The disadvantage was that such “undefined opcodes” were not guaranteed by the manufacturer and often no longer worked on successor models of a processor. Today's processors, on the other hand, either do nothing at all if they encounter an unknown opcode in a program, or they go into a clearly defined error state.
The 8-bit processor Z80
|04||INC B||increase register B by one ( inc rement B )|
|05||DEC A||diminish register A by one (Engl. dec rement A )|
|90||SUB B||sub trahiere Register B of accumulator A|
|21 ll hh||LD HL, hhll||l oa d HL with the constant hhll|
Note: There are two different mnemonics for the Z80 (LD M, B or LD (HL), B), plus a third one for the predecessor CPU Intel 8085 (MOV M, B).
The following Z80 mnemonics
DEC A INC B SUB B LD HL,1234h
generate the following Z80 machine program
05 04 90 21 34 12
Nowadays machine programs are practically always represented in hexadecimal (rare alternatives: decimal, octal ). Sometimes 16-bit values are displayed not as two 8-bit words in byte order in main memory but as one 16-bit word in logical order:
05 04 90 21 1234
The relay computer Zuse Z3
The Z3 by Konrad Zuse from 1941 had two registers R1 and R2, 64 memory cells and was capable of input / output, the four basic arithmetic operations and the calculation of the roots of floating point numbers. Programs were stored on punched tape, the opcodes of the nine commands consisted of the following punched tape encodings:
||Lu||Stop, enter R1 a decimal number from the keyboard, R2: = undef|
||Ld||Stop, decimal display of the result|
||Pr z||Reading of memory cell z (first R1, then always R2)|
||Ps z||Writing R1 into memory cell e.g.|
||La||Addition R1: = R1 + R2, R2: = 0|
||Ls||Subtraction R1: = R1-R2, R2: = 0|
||Lm||Multiplication R1: = R1 * R2, R2: = 0|
||Li||Division R1: = R1 / R2, R2: = 0|
||Lw||Root calculation R1: = Root (R1), R2: = 0|
O: hole, -: no hole, ·: transport hole, zzzzzz: binary coded memory location 0… 63
The punched tape is 8 bits wide and has an asymmetrical transport hole between the second and third bit. The opcodes for memory access only occupy 2 bits, followed by the address of the memory cell. The other opcodes occupy 5 bits, 3 bits are unused.
RAÚL ROJAS: Konrad Zuse's Legacy: The Architecture of the Z1 and Z3 . In: IEEE Annals of the History of Computing, Vol. 19, No. 2, 1997 . S. 5–16 (English, ed-thelen.org [PDF; accessed October 11, 2018]).