TDEB Top Bar

Trending
Semiconductor Industry News — The Digital Electronics Blog
Follow Us

TDEB Masthead

The Semiconductor Hub

Semiconductors.  Chips and Systems.  Innovation.

Indexed by leading Search Engines, AI and referenced by Engineers worldwide 500+ Articles 10M+ Pageviews 30+ Reports 50+ Citations

Interview Question: CPU Instruction Format and Opcode Bits

mg
by
1
Share Facebook X LinkedIn WhatsApp Email

A CPU has a memory unit with 32-bit instructions and a register file with 32 registers. The instruction set consists of 95 different operations. The instruction consists of an opcode, two register file addresses, and an immediate operand part. Each instruction is stored in one word of memory.

(a) How many bits are needed for the opcode part of the instruction?
(b) How many bits are needed to indicate the registers?
(c) How many bits remain for the immediate operand part?
(d) If the immediate operand is always a memory address, what is the maximum number of words that can be addressed in memory?
(e) What are the largest and smallest algebraic values of signed 2's complement binary numbers that can be accommodated as an immediate operand?

The idea behind the question

Every instruction a processor executes is a fixed-size word of bits, split into fields. One field (the opcode) says what to do, others say which registers to use, and the rest can hold a constant or an address (the immediate). The question tests one simple rule: to give each of N different things its own code, you need enough bits that 2 raised to that number is at least N. Once you know how many bits each field needs, whatever is left over goes to the immediate field.

Answers

PartAnswerReason
(a) Opcode7 bits2^6 = 64 is too few for 95 operations; 2^7 = 128 is enough
(b) Registers10 bits32 registers need 5 bits each (2^5 = 32), and there are two register fields
(c) Immediate15 bits32 - 7 - 10 = 15
(d) Addressable words32,768 words2^15 = 32,768
(e) Immediate range-16,384 to +16,383A 15-bit 2's complement number ranges from -2^14 to 2^14 - 1

Instruction layout

bit 31        25 24      20 19      15 14                    0
   +-----------+----------+----------+-----------------------+
   |  opcode   |   reg A  |   reg B  |       immediate       |
   |  7 bits   |  5 bits  |  5 bits  |        15 bits        |
   +-----------+----------+----------+-----------------------+

The order of the fields is a design choice; the question only fixes their widths. Real instruction sets usually place the opcode in a fixed position so the decoder can find it before it knows anything else about the instruction.

Why designers care

Every bit given to one field is taken from another. A larger register file needs wider register fields and leaves a smaller immediate. More opcodes need a wider opcode field. A smaller immediate means more constants and addresses that do not fit in one instruction, so the compiler must build them with two or more instructions. Instruction format design is a balance between these demands, all inside a fixed word size.

Deep dive: the numbers and details

This part holds the general formulas, the 2's complement range and how real processors handle the same trade-off. Skip it if the overview is all you need.

The general rule

The number of bits needed to encode N distinct values is the ceiling of log2(N):

bits(N) = ceil(log2 N)
bits(95) = ceil(6.57) = 7
bits(32) = 5

With 7 opcode bits there are 128 codes and only 95 are used, so 33 codes are spare. Architects often keep spare codes for future instructions, or use some of them as escapes to extended formats.

The 2's complement range

An n-bit 2's complement number covers -2^(n-1) to 2^(n-1) - 1. For n = 15:

largest  = 2^14 - 1 = 16,383   = 011 1111 1111 1111
smallest = -2^14    = -16,384  = 100 0000 0000 0000

The range is not symmetric: there is one more negative value than positive, because zero takes one of the non-negative codes.

Part (d) in bytes

The question addresses words, not bytes. If each word is 32 bits (4 bytes), 32,768 words is 131,072 bytes, or 128 KiB. If the immediate were treated as a byte address instead, the same 15 bits would reach only 32 KiB. This is a common place to lose marks: check whether memory is word-addressed or byte-addressed.

Checking the answer

All fields must add up to the instruction width: 7 + 5 + 5 + 15 = 32. If the sum is wrong, one of the field widths is wrong.

Variations to practice

ChangeOpcodeRegistersImmediateImmediate range
As given: 95 ops, 32 registers, 2 register fields71015-16,384 to 16,383
64 registers71213-4,096 to 4,095
3 register fields, 32 registers71510-512 to 511
130 operations, 32 registers81014-8,192 to 8,191

How real processors handle large constants

A 32-bit instruction can never hold a full 32-bit constant plus an opcode, so real instruction sets split the work. A common pattern is one instruction that loads the upper part of a register and a second that adds the lower part. Another is PC-relative addressing, where the immediate is an offset from the current instruction rather than an absolute address, so a small field can still reach code and data near the instruction. Both approaches are answers to the limit this question asks you to compute in part (d).

Interview angle

Interviewers use this question to check that you can size fields with log2, handle 2's complement ranges, and add the fields back up to the word size. A good follow-up to be ready for: "What would you do if you needed 33 registers?" The answer is that you would need 6 bits per register field, taking 2 bits from the immediate, so most designs stay at a power of two.

Share Facebook X LinkedIn WhatsApp Email

Post a Comment

1 Comments

Your comments will be moderated before it appears here.

  1. (a) The number of bits needed for the opcode part of the instruction is determined by the number of different operations in the instruction set. In this case, there are 95 different operations, so we need at least 7 bits to represent all of them. However, since 2^7 = 128, which is more than 95, we can represent all the operations with 7 bits.

    (b) The register file has 32 registers, which can be represented using 5 bits (2^5 = 32).

    (c) The remaining bits for the immediate operand part can be calculated as follows: 32-bit instruction - 7 bits for opcode - 2 x 5 bits for register addresses = 15 bits.

    (d) If the immediate operand is always a memory address, then the maximum number of words that can be addressed in memory is determined by the number of bits used to represent the memory address. In this case, we have 15 bits for the immediate operand, so the maximum number of words that can be addressed is 2^15 = 32,768 words.

    (e) Since the immediate operand is signed 2's complement, the largest and smallest algebraic values that can be accommodated are determined by the number of bits used to represent the immediate operand. In this case, we have 15 bits for the immediate operand, so the largest algebraic value that can be accommodated is (2^14) - 1 = 32,767, and the smallest algebraic value that can be accommodated is -2^14 = -32,768.

    ReplyDelete
Post a Comment

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!