G27
Self-checkedTier 1 12 min readcoa.instruction-formats-opcodes

Instruction formats and expanding opcodes

How an instruction word is divided into fields, how to compute field widths from the machine's parameters, fixed-format examples, branch and jump reach, and the expanding-opcode technique that GATE counts again and again.

Why it exists

An instruction is a single binary word, so everything the processor needs to know must be packed into its bits: which operation, which operands, which addressing mode. How many bits each field gets is a design trade-off: more opcode bits mean more operations, more register bits mean more registers, more address bits mean more memory can be named directly. Questions on this topic turn that trade-off into arithmetic: compute a field width, a range, or how many instructions fit. The expanding opcode technique, where a rarely needed long opcode is hidden inside the bits that another instruction uses for an address, is one of the most repeated numerical problems in the subject.

Prerequisites

You need the classification of instructions by their number of addresses from ISA design, and binary arithmetic from number systems.

Precise definitions

Instruction format. The division of the instruction word into fields: an opcode (which operation), zero to three operand specifiers (registers or memory addresses), possibly an addressing-mode field and an immediate or displacement constant. The same machine may have several formats, told apart by the opcode.

Field widths.

  • Opcode: with nn different operations and a fixed-size opcode field, ⌈log⁡2n⌉\lceil \log_2 n \rceil bits.
  • Register specifier: ⌈log⁡2r⌉\lceil \log_2 r \rceil bits for rr registers.
  • Memory address: ⌈log⁡2M⌉\lceil \log_2 M \rceil bits to name one of MM addressable units directly.
  • Immediate or offset: kk bits give a signed range −2k−1-2^{k-1} to 2k−1−12^{k-1} - 1 (2's complement).
  • Instruction length = sum of the fields. Lengths are usually rounded to bytes or words, and unused bits are left as padding or used for extra opcode bits.

Fixed-length formats (a MIPS-style example, 32 bits). The widths below are those of the classic textbook MIPS architecture:

FormatFields (bits)
R (register)opcode 6, rs 5, rt 5, rd 5, shamt 5, funct 6
I (immediate)opcode 6, rs 5, rt 5, immediate 16
J (jump)opcode 6, address 26

Five bits name one of 32 registers. The R-format instructions share opcode 0 and are told apart by the 6-bit function field.

Branch and jump reach. In the I format the 16-bit signed field is a word offset from the next instruction: with 4-byte instructions the reach is −215-2^{15} to 215−12^{15} - 1 words, that is −128-128 KB to +128+128 KB −4- 4 bytes around the branch. The J format gives a 26-bit word address, i.e. 2262^{26} words =228= 2^{28} bytes =256= 256 MB; the top bits of the new PC come from the current PC, so a jump stays inside a 256 MB region.

Variable-length formats. Instructions of different sizes (for example 1 to 15 bytes in x86) give compact code and many addressing modes, but the length is only known after decoding the first bytes, which complicates fetch and pipelining.

Expanding opcodes. Suppose the instruction word has WW bits and every address field has aa bits. If all instructions had two addresses there would be 2W−2a2^{W - 2a} opcodes. Use only some of them for two-address instructions and keep the unused opcode values as escape codes: an escape tells the decoder that the opcode continues into the first address field, which therefore no longer holds an address. Each stage multiplies the number of available opcodes by 2a2^a:

N2=2W−2a,N1=(N2−n2) 2a,N0=(N1−n1) 2a,N_2 = 2^{W-2a},\qquad N_1 = (N_2 - n_2)\,2^{a},\qquad N_0 = (N_1 - n_1)\,2^{a},

where n2n_2 and n1n_1 are the numbers of two-address and one-address instructions actually defined. Then N1N_1 is the number of one-address opcodes available and N0=(N1−n1) 2aN_0 = (N_1 - n_1)\,2^a is the maximum number of zero-address instructions. The formulas require that each count is not negative (n2≤N2n_2 \le N_2, n1≤N1n_1 \le N_1). The idea works because the decoder reads the opcode left to right: a pattern that starts with a used two-address opcode is that instruction, while an escape pattern means "keep reading".

What the decoder needs. The code must be prefix-free: no opcode is the beginning of another one. The escape construction guarantees this, and it uses all patterns exactly once when no opcode is wasted.

How it works

For W=8W = 8 and a=2a = 2: N2=28−4=16N_2 = 2^{8-4} = 16 two-address opcodes. Use 15 of them. The unused pattern 1111 is an escape, giving (16−15)×22=4(16 - 15) \times 2^2 = 4 one-address opcodes. Use 3. The remaining one is a second escape giving (4−3)×22=4(4 - 3) \times 2^2 = 4 zero-address opcodes. The animation builds the patterns step by step; an x marks an address bit.

Expanding opcodes: 15 + 3 + 4 instructions in 8 bits: The 8-bit word

Computed exactly, not typed

Working

8-bit instruction, 2-bit address fields

[ opcode: 4 bits ][ addr 1: 2 ][ addr 2: 2 ]

Value, format and bits (changed bits in amber)

two-address formatpattern= opcode 0000, two address fields
0000xxxx

Step 1

Two address fields of 2 bits leave 4 bits for the opcode, so there are 16 different opcodes if every instruction has two addresses. An x marks an address bit.

1 / 4
computed by script: a simulation of the machine, one register transfer per step. Python 3.15.1, darwin arm64. python3 scripts/gen-coa-traces.py

The C program below decodes every one of the 256 words under this scheme and counts how many belong to each format: 240, 12 and 4, which add up to exactly 256 with no overlaps and no gaps.

Worked examples

EX 1Decode every word of the 8-bit scheme

Easy
#include <stdio.h>
 
/* decode an 8-bit instruction of the expanding-opcode scheme: 2-bit address fields,
   0000..1110 two-address, 111100..111110 one-address, 111111xx zero-address */
static const char *classify(unsigned w, int *index) {
    unsigned top = w >> 4;
    if (top < 15) { *index = (int)top; return "two-address"; }
    unsigned mid = (w >> 2) & 3;
    if (mid < 3) { *index = (int)mid; return "one-address"; }
    *index = (int)(w & 3);
    return "zero-address";
}
 
int main(void) {
    int count[3] = {0, 0, 0};
    for (unsigned w = 0; w < 256; w++) {
        int idx;
        const char *c = classify(w, &idx);
        count[c[0] == 't' ? 0 : c[0] == 'o' ? 1 : 2]++;
    }
    printf("words decoding as two-address: %d (15 opcodes x 16 address combinations)\n", count[0]);
    printf("words decoding as one-address: %d (3 opcodes x 4)\n", count[1]);
    printf("words decoding as zero-address: %d (4 opcodes)\n", count[2]);
    printf("total %d of 256\n", count[0] + count[1] + count[2]);
    int idx;
    const char *c = classify(0xF6, &idx);
    printf("0xF6 = 11110110 is %s instruction number %d\n", c, idx);
    c = classify(0xFE, &idx);
    printf("0xFE = 11111110 is %s instruction number %d\n", c, idx);
    c = classify(0x5A, &idx);
    printf("0x5A = 01011010 is %s instruction number %d\n", c, idx);
    /* the 16-bit example: 6-bit address fields, 14 two-address, 31 one-address */
    long two = 14, one = 31;
    long left_for_one = (16 - two) * 64;
    long left_for_zero = (left_for_one - one) * 64;
    printf("16-bit word, 6-bit addresses: %ld one-address codes available, %ld zero-address codes left\n", left_for_one, left_for_zero);
    return 0;
}
words decoding as two-address: 240 (15 opcodes x 16 address combinations)
words decoding as one-address: 12 (3 opcodes x 4)
words decoding as zero-address: 4 (4 opcodes)
total 256 of 256
0xF6 = 11110110 is one-address instruction number 1
0xFE = 11111110 is zero-address instruction number 2
0x5A = 01011010 is two-address instruction number 5
16-bit word, 6-bit addresses: 128 one-address codes available, 6208 zero-address codes left

Reading it. 15 two-address opcodes use 15×24=24015 \times 2^4 = 240 words (each with 16 address combinations), 3 one-address opcodes use 3×22=123 \times 2^2 = 12 words, and 4 zero-address opcodes use 4 words: 240+12+4=256240 + 12 + 4 = 256. The last block shows the 16-bit case: with 6-bit addresses, 14 two-address instructions leave (16−14)×64=128(16-14) \times 64 = 128 one-address opcodes; using 31 of them leaves (128−31)×64=6208(128 - 31) \times 64 = 6208 zero-address instructions.

EX 2Encode a MIPS-style instruction

Easy

The R-format instruction that adds registers 9 and 10 into register 8 has opcode 0, rs=9rs = 9, rt=10rt = 10, rd=8rd = 8, shamt=0shamt = 0 and function code 32 (decimal): in binary 000000 01001 01010 01000 00000 100000, which is 0x012A4020\texttt{0x012A4020}. The I-format load lw of register 8 from 8 bytes above the address in register 29 has opcode 35, rs=29rs = 29, rt=8rt = 8, immediate 8: 0x8FA80008\texttt{0x8FA80008}. The checking script builds both words from the fields and decodes the first back to (0,9,10,8,0,32)(0, 9, 10, 8, 0, 32).

EX 3Two-address, one-address and zero-address counts

Medium

A 16-bit instruction has 6-bit address fields. 14 two-address instructions are defined; 31 one-address instructions are defined. How many zero-address instructions can still be added?

Step 1. Two-address opcode space: 216−12=162^{16 - 12} = 16. Unused: 16−14=216 - 14 = 2.

Step 2. One-address opcode space: 2×26=1282 \times 2^6 = 128. Unused: 128−31=97128 - 31 = 97.

Step 3. Zero-address opcodes: 97×26=620897 \times 2^6 = 6208.

Check. The C program prints exactly 128 and 6208.

EX 4Choose a word size

Medium

A designer wants 10 two-address, 20 one-address and 100 zero-address instructions with 4-bit address fields. What is the smallest word size? The two-address opcode space must hold at least 10: 2W−8≥102^{W - 8} \ge 10 needs W−8≥4W - 8 \ge 4, so W≥12W \ge 12. At W=12W = 12 there are 16 two-address codes, 6 left after using 10, giving 6×16=966 \times 16 = 96 one-address codes; 76 remain after using 20, giving 76×16=121676 \times 16 = 1216 zero-address codes, enough for 100 (with 1216−100=11161216 - 100 = 1116 to spare). So 12 bits. (The script searches all sizes from 8 up and finds that 11 bits does not work.)

EX 5Field widths from machine parameters

GATE level

A processor has 64 registers, 100 different operations and a 32-bit instruction. A three-register instruction needs ⌈log⁡2100⌉=7\lceil \log_2 100 \rceil = 7 opcode bits and 3×6=183 \times 6 = 18 register bits, 25 bits in all, leaving 7 spare bits for a function or shift field. In a different design with an 8-bit opcode and two 5-bit register fields, the immediate field has 32−8−10=1432 - 8 - 10 = 14 bits and a range of −8192-8192 to 81918191. A constant outside that range cannot be an immediate and must be built from two instructions or loaded from memory.

EX 6Branch offset

GATE level

A branch at address 100 (4-byte instructions) targets address 120. The offset is measured from the next instruction (104): (120−104)/4=4(120 - 104)/4 = 4 words. The reach of a 16-bit word offset is ±215\pm 2^{15} words, that is from 131072 bytes before to 131068 bytes after the next instruction. A target 200 KB away needs a different instruction (a jump through a register, or a long-branch sequence).

Cheat box

Cheat box

  • Fixed opcode bits: ⌈log⁡2n⌉\lceil \log_2 n \rceil. Register field: ⌈log⁡2r⌉\lceil \log_2 r \rceil. Signed kk-bit immediate: −2k−1-2^{k-1} to 2k−1−12^{k-1}-1.
  • Expanding opcode: N2=2W−2aN_2 = 2^{W-2a}; N1=(N2−n2)2aN_1 = (N_2 - n_2) 2^a; N0=(N1−n1)2aN_0 = (N_1 - n_1) 2^a. Maximum zero-address instructions =N0= N_0.
  • 8 bits, a=2a = 2: 15 two-address, then 3 one-address, then 4 zero-address use all 256 words.
  • MIPS-style 32 bits: R = 6/5/5/5/5/6, I = 6/5/5/16, J = 6/26.
  • Branch: offset from the next instruction, in words: 16 bits gives ±128\pm 128 KB. Jump: 26 words bits = 256 MB region.
  • Fixed length: easy decode. Variable: dense code, harder fetch.

Traps and GATE patterns

Connections

The number of address fields in each format comes from the ISA class in instruction set architecture, and the mode bits and displacement fields determine the addressing modes available. Fixed-length formats are central to the RISC philosophy. The control unit's first job is to decode these fields, which is a decoder feeding the control logic.

PYQs

11 previous year questions from your book. I re-solved each one myself (10 by running or simulating, 1 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2026 · MCQQ3 · page 474 of the book
Consider a processor that has 16 general purpose
registers and it uses 2-byte instruction format for all
its instructions. Variable-sized opcodes are
permitted. There are three different types of
instructions; M-type, R-type, and C-type. Each M-
type instruction has 2 register operands and a 6-bit
immediate operand. Each R-type instruction has 3
register operands. Each C-type instruction has a
register operand and a 6-bit offset value. If there are
2 unique M-type opcodes and 7 unique R-type
opcodes, which one of the following options gives
the maximum number of unique opcodes possible
for C-type instructions?
  • (a)8
  • (b)4
  • (c)64
  • (d)16
GATE 2025 · Set 1 · MCQQ6 · page 475 of the book
A processor has 64 general-purpose registers and
50 distinct instruction types. An instruction is
encoded in 32-bits. What is the maximum number
of bits that can be used to store the immediate
operand for the given instruction?
  • (a)16
  • (b)20
  • (c)22
  • (d)24
GATE 2024 · NATQ7 · page 475 of the book
A processor with 16 general purpose registers uses
a 32-bit instruction format. The instruction format
consists of an opcode field, an addressing mode
field, two register operand fields, and a 16-bit
scalar field. If 8 addressing modes are to be
supported, the maximum number of unique
opcodes possible for every addressing mode is
GATE 2024 · NATQ8 · page 475 of the book
A processor uses a 32-bit instruction format and
supports byte-addressable memory access. The
ISA of the processor has 150 distinct instructions.
The instructions are equally divided into two
types, namely R-type and I-type, whose formats
are shown below.
R-type Instruction Format:

I- type Instruction Format:

In the OPCODE, 1 bit is used to distinguish between
I-type and R-type instructions, and the remaining
bits indicate the operation. The processor has 50
architectural registers, and all register fields in the
instructions are of equal size.
Let X be the number of bits used to encode the
UNUSED field, Y be the number of bits used to
encode the OPCODE field, and Z be the number of
bits used to encode the immediate value/address
field The value of X + 2Y + Z is
.
GATE 2020 · NAT · 2 marksQ11 · page 476 of the book
A processor has 64 registers and uses 16-bit
instruction format. It has two types of instructions:
I-type and R-type. Each I-type instruction contains
an opcode, a register name, and a 4-bit immediate
value. Each R-type instruction contains an opcode
and two register names. If there are 8 distinct I-type
opcodes, then the maximum number of distinct R-
type opcodes is
.
GATE 2018 · NAT · 2 marksQ12 · page 476 of the book
A processor has 16 integer registers (R0, R1, ....,
R15) and 64 floating point registers (F0, F1, ....,
F63). It uses a 2-byte instruction format. There are
four categories of instructions: Type-1, Type-2,
Type-3 and Type-4. Type-1 category consists of four
instructions, each with 3 integer register operands
(3Rs). Type-2 category consists of eight instructions,
each with 2 floating point register operands (2Fs).
Type-3 category consists of fourteen instructions,
each with one integer register operand and one
floating point register operand (1R+1F). Type-4
category consists of N instructions, each with a
floating-point register operand (1F). The maximum
value of N is
.
year not read yetQ16 · page 477 of the book
A processor has 40 distinct instructions and 24
general purpose registers. A 32-bit instruction word
has an opcode, two register operands and an
immediate operand The number of bits available for
the immediate operand field is
.
GATE 2016 · NATQ17 · page 477 of the book
Consider a Processor with 64 registers and an
instruction set of size twelve. Each instruction has
five distinct fields, namely, opcode, two source
register identifiers, one destination register
identifier, and a twelve-bit immediate value. Each
instruction must be stored in memory in a byte
aligned fashion. If a program has 100 instructions,
the amount of memory (in bytes) consumed by the
program text is
.
GATE 2015 · MCQQ19 · page 478 of the book
For computers based on three-address instruction
formats, each address field can be used to specify
which of the following:
S1: A memory operand
S2: A processor register
S3: An implied accumulator register
  • (a)Either S1 or S2
  • (b)Either S2 or S3
  • (c)Only S2 and S3
  • (d)All of S1, S2 and S3
GATE 2014 · NATQ20 · page 478 of the book
A machine has a 32-bit architecture, with 1-word
long instructions. It has 64 registers, each of which
is 32 bits long. It needs to support 45 instructions,
which have an immediate operand in addition to
two register operands. Assuming that the
immediate operand is an unsigned integer, the
maximum value of the immediate operand is

.
GATE 2006 · MCQ · 1 markQ31 · page 481 of the book
A CPU has 24-bit instructions. A program starts at
address 300 (in decimal). Which one of the
following is a legal program counter (all values in
decimal)?
  • (a)400
  • (b)500
  • (c)600
  • (d)

Self-check

Question 1

An 8-bit instruction has 2-bit address fields. 12 two-address instructions are defined. How many one-address instructions can be added at most, and how many zero-address instructions if all of those are used?

Show answer

N2=16N_2 = 16; unused 16−12=416 - 12 = 4; N1=4×4=16N_1 = 4 \times 4 = 16 one-address opcodes. If all 16 are used, the next level has (16−16)×4=0(16 - 16) \times 4 = 0 zero-address opcodes. (With 12 two-address instructions, using 15 one-address ones leaves 1×4=41 \times 4 = 4 zero-address.)

Question 2

A 32-bit instruction has a 6-bit opcode, two 5-bit register fields and an immediate. What is the largest positive immediate?

Show answer

The immediate has 32−6−10=1632 - 6 - 10 = 16 bits, signed: the largest is 215−1=327672^{15} - 1 = 32767.

Question 3

How many bits does the opcode need if the processor has 70 instructions with a fixed-size opcode?

Show answer

⌈log⁡270⌉=7\lceil \log_2 70 \rceil = 7 bits (since 26=64<70≤1282^6 = 64 < 70 \le 128).

Question 4

A branch at address 2000 (4-byte instruction) has the word offset −3-3. What is the target address?

Show answer

Next instruction at 2004, target 2004+4×(−3)=19922004 + 4 \times (-3) = 1992.

Revision card

One-screen revision

  • Fields: opcode ⌈log⁡2n⌉\lceil \log_2 n \rceil, register ⌈log⁡2r⌉\lceil \log_2 r \rceil, immediate kk bits signed. Length = sum of fields.
  • Expanding opcode: N1=(N2−n2)2aN_1 = (N_2 - n_2)2^a, N0=(N1−n1)2aN_0 = (N_1 - n_1)2^a.
  • 8-bit, a=2a = 2: 15 + 3 + 4 = 22 instructions fill all 256 words.
  • MIPS-style: R 6/5/5/5/5/6, I 6/5/5/16, J 6/26. Branch offset in words from the next instruction.

References

  • ·Hennessy and Patterson, Computer Organization and Design, Chapter 2 Instructions: Language of the Computer (representing instructions, MIPS fields)
  • ·Morris Mano, Computer System Architecture, 3rd ed., Chapter 8 Central Processing Unit (instruction formats)
  • ·Hamacher, Vranesic, Zaky, Computer Organization, 5th ed., Chapter 2 Machine Instructions and Programs (instruction encoding)

Verification

The expanding-opcode capacity formula is checked against explicit encodings of random 8-, 9-, 10- and 12-bit examples (every word is decoded and the number of words per class is counted), the page's 16-bit and 8-bit examples are computed and the 8-bit scheme is also decoded by the C program (npm run verify:c), and its animation is checked in tests/coa-traces.test.ts for disjoint patterns. The MIPS-style field widths, encodings, branch and jump reach and the smallest-word search are computed in scripts/enum-checks/coa_isa.py. The MIPS opcode and function numbers (add 0/0x20, lw 0x23, addi 0x08) are standard textbook facts used here from memory and not checked against a manual. Textbook chapter titles are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand (long C programs were not re-read line by line: they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose). No defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never