Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 4.21 This exercise is intended to help you understand the relationship

ID: 3710941 • Letter: E

Question

Exercise 4.21

This exercise is intended to help you understand the relationship between forwarding, hazard detection, and ISA design. Problems in this exercise refer to the following sequences of instructions, and assume that it is executed on a 5-stage pipelined datapath:

Instruction sequence

a.

ADD R5,R2,R1

LW R3,4(R5)

LW R2,0(R2)

OR R3,R5,R3

SW R3,0(R5)

b.

LW R2,0(R1)

AND R1,R2,R1

LW R3,0(R2)

LW R1,0(R1)

SW R1,0(R2)

4.21.1 [5] <4.7> If there is no forwarding or hazard detection, insert NOPs to ensure correct execution.
4.21.2 [10] <4.7> Repeat 4.21.1 but now use NOPs only when a hazard cannot be avoided by changing or rearranging these instructions. You can assume register R7 can be used to hold temporary values in your modified code.

4.21.3 [10] <4.7> If the processor has forwarding, but we forgot to implement
the hazard detection unit, what happens when this code executes?

4.21.5 [10] <4.7> If there is no forwarding, but there is a hazard detection unit, what happens when this code executes?

4.21.6 [20] <4.7> Employing data forwarding, hazard detection and instruction reordering, what is the best sequence for this code to execute in?

Explanation / Answer

answer

4.21.1.

aAns:// lw $1,40($6)
nop
nop
add $2,$3,$1
add $1,$6,$4
nop
sw $2,20($4)
add $1,$1,$4 //

bAns. // add $1,$5,$3
nop
nop
sw $1,0($2)
lw $1,4($2)
nop
nop
add $5,$5,$1
sw $1,0($2) //

4.21.2:

We can move up an instruction by swapping its place with another instruction
that has no dependences with it, so we can try to fi ll some nop slots with such
instructions. so use "R7" to eliminate ""WAW / WAR"" dependences so that we can
have "more" -instructions to move_up.

a. //I1: lw $7,40($6) Produce $7 instead of $1
I3: add $1,$6,$4 Moved up to fi ll NOP slot
nop
I2: add $2,$3,$7 Use "$7" instead of "$1"
I5: and $1,$1,$4 Movedd upto fill "NOP" slot
nop
I4: sw $2,20($4) //

b. // I1: add $7,$5,$3 Produce "$7" instead of "$1"
I3: lw $1,4($2) Moved upto fill "NOP" slot
nop
I2: sw $7,0($2) Usee "$7" instead of "$1"
I4: add $5,$5,$1
I5: sw $1,0($2) //

4.21.3::

With 'forwarding'., "the hazard_detection_unit" is still needed because as it must
'insert' a "one-cycle" stall whenever the 'load' supplies value to instruction that
'immediately' follows that 'load'. Without the 'hazard-detection-unit'.., the 'instruction'
that depends on the immediately 'preceding' load gets the stale_value the 'register' had
before the 'load_instruction'.
1. // 'I2' gets the value of '$1' from before 'I1'., not from 'I1' as it should.//
2. // 'I4' gets the value of '$1' from 'I1', not from 'I3' as it should. //