How to calculate the number of tag, index and offset bits of different caches?

Specifically: 1) A direct-mapped cache with 4096 blocks/lines in which each block has 8 32-bit words. How many bits are needed for the tag and index fields, assuming a 32-bit address? 2) Same question as 1) but for fully associative cache? Correct me if I'm wrong, is it:

tag bits = address bit length - exponent of index - exponent of offset? [Is the offset = 3 due to 2^3 = 8 or is it 5 from 2^5 = 32?]

123 7 7 bronze badges asked Jul 20, 2013 at 9:08 403 1 1 gold badge 3 3 silver badges 10 10 bronze badges

$\begingroup$ some explanations about tag, index and offset in cs.stackexchange.com/questions/33818/… $\endgroup$

Commented May 11, 2016 at 14:15

5 Answers 5

$\begingroup$

The question as stated is not quite answerable. A word has been defined to be 32-bits. We need to know whether the system is "byte-addressable" (you can access an 8-bit chunk of data) or "word-addressable" (smallest accessible chunk is 32-bits) or even "half-word addressable" (the smallest chunk of data you can access is 16-bits.) You need to know this to know what the lowest-order bit of an address is telling you.

Then you work from the bottom up. Let's assume the system is byte addressable.

Then each cache block contains 8 words*(4 bytes/word)=32=2 5 bytes, so the offset is 5 bits.

The index for a direct mapped cache is the number of blocks in the cache (12 bits in this case, because 2 12 =4096.)

Then the tag is all the bits that are left, as you have indicated.

As the cache gets more associative but stays the same size there are fewer index bits and more tag bits.