Cache Solved Examples PDF
Cache Solved Examples PDF
A computer system uses 16-bit memory addresses. It has a 2K-byte cache organized in a direct-mapped
manner with 64 bytes per cache block. Assume that the size of each memory word is 1 byte.
(a) Calculate the number of bits in each of the Tag, Block, and Word fields of the memory address.
(b) When a program is executed, the processor reads data sequentially from the following word
addresses:
All the above addresses are shown in decimal values. Assume that the cache is initially empty.
For each of the above addresses, indicate whether the cache access will result in a hit or a miss.
Solution
(b) The cache is initially empty. Therefore, all the cache blocks are invalid.
Access # 1:
Address = (128)10 = (0000000010000000)2
(Note: Address is shown as a 16-bit number, because the computer uses 16-bit addresses)
For this address, Tag = 00000, Block = 00010, Word = 000000
Since the cache is empty before this access, this will be a cache miss
After this access, Tag field for cache block 00010 is set to 00000
Access # 2:
Address = (144)10 = (0000000010010000)2
For this address, Tag = 00000, Block = 00010, Word = 010000
Since tag field for cache block 00010 is 00000 before this access, this will be a cache hit (because
address tag = block tag)
Access # 3:
Address = (2176)10 = (0000100010000000)2
For this address, Tag = 00001, Block = 00010, Word = 000000
Since tag field for cache block 00010 is 00000 before this access, this will be a cache miss
(address tag ≠ block tag)
After this access, Tag field for cache block 00010 is set to 00001
Access # 4:
Address = (2180)10 = (0000100010000100)2
For this address, Tag = 00001, Block = 00010, Word = 000100
Since tag field for cache block 00010 is 00001 before this access, this will be a cache hit (address
tag = block tag)
Access # 5:
Address = (128)10 = (0000000010000000)2
For this address, Tag = 00000, Block = 00010, Word = 000000
Since tag field for cache block 00010 is 00001 before this access, this will be a cache miss
(address tag ≠ block tag)
After this access, Tag field for cache block 00010 is set to 00000
Access # 6:
Address = (2176)10 = (0000100010000000)2
For this address, Tag = 00001, Block = 00010, Word = 000000
Since tag field for cache block 00010 is 00001 before this access, this will be a cache miss
(address tag ≠ block tag)
After this access, Tag field for cache block 00010 is set to 00001
Problem # 2
Repeat Problem # 1, if the cache is organized as a 2-way set-associative cache that uses the LRU
replacement algorithm.
Solution
(b) The cache is initially empty. Therefore, all the cache blocks are invalid.
Access # 1:
Address = (128)10 = (0000000010000000)2
For this address, Tag = 000000, Set = 0010, Word = 000000
Since the cache is empty before this access, this will be a cache miss
After this access, Tag field for the first block in set 0010 is set to 000000
Access # 2:
Address = (144)10 = (0000000010010000)2
For this address, Tag = 000000, Set = 0010, Word = 010000
The tag field for this address matches the tag field for the first block in set 0010. Therefore, this
access will be a cache hit.
Access # 3:
Address = (2176)10 = (0000100010000000)2
For this address, Tag = 000010, Set= 0010, Word = 000000
The tag field for this address does not match the tag field for the first block in set 0010. The
second block in set 0010 is empty. Therefore, this access will be a cache miss.
After this access, Tag field for the second block in set 0010 is set to 000010
Access # 4:
Address = (2180)10 = (0000100010000100)2
For this address, Tag = 000010, Set = 0010, Word = 000100
The tag field for this address matches the tag field for the second block in set 0010. Therefore,
this access will be a cache hit.
Access # 5:
Address = (128)10 = (0000000010000000)2
For this address, Tag = 000000, Set = 0010, Word = 000000
The tag field for this address matches the tag field for the first block in set 0010. Therefore, this
access will be a cache hit.
Access # 6:
Address = (2176)10 = (0000100010000000)2
For this address, Tag = 000010, Set = 0010, Word = 000000
The tag field for this address matches the tag field for the second block in set 0010. Therefore,
this access will be a cache hit.