Finding a name in an unsorted list means reading each entry. If the list could be asked, "who has this name?", and every entry answered at once, the search would be immediate. An associative memory does exactly this. A cache uses a small, quick memory so that the processor does not often wait for the main memory.

Associative Memory

An associative memory, or content-addressable memory (CAM), is found by the content of the data, not by an address. The word to be found is placed in the argument register. The key register has a mask, with 1 in the bits to be compared. Every word in the memory is compared with the argument at the same time, and the words that match set their bit in the match register. A search that takes many steps in ordinary memory takes one step here, but the hardware is costly.

Definition: Associative memory. A memory in which data is accessed by its content rather than by an address, all stored words being compared with the search argument at the same time.

Cache Memory

Cache memory is a small, fast memory placed between the processor and main memory. It holds the portions of the program and data that are being used. When the processor needs a word, it looks in the cache first. If the word is found, this is a hit, and if not it is a miss and the word is fetched from main memory along with its neighbours. The hit ratio is the number of hits divided by the total number of accesses.

Mapping and Writing

The correspondence between main memory blocks and cache lines is called mapping. In direct mapping each block can go to only one line, chosen by the index part of the address. In associative mapping a block may go into any line. In set-associative mapping a block can go into any line of one set. When the processor writes, the write-through policy updates the cache and main memory together, and the write-back policy updates main memory only when the line is replaced.

Table 4.11: Cache mapping methods

Method Placement Cost Flexibility
Direct One fixed line Low Low
Associative Any line High High
Set-associative Any line in one set Medium Medium

Example 4.10: Effective access time with a cache

Problem: The cache access time is 10 ns and the main memory access time is 100 ns. Of 1000 accesses 950 are hits. Find the hit ratio and the average access time.

Solution:

  1. Hit ratio h = 950 / 1000 = 0.95.
  2. Average access time = h × tc + (1 - h) × tm.
  3. Time = 0.95 × 10 + 0.05 × 100 = 9.5 + 5 = 14.5 ns.

Result: The hit ratio is 0.95 and the average access time is 14.5 ns, far below 100 ns.

h, tc, tm = 950 / 1000, 10, 100
print(h, round(h * tc + (1 - h) * tm, 1))

Output

0.95 14.5

Common mistakes

  • Believing that a cache holds the whole program. It holds only the active parts.
  • Adding the cache time to the main memory time on every access.
  • Confusing a hit, which is found in cache, with a miss.

Key takeaways

  • Associative memory is searched by content, all words at once.
  • A hit is found in the cache; a miss is fetched from main memory.
  • Direct, associative and set-associative are the mapping methods.
  • Average access time = h x tc + (1 - h) x tm.

Practice questions

  1. What is a content-addressable memory?
  2. Define hit ratio.
  3. What is the difference between write-through and write-back?

Answers to the practice questions

  1. A memory that is searched by the content of the data, comparing all words at the same time.
  2. The number of cache hits divided by the total number of memory accesses.
  3. Write-through updates cache and main memory together; write-back updates main memory only when the line is replaced.

Sources

  • Official BCA syllabus Computer Architecture, course code 25BCA401DS02, Unit IV