Electronic – Is the cache hit rate calculation correct

cache

I study cache memories. I'm supposed to calculate the data hit rate for a function call with a 1024 byte direct mapped data cache and block size 16 byte.

sum(0x55aa1000,10);

The function is in MIPS 32 assembly

sum:
    xor $v0, $v0, $v0

loop:
    lb $t0, 0($a0)
    add $v0, $v0, $t0
    addi $a0, $a0, 1
    addi $a1, $a1, -1
    bne $a1, $zero, loop
    jr $ra

My attempt at solution:

The set field is 7 bits because 1024/16=128=2^7 and the byte offset field is 4 bits because the block size is 16. Therefore the tag field is 32-7-4=21 bits. The tags are the same for all data accesses(?)

Since the loop increments data offset by 1 byte and decrements the counter by 1, it will be run 10 times, the first time will be a miss and the rest will be a hit because it is within the same block.

Therefore the hit rate will be 90 %.

Is my solution correct?

Best Answer

I haven't touched MIPS in years so this code is cryptic as heck. I think I understand what it's doing, but correct me if I'm wrong.

Also I don't think this first line is actually mips here so that makes it even more confusing.

sum(0x55aa1000,10);

From this direction, 0x55aa1000 gets sent into $a0 and 10 into $a1?

sum:
    xor $v0, $v0, $v0     # Xor with itself makes $v0=0

loop:
    lb $t0, 0($a0)        # Load addr 0x55aa1000 into $t0 load. Then that addr+1 each loop. 
    add $v0, $v0, $t0     # $v0 stores the sum of the bytes.
    addi $a0, $a0, 1      # Increment the offset by one.
    addi $a1, $a1, -1     # Decrement the counter ($a1) by one.
    bne $a1, $zero, loop  # Keep looping until the counter is zero.
    jr $ra                # return to the return address prior to the function call.

If the directive to send those two values into $a0 and $a1 are correct, then I think your analysis is also correct. You've pulled in one cacheline (aka block) into the cache on the first run and never have to load anything else into the cache afterwards since you aren't exceeding the 16 byte block-size and you're pulling from the same block. 90% hit rate in this case seems correct. It would be much more interesting if $a1 was loaded with a larger value. In that case, it would trend towards the best case of 93.75% hit rate for this function.