How to calculate the number of tag, index and offset bits of 2 different caches? [closed]

I have two exercises:
1) A 128KB direct-mapped cache, with lines from 32bytes/lines and 36-bit address.
2) A 512KB 2-way associative cache, with lines from 64bytes/lines and 36-bit address.
For both exercises i have to find the number of bits needed for the tag,index and offset fields.
I tried to do both exercises and i found these results:
1)5 bit offset, 17 bit index, 14 bit tag
2)6 bit offset, 12 bit index, 18 bit tag
First exercise:
128KB = 217
32 bytes = 25 so 5 bit offset
17 bit index
36-5-17=14 bit tag
Second exercise:
64 bytes = 26 so i have 6 bit offset
512KB = 219
512/2 = 219/2 = 218 cache lines number
218/26= 212 so i have 12 bit index
36-(12+6)=18 bit tag

• In general, I think "is this correct" type of questions are off-topic here. Anyway, I think your part 2) is correct, but in part 1), the index should probably be 12 bit as there are 4KB=$2^{12}$ sets. Your question could be more useful (probably) if you could mention how you arrived at your answers. Feb 18 '17 at 18:37
• so in the first excercise i have to do: 2^17/2^5 = 2^12? Feb 18 '17 at 19:24
• In the first example, $2^{17}$ is the number of bytes. However, the number of sets, which corresponds to the number of indices, equals the number of lines. One way to look at it is that 5 bits will be used to differentiate bytes belonging to the same line, so the number of bits needed is 17-5=12. That is exactly what has been done in case 2) Feb 18 '17 at 19:29
• ok thanks , and if the cache is fully-associative there's no index bit right? Feb 18 '17 at 20:25
• We discourage "please check whether my answer is correct" questions, as only "yes/no" answers are possible, which won't help you or future visitors. See here and here. Can you edit your post to ask about a specific conceptual issue you're uncertain about? As a rule of thumb, a good conceptual question should be useful even to someone who isn't looking at the problem you happen to be working on. If you just need someone to check your work, you might seek out a friend, classmate, or teacher.
– D.W.
Feb 20 '17 at 22:04

Assuming that your machine is byte addressable(1 word = 1 byte), Let us solve the problem step by step.

1.

Physical address = 36 bits. Since 32 bytes/line and size of cache line = size of main memory block, this means block offset = 5 bits. Hence remaining 31 bits is block number( = tag + index).

number of cache lines = 128KB/32B, therefore, 12 bits for index and hence remaining 19 bits for tag.

2.

Physical address = 36 bits. Since 64 bytes/line and size of cache line = size of main memory block, this means block offset = 6 bits. 2-way associative cache means that two lines in one set.

number of sets = total cache lines/2 = (512KB/64)/2 = 2^12 , therefore 12 bits for set, and hence remaining 18 bits for the tag.