waylonwhales
asked on
Simple quicksort assembly program
I've coded an assembly program to perform a quicksort on a group of 10 decimal numbers. Either the original code which is in C is wrong, or the translation into assembly is not correct. I'm using emacs as the compiler for the assembly program. Can you help discover why this program compiles and runs but does not sort the numbers, it just leaves them all the way they were entered into the program. The code for both is below:
original c code
int i,j;
int pivot=a[high];
for(i=low, j=high;;){
while(a[i]<pivot)
i++;
while(--j>i && pivot<a[j])
;
if (i>=j)
break;
t = a[i];
a[i]=a[j];
a[j]=t;
}
t = a[i];
a[i]=a[high];
a[high]=t;
This is supposed to split the array a aroung the pivot, a[high]. All elements below the pivot are less than it, and all are more than it after the algorithm runs.
Eg. 8 1 4 9 0 3 5 2 7 6 becomes (6 is the pivot)
2 1 4 5 0 3 6 8 7 9
assembly translation:
define( iR, l0)
define( jR, l1)
define( aS, -400)
define( aR, i0)
define( lowR, i1)
define( highR, i2)
define( pivotR, l2)
define( tR, l4)
.global main
main: save %sp, (-92 + aS) &-8, %sp
mov 8, %o0
st %o0, [%fp + aS + 0]
mov 1, %o0
st %o0, [%fp + aS + 4]
mov 4, %o0
st %o0, [%fp + aS + 8]
mov 9, %o0
st %o0, [%fp + aS + 12]
mov 0, %o0
st %o0, [%fp + aS + 16]
mov 3, %o0
st %o0, [%fp + aS + 20]
mov 5, %o0
st %o0, [%fp + aS + 24]
mov 2, %o0
st %o0, [%fp + aS + 28]
mov 7, %o0
st %o0, [%fp + aS + 32]
mov 6, %o0
st %o0, [%fp + aS + 36]
begin:
add %fp, aS, %aR
mov 0, %lowR
mov 9, %highR
sll %highR, 2, %l3
ld [%aR + %l3], %pivotR
mov %lowR, %iR
mov %highR, %jR
quicksortLoop:
sll %iR, 2, %l3
ld [%aR + %l3], %l3
cmp %l3, %pivotR
bl quicksortLoop
add %iR, 1, %iR
quicksortLoop2:
sll %jR, 2, %l5
ld [%aR + %l5], %l5
cmp %pivotR, %l5
bl quicksortLoop2
sub %jR, 1, %jR
check:
cmp %iR, %jR
bge test
sll %iR, 2, %l3
ld [%aR + %l3], %l3
mov %tR, %l3
mov %l3, %l5
mov %l5, %tR
test:
mov %tR, %l3
sll %highR, 2, %l6
ld [%aR + %l6], %l6
mov %l3, %l6
mov %l6, %tR
end:
ret
restore
Thank you very much for your time!!
original c code
int i,j;
int pivot=a[high];
for(i=low, j=high;;){
while(a[i]<pivot)
i++;
while(--j>i && pivot<a[j])
;
if (i>=j)
break;
t = a[i];
a[i]=a[j];
a[j]=t;
}
t = a[i];
a[i]=a[high];
a[high]=t;
This is supposed to split the array a aroung the pivot, a[high]. All elements below the pivot are less than it, and all are more than it after the algorithm runs.
Eg. 8 1 4 9 0 3 5 2 7 6 becomes (6 is the pivot)
2 1 4 5 0 3 6 8 7 9
assembly translation:
define( iR, l0)
define( jR, l1)
define( aS, -400)
define( aR, i0)
define( lowR, i1)
define( highR, i2)
define( pivotR, l2)
define( tR, l4)
.global main
main: save %sp, (-92 + aS) &-8, %sp
mov 8, %o0
st %o0, [%fp + aS + 0]
mov 1, %o0
st %o0, [%fp + aS + 4]
mov 4, %o0
st %o0, [%fp + aS + 8]
mov 9, %o0
st %o0, [%fp + aS + 12]
mov 0, %o0
st %o0, [%fp + aS + 16]
mov 3, %o0
st %o0, [%fp + aS + 20]
mov 5, %o0
st %o0, [%fp + aS + 24]
mov 2, %o0
st %o0, [%fp + aS + 28]
mov 7, %o0
st %o0, [%fp + aS + 32]
mov 6, %o0
st %o0, [%fp + aS + 36]
begin:
add %fp, aS, %aR
mov 0, %lowR
mov 9, %highR
sll %highR, 2, %l3
ld [%aR + %l3], %pivotR
mov %lowR, %iR
mov %highR, %jR
quicksortLoop:
sll %iR, 2, %l3
ld [%aR + %l3], %l3
cmp %l3, %pivotR
bl quicksortLoop
add %iR, 1, %iR
quicksortLoop2:
sll %jR, 2, %l5
ld [%aR + %l5], %l5
cmp %pivotR, %l5
bl quicksortLoop2
sub %jR, 1, %jR
check:
cmp %iR, %jR
bge test
sll %iR, 2, %l3
ld [%aR + %l3], %l3
mov %tR, %l3
mov %l3, %l5
mov %l5, %tR
test:
mov %tR, %l3
sll %highR, 2, %l6
ld [%aR + %l6], %l6
mov %l3, %l6
mov %l6, %tR
end:
ret
restore
Thank you very much for your time!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.