Sub topic 4
Expanding opcodes and counting encodable instructions
This chapter assumes you can work with binary numbers and powers of two. It also assumes you know that an instruction has an opcode field and one or more address fields, as covered in the previous chapter on instruction formats.
Why opcodes need to expand
In a fixed format, every instruction has the same opcode width. A 4-bit opcode gives 16 codes, and each code names one operation. If every instruction carries two address fields, every instruction pays for two addresses, even one like NOT R1 that needs only one operand.
An expanding opcode fixes this. It uses opcode codes that are shorter for the instructions that need many operands and longer for the ones that need few. The instruction length stays fixed, so the bits an instruction does not need for addresses become extra opcode bits.
The cost is decoding. The decoder cannot tell the operation from the first four bits alone. For some patterns it must read more bits before it knows which instruction it has.
The core idea: reserve a prefix, then extend it
Take 16-bit instructions with a 4-bit opcode and two 6-bit address fields.
- The 4-bit opcode field has 2^4 = 16 patterns.
- Suppose 14 of them (
0000to1101) are used for two-address instructions. - The patterns
1110and1111are unused. These are free prefixes.
A one-address instruction needs only one 6-bit address, so the other 6-bit field can be added to the opcode. Its opcode is then 4 + 6 = 10 bits long. Each free 4-bit prefix combines with any of the 2^6 = 64 values of the freed field. This gives 2 × 64 = 128 possible one-address opcodes.
The same step repeats. If only 100 one-address opcodes are used, 28 remain. Each of those can be extended by the second 6-bit field, which gives 28 × 64 = 1792 zero-address opcodes. A zero-address instruction uses all 16 bits as opcode.
The counting rule
At each level, the number of codes available to the next level is:
next level codes = (codes available at this level − codes used at this level) × 2^(freed address bits)
This holds when each step frees one address field. The counts are therefore multiplied by the field width at every step, not added.
The following program applies the rule at any depth.
def leftover_codes(opcode_bits, freed_bits, used_per_level):
"""Codes left for the last (shortest-format) level.
opcode_bits: width of the first opcode field.
freed_bits: bits freed at each extension (one address field).
used_per_level: instructions used at each level before the last one.
"""
free = 2 ** opcode_bits
for level, used in enumerate(used_per_level):
if used > free:
raise ValueError(f"level {level}: {used} used but only {free} available")
free = (free - used) * 2 ** freed_bits
return free
# 16-bit: 4-bit opcode, two 6-bit addresses.
# 14 two-address and 100 one-address instructions.
print(leftover_codes(4, 6, [14, 100])) # 1792 zero-address codes
# 16-bit: 4-bit opcode, three 4-bit addresses.
# 15 three-address, 14 two-address, 31 one-address instructions.
print(leftover_codes(4, 4, [15, 14, 31])) # 16 zero-address codesThe second call traces as follows:
- 16 top-level codes, 15 used, 1 free. 1 × 16 = 16 codes at the two-address level.
- 14 used, 2 free. 2 × 16 = 32 codes at the one-address level.
- 31 used, 1 free. 1 × 16 = 16 codes at the zero-address level.
How to read a GATE-style question
Most questions give you the instruction length, the opcode or address widths, and counts for all but one level. They then ask for the maximum number of instructions of the remaining kind. Follow this routine.
- Find the address field width. If memory has 2^n locations, a memory address is n bits. A register field is log2(number of registers) bits.
- Compute the first-level opcode patterns, 2^(opcode bits).
- Subtract the used patterns, then multiply by 2^(field width). Repeat for each level.
- The final number is the maximum for the last level.
| Quantity | Formula | Example (4-bit opcode, 6-bit fields) |
|---|---|---|
| Top-level codes | 2^opcode bits | 16 |
| Codes left after two-address use | 16 − 14 | 2 |
| One-address capacity | 2 × 2^6 | 128 |
| Zero-address capacity after 100 used | (128 − 100) × 2^6 | 1792 |
Edge cases and common errors
- Adding instead of multiplying. Free prefixes multiply by the freed field width. Writing 2 + 64 gives 66, which is wrong.
- Forgetting that "maximum" depends on usage above. The count at a level shrinks if the level above uses more codes. Each used code at a level removes a whole block of codes below it. Using one extra one-address opcode removes 64 zero-address opcodes.
- Unequal field widths. If the freed field has a different width from the other, use the width of the field that is actually released at that step. Register fields and memory fields often differ.
- Partial extension. You may extend only some of the free prefixes. Unused codes are then wasted, so the maximum assumes you extend all of them.
- Counting zero-address instructions that need no operands. They use every bit as opcode. Instructions such as
HALTorREThave no operand, so they fit here. - Mixing up bits and codes. 6 bits give 64 codes. The answer in these problems is always a code count.
Decoding cost
A fixed 4-bit opcode is decoded in one step. An expanding opcode needs a decoder that first checks the top bits and then, for the free prefixes, checks the next group of bits. In hardware this is a few extra gates on the decode path or a second-stage decoder. The saving is code density: you fit more operations into the same instruction width without making every instruction longer.
Exercise
A processor has 16-bit instructions. It has a 4-bit opcode field and two 6-bit address fields. Of the first-level opcode patterns, 13 are used for two-address instructions. A further 150 opcodes are used for one-address instructions. What is the maximum number of zero-address instructions?
Show answer
Free first-level patterns: 16 − 13 = 3. One-address capacity: 3 × 2^6 = 192. After 150 are used, 42 remain. Zero-address capacity: 42 × 2^6 = 2688.
Exercise
Memory has 256 words, and each instruction is 20 bits long. The format has an 4-bit opcode and one memory-address field per operand, with up to two operands. How many bits does each address field need, and does this format have room for a 4-bit opcode plus two address fields?
Show answer
256 words need log2(256) = 8 address bits. Two fields use 16 bits, and 4 + 16 = 20 bits, so the format fits exactly with no spare bits. Freed-field extension would then use 8 bits per step, giving 2^8 = 256 codes per free prefix.