Skip to main content
@shmVirus

Instruction Set Design

Encode operations and operands, compare zero- through three-address formats, calculate expanding opcodes and Huffman codes, resolve addressing modes, and trace program control.

Updated

Learning objectives

  • Separate opcode, addressing-mode, register, and memory-address fields
  • Compare block, expanding-opcode, and Huffman encoding
  • Calculate encoding capacity for mixed address formats
  • Compute effective addresses for major addressing modes
  • Explain data transfer, arithmetic, logic, system, branch, subroutine, and interrupt instructions
  • Relate condition flags to conditional program control

Prerequisites

  • Computer Organization
  • Datapath and Microoperations

An instruction tells the processor what operation to perform and where its operands come from. A sequence of instructions forms a program. The instruction-set architecture (ISA) defines the binary contract between software and the processor.

instruction=opcode+operand/address fields+optional mode fields.\text{instruction}=\text{opcode}+\text{operand/address fields}+\text{optional mode fields}.
TermDefinition
OpcodeThe bits that identify the operation, such as add, load, or branch.
OperandA value consumed by the operation; it may be explicit or implied.
Address fieldBits that help identify an operand location, not necessarily the final memory address.
Addressing modeThe rule that interprets operand fields and forms the effective address.
Effective address (EA)The memory address obtained after applying the addressing-mode calculation.

Do not equate an opcode with an instruction. The opcode says what operation to perform; the remaining fields say which values participate and how to find them.

Operand-Address Formats

Zero-address, one-address, two-address, and three-address instruction formats with stack, accumulator, and register examples
Fewer explicit addresses shorten an instruction but require more implicit machine state or more instructions to express the same calculation.

For the expression X=(A+B)×(CD)X=(A+B)\times(C-D):

FormatRepresentative sequenceImplicit state
Three-addressADD R1,A,B; SUB R2,C,D; MUL X,R1,R2Destinations are explicit
Two-addressMOV R1,A; ADD R1,B; MOV R2,C; SUB R2,D; MUL R1,R2First operand is also destination
One-addressLOAD A; ADD B; STORE T; LOAD C; SUB D; MUL T; STORE XAccumulator
Zero-addressPUSH A; PUSH B; ADD; PUSH C; PUSH D; SUB; MUL; POP XStack top

Short instructions occupy less memory and can be fetched with fewer bytes. They may also require more instructions, so total program size and execution time depend on the complete encoding rather than one instruction in isolation.

Fixed-Length Block-Code Encoding

A processor can execute an operation only after its opcode has a unique binary pattern. With KK opcode bits, block coding offers at most 2K2^K patterns. A KK-to-2K2^K decoder can activate one instruction line.

OpcodePatternOpcodePattern
LDA000AND011
STA001CMA100
ADD010INCA101
JMP110HLT111

Three bits encode exactly eight operations. Adding a ninth operation requires a wider fixed opcode or a different encoding scheme.

Expanding Opcodes

An expanding opcode reuses an otherwise unused short opcode as a prefix for a longer opcode. This trades some operand-field combinations for additional operations with fewer explicit operands.

A fixed three-to-eight opcode decoder beside progressively expanded three-address, two-address, one-address, and zero-address instruction formats
A fixed code always stops after the same number of bits. An expanding code uses a reserved escape prefix to move the opcode boundary into a former address field.

Consider a 16-bit format with a 4-bit opcode followed by three 4-bit addresses. It can encode sixteen three-address operations. If only fifteen are used, prefix 1111 can signal that the next 4-bit field extends the opcode:

ordinary three-address: [ opcode 4 ][ A1 4 ][ A2 4 ][ A3 4 ]
two-address extension:  [ 1111 ][ opcode 4 ][ A1 4 ][ A2 4 ]
one-address extension:  [ 1111 ][ 1111 ][ opcode 4 ][ A1 4 ]
zero-address extension: [ 1111 ][ 1111 ][ 1111 ][ opcode 4 ]

The exact allocation is a design choice. The rule is that decoding must be unambiguous.

Capacity method

