Skip to main content

Computer Organization

Cache & Memory

Compute #lines/#sets first, split the address, then AMAT — this is the highest-yield COA numerical block.

~4 hrs ~2–3 marks · Must-score 10 samples · 10 practice · 10 cards

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).

Memory hierarchy pyramid from registers and cache down to secondary storage
Temporal + spatial locality justify why caches and multi-byte blocks exist.

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
MappingWhere a block can goConflict risk
Direct-mapped1 fixed lineHigh
k-way set assoc.k lines in one setMedium
Fully associativeAny lineNone 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.

Cache address split into tag index and block-offset fields with bit formulas
Compute #lines and #sets before touching tag/index/offset widths.

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.

AMAT formula HitTime plus MissRate times MissPenalty with example
HitTime every access; MissPenalty only on misses — keep units consistent.

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.

Direct-mapped set-associative and fully associative cache placement comparison
More associativity cuts conflict misses — not compulsory or capacity.

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.
Exam pattern · 10 questions

GATE sample questions & answers

10 GATE-style MCQs for Cache & Memory with full explanations. Attempt each question before revealing the answer.

Q1 Cache geometry

16 KB direct-mapped cache, 32 B blocks — number of lines?

  • A) 256
  • B) 512
  • C) 1024
  • D) 2048
Show answer & explanation

Answer: B

16×1024/32 = 512.

Q2 AMAT

AMAT with HitTime=1 ns, MissRate=0.1, MissPenalty=20 ns:

  • A) 2 ns
  • B) 3 ns
  • C) 21 ns
  • D) 1.1 ns
Show answer & explanation

Answer: B

1 + 0.1×20 = 3.

Q3 Sets

k-way set-associative with L lines has sets:

  • A) L×k
  • B) L/k
  • C) k/L
  • D) L+k
Show answer & explanation

Answer: B

#sets = L/k.

Q4 Offset

64 B block needs how many offset bits?

  • A) 4
  • B) 5
  • C) 6
  • D) 8
Show answer & explanation

Answer: C

log2(64)=6.

Q5 Write-back

Write-back updates main memory:

  • A) On every store hit
  • B) When a dirty line is evicted/flushed
  • C) Never
  • D) Only on I-fetch
Show answer & explanation

Answer: B

Dirty eviction/flush writes back.

Q6 Locality

Spatial locality means:

  • A) Reuse same address soon
  • B) Use nearby addresses soon
  • C) Only cold misses
  • D) No locality
Show answer & explanation

Answer: B

Nearby addresses.

Q7 Mapping

Direct-mapped: block 20 with 8 lines maps to line:

  • A) 2
  • B) 4
  • C) 5
  • D) 0
Show answer & explanation

Answer: B

20 mod 8 = 4.

Q8 Address split

Address fields in set-associative cache:

  • A) only tag
  • B) tag | index | offset
  • C) only offset
  • D) only PC
Show answer & explanation

Answer: B

Standard split.

Q9 Miss types

Fully associative conflict misses:

  • A) Highest possible
  • B) None due to set conflicts (capacity/compulsory remain)
  • C) Only write misses
  • D) Infinite
Show answer & explanation

Answer: B

No set-conflict; capacity/compulsory still exist.

Q10 AMAT intuition

HitTime is paid:

  • A) Only on misses
  • B) On every access (hit path)
  • C) Only on DMA
  • D) Never
Show answer & explanation

Answer: B

Every reference pays hit time; misses add penalty.

Topic drill · 10 questions

10 practice questions — Cache & Memory

Extra page-specific practice beyond the GATE samples. Attempt all 10, then reveal answers to check your reasoning.

P1

32 KB cache, 64 B blocks, 4-way. Number of sets:

  • A) 64
  • B) 128
  • C) 256
  • D) 512
Show answer & explanation

Answer: B) 128

Lines=512; sets=512/4=128.

P2

HitTime=2 ns, MissRate=4%, MissPenalty=50 ns. AMAT:

  • A) 4 ns
  • B) 2.08 ns
  • C) 2.4 ns
  • D) 52 ns
Show answer & explanation

Answer: A) 4 ns

2 + 0.04×50 = 4.

P3

log2(128) offset bits for 128 B blocks:

  • A) 5
  • B) 6
  • C) 7
  • D) 8
Show answer & explanation

Answer: C) 7

2^7=128.

P4

Compulsory miss means:

  • A) First reference to a block
  • B) Conflict only
  • C) Disk error
  • D) DMA fault
Show answer & explanation

Answer: A) First reference to a block

Cold miss.

P5

Write-allocate on write miss typically:

  • A) Skips cache
  • B) Brings block into cache then writes
  • C) Formats disk
  • D) Clears TLB only
Show answer & explanation

Answer: B) Brings block into cache then writes

Allocate line on write miss.

P6

Index bits for 128 sets:

  • A) 5
  • B) 6
  • C) 7
  • D) 8
Show answer & explanation

Answer: C) 7

log2(128)=7.

P7

Dirty bit is used in:

  • A) Write-through only
  • B) Write-back caches
  • C) ROM
  • D) Only registers
Show answer & explanation

Answer: B) Write-back caches

Marks modified lines.

P8

Capacity miss happens when:

  • A) Cache too small for working set
  • B) Only first access
  • C) Only bus error
  • D) Only page fault
Show answer & explanation

Answer: A) Cache too small for working set

Working set exceeds cache size.

P9

Byte-addressable with 32 B blocks: offset bits:

  • A) 4
  • B) 5
  • C) 6
  • D) 8
Show answer & explanation

Answer: B) 5

log2(32)=5.

P10

Tag bits identify:

  • A) Byte in block only
  • B) Which memory block sits in the line/set
  • C) Only CPI
  • D) Disk cylinder
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.

Quick revision · 10 cards

10 flashcards — Cache & Memory

Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.

Q1 What is the formula for Average Memory Access Time (AMAT)?
Q2 In a k-way set-associative cache with L lines, how many sets are there?
Q3 How many block-offset bits are required for a cache block of size B bytes?
Q4 How does write-through differ from write-back when updating main memory?
Q5 What is temporal locality in the context of memory hierarchy?
Q6 What is spatial locality in the context of caches?
Q7 How do you compute the line index for a block in a direct-mapped cache?
Q8 How is a physical address split for a set-associative cache lookup?
Q9 How do you compute the number of cache lines from capacity and block size?
Q10 Why is SRAM typically used to build CPU caches instead of DRAM?