Cache Placement Example Calculations
You'll learn: How to work through real examples that show exactly where memory addresses land in direct-mapped, set-associative, and fully associative caches.
Why Practice Matters
You've learned the theory behind three cache organizations. Now it's time to practice the math—calculating which cache location stores a given memory address. This skill is essential for understanding cache behavior and debugging performance issues.
Working Through Examples
Let's say you have a 32-byte cache with 8-byte cache lines (so 4 lines total) and you need to place address 0x2C (44 in decimal).
Direct-Mapped Cache
- Block offset: Last 3 bits (log₂(8) = 3):
0x2C & 0x7 = 4 - Index: Next 2 bits (log₂(4) = 2):
(0x2C >> 3) & 0x3 = 1 - Tag: Remaining bits:
0x2C >> 5 = 1
Address 0x2C must go in line 1.
Fully Associative Cache
- Block offset: Last 3 bits:
4 - Tag: All remaining bits:
0x2C >> 3 = 5 - Index: None
Address 0x2C can go in any of the 4 lines.
2-Way Set-Associative Cache
- Block offset: Last 3 bits:
4 - Set index: Next 1 bit (log₂(2) = 1):
(0x2C >> 3) & 0x1 = 1 - Tag: Remaining bits:
0x2C >> 4 = 2
Address 0x2C can go in either line in set 1.
The Pattern
Calculate: block offset → index/set → tag. The associativity determines how flexible placement is.
Key Takeaway: Cache placement calculations involve bit-masking and shifting addresses to extract offset, index, and tag fields—with direct-mapped having one choice, fully associative having all choices, and set-associative offering limited flexibility within a set.