Tutorial 11 Longer Solution

advertisement
1.
The cache coherence problem can also be solved by providing an instruction
"invalidate" which deletes the block containing a shared variable in a writethrough cache. Explain why. Then discuss the solution for a write-back cache.
ans:
Invalidate switches the valid flag of the shared variable’s block to ‘0’. Next
access causes a cache miss, causing a re-read of the latest value from main
memory. Thus coherence is assured. Effectively the same as not caching the
shared variables.
For write-back, we introduce a new “update_memory” instruction that
allows us to selectively write-back cache blocks to main memory before the
block is actually flushed. Hence we effectively enforce write-through on
shared variables, but other variables remain write-back. Use of Invalidate
will guarantee coherence as before.
Note that Invalidate effectively switches off caching for shared variables,
while update_memory effectively forces cache writes to shared variables to
be write-back. Hence shared variables must be kept to a minimum or cache
becomes ineffective.
2.
Explain the compilation of IF X THEN Y ELSE Z with the use of a stack.
General schema of if..then..else
0: test r1, x
// Sets r1 to non-zero if x condition is true
1: jz Z
// r1 is 0, meaning x is false. Jump to Z
2: <code for y> // This section is executed if x is true
.
.
.
n: j after_Z
// Skip over Z
n+1: <code for Z>
.
.
.
after_Z: <execution continues>
At this point we do not know the addresses of Z and after_Z. We can use a
stack to solve this.
1. Push jz Z onto the stack
2. count number of instructions in Y. Say there are 30 instructions
3. Pop jz Z, substitute Z = 30 + 1 +1 = 32 . +1 because we count from the
instruction after the jz, and +1 again because the jz is 1 instruction
from the beginning of the program.
4. Replace jz Z with jz 32
5. Push j after_Z onto the stack
6. Count number of instructions in Z. Say it is 20 instructions.
7. pop j after_Z, replace after_Z with 32 + 1 + 20 = 53. j after_Z now
becomes j 53.
This scheme is particularly useful for nested if-then-else statements.
Download