Count complete bit patterns rather than merely “unused names.” An LL-bit instruction space contains 2L2^L encodings. An instruction with aa address bits consumes 2a2^a complete encodings because its address can vary independently.

Example 1: one- and zero-address instructions

A byte-addressable 64 B memory needs 6 address bits. With a 4-bit opcode, there are 16 short opcode patterns. Ten are used for one-address instructions, leaving six prefixes. Each prefix can absorb all 262^6 former address patterns:

N0=(1610)26=6×64=384N_0=(16-10)2^6=6\times64=384

zero-address instructions.

Example 2: three- to two-address expansion

A 22-bit instruction has one 4-bit register field, two 6-bit memory-address fields, and therefore a 6-bit three-address opcode. Of 64 possible opcodes, 62 are used. Each of the two remaining prefixes can extend through one 6-bit field:

N2=(6462)26=128N_2=(64-62)2^6=128

two-address operations.

Example 3: mixed 58 and 25 operations

A processor needs 58 three-address operations and 25 two-address operations; each address selects one of 16 locations.

  • Address width: log216=4\log_2 16=4 bits.
  • Three-address opcode width: log258=6\lceil\log_2 58\rceil=6 bits.
  • Fixed instruction length: 6+3(4)=186+3(4)=18 bits.
  • Unused 6-bit prefixes: 6458=664-58=6.
  • Two-address capacity: 6×24=966\times2^4=96, enough for 25 operations.
  • Two-address opcode width: 6+4=106+4=10 bits.

Example 4: count the complete 11-bit space

An 11-bit format has 4-bit address fields, five two-address instructions, and 32 one-address instructions:

2115(24)232(24)=20481280512=2562^{11}-5(2^4)^2-32(2^4) =2048-1280-512=256

zero-address encodings remain.

Huffman Opcode Encoding

If opcode frequencies are known, a prefix-free Huffman code minimizes the weighted average code length:

A prefix-free Huffman tree giving a one-bit code to a common operation and two-bit codes to less frequent operations, with a weighted-average calculation
A leaf completes an opcode. Internal nodes mean “read another bit,” so no completed code can be the prefix of another.
  1. Create one leaf for each operation and label it with its probability.
  2. Repeatedly combine the two smallest unmarked probabilities.
  3. Label the two branches 0 and 1.
  4. Read each code from root to leaf.

Frequent instructions get short codes; rare instructions get long codes. The source example assigns codes of lengths 2–4 to operations such as LOAD, STORE, SHIFT, NOT, JUMP, HALT, AND, and ADD.

For the characters in Mississippi, frequencies are m:1m:1, p:2p:2, s:4s:4, and i:4i:4. One valid Huffman tree gives lengths 3, 3, 2, and 1:

1(3)+2(3)+4(2)+4(1)=21 bits,21/11=1.909 bits per character.1(3)+2(3)+4(2)+4(1)=21\text{ bits}, \qquad 21/11=1.909\text{ bits per character}.

The advantage is minimum expected bits. The disadvantages are variable boundaries, serial prefix recognition, and a more complex decoder. Practical instruction sets usually favor regular parallel decoding even when a pure Huffman code is denser.

Addressing Modes

An addressing mode defines how an instruction obtains an operand or its effective address (EAEA).

Data paths and effective-address equations for immediate, direct, register, register-indirect, indexed, base-relative, PC-relative, auto-increment, and auto-decrement addressing
The same address or displacement bits can mean a literal, a memory location, or part of an effective-address calculation depending on the mode.

Assume the instruction contains value/address 250250, register R2=1000R2=1000, word size k=4k=4, and PC=400PC=400:

ModeOperand or effective addressExample meaning
InherentOperand implied by opcodeCLI clears the interrupt-enable flag
ImmediateOperand =250=250ADD #250,R1
Direct/absoluteEA=250EA=250Operand =M[250]=M[250]
RegisterOperand =R2=R2No data-memory access
Register indirectEA=R2=1000EA=R2=1000Operand =M[1000]=M[1000]
Auto-incrementEA=1000EA=1000, then R21004R2\leftarrow1004Traverse forward through words
Auto-decrementR2996R2\leftarrow996, then EA=996EA=996Stack push or reverse traversal
IndexedEA=R2+250=1250EA=R2+250=1250Large index plus address/offset
Base plus displacementEA=Rb+small displacementEA=R_b+\text{small displacement}Structure field or stack-frame item
PC-relativeEA=PC+signed displacementEA=PC+\text{signed displacement}Position-independent branch
Memory indirectEA=M[250]EA=M[250]Address field points to a pointer

