3

我想知道是否有人可以验证我对这个问题的回答!我下周有期中考试,TA还没有发布这个问题的解决方案:

考虑以下 MIPS 汇编代码并在没有实现管道优化(包括转发)的假设下识别所有管道危险。第一列数字是行号,您可以在解释中参考。

1. addi $3, $0, 100
2. addi $4, $0, 0
3. loop: addi $4, $4, 4
4. add $5, $4, $3
5. sw $5, 100($4)
6. addi $1, $1, -1
7. slt $2, $4, $3
8. bne $2, $0, loop
9. jr $31

重新排序指令以将停顿的数量减少到最低限度

我的答案:

从第 2 行移动到第 3 行(从外部循环到内部),存在危险,因为第 3 行需要的 $4 用于加法取决于第 2 行 $4 中设置的值。

第 4 行有一个危险,因为它依赖于第 3 行中为 $4 设置的值。

第 5 行有一个危险,因为它依赖于第 4 行中为 $4 设置的值。

第 8 行有一个危险,因为它依赖于第 7 行中为 $2 设置的值。

重新排序的说明:

        addi $4, $0, 0      2
        addi $3, $0, 100    1
loop:   addi $4, $4, 4      3
        addi $1, $1, -1     6
        add  $5, $4, $3     4
        slt  $2, $4, $3     7
        sw   $5, 100($4)    5
        bne  $2, $0, loop   8
        jr   $31        9
4

2 回答 2

0
Data hazards:
(a) Line 3 needs to wait for line 2 to evaluate the value of $4 (in the
first iteration)
(b) Line 4 needs to wait for line 2 to evaluate the value of $4 (every
iteration)
(c) Line 5 needs to wait for line 4 to evaluate the value of $5 (every
iteration)
(d) Line 8 needs to wait for line 7 to evaluate the value of $2

Control hazard
(a) Line 8 will stall while determining if $2 is equal to $0

Moving lines 6 and 7 to between lines 4 and 5 (alternatively moving
line 5 to between line 7 and 8) and swapping the order, i.e. line 7
before line 6, would provide the most savings with stalls, because that
stall occurs on each iteration of the loop. The swap is necessary to
avoid the data hazard with line 8.
于 2012-10-22T22:00:59.403 回答
0
  1. Line 3 is dependant on Line 2 (for $4)
  2. Line 4 is dependant on Line 3 (for $4)
  3. Line 5 dependant on Line 3 (for $4) and Line 4 (In the WB of add: value will be written to the register file --in the first half of clock cycle. At the same time MEM of sw would be going on, the value would be needed --in the first half of clock cycle. So Hazard condition exists between these two)
  4. Line 8 is dependant on Line 7
于 2014-10-07T17:41:27.093 回答