This part is wrong:
lw $t2, ($t2) # $t2 = pos[count];
add $t3, $s5, $t1 # $t3 = &x[count];
lw $t3, ($t3) # $t3 = x[count];
sw $s2, ($t2) # pos[count] = i;
Why are you loading pos[count]
and x[count]
when you want to write to both? Not only is this unnecessary, it will destroy $t2
and $t3
so when you really want to write, they will no longer be valid.
Also, the end of the loop is wrong, the count++
should be inside the condition. For that you will need to swap the last two lines (including the label).
A slightly cleaned up version could look like:
.data
x: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
pos: .word -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
d: .word 73, 47, 23, 26, 67, 99, 13, 53, 1, 97
# $s0 count
# $s1 key
# $s2 i
.text
.globl main
main: addi $s0, $0, 0 # int count = 0;
addi $s1, $0, 24 # int key = 24;
addi $s2, $0, 0 # int i = 0;
# for (int i=0; i<10; i++) {
loop: sll $t0, $s2, 2 # $t0 = i * sizeof(int)
lw $t0, d($t0) # $t0 = d[i]
blt $t0, $s1, loop1 # if (d[i] < key)
sll $t1, $s0, 2 # $t1 = count * sizeof(int)
sw $s2, pos($t1) # pos[count] = i
sw $t0, x($t1) # x[count] = d[i]
addi $s0, $s0, 1 # count++;
loop1: addi $s2, $s2, 1 # i++;
blt $s2, 10, loop