Allocating variable-length opcodes
Instructions & Addressing Modes

Answer & solution
Official answer: B
Givens and goal
Instructions are 16 bits and there are 16 registers. An M instruction has two register operands plus a 6-bit immediate; R has three register operands; C has one register plus a 6-bit offset. Given 2 M opcodes and 7 R opcodes, maximize C opcodes. Options: A 8; B 4; C 64; D 16.
Approach
Count occupied instruction encodings, then show the remaining space can be decoded unambiguously.
Step-by-step solution
1. Each register field needs log₂16=4 bits. M operands use 4+4+6=14 bits, leaving a 2-bit opcode. Each M opcode covers 2¹⁴ instruction words; two cover 2¹⁵, half of all 2¹⁶ words.
2. R operands use 12 bits, leaving a 4-bit opcode. Seven R opcodes cover 7×2¹²=28672 words, or 7/16 of the full space. M and R together occupy 1/2+7/16=15/16.
3. C operands use 4+6=10 bits, leaving a 6-bit opcode. Each C opcode covers 2¹⁰ words. The remaining capacity is (1/16)×2¹⁶=4096 words, so at most 4096/1024=4 C opcodes fit.
4. This upper bound is achievable with prefixes: use 00 and 01 for M. Of the eight remaining length-four prefixes 1000 through 1111, reserve seven for R. Extend the last prefix 1111 by 00,01,10,11 for four six-bit C opcodes.
5. No instruction format’s opcode is a prefix of another assigned opcode in this construction. Therefore four is both feasible and maximal, giving B. Simply observing that six bits permit 64 values ignores the already occupied encoding space.

