Help printing MIPS array of 5 - 15
I am trying to understand arrays in MIPS and am having a really hard time in doing so. The array that i am working with look like this in 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;
}
I have this MIPS code so far but it has errors and prints all 0
's:
.data
array: .space 40
.globl main
.text
<code>main:
li $t0, 0 开发者_Go百科 # 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
I see 3 errors:
add $t2, $t1, $t1 # 2 * i
should be
add $t2, $t0, $t0 # 2 * i
because $t1 = $t0 + 5
Secondly,
sw $t3, 0($t2)
should be
sw $t1, 0($t2)
Finally,
sw $t6, 0($t5)
should be
lw $t6, 0($t5)
精彩评论