Computer Organisation and Architecture · Direct Mapped Cache
Address Split into Block Number and Block Offset
Overview
A main-memory address in a direct mapped cache is split into exactly two fields: the block number (which block of main memory to look into) and the block offset (which word inside that block is wanted).
This split is what lets the cache locate a word: the block number decides whether the block is in the cache, and the block offset picks the word once the block is there.
Key concepts
- Block offset — the part of the address that identifies which word inside the block is wanted, i.e. the word's position within the block. It is formed from the least significant bits of the address.
- Block number — the part of the address that identifies which block of main memory must be looked into. It is formed from the most significant bits of the address.
- Because main memory is divided into blocks, any main-memory address splits into exactly these two parts: least significant bits → block offset, most significant bits → block number.
- Bit widths come from sizes: the number of offset bits is and the number of block-number bits is .
- Words inside a block are numbered starting from 0, so offset 2 means the third word of the block.
- Main memory has many more blocks (16 in the example) than the cache (4), so not all blocks can be held at once; how blocks are placed is decided by the mapping scheme.
How it works
- Decide how many bits each part needs from the memory sizes: block-number bits from the number of blocks, offset bits from the block size in words.
- Split the address: the most significant bits are the block number, the least significant bits are the block offset.
- To locate a word: read the block number and go to that block in main memory; read the block offset and, inside that block, count words starting from location 0 and pick the word at that offset.
- Cache lookup: first determine whether the required block is present in the cache.
- Hit (block present): use the block offset to fetch the required word from that block.
- Miss (block not present): bring the entire block from main memory and place it in the cache, then use the block offset to get the word.
Formulas
Worked examples
Setup: main memory has 16 blocks and the block size is 4 words per block.
- Block number needs bits (most significant bits).
- Block offset needs bits (least significant bits).
- Total address length bits.
- Split: bits [5:2] (the 4 MSBs) = block number; bits [1:0] (the 2 LSBs) = block offset.
Example 1: address 0001 01
- Address bits:
000101(6 bits). - Block number = MSBs
0001= 1 → go to block number 1. - Block offset = LSBs
01= 1 → inside block 1, take the word at location 1 (counting from 0).
Example 2: address 0010 10
- Address bits:
001010(6 bits). - Block number = MSBs
0010= 2 → go to block number 2. - Block offset = LSBs
10= 2 → inside block 2, take the word at location 2; since counting starts at 0, this is the third location inside the block, and the word10is present there.
| Address (binary) | Block number (4 MSBs) | Block (decimal) | Block offset (2 LSBs) | Word inside block (from 0) |
|---|---|---|---|---|
| 0001 01 | 0001 | 1 | 01 | 1 |
| 0010 10 | 0010 | 2 | 10 | 2 (third location) |
Revision summary
- The address splits into exactly two fields: block number (MSBs) and block offset (LSBs).
- Offset bits ; block-number bits .
- Words inside a block are numbered from 0, so offset 2 is the third word — not the second.
- Lookup order matters: check whether the block is in the cache first; on a hit use the offset to fetch the word, on a miss bring the whole block into the cache first.
- Main memory has more blocks (16) than the cache (4), so not all blocks fit; the mapping scheme decides placement.
Previous-year exam insights
- In a direct mapped cache the block number field is further split into a tag (the high bits identifying which main-memory block occupies the line) and a line/index number (the low bits selecting the cache line);
the index bits = , tag bits = block-number bits − index bits. - The number of cache lines equals cache size / block size, and the number of main-memory blocks equals main-memory size / block size;
address length = , consistently in the same unit as block size. - The block offset is only when the block size is expressed in addressable units (words or bytes);
if block size is given in bytes and the address is word-addressable, convert first (offset bits = ). - A main-memory block with block number B maps to cache line B mod (number of cache lines); this is the defining placement rule of direct mapping and determines which blocks conflict.
- On a cache miss the whole block (not just the requested word) is transferred from main memory to the cache, so the number of block transfers equals the number of misses, and each transfer moves block-size units.
- The tag stored in a cache line is compared with the tag field of the address on every access; a hit requires both a valid bit set and tag match, otherwise it is a miss even if the index points to a filled line.