我正在尝试理解MIPS中的数组,并且我很难这样做。我正在使用的数组在C ++中看起来像这样:
int array [10];
void main(){
int i;
for(i = 0; i < 10; i++){
array[i] = i + 5;
}
for(i = 0; i < 10; i++){
cout << array[i] << endl;
}
return 0;
}
到目前为止我有这个MIPS代码,但它有错误并打印所有0
:
.data
array: .space 40
.globl main
.text
<code>main:
li $t0, 0 # i=0
li $t4, 0 # i=0 for print loop
li $s1, 10 # $s1 = 10
la $a1, array # loads array to $a1
LOOP:
bge $t0, $s1, print # branch to print if i<10
addi $t1, $t0, 5 # i+5
add $t2, $t1, $t1 # 2 * i
add $t2, $t2, $t2 # 4 * i
add $t2, $t2, $a1 # $t2=address of array[i]
sw $t3, 0($t2)
addi $t0, $t0, 1 # i++
j LOOP # jumps to top of loop
print:
bge $t4, $s1, exit # branch to exit if i < 10
add $t5, $t4, $t4 # 2 * i
add $t5, $t5, $t5 # 4 * i
add $t5, $t5, $a1 # $t2=address of array[i]
sw $t6, 0($t5)
li $v0, 1
move $a0, $t6 #moves value to $a0 to be printed
syscall
addi $t4, $t4, 1 # i++
j print # jumps to top of print
exit:
li $v0, 10 #load value for exit
syscall #exit program
答案 0 :(得分:1)
我看到3个错误:
add $t2, $t1, $t1 # 2 * i
应该是
add $t2, $t0, $t0 # 2 * i
因为$t1 = $t0 + 5
其次,
sw $t3, 0($t2)
应该是
sw $t1, 0($t2)
最后,
sw $t6, 0($t5)
应该是
lw $t6, 0($t5)