Computer Organization
Cache & Memory
Compute #lines/#sets first, split the address, then AMAT — this is the highest-yield COA numerical block.
GATE CSE cache and memory hierarchy — locality, mapping, address split, AMAT, write policies, miss types, with samples and drills.
Learning outcomes
- Compute lines, sets, and tag/index/offset field widths for direct and set-associative caches
- Solve AMAT numericals (including simple multilevel wording) with consistent units
- Explain write-through vs write-back and write-allocate vs no-write-allocate
- Classify compulsory, capacity, and conflict misses and map a block to a line/set
Memory hierarchy & locality
- Hierarchy (fast/small → slow/large): registers → L1/L2/L3 cache → main memory (DRAM) → secondary storage. Each level caches a subset of the level below.
- Temporal locality: a recently accessed location is likely to be accessed again soon (loops, stack slots).
- Spatial locality: nearby addresses are likely to be used soon (arrays, sequential code) — this justifies multi-byte cache blocks.
- GATE assumes byte-addressable memory unless the stem says word-addressable — that choice changes how addresses are counted and how offset bits are computed.
- SRAM builds caches (fast, expensive, lower density). DRAM builds main memory (slower, denser, needs refresh).
Worked example
Problem. Why does increasing block size often help spatial locality but can hurt miss penalty?
Solution. Larger blocks bring more neighbouring bytes on a miss (good for spatial locality) but transfer more data from memory (higher miss penalty / bus traffic).
Cache geometry & mapping
- Cache capacity C, block (line) size B → number of lines L = C / B. Always compute L before indexing.
- Direct-mapped: each memory block maps to exactly one line. Index = (block address) mod L. High conflict-miss risk.
- Fully associative: a block may sit in any line; search compares tags widely. No conflict misses from set contention; capacity/compulsory remain.
- k-way set-associative: L lines grouped into S = L / k sets; a block maps to one set and may occupy any of the k ways in that set.
- Physical address split: [ tag | set-index | block-offset ]. Offset bits = log2(B). Index bits = log2(S) (or log2(L) if direct-mapped). Tag = remaining high bits.
- Block address = floor(byte_address / B). Use block address for mod indexing; use byte address when extracting bit fields.
- On a miss in associative caches, a replacement policy chooses a victim: LRU, FIFO, or random. GATE stems often assume LRU unless stated.
Formulas to remember
-
#lines L = Capacity / BlockSize -
#sets S = L / Associativity -
Offset bits = log2(BlockSize) -
Index bits = log2(#sets) -
Tag bits = AddressBits − IndexBits − OffsetBits -
AMAT = HitTime + MissRate × MissPenalty
| Mapping | Where a block can go | Conflict risk |
|---|---|---|
| Direct-mapped | 1 fixed line | High |
| k-way set assoc. | k lines in one set | Medium |
| Fully associative | Any line | None from set conflicts |
Worked example
Problem. 32 KB direct-mapped, 32 B blocks. Address 0x3E8 — line index?
Solution. L = 32KB/32B = 1024. Block addr = 0x3E8/32 = 31. Index = 31 mod 1024 = 31.
Miss types, AMAT & write policies
- Compulsory (cold) miss: first reference to a block. Capacity miss: working set larger than the cache. Conflict miss: set contention in direct/set-associative caches even when capacity remains.
- AMAT = HitTime + MissRate × MissPenalty. HitTime is paid on every access; MissPenalty is added only on misses. Keep ns vs cycles consistent.
- MissRate = misses / accesses. HitRate = 1 − MissRate. Sometimes stems give miss penalty in memory cycles — convert using clock period if needed.
- Multilevel caches: expand using the stem’s definition of local vs global miss rates. A common form: AMAT = HitTime_L1 + MissRate_L1 × (HitTime_L2 + MissRate_L2 × MissPenalty_Mem).
- Write-through: every store updates lower-level memory (often with a write buffer). Write-back: stores update the cache line and set a dirty bit; memory is updated on dirty eviction/flush.
- Write miss policy: write-allocate brings the block into cache then writes; no-write-allocate writes around to memory without allocating a line.
Worked example
Problem. HitTime=1 ns, MissRate=0.1, MissPenalty=20 ns. AMAT?
Solution. AMAT = 1 + 0.1×20 = 3 ns.
GATE exam tricks — cache
- Always compute #lines and #sets before touching address bit fields.
- Offset bits from block size; index bits from #sets (or #lines if direct). Tag gets whatever bits remain.
- AMAT traps: forgetting HitTime; treating MissPenalty as total time instead of the extra miss cost; mixing ns with cycles.
- Raising associativity reduces conflict misses; it does not remove compulsory or capacity misses.
- Dirty bit appears with write-back — write-through typically does not need dirty tracking the same way.
- If the stem gives “words” and word size, convert everything to bytes before log2.
- Direct-mapped is 1-way set-associative; fully associative is one set with L ways.
Worked example
Problem. 32 KB, 64 B blocks, 4-way. Index bits?
Solution. L=512, S=512/4=128, index bits = log2(128)=7.
Exam tips
- Geometry first: C, B, associativity → lines/sets.
- AMAT: same time unit everywhere.
- Write-back ≠ write-through — dirty bit matters.
- Block address = byte_address / B before mod indexing.
- State miss type before blaming mapping.
10 GATE-style MCQs for Cache & Memory with full explanations. Attempt each question before revealing the answer.
16 KB direct-mapped cache, 32 B blocks — number of lines?
Show answer & explanation
Answer: B
16×1024/32 = 512.
AMAT with HitTime=1 ns, MissRate=0.1, MissPenalty=20 ns:
Show answer & explanation
Answer: B
1 + 0.1×20 = 3.
k-way set-associative with L lines has sets:
Show answer & explanation
Answer: B
#sets = L/k.
64 B block needs how many offset bits?
Show answer & explanation
Answer: C
log2(64)=6.
Write-back updates main memory:
Show answer & explanation
Answer: B
Dirty eviction/flush writes back.
Spatial locality means:
Show answer & explanation
Answer: B
Nearby addresses.
Direct-mapped: block 20 with 8 lines maps to line:
Show answer & explanation
Answer: B
20 mod 8 = 4.
Address fields in set-associative cache:
Show answer & explanation
Answer: B
Standard split.
Fully associative conflict misses:
Show answer & explanation
Answer: B
No set-conflict; capacity/compulsory still exist.
HitTime is paid:
Show answer & explanation
Answer: B
Every reference pays hit time; misses add penalty.
Extra page-specific practice beyond the GATE samples. Attempt all 10, then reveal answers to check your reasoning.
32 KB cache, 64 B blocks, 4-way. Number of sets:
Show answer & explanation
Answer: B) 128
Lines=512; sets=512/4=128.
HitTime=2 ns, MissRate=4%, MissPenalty=50 ns. AMAT:
Show answer & explanation
Answer: A) 4 ns
2 + 0.04×50 = 4.
log2(128) offset bits for 128 B blocks:
Show answer & explanation
Answer: C) 7
2^7=128.
Compulsory miss means:
Show answer & explanation
Answer: A) First reference to a block
Cold miss.
Write-allocate on write miss typically:
Show answer & explanation
Answer: B) Brings block into cache then writes
Allocate line on write miss.
Index bits for 128 sets:
Show answer & explanation
Answer: C) 7
log2(128)=7.
Dirty bit is used in:
Show answer & explanation
Answer: B) Write-back caches
Marks modified lines.
Capacity miss happens when:
Show answer & explanation
Answer: A) Cache too small for working set
Working set exceeds cache size.
Byte-addressable with 32 B blocks: offset bits:
Show answer & explanation
Answer: B) 5
log2(32)=5.
Tag bits identify:
Show answer & explanation
Answer: B) Which memory block sits in the line/set
Tag match decides hit.
Topic FAQs
What is AMAT?
Average Memory Access Time = HitTime + MissRate × MissPenalty.
How many sets in a k-way cache with L lines?
L / k.
What is a conflict miss?
A miss caused by too many blocks mapping to the same set/line even though the cache still has free capacity elsewhere.
When is main memory updated in a write-back cache?
When a dirty line is evicted or explicitly flushed — not on every store hit.
Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.