Well, you need to understand what the program does and in case of the program you mention, there is no other way that checking step by step what the content of the memory, the cache, and the registers is. Without the simulator that is quite some work, of course.
We have first executed some code such that reg[i]=i holds. Then, we execute the following:
sti $1,$0,0 // mem[0] := 1
sti $2,$0,1 // mem[1] := 2
ldi $2,$0,1 // reg[2] := mem[1] = 2
sti $3,$0,2 // mem[2] := 3
sti $4,$0,3 // mem[3] := 4
ldi $2,$0,1 // reg[2] := mem[1] = 2
sti $5,$0,7 // mem[7] := 5
sti $6,$0,8 // mem[8] := 6
sti $7,$0,9 // mem[9] := 7
ldi $2,$0,1 // reg[2] := mem[1] = 2
We have a cache with 8 bytes, 2 bytes, i.e., one memory word, in a block and we use direct mapping. Hence, a memory address m is split into its cache address adr(m) = m mod 4 and its tag tag(m) = m div 4. This gives the following execution with memory transactions:
sti $1,$0,0 // mem[0] := 1 cache[0] := mem[0]
sti $2,$0,1 // mem[1] := 2 cache[1] := mem[1]
ldi $2,$0,1 // reg[2] := mem[1] = 2
sti $3,$0,2 // mem[2] := 3 cache[2] := mem[2]
sti $4,$0,3 // mem[3] := 4 cache[3] := mem[3]
ldi $2,$0,1 // reg[2] := mem[1] = 2
sti $5,$0,7 // mem[7] := 5 cache[3] := mem[7]; mem[3] := cache[3]
sti $6,$0,8 // mem[8] := 6 cache[0] := mem[8]; mem[0] := cache[0]
sti $7,$0,9 // mem[9] := 7 cache[1] := mem[9]; mem[1] := cache[1]
ldi $2,$0,1 // reg[2] := mem[1] = 2 cache[1] := mem[1]; mem[9] := cache[1]
Note that the following memory addresses refer to the same cache address:
- 0,4,8,12
- 1,5,9,13
- 2,6,10,14
- 3,7,11,15
After each step, you need to remember the content of the cache, i.e., which memory block is currently at which address in the cache to find out whether there is a hit or a miss and which memory transaction is required. In particular, the last instruction is a miss since at that point of time, there is mem[9] at cache address 1 and we want to load address 1 which is also mapped to the same cache address. Hence, we first have to write back cache address 1 before loading mem[1] into cache[1].