5.4.2 · D3Memory Hierarchy & Caches

Worked examples — Cache organization (direct-mapped)

2,567 words12 min readBack to topic

You already met the parking-garage picture in Cache organization (direct-mapped): every memory address has one cache line it is allowed to live in. This page does the boring-but-essential work: we take that one rule and hit it with every kind of input until nothing can surprise you in an exam.

Before we start, the three words we will use constantly, each earned from the address itself.

See the address being sliced:

Figure — Cache organization (direct-mapped)

If any word here feels shaky, the parent note derives each field from scratch; and Memory Addressing explains why addresses are counted in bytes at all.


The scenario matrix

A direct-mapped cache is small, so the list of qualitatively different things that can happen is finite. Here it is — every worked example below is tagged with the cell(s) it covers.

Cell What makes it different Covered by
A. Cold access First-ever touch, valid bit = 0 Ex 1
B. Hit Line valid, tag matches Ex 1, Ex 2
C. Same block, different offset Two addresses, same index+tag, different byte Ex 2
D. Conflict miss / thrash Same index, different tag, alternating Ex 3
E. Degenerate: block size = 1 , no offset field at all Ex 4
F. Degenerate: one line , everything maps to line 0 Ex 4
G. Boundary: block-crossing access A read straddling two blocks Ex 5
H. Wrap-around of index Block landing back on line 0 Ex 6
I. Parameter derivation Given sizes → find Ex 7
J. Real-world word problem Loop over an array, count misses Ex 8
K. Exam twist Change one parameter, predict effect Ex 9

We keep the same tiny cache for Examples 1–3 and 5–6 so the numbers stay familiar.


The worked examples

Example 1 — Cold access, then a hit (cells A, B)

Example 2 — Same block, different offset (cells B, C)

Example 3 — Conflict miss and thrashing (cell D)

Figure — Cache organization (direct-mapped)

Example 4 — Two degenerate caches (cells E, F)

Example 5 — Block-crossing access (cell G)

Example 6 — Index wrap-around (cell H)

Figure — Cache organization (direct-mapped)

Example 7 — Parameter derivation (cell I)

Example 8 — Real-world word problem (cell J)

Example 9 — Exam twist: change one knob (cell K)


Recall Quick self-test on the whole matrix

Which field decides hit vs miss? ::: Tag and Index together (with the valid bit); the offset never does. Why can a cache with empty lines still miss every time? ::: Direct-mapping forces colliding blocks onto one line (conflict/thrash), ignoring free lines — cell D. If block size byte, which field disappears? ::: The offset () — cell E. Double block size, hold cache size fixed: what happens to the tag? ::: It stays the same; offset , index cancel — cell K. A 4-byte word read at 0x1F needs how many block fetches? ::: Two — it straddles a block boundary (cell G).

Related deeper dives: Cache Replacement Policies (what to evict when lines are shared — trivial for direct-mapped, only one candidate!), Cache Write Policies (what happens on a write hit/miss), and Set-Associative Caches (the cure for the thrashing in Example 3).