Hi guys,
I'm trying to implement a bubble sort algorithm in MIPS assembly language, but running into a few problems using SPIM.
The code I need to translate looks like:
and I have produced:
Any hints on this would be great as it's really frying my brain
Cheers
I'm trying to implement a bubble sort algorithm in MIPS assembly language, but running into a few problems using SPIM.
The code I need to translate looks like:
Code:
int a[] = { 2, 6, 4, 3, 1, 7, 5, 8 };
int n = 8;
for( int i = n - 1; i > 0; i--)
{
int flag = 0;
for( int j = 0; j < i; j++ )
{
if( a[j+0] > a[j+1] )
{
int t = a[j+0];
a[j+0] = a[j+1];
a[j+1] = t;
flag = 1;
}
}
if( !flag )
break;
}
and I have produced:
Code:
.data
n: .word 8
a: .word 2,6,4,3,1,7,5,8
.text
main:
lw $v1, n # Let v1 = n
addi $v1, $v1, -1 # i = n--
for1:
li f0, $0 # flag = 0
#######-------Begin outer for loop-------#######
blez $v2, end
#######------Begin inner for loop-------#######
for2:
li $v3, 0 # Let v3 = 0
beq $v3, $v2, for1 # If v3=v1 goto for1
########--------Begin if--------########
addi $v4, $v3, 1
sub $v5, A($v3), A($v4)
bgtz $v5, swap
addi $v3, $v3, 1
jal for2
addi $v2, $v2, -1
blez $f0, end
jal for1
swap:
lw $t0, A($v3)
lw A($v3), A($v4)
lw A($v4), $t0
addi $f0,$f0,1
jal for2
end:
break
Any hints on this would be great as it's really frying my brain

Cheers