Blog
Computer Architecture Past Paper 2015 – BSCS 4th Term UOS (CS-4240)
University of Sargodha BS Computer Science 4th term Computer Architecture (CS-4240) paper, 2015 exam (80 marks, 2:30 hours). The paper is retyped below without the handwritten notes, followed by solved answers for all 16 short questions and all six subjective questions: cache mapping and replacement, multiprocessors, memory hierarchy, ILP and precise exceptions, addressing modes and the 8086 architecture.
Solved answers
Q1 – Short answers (16 × 2)
1. Write-back policy. On a write hit only the cache block is updated and marked dirty. Main memory is updated later, when that dirty block is replaced. This saves memory traffic but memory can be temporarily out of date.
2. Amdahl’s Law. The overall speed-up from improving one part of a system is limited by the fraction of time that part is used: Speedup = 1 / ((1 − f) + f / s), where f is the fraction enhanced and s is the speed-up of that part. Example: f = 0.5, s = 10 gives only 1/(0.5 + 0.05) ≈ 1.82.
3. Moore’s law. Gordon Moore’s observation (1965, revised 1975) that the number of transistors on an integrated circuit doubles about every two years, giving steadily faster and cheaper processors and memory.
4. Principle of locality. Programs access a small part of their address space at a time. Temporal locality: an item used now will probably be used again soon (loop variables). Spatial locality: items near a used address will probably be used soon (arrays, sequential code). Caches work because of locality.
5. Measuring performance. Performance = 1 / execution time. CPU time = Instruction count × CPI × clock cycle time (= IC × CPI / clock rate). Other measures are MIPS, MFLOPS, throughput, response time and standard benchmarks such as SPEC.
6. Quantitative measurement of a computer system. Describing performance with numbers rather than opinion: execution time, CPI, clock rate, MIPS/FLOPS, throughput, speed-up (Amdahl’s law), cache hit/miss rate, average memory access time, power and cost – usually measured with benchmark programs.
7. Three issues in an instruction pipeline. (1) Structural hazards – two instructions need the same hardware at once; (2) data hazards – an instruction needs a result not yet produced; (3) control hazards – branches change the flow before the target is known. Exceptions and unequal stage delays are further issues.
8. Exceptions in pipelining. Unexpected events (arithmetic overflow, undefined opcode, page fault, I/O interrupt) that occur while several instructions are in flight. The pipeline must stop the faulting instruction, flush the instructions after it, let the earlier ones complete, save the PC (in EPC) and jump to the handler – so that exceptions are precise.
9. Where is cache in the memory hierarchy? Between the CPU registers and main memory (RAM). Level 1 cache is inside the CPU core, L2 is per core or shared, and L3 is shared on the chip; below them come main memory and then secondary storage.
10. “Each block of main memory maps to only a single line of cache.” True for a direct-mapped cache (line = block number mod number of lines). It is false for associative and set-associative caches, where a block can go in any line or any line of a set. Diagram (direct mapped, 4 lines): blocks 0, 4, 8, 12 → line 0; blocks 1, 5, 9, 13 → line 1; blocks 2, 6, 10, 14 → line 2; blocks 3, 7, 11, 15 → line 3.
11. Write-through policy. Every write updates both the cache and main memory at the same time, so memory is always consistent. It causes more memory traffic, so a write buffer is normally used.
12. MIMD. Multiple Instruction stream, Multiple Data stream (Flynn’s taxonomy): several processors each run their own program on their own data at the same time – multicore CPUs, multiprocessors and clusters.
13. Vector vs scalar data. Scalar data is a single value processed by one instruction (one add of two numbers). Vector data is an array of values; one vector instruction operates on all elements at once (add two 64-element vectors), which is used by vector processors and SIMD units.
14. Threads. A thread is the smallest unit of execution scheduled by the OS – a sequence of instructions with its own PC, registers and stack, sharing the code, data and files of its process with other threads.
15. Can threads improve performance? Yes. On multicore or hyper-threaded processors threads run in parallel; even on one core, one thread can run while another waits for I/O or memory. Gains are limited by the serial part (Amdahl’s law) and by synchronisation overhead.
16. Predictions. Guessing an outcome before it is known so the pipeline does not stall – mainly branch prediction (static: always not-taken; dynamic: 1-bit/2-bit predictors, branch target buffer). Value and memory-dependence prediction are other kinds. Wrong guesses are flushed.
Q2 – Cache memory, mapping functions and replacement algorithms
Cache memory is a small, very fast SRAM memory placed between the CPU and main memory. It keeps copies of recently used blocks of main memory. When the CPU needs a word it checks the cache first: if present it is a hit; otherwise it is a miss and the whole block is brought from memory into a cache line. Performance is measured by hit ratio and average memory access time: AMAT = hit time + miss rate × miss penalty.
Main memory address is split into fields: tag, line/set index and word (offset). Each cache line stores a tag, a valid bit (and a dirty bit for write-back) and the data block.
Mapping functions
- Direct mapping – each block goes to exactly one line: line = block number mod number of lines. Address = tag | line | word. Simple and fast (one comparison), but two blocks that map to the same line keep replacing each other (conflict misses).
- Associative mapping – a block can be placed in any line. Address = tag | word. All tags are compared in parallel. Most flexible, fewest conflict misses, but expensive comparison hardware.
- Set-associative mapping – the cache is divided into sets of k lines (k-way). A block maps to one set (set = block number mod number of sets) and may go in any line of that set. Address = tag | set | word. A practical compromise; 2-, 4- and 8-way caches are common.
Example: 64 KB cache, 4-byte blocks, 16 MB memory (24-bit address). Direct mapped: 16K lines → word 2 bits, line 14 bits, tag 8 bits. Fully associative: tag 22 bits, word 2 bits. 2-way set associative: 8K sets → word 2, set 13, tag 9 bits.
Replacement algorithms (needed for associative and set-associative caches when the set is full):
- LRU (Least Recently Used) – replace the line not used for the longest time; best in practice, uses usage bits/counters.
- FIFO – replace the line that has been in the cache longest (round-robin pointer).
- LFU (Least Frequently Used) – replace the line with the fewest references (counter per line).
- Random – pick any line at random; cheap and only slightly worse than LRU.
Write policies (write-through, write-back) and multilevel caches (L1, L2, L3) complete the cache design.
Q3 – Multiprocessor
A multiprocessor is a computer with two or more CPUs (or cores) that share main memory and I/O under one operating system, to increase throughput, speed up parallel programs and improve reliability. In Flynn’s taxonomy it is an MIMD machine.
Types by memory organisation
- Shared-memory (tightly coupled) – UMA/SMP: all processors share one memory through a bus or crossbar with equal access time. Easy to program; the bus limits scaling to a few tens of processors.
- NUMA (Distributed shared memory): each node has local memory but all memory is in one address space; local access is faster than remote access. Scales to hundreds of processors.
- Distributed-memory (loosely coupled) / message passing: each processor has private memory and processors communicate by sending messages (clusters, MPI).
Interconnection networks: shared bus, crossbar switch, multistage networks (Omega), mesh, ring, hypercube.
Key issues
- Cache coherence – several caches may hold copies of the same block; a write by one CPU must be seen by others. Solved by snooping protocols (MSI/MESI, write-invalidate) on buses and directory-based protocols in NUMA.
- Synchronisation – locks, semaphores and barriers built on atomic instructions (test-and-set, compare-and-swap, LL/SC).
- Memory consistency – rules about the order in which writes become visible (sequential consistency, relaxed models).
- Load balancing and scheduling of threads across processors.
Advantages: higher throughput, parallel speed-up, fault tolerance (graceful degradation), cost sharing of memory and I/O. Disadvantages: complex hardware and OS, coherence and synchronisation overhead, speed-up limited by Amdahl’s law. Multicore chips (Intel Core i7, AMD Ryzen) are today’s common multiprocessors.
Q4 – Memory hierarchy design
No single memory is fast, large and cheap at the same time, so computers use a hierarchy: small, fast, costly memories near the CPU and large, slow, cheap memories further away. Because of locality most accesses are satisfied by the upper levels, so the system appears almost as fast as the top level and as large as the bottom level.
| Level | Technology | Typical size | Access time |
|---|---|---|---|
| Registers | flip-flops in CPU | < 1 KB | < 1 ns |
| L1 cache | SRAM on core | 32–64 KB | ~1 ns |
| L2 / L3 cache | SRAM on chip | 256 KB – 32 MB | 3–15 ns |
| Main memory | DRAM | 4–64 GB | 50–100 ns |
| Secondary storage | SSD / magnetic disk | 0.5–4 TB | 0.1 ms – 10 ms |
| Tertiary/backup | tape, optical, cloud | very large | seconds |
Design issues (the four questions for each level):
- Block placement – where can a block be placed? (direct mapped, set-associative, fully associative).
- Block identification – how is a block found? (tag, index, offset; valid bit; page tables and TLB for virtual memory).
- Block replacement – which block is replaced on a miss? (LRU, FIFO, random).
- Write strategy – write-through or write-back, with write-allocate or no-write-allocate.
Performance: AMAT = Hit timeL1 + Miss rateL1 × (Hit timeL2 + Miss rateL2 × Miss penaltyL2). Designers reduce miss rate (larger blocks, larger caches, higher associativity, prefetching), miss penalty (multilevel caches, critical word first, write buffers) and hit time (small simple L1, virtual-indexed caches).
Virtual memory is the level between main memory and disk: programs use virtual addresses, translated by page tables and a TLB, with pages loaded on demand. It gives each process a large private address space and protection.
Q5 – Instruction-level parallelism and dynamic handling of exceptions
Instruction-level parallelism (ILP) is the overlap of execution of independent instructions within one program. Techniques that exploit ILP:
- Pipelining – different stages of several instructions run at once.
- Superscalar execution – several instructions are issued per clock to multiple functional units.
- Dynamic scheduling (out-of-order execution) – Tomasulo’s algorithm with reservation stations and register renaming lets an instruction run as soon as its operands are ready, removing WAR/WAW hazards.
- Branch prediction and speculation – instructions after a predicted branch are executed speculatively.
- Static techniques – loop unrolling, compiler scheduling, VLIW.
Limits on ILP are true data dependences, control dependences, limited registers and functional units, and memory latency.
Dynamic handling of exceptions. With out-of-order and speculative execution an instruction may raise an exception (overflow, page fault, divide by zero) before older instructions have finished, or may itself be on a wrongly predicted path. To keep exceptions precise the processor uses a reorder buffer (ROB):
- Instructions are issued in order and given a ROB entry; they execute out of order and write results into the ROB, not into the registers.
- If an instruction causes an exception, the exception is only recorded in its ROB entry.
- Instructions commit (retire) in program order. When the faulting instruction reaches the head of the ROB, the exception is raised: all later instructions in the ROB are flushed, the PC of the faulting instruction is saved (EPC) and control passes to the handler.
- If the instruction was on a mispredicted path it is flushed before it reaches the head, so its exception is never raised.
This way the machine state looks exactly as if instructions had executed one at a time, so the OS can handle the exception and restart the program.
Q6 – Five addressing modes
| Mode | Meaning | Example |
|---|---|---|
| Immediate | Operand is a constant inside the instruction | MOV AX, 5 / addi $t0,$t1,5 |
| Register | Operand is in a CPU register | ADD AX, BX |
| Direct (absolute) | Instruction holds the memory address of the operand | MOV AX, [1000h] |
| Register indirect | A register holds the address of the operand | MOV AX, [BX] |
| Indexed / base + displacement | EA = register + constant offset | lw $t0, 8($s1), MOV AX, [SI+4] |
| Indirect (memory) | Memory word holds the address of the operand | EA = M[1000h] |
| PC-relative | EA = PC + offset; used for branches | beq $t0,$t1,label |
Immediate is fastest (no memory access) but limited in size; register is fast with short instructions; direct is simple but the address field limits the range; register indirect and indexed suit pointers and arrays; PC-relative gives position-independent code.
Q7 – Architecture of a processor (8086)
The Intel 8086 is a 16-bit processor with a 20-bit address bus (1 MB). It is divided into two units that work in parallel (an early form of pipelining):
+------------------------- BIU (Bus Interface Unit) -------------------------+
| Segment registers: CS DS SS ES IP (instruction pointer) |
| Address adder (segment x 16 + offset -> 20-bit physical address) |
| 6-byte instruction queue <-- prefetch --> bus control <--> system bus |
+----------------------------------+-----------------------------------------+
| instruction bytes
+----------------------------------v------- EU (Execution Unit) ------------+
| Control unit / instruction decoder |
| General registers: AX(AH,AL) BX(BH,BL) CX(CH,CL) DX(DH,DL) |
| Pointer/index: SP BP SI DI ALU (16-bit) FLAGS register |
+----------------------------------------------------------------------------+- BIU fetches instructions and data, computes 20-bit physical addresses (segment × 16 + offset) and keeps a 6-byte prefetch queue filled while the EU executes.
- EU decodes and executes instructions using the ALU, general-purpose registers (AX, BX, CX, DX), pointer and index registers (SP, BP, SI, DI) and the flag register (CF, PF, AF, ZF, SF, TF, IF, DF, OF).
- Because the BIU prefetches while the EU executes, fetch and execute overlap and the processor is faster than a strictly sequential design.
