Machine language is built up from discrete
statements or
instructions. On the processing architecture, a given instruction may specify: •
opcode (the instruction to be performed) e.g. add, copy, test • any explicit operands: ::
registers ::literal/constant values ::
addressing modes used to access memory More complex operations are built up by combining these simple instructions, which are executed sequentially, or as otherwise directed by
control flow instructions.
Instruction types Examples of operations common to many instruction sets include:
Data handling and memory operations •
Set a
register or memory to a fixed constant value. •
Copy data from a one place to another. This operation is often called
load or store. Although the machine instruction is often called
move, the term is misleading because the source remains unchanged. These operations are used to store the contents of a register, the contents of another memory location or the result of a computation, or to retrieve stored data to perform a computation later. •
Read or
write data from hardware devices.
Arithmetic and logic operations •
Add,
subtract,
multiply, or
divide the values of two registers, placing the result in a register, possibly setting one or more
condition codes in a
status register. • '
, ' in some ISAs, saving operand fetch in trivial cases. • Perform
bitwise operations, e.g., taking the
conjunction and
disjunction of corresponding bits in a pair of registers, taking the
negation of each bit in a register. •
Compare two values in registers (for example, to see if one is less, or if they are equal). •
s for arithmetic on floating-point numbers.
Control flow operations •
Branch to another location in the program and execute instructions there. •
Conditionally branch to another location if a certain condition holds. •
Indirectly branch to another location. •
Skip one or more instructions, depending on conditions (a conditional branch a fixed number of instructions forward) •
Trap Explicitly cause a software
interrupt, either conditionally or unconditionally. •
Call another block of code, while saving the location of the next instruction as a point to return to. • Return from a previous call by retrieving the saved location.
Coprocessor instructions • Load/store data to and from a coprocessor or exchanging with CPU registers. • Perform coprocessor operations. :Some examples of coprocessor instructions include those for the
IBM 3090 Vector facility and the
Intel 8087.
Complex instructions Processors may include "complex" instructions in their instruction set. A single "complex" instruction does something that may take many instructions on other computers. Such instructions are
typified by instructions that take multiple steps, control multiple functional units, or otherwise appear on a larger scale than the bulk of simple instructions implemented by the given processor. Some examples of "complex" instructions include: • transferring multiple registers to or from memory (especially the
stack) at once • moving large blocks of memory (e.g.
string copy or
DMA transfer) • complicated integer and
floating-point arithmetic (e.g.
square root, or
transcendental functions such as
logarithm,
sine,
cosine, etc.) •
s, a single instruction performing an operation on many homogeneous values in parallel, possibly in dedicated
SIMD registers • performing an atomic
test-and-set instruction or other
read–modify–write atomic instruction • instructions that perform
ALU operations with an operand from memory rather than a register: exceptions to this rule being architectures that were designed as memory-based from the ground up, such as the
CDC STAR-100. Complex instructions are more common in CISC instruction sets than in RISC instruction sets, but RISC instruction sets may include them as well. RISC instruction sets generally do not include ALU operations with memory operands, or instructions to move large blocks of memory, but most RISC instruction sets include
SIMD or
vector instructions that perform the same arithmetic operation on multiple pieces of data at the same time. SIMD instructions have the ability of manipulating large vectors and matrices in minimal time. SIMD instructions allow easy
parallelization of algorithms commonly involved in sound, image, and video processing. Various SIMD implementations have been brought to market under trade names such as
MMX,
3DNow!, and
AltiVec.
Instruction encoding On traditional architectures, an instruction includes an
opcode that specifies the operation to perform, such as
add contents of memory to register—and zero or more
operand specifiers, which may specify
registers, memory locations, or literal data. The operand specifiers may have
addressing modes determining their meaning or may be in fixed fields. In
very long instruction word (VLIW) architectures, which include many
microcode architectures, multiple simultaneous opcodes and operands are specified in a single instruction. Some exotic instruction sets do not have an opcode field, such as
transport triggered architectures (TTA), only operand(s). Most
stack machines have "
0-operand" instruction sets in which arithmetic and logical operations lack any operand specifier fields; only instructions that push operands onto the evaluation stack or that pop operands from the stack into variables have operand specifiers. The instruction set carries out most ALU actions with postfix (
reverse Polish notation) operations that work only on the expression
stack, not on data registers or arbitrary main memory cells. This can be very convenient for compiling high-level languages, because most arithmetic expressions can be easily translated into postfix notation.
Conditional instructions Conditional instructions often have a predicate field—a few bits that encode the specific condition to cause an operation to be performed rather than not performed. For example, a conditional branch instruction will transfer control if the condition is true, so that execution proceeds to a different part of the program, and not transfer control if the condition is false, so that execution continues sequentially. Some instruction sets also have conditional moves, so that the move will be executed, and the data stored in the target location, if the condition is true, and not executed, and the target location not modified, if the condition is false. Similarly, IBM
z/Architecture has a conditional store instruction. A few instruction sets include a predicate field in every instruction. Having predicates on instructions is called
predication, and can
include conditional-branches, such as on the
SuperH.
Number of operands Instruction sets may be categorized by the maximum number of operands
explicitly specified in instructions. (In the examples that follow,
a,
b, and
c are (direct or calculated) addresses referring to memory cells, while
reg1 and so on refer to machine registers.) C = A+B • 0-operand (
zero-address machines), so called
stack machines: All arithmetic operations take place using the top one or two positions on the stack: push a, push b, add, pop c. • C = A+B needs
four instructions. For stack machines, the terms "0-operand" and "zero-address" apply to arithmetic instructions, but not to all instructions, as 1-operand push and pop instructions are used to access memory. • 1-operand (
one-address machines), so called
accumulator machines, include early computers and many small
microcontrollers: most instructions specify a single right operand (that is, constant, a register, or a memory location), with the implicit
accumulator as the left operand (and the destination if there is one): load a, add b, store c. • C = A+B needs
three instructions. • 2-operand — many CISC and RISC machines fall under this category: • CISC — move A to
C; then add B to
C. • C = A+B needs
two instructions. This effectively 'stores' the result without an explicit
store instruction. • CISC — Often machines are limited to one memory operand per instruction: load a,reg1; add b,reg1; store reg1,c; This requires a load/store pair for any memory movement regardless of whether the add result is an augmentation stored to a different place, as in C = A+B, or the same memory location: A = A+B. • C = A+B needs
three instructions. • RISC — Requiring explicit memory loads, the instructions would be: load a,reg1; load b,reg2; add reg1,reg2; store reg2,c. • C = A+B needs
four instructions. • 3-operand, allowing better reuse of data: • CISC — It becomes either a single instruction: add a,b,c • C = A+B needs
one instruction. • CISC — Or, on machines limited to two memory operands per instruction, move a,reg1; add reg1,b,c; • C = A+B needs
two instructions. • RISC — arithmetic instructions use registers only, so explicit 2-operand load/store instructions are needed: load a,reg1; load b,reg2; add reg1+reg2->reg3; store reg3,c; • C = A+B needs
four instructions. • Unlike 2-operand or 1-operand, this leaves all three values a, b, and c in registers available for further reuse. While embedded instruction sets such as
Thumb suffer from extremely high register pressure because they have small register sets, general-purpose RISC ISAs like
MIPS and
Alpha enjoy low register pressure. CISC ISAs like x86-64 offer low register pressure despite having smaller register sets. This is due to the many addressing modes and optimizations (such as sub-register addressing, memory operands in ALU instructions, absolute addressing, PC-relative addressing, and register-to-register spills) that CISC ISAs offer.
Instruction length The size or length of an instruction varies widely, from as little as four bits in some
microcontrollers to many hundreds of bits in some
VLIW systems. Processors used in
personal computers,
mainframes, and
supercomputers have minimum instruction sizes between 8 and 64 bits. The longest possible instruction on x86 is 15 bytes (120 bits). Within an instruction set, different instructions may have different lengths. In some architectures, notably most
reduced instruction set computers (RISC), , typically corresponding with that architecture's
word size. In other architectures, instructions have
variable length, typically integral multiples of a
byte or a
halfword. Some, such as the
ARM with
Thumb-extension have
mixed variable encoding, that is two fixed, usually 32-bit and 16-bit encodings, where instructions cannot be mixed freely but must be switched between on a branch (or exception boundary in ARMv8). Fixed-length instructions are less complicated to handle than variable-length instructions for several reasons (not having to check whether an instruction straddles a cache line or virtual memory page boundary,
Minimal instruction set computers (MISC) are commonly a form of
stack machine, where there are few separate instructions (8–32), so that multiple instructions can be fit into a single machine word. These types of cores often take little silicon to implement, so they can be easily realized in an FPGA (
field-programmable gate array) or in a
multi-core form. The code density of MISC is similar to the code density of RISC; the increased instruction density is offset by requiring more of the primitive instructions to do a task. There has been research into
executable compression as a mechanism for improving code density. The mathematics of
Kolmogorov complexity describes the challenges and limits of this. In practice, code density is also dependent on the
compiler. Most
optimizing compilers have options that control whether to optimize code generation for execution speed or for code density. For instance
GCC has the option -Os to optimize for small machine code size, and -O3 to optimize for execution speed at the cost of larger machine code.
Representation The instructions constituting a program are rarely specified using their internal, numeric form (
machine code); they may be specified by programmers using an
assembly language or, more commonly, may be generated from
high-level programming languages by
compilers. ==Design==