1. Gyanastra
  2. Computer Organisation and Architecture
  3. Direct Mapped Cache
  4. Direct Mapping of Main Memory Blocks to Cache Lines
Free lesson, no account needed 9 min read 11 practice questions after it
Asked in past papers7 times
  • GATE CSE2026202220082007
  • ISRO20172011
  • UGC NET2018

Computer Organisation and Architecture · Direct Mapped Cache

Direct Mapping of Main Memory Blocks to Cache Lines

Overview

Mapping is the rule that decides which cache line a main-memory block is placed in.
Because main memory has far more blocks than the cache has lines (16 blocks vs 4 lines in the example), not all blocks can be held at once, so a fixed placement rule is needed.
Direct mapping is the simplest such rule: each block is permanently assigned to exactly one cache line by a modulo (round-robin) scheme.

Key concepts

  • Mapping: the rule that fixes the position a main-memory block may occupy whenever it is brought into the cache.
  • Direct mapping: each main-memory block is permanently assigned to exactly one cache line, decided by a round-robin (modulo) rule.
  • Block number: the upper 4 bits of the 6-bit physical address; identifies which main-memory block is referenced.
  • Block offset: the lower 2 bits of the address; identifies the word inside the block (block size = 4 words, so 2 offset bits).
  • Cache lines: the cache has 4 lines, numbered line 0 to line 3.
  • Many-to-one function: several main-memory blocks map to the same cache line; the assignment does not mean all 16 blocks sit in the cache at once.
    Whenever block 0 is fetched it must go into line 0, and whenever block 4 is fetched it must also go into line 0 (likewise 8 and 12).
  • Tag: the remaining upper bits of the block number, stored to identify which of the several blocks mapped to the same line is currently in that line.
  • Line selection rule: the two least significant bits of the block-number part (not of the whole address) directly give the cache line number.
Memory diagram: Direct Mapping of Main Memory Blocks to Cache Lines
Figure. Direct Mapping of Main Memory Blocks to Cache Lines

How it works

  1. Split the 6-bit physical address into a 4-bit block number (upper bits) and a 2-bit block offset (lower bits).
  2. Number the 4 cache lines 0, 1, 2, 3.
  3. Assign blocks round-robin: block 0 → line 0, block 1 → line 1, block 2 → line 2, block 3 → line 3, then block 4 → line 0 again, block 5 → line 1, and so on up to block 15.
  4. The resulting many-to-one picture:
    • Line 0 ← blocks 0, 4, 8, 12
    • Line 1 ← blocks 1, 5, 9, 13
    • Line 2 ← blocks 2, 6, 10, 14
    • Line 3 ← blocks 3, 7, 11, 15
  5. Write the 4-bit block numbers in binary and read the two least significant bits of the block number:
Block numberBinary (4 bits)Two LSBs of block numberCache line
00000000
10001011
20010102
30011113
40100000
50101011
60110102
70111113
81000000
91001011
101010102
111011113
121100000
131101011
141110102
151111113
  1. Blocks 0, 4, 8, 12 all have 00 in the two least significant bits of their block number, and only these four blocks do, so they always occupy line 0. Blocks 1, 5, 9, 13 have 01 and only they do → line 1.
    Blocks 2, 6, 10, 14 have 10 → line 2. Blocks 3, 7, 11, 15 have 11 → line 3.
  2. The organization is convenient because the address splits mechanically: the least 2 bits are the block offset, the remaining 4 bits are the block number, the two least significant bits of the block number give the line number, and the remaining upper bits of the block number form the tag.

Formulas

  • Cache line number=(Block number) mod (Number of cache lines)\text{Cache line number} = (\text{Block number}) \bmod (\text{Number of cache lines}), where Block number is the main-memory block address and Number of cache lines = cache size / block size.

  • Block offset bits=log⁡2(block size in words)=log⁡2(4)=2\text{Block offset bits} = \log_2(\text{block size in words}) = \log_2(4) = 2 bits

  • Block number bits=6−2=4\text{Block number bits} = 6 - 2 = 4 bits

  • Line number bits=log⁡2(number of lines)=log⁡2(4)=2\text{Line number bits} = \log_2(\text{number of lines}) = \log_2(4) = 2 bits (taken from the LSBs of the block number)

  • Tag bits=block number bits−line number bits=4−2=2\text{Tag bits} = \text{block number bits} - \text{line number bits} = 4 - 2 = 2 bits

  • Number of blocks mapping to one line=Number of main-memory blocksNumber of cache lines=164=4\text{Number of blocks mapping to one line} = \dfrac{\text{Number of main-memory blocks}}{\text{Number of cache lines}} = \dfrac{16}{4} = 4

  • Address split and line selection

  • Shows how the 6-bit address splits into block number and offset, with the block number's two LSBs selecting the cache line and the upper bits forming the tag.

  • Blocks mapped to each cache line

  • Shows the many-to-one direct mapping of the 16 main-memory blocks onto the 4 cache lines.

