Sub topic 19
Worked GATE-style problem patterns
This chapter assumes you have read the earlier chapters on instruction formats, addressing modes, two's complement, status flags and control units. GATE questions on this topic repeat a few patterns. Each one has a fixed procedure, and most lost marks come from skipping a step in that procedure. The sections below cover five patterns, each with a worked example.
How to recognize the pattern
Most questions can be sorted by what they ask you to count or compute.
| Question asks for | Pattern | First step |
|---|---|---|
| Number of instructions that can still be encoded | Expanding opcode | Count unused opcode prefixes |
| Operand value or memory address accessed | Effective address | Identify the mode, then derive EA |
| Flag values after an add or subtract | Carry and overflow | Work in the stated bit width |
| Delay of an adder | Ripple versus lookahead | Find the longest carry path |
| Size of the control store in bits or bytes | Microinstruction width | Add up every field |
Pattern 1: Expanding opcodes
A 16-bit instruction has a 4-bit opcode and two 6-bit address fields. Fourteen two-address instructions are defined. How many zero-address instructions can be encoded if 100 one-address instructions are defined?
- The 4-bit opcode gives 16 prefixes. Fourteen are used, so 2 remain.
- Each remaining prefix can be extended by one 6-bit field. That gives 2 × 64 = 128 one-address codes.
- Using 100 of them leaves 28.
- Each leftover code can be extended by the last 6-bit field. That gives 28 × 64 = 1792 zero-address codes.
opcode_bits = 4
addr_bits = 6
two_address = 14
one_address = 100
free_prefixes = 2**opcode_bits - two_address
one_capacity = free_prefixes * 2**addr_bits
zero_capacity = (one_capacity - one_address) * 2**addr_bits
print(one_capacity, zero_capacity) # 128 1792Pattern 2: Effective address and operand
Let memory hold M[100] = 500, M[500] = 7 and M[600] = 42. Let R1 = 100 and the index register XR = 4. The address field is x. The effective address (EA) and operand for each mode are:
| Mode | x | EA | Operand |
|---|---|---|---|
| Immediate | 600 | none | 600 |
| Direct | 600 | 600 | 42 |
| Indirect | 100 | M[100] = 500 | 7 |
| Register indirect | R1 | R1 = 100 | 500 |
| Indexed | 596 | 596 + 4 = 600 | 42 |
The immediate row returns x itself, with no memory access. Indirect mode costs one extra memory read to find EA. Questions about the number of memory references count these reads. Include the instruction fetch if the question says "per instruction cycle".
For PC-relative mode, use the PC value after the fetch has incremented it. If an instruction at address 2000 is 2 words long, the PC holds 2002 when EA is computed. A question that says "relative to the next instruction" is hinting at this.
Pattern 3: Carry versus overflow
For unsigned numbers, the carry-out signals a wrong result. For signed numbers, overflow does, and overflow means both operands had the same sign and the result had a different sign. Subtraction A − B is computed as A + (NOT B) + 1. Here the carry-out of 1 means no borrow.
def add8(a, b, cin=0):
total = a + b + cin
result = total & 0xFF
carry = total >> 8
sa, sb, sr = (a >> 7) & 1, (b >> 7) & 1, (result >> 7) & 1
overflow = int(sa == sb and sr != sa)
return result, carry, overflow
print(add8(0x7F, 0x01)) # (128, 0, 1)
print(add8(0xFF, 0x01)) # (0, 1, 0)
# 0x80 - 0x01: add NOT(0x01) = 0xFE with carry-in 1
print(add8(0x80, 0xFE, 1)) # (127, 1, 1): -128 - 1 overflowsIn the third call the subtraction is done as 0x80 + 0xFE + 1. Both operands are negative and the result 0x7F is positive, so overflow is 1. The carry of 1 here only says there was no borrow. Do not read it as an error.
Pattern 4: Adder delay
In an n-bit ripple-carry adder, the carry passes through every full adder in turn. If each full adder takes 2 gate delays to produce its carry, a 32-bit adder needs about 32 × 2 = 64 gate delays for the final carry.
A carry-lookahead adder computes, for each bit i, a generate term Gi = Ai · Bi and a propagate term Pi = Ai XOR Bi. Then C(i+1) = Gi + Pi · Ci, which is expanded so that each carry depends only on G, P and C_0. The delay is then roughly constant, independent of n, although the gate fan-in grows. Questions usually state the delay of the lookahead block and ask you to compare total times. Add the delays of the stages along the longest path. Do not multiply by the number of bits unless the stages are chained.
Pattern 5: Control store size
A microprogrammed control unit has 1024 control store words. It has 40 control signals, 8 testable conditions and a next-address field.
- Horizontal format: one bit per signal gives 40 bits. A condition select field takes 3 bits, since 2^3 = 8. The next address takes 10 bits, since 2^10 = 1024. The width is 53 bits.
- Vertical format: suppose the signals fall into 4 groups of 10 mutually exclusive signals. Each group needs ceil(log2(10 + 1)) = 4 bits, which includes one code for no signal. The signal part is 16 bits and the width is 16 + 3 + 10 = 29 bits.
def control_store(words, signal_bits, cond_bits):
addr_bits = (words - 1).bit_length()
width = signal_bits + cond_bits + addr_bits
return width, words * width
print(control_store(1024, 40, 3)) # (53, 54272) bits = 6784 bytes
print(control_store(1024, 16, 3)) # (29, 29696) bits = 3712 bytesThe vertical format saves about 45% of the storage. It costs an extra decoder per group and slower signal generation. A question may also include a field for the mapped opcode or a flag bit. Add every field the question lists.
Common slips to check before answering
- Check the bit width. An 8-bit flag question answered with 4-bit reasoning gives a wrong overflow.
- Check the units. Control store answers may be asked in bits, bytes or words.
- Check the byte addressing. If the memory is byte-addressable and instructions are 4 bytes, the PC increment is 4, not 1.
- Check that you counted memory references for the instruction fetch when the question asks for the total.
Exercise
A 12-bit instruction has a 4-bit opcode and two 4-bit address fields. Eleven two-address instructions and 60 one-address instructions are defined. How many zero-address instructions can still be encoded?
Show answer
The opcode has 16 prefixes and 11 are used, so 5 remain. One-address codes: 5 × 16 = 80. After using 60, 20 remain. Zero-address codes: 20 × 16 = 320.
Exercise
Add the 8-bit values 0x90 and 0x90. Give the result, the carry-out and the signed overflow flag.
Show answer
The sum is 0x120, so the 8-bit result is 0x20 and the carry-out is 1. Both operands are negative (sign bit 1) and the result is positive (sign bit 0), so overflow is 1. As signed numbers, -112 + -112 = -224, which is outside the range -128 to 127.
Exercise
A control store has 512 words. Each word has a 24-bit signal field, a 2-bit condition select and a next-address field. What is the total size in bytes?
Show answer
The next-address field needs 9 bits because 2^9 = 512. The width is 24 + 2 + 9 = 35 bits. The total is 512 × 35 = 17920 bits, which is 2240 bytes.