The terms indexed and base-relative can use identical addition hardware. Their distinction is the programming interpretation and typical field width. Auto-indexing means the register is modified as part of operand access.

Instruction Classes

Data transfer

MOV, LOAD, STORE, stack push/pop, and register exchange move values without changing their bit patterns. The datapath still needs source selection, a route, and a destination write enable.

Arithmetic and logic

ADD, SUB, MUL, DIV, AND, OR, NOT, shifts, and rotates select ALU or iterative arithmetic functions. These instructions usually update condition flags.

System control and I/O

System instructions change privileged state, interrupt masks, or processor modes. Separate-I/O architectures use explicit input/output instructions; memory-mapped I/O uses ordinary load/store operations to special addresses.

Program control

  • unconditional branch: BR NEXT;
  • conditional branch: test a flag and possibly replace PCPC;
  • counted branch: decrement a register and branch until a condition;
  • subroutine call: save a return address and branch;
  • return: restore the saved address;
  • trap: request a synchronous software interrupt.

Status Register and Conditional Control

Status register containing zero, negative, carry, overflow, and interrupt-enable flags connected to ALU results, a branch decision, subroutine stack, and interrupt service path
Arithmetic writes condition flags; later control-flow instructions read them. Calls and interrupts additionally preserve a return address and, often, the old status word.
FlagTypical setting conditionUnsigned/signed use
ZZResult is all zerosEquality and loop termination
NN or SSResult sign bit is 1Signed negative
CCCarry out / no borrow under the chosen conventionUnsigned overflow and multiword arithmetic
VVSigned result is outside representable rangeSigned overflow
IENIENInterrupts globally enabledControls asynchronous service acceptance

For four-bit addition 1111 + 0001 = 1 0000, Z=1Z=1, C=1C=1, N=0N=0, and V=0V=0: unsigned 15 overflowed to zero, while signed 1+1=0-1+1=0 is valid.

An external interrupt resembles a hardware-generated subroutine call but may arrive asynchronously. The processor finishes or safely suspends current work, saves PCPC and required status/register state, loads an interrupt-vector address, runs the service routine, then restores state before returning.

Instruction Execution at Register-Transfer Level

The foundation cycle becomes concrete when fields and addressing modes are included:

PhaseRepresentative microoperations
Fetch addressMARPCMAR\leftarrow PC
Fetch wordMDRM[MAR], PCPC+1MDR\leftarrow M[MAR],\ PC\leftarrow PC+1
Capture/decodeIRMDRIR\leftarrow MDR; decode opcode and mode
Effective addressMARRb+signext(IR.disp)MAR\leftarrow R_b+\operatorname{signext}(IR.disp) if required
Operand readMDRM[MAR]MDR\leftarrow M[MAR] or select source register/immediate
ExecuteZXopYZ\leftarrow X\operatorname{op}Y; compute flags
Write-backdestination Z\leftarrow Z
Interrupt checkif enabled request pending, save state and load vector

Not every instruction uses every phase. A register-register ADD needs no data-memory operand access; a STORE has no ALU result write-back; a taken branch writes PCPC.

Self-Check

  1. Why is a zero-address instruction not an instruction with no operands?
  2. A fixed opcode has 37 operations. What minimum opcode width is required?
  3. With R3=1200R3=1200 and displacement =24=-24, what is the base-relative effective address?
  4. Why can C=1C=1 and V=0V=0 for the same addition?
Answers
  1. Its operands are implicit, usually at the top of a stack.
  2. log237=6\lceil\log_2 37\rceil=6 bits.
  3. EA=1176EA=1176.
  4. Carry reports the unsigned boundary; overflow reports the signed boundary.