Worked examples

Which line does block 13 go to?

  1. Block number 13 in 4-bit binary = 1101.
  2. Two least significant bits of the block number = 01.
  3. Therefore block 13 always goes to cache line 1 (together with blocks 1, 5, 9).
  4. The remaining upper bits of the block number (11) form the tag.

Which blocks share line 0?

  1. Line 0 requires the two LSBs of the block number to be 00.
  2. Block numbers with 00 in the last two positions: 0000 (0), 0100 (4), 1000 (8), 1100 (12).
  3. So blocks 0, 4, 8, 12 all map to line 0; no other block ever maps there.

Comparison

AspectDirect mapping (this scheme)
Position of a blockFixed: exactly one line, given by block number mod number of lines
Type of functionMany-to-one (4 main-memory blocks per cache line in the example)
Line selectionTwo LSBs of the block number
TagRemaining upper bits of the block number, stored to identify which block is in the line
AdvantageSimplest mapping; address split is mechanical, so lookup is fast and cheap
LimitationBlocks sharing a line evict each other even if other lines are free (conflict misses)

Revision summary

  • Mapping answers: which cache line does a main-memory block go into? Direct mapping is the simplest answer.
  • Example parameters: 16 main-memory blocks, 4 cache lines, block size 4 words, 6-bit address → 4-bit block number + 2-bit block offset.
  • Rule: line number = block number mod number of lines; here the two LSBs of the block number give the line.
  • Fixed assignment: 0, 4, 8, 12 → line 0; 1, 5, 9, 13 → line 1; 2, 6, 10, 14 → line 2; 3, 7, 11, 15 → line 3.
  • The mapping is many-to-one: a block is always placed in its one fixed line whenever it is fetched, not all blocks resident at once.
  • Trap: the two LSBs used for the line number come from the block-number part only, not from the whole address.
  • The remaining upper bits of the block number become the tag, identifying which competing block currently occupies the line.
  • Cache is a very frequently asked exam topic (at least one question every year); the questions are easy once the address split and mapping rule are clear, so learn them thoroughly.

Previous-year exam insights

  • For a byte-addressable direct-mapped cache, split the physical address as tag | line/index | block offset.
    Offset bits = log⁡2(block size in bytes)\log_{2}(\text{block size in bytes});
    index/line bits = log⁡2(cache size/block size)\log_{2}(\text{cache size} / \text{block size});
    tag bits = total physical address bits - offset bits - index bits, where total physical address bits = log⁡2(physical address space in bytes)\log_{2}(\text{physical address space in bytes}).
    Asked inGATE 2026GATE 2022GATE 2007ISRO 2011
  • Number of cache lines = cache size / block size.
    For byte addressing, block number = floor(byte address / block size) and cache line/index = block number mod number of cache lines;
    when the number of lines is a power of two, this modulo is simply the low index bits of the block number.
    Asked inGATE 2026ISRO 2011
  • In a direct-mapped cache there is exactly one candidate line for a block and no replacement policy choice: on a miss (including the first cold-start reference) the requested block must replace whatever block currently occupies that line; later references hit only if that block has not since been evicted.
    Asked inGATE 2026GATE 2022
  • Two different blocks conflict in a direct-mapped cache exactly when their block numbers are congruent modulo the number of cache lines; equivalently, their block start addresses differ by a multiple of the cache size (number of lines × block size).
    Such blocks evict each other even if other cache lines are free.
    Asked inGATE 2026GATE 2022GATE 2007
  • On a direct-mapped access, the index bits select the line, and a cache hit requires valid bit = 1 and stored tag = address tag;
    otherwise it is a miss and the block is fetched.
    Standard cache mapping schemes are direct, fully associative and set-associative; segmented/page mapping is not a cache mapping scheme.
    Asked inGATE 2026GATE 2022GATE 2007UGC NET 2018
  • For row-major two-dimensional arrays, consecutive elements in a row are contiguous in memory, so one cache block loads several consecutive elements.
    In nested-loop row scans, compulsory misses occur on the first element of each new block, and extra conflict misses occur when addresses from different rows or strides map to the same direct-mapped cache line; use the address-split and modulo mapping formula to count them.
    Asked inGATE 2007ISRO 2017
Pages 1–2 of 2