2012-10-20 39 views
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是依赖于值设置在$ 4第2行

线4具有危险,因为它是依赖于在线为$ 4中设置的值3.

线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 

回答

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. 
0
  1. 第3行是依赖于第2行(4 $)
  2. 4行是依赖于线3(4 $)
  3. 第5行依赖于第3行($ 4)和第4行(在WB中,add:value将写入寄存器文件 - 在时钟周期的前半部分。与此同时,sw的MEM会继续运行,这个值将需要 - 在时钟周期的前半部分。所以这两者之间存在危险状况)
  4. 第8行依赖于第7行