这是在用 Verilog 编写的 5 级流水线 MIPS 上运行的机器代码(mem 是其指令存储器):
mem[0] <= 32'h3c040000; // lui $4, 0
mem[1] <= 32'h3409000a; // ori $9, $0, 10
mem[2] <= 32'h340a0000; // ori $10, $0, 0
mem[3] <= 32'h8c880000; // lw $8, 0($4)
mem[4] <= 32'h208b0028; // addi $11, $4, 40
mem[5] <= 32'h208c002c; // addi $12, $4, 44
mem[6] <= 32'had680000; // sw $8, 0($11)
mem[7] <= 32'h8c8d0000; // lw $13, 0($4)
mem[8] <= 32'h010d082a; // slt $1, $8, $13
mem[9] <= 32'h10200004; // beq $1, $0, 16
mem[10] <= 32'h000d4021; // addu $8, $0, $13
mem[11] <= 32'had6d0000; // sw $13, 0($11)
mem[12] <= 32'had8a0000; // sw $10, 0($12)
mem[13] <= 32'h20840004; // addi $4, $4, 4
mem[14] <= 32'h214a0001; // addi $10, $10, 1
mem[15] <= 32'h2129ffff; // addi $9, $9, -1
mem[16] <= 32'h1520fff7; // bne $9, $0, -36
这段代码应该找到 10 个元素数组中的最大数字及其索引,并将它们存储在紧邻数组之后的数据存储器中。这是我的数据存储器:
Memory[0] <= 32'h5;
Memory[1] <= 32'h1;
Memory[2] <= 32'hc;
Memory[3] <= 32'h7;
Memory[4] <= 32'h2;
Memory[5] <= 32'h8;
Memory[6] <= 32'hb;
Memory[7] <= 32'h4;
Memory[8] <= 32'h6;
Memory[9] <= 32'h3;
// end of array
Memory[10] <= 32'h0; // the greatest number in data memory
Memory[11] <= 32'h0; // index of the greatest number in memory
为什么这段汇编代码不起作用?
运行后,数据内存一定是这样的:
// Memory[10] is equal to 32'hc (the greatest number in array)
// Memory[11] is equal to 32'h2 (the array index of the greatest number)
但是我得到了:
// Memory[10] is equal to 32'h8 (the greatest number in array)
// Memory[11] is equal to 32'h2 (the array index of the greatest number)
这是写入序列(控制台日志):
WriteData = 00000005 (greatest number)
WriteData = 00000005 (greatest number)
WriteData = 00000001 (greatest number)
WriteData = 0000000c (greatest number)
WriteData = 00000002 (index of greatest number)
WriteData = 00000007 (greatest number)
WriteData = 00000002 (greatest number)
WriteData = 00000008 (greatest number)
在下面,您能看到与机器代码的区别吗?分支的立即数减少了 1?
从这段代码开始:
.set noat
.text
lui $4, 0
ori $9, $0, 10
ori $10, $0, 0
lw $8, 0($4)
addi $11, $4, 40
addi $12, $4, 44
sw $8, 0($11)
Loop1:
lw $13, 0($4)
slt $1, $8, $13
beq $1, $0, EndIf1
addu $8, $0, $13
sw $13, 0($11)
sw $10, 0($12)
EndIf1:
addi $4, $4, 4
addi $10, $10, 1
addi $9, $9, -1
bne $9, $0, Loop1
MARS 生成以下内容:
3c040000
3409000a
340a0000
8c880000
208b0028
208c002c
ad680000
8c8d0000
010d082a
10200003 <----- this is the forward branch for the if-then statement
000d4021
ad6d0000
ad8a0000
20840004
214a0001
2129ffff
1520fff6 <----- this is the backward branch for the do/while loop
火星截图:
这是使用延迟分支并默认关闭/未选中设置的 QtSpim: