Blog
Computer Architecture Past Paper 2015 – BSCS 6th Term UOS (CS-420)
University of Sargodha BS Computer Science 6th term Computer Architecture (CS-420) paper, 2015 exam (80 marks, 2:30 hours). The paper is retyped below without the handwritten ticks, followed by solved answers for all 16 short questions (including the bus cycle, MARIE address bits and little-endian storage) and all five subjective questions: the fetch-decode-execute cycle, MIPS addressing modes, a load in the single-cycle datapath, pipeline hazards and direct-mapped cache mapping.
Solved answers
Q1 – Short answers (16 × 2)
i. Bus cycle at 133 MHz. Cycle time = 1 / f = 1 / (133 × 106) ≈ 7.52 × 10−9 s ≈ 7.5 ns.
ii. Memory interleaving. Main memory is divided into several banks and consecutive addresses are placed in different banks (address mod number of banks). The CPU can access several banks in parallel or overlapped, which increases memory bandwidth.
iii. Types of buses. By function: data bus, address bus and control bus. By use: processor-memory (system) bus, I/O bus (PCI/PCIe, USB, SATA) and backplane bus; buses may also be synchronous or asynchronous.
iv. CISC vs RISC. CISC: many complex, variable-length instructions, many addressing modes, memory operands in most instructions, microprogrammed control, multi-cycle instructions (x86). RISC: few simple fixed-length instructions, load/store architecture, many registers, hard-wired control, about one instruction per cycle with pipelining (MIPS, ARM).
v. Interrupts. Signals that make the CPU suspend the current program, save its state and run an interrupt service routine, then resume. Hardware interrupts come from devices (keyboard, timer, I/O complete); software interrupts/traps come from instructions or errors.
vi. MBR vs MAR. The MAR (memory address register) holds the address of the memory location to be read or written. The MBR (memory buffer/data register) holds the data word just read from, or about to be written to, that location.
vii. Load-store architecture. Only load and store instructions access memory; all arithmetic and logic instructions work on registers only (e.g. lw, add, sw in MIPS). It simplifies instruction format and pipelining – typical of RISC.
viii. MARIE with 16K words. 16K = 24 × 210 = 214 words, so 14 address bits are needed.
ix. Effective address. The actual memory address of the operand after applying the addressing mode, e.g. for lw $t0, 8($s1) with $s1 = 1000, EA = 1000 + 8 = 1008.
x. MIPS store instruction. sw rt, offset(rs) copies a 32-bit word from register rt to memory address rs + offset. Example: sw $t0, 12($s0) stores $t0 at Mem[$s0 + 12]. It is an I-type instruction (opcode 43).
xi. J-type format. opcode (6 bits) | address (26 bits). Target = PC+4[31:28] concatenated with address × 4. Example: j label (opcode 2), jal (opcode 3).
xii. 123ABC78 on a little-endian machine. The least significant byte is stored at the lowest address: address +0: 78, +1: BC, +2: 3A, +3: 12. (Big endian would be 12, 3A, BC, 78.)
xiii. Execution time formula. CPU time = Instruction count × CPI × Clock cycle time = (Instruction count × CPI) / Clock rate.
xiv. Data hazard. An instruction needs data that a previous instruction in the pipeline has not yet produced, e.g. add $s0,$t0,$t1 followed by sub $t2,$s0,$t3. Solved by forwarding, stalls or instruction reordering.
xv. Advantages of pipelining. Higher throughput (ideally one instruction finishes every cycle), higher clock frequency because each stage is short, better use of hardware; the speed-up approaches the number of stages for long instruction streams.
xvi. Miss rate. The fraction of memory accesses not found in the cache: miss rate = misses / total accesses = 1 − hit rate. It is used in AMAT = hit time + miss rate × miss penalty.
Q2 – Fetch-decode-execute cycle and the registers
The CPU repeats the instruction cycle for every instruction. Registers involved: PC (program counter), MAR, MBR/MDR, IR (instruction register), AC/general registers and the flags.
- Fetch: MAR ← PC; the address goes on the address bus and a read is signalled. MBR ← M[MAR]; IR ← MBR; PC ← PC + 1 (or + 4 in MIPS) so PC points to the next instruction.
- Decode: the control unit decodes the opcode in IR and identifies the operands; the operand address field is placed in MAR (MAR ← IR[address]). For register operands the register file is read.
- Fetch operand (if needed): MBR ← M[MAR] – the operand is read from memory (for indirect addressing an extra memory read is done first).
- Execute: the ALU performs the operation, e.g. AC ← AC + MBR for ADD; for STORE, MBR ← AC then M[MAR] ← MBR; for a branch, PC ← target address if the condition holds. Flags are updated.
- Interrupt check: if an interrupt is pending and enabled, PC is saved and PC ← ISR address; otherwise the next cycle begins.
Example (MARIE, ADD 104): MAR ← PC; IR ← M[MAR]; PC ← PC + 1; MAR ← IR[11-0] = 104; MBR ← M[104]; AC ← AC + MBR.
Q3 – Addressing modes in MIPS
- Register addressing – all operands in registers (R-type):
add $t0, $t1, $t2. - Immediate addressing – one operand is a 16-bit constant in the instruction:
addi $t0, $t1, 10. - Base (displacement) addressing – memory address = register + 16-bit signed offset; used by loads/stores:
lw $t0, 32($s3). - PC-relative addressing – branch target = (PC + 4) + offset × 4:
beq $t0, $t1, Loop. - Pseudo-direct addressing – jump target = upper 4 bits of PC+4 concatenated with the 26-bit address × 4:
j Exit.
MIPS keeps only these five simple modes so that all instructions are 32 bits long and can be decoded quickly – a RISC design choice. Complex modes are built from them (e.g. an array element: sll $t1,$s2,2; add $t1,$t1,$s1; lw $t0,0($t1)).
Q4 – Executing a load (lw) in the single-cycle microarchitecture
Instruction: lw $rt, imm($rs) (I-type: op = 35 | rs | rt | imm16). All steps happen in one long clock cycle:
- Fetch: PC sends the address to instruction memory; the 32-bit instruction is read. An adder computes PC + 4.
- Read register: bits 25-21 (rs) go to Read register 1; the register file outputs the base address.
- Sign-extend: the 16-bit immediate is sign-extended to 32 bits.
- ALU: ALUSrc = 1 selects the extended immediate; ALU adds base + offset = memory address.
- Data memory: MemRead = 1; the word at that address is read.
- Write back: MemtoReg = 1 selects memory data, RegDst = 0 selects rt (bits 20-16) as destination, RegWrite = 1 writes the value at the end of the cycle.
- Next PC: PCSrc = 0, so PC ← PC + 4.
+-----+ PC+4
+--->| Add |-------------------------------------------+
| +-----+ |
+-+--+ +-------------+ rs +-----------+ RD1 +-----+ | addr +--------+ RD
| PC |-->| Instruction |----->| Register |----->| | +----->| Data |----+
+----+ | Memory | rt | File | | ALU |------->| Memory | |
| |--+ | | +-->| + | +--------+ |
+-------------+ | | WD WA | | +-----+ MemRead=1 |
| imm16 | +---^---^---+ | |
v +-------|---+ (RegDst=0 -> rt) |
+-----------+ +-----------------------------------------+
| Sign ext. |-------------------> ALUSrc=1 (MemtoReg=1)
+-----------+Control signals for lw: RegDst = 0, ALUSrc = 1, MemtoReg = 1, RegWrite = 1, MemRead = 1, MemWrite = 0, Branch = 0, ALUOp = 00 (add). The load is the slowest instruction (instruction memory → register file → ALU → data memory → register file), so it sets the clock period of the single-cycle design.
Q5 – Types of pipeline hazards
A hazard is a situation that prevents the next instruction from executing in its clock cycle.
1. Structural hazards – two instructions need the same hardware resource at the same time. Example: a single memory used for both instruction fetch and data access – a load in MEM and an instruction fetch in IF collide. Solution: separate instruction and data memories (Harvard / split caches), duplicate resources or stall.
2. Data hazards – an instruction depends on the result of an earlier instruction still in the pipeline.
add $s0, $t0, $t1 # $s0 written in WB (cycle 5)
sub $t2, $s0, $t3 # needs $s0 in EX (cycle 4) -> RAW hazard- Types: RAW (read after write – true dependence), WAR and WAW (only in out-of-order pipelines).
- Solutions: forwarding/bypassing the ALU result directly to the next instruction; stalls (bubbles) when forwarding is not enough – the load-use hazard (
lw $s0,0($t1)thenadd $t2,$s0,$t3) needs one stall even with forwarding; compiler scheduling to put an independent instruction in between.
3. Control (branch) hazards – the next instruction to fetch depends on a branch outcome not yet known.
beq $t0, $t1, L1 # outcome known only in EX/MEM
add $s1, $s2, $s3 # fetched before we know if it should run- Solutions: stall until the branch resolves; move branch decision earlier (ID stage); branch prediction (predict not-taken, or dynamic 2-bit predictors) and flush on misprediction; delayed branch (the instruction after the branch always executes).
Q6 – Mapping main memory to a direct-mapped cache
In a direct-mapped cache every main-memory block can be placed in exactly one cache line:
cache line = (block address) mod (number of lines in cache)
The memory address is divided into three fields:
| Tag | Line (index) | Word / byte offset |
t bits r bits w bits
block size = 2^w bytes, lines = 2^r, tag = address bits - r - w- Offset selects the byte/word inside the block.
- Index selects the cache line.
- Tag is stored with the line and compared to tell which of the many blocks that share the line is present. A valid bit shows whether the line holds data.
Access: the index selects a line; if valid = 1 and stored tag = address tag → hit, the offset selects the word. Otherwise → miss: the block is fetched from memory, placed in that line (replacing the old block – no choice is needed), tag and valid bit are updated.
Example: 32-bit byte address, cache of 1024 lines, 16-byte blocks. Offset = 4 bits, index = 10 bits, tag = 32 − 10 − 4 = 18 bits. Address 0x0001_2344 → offset = 4, index = (0x12344 >> 4) mod 1024 = 0x234 = 564, tag = 0x12344 >> 14 = 4.
Small example: 8 lines, 1-word blocks. Word addresses 22 (10110) → line 110 = 6, tag 10; 26 (11010) → line 010 = 2, tag 11; 18 (10010) → line 010 = 2, tag 10 – it replaces block 26, a conflict miss.
Pros: simple, cheap, fast (one tag comparison). Cons: blocks that map to the same line thrash, so the miss rate is higher than in set-associative caches.
