'Turning C program to MIPS assembly

I have an assignment and I don't understand how to turn C code to assembly.

Suppose you have an array of 12. Rearrange the contents of the array so they are sorted from smallest to largest (from A[0] to A[12]).**

Address: 12, 8, 4, 0

Data: 1, 6, 4, 2

So the end result should be: A[0]==1, A[4]==2, A[8]==4, A[12]==6

This is my C code:

int x, i=12;
while(A[i] < A[i-4])
{
x = A[i];
A[i] = A[i-4];
A[i-4] = x;
i -= 4;
}

So now I have to translate that code to mips but I'm stuck. This is what I've made so far:

addi $t1, $t1, 12   #$t1 = i
sll $t0, $t1, 2
add $t0, $t0, $s6   #$s6 = A[]

lw $t2, 0($s6)
lw $t3, 4($s6)
lw $t4, 8($s6)
lw $t5, 12($s6)
loop: 
slt $t6, $t5, $t4
bne $t6, 1 END
lw $t6, $t5
lw $t5, $t4
lw $t4, $t6
addi $t5, $t5, -4
addi $t4, $t4, -4
j loop
END

I know this isn't correct but I don't know how to make it work.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source