# 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:
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

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!!
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Hi there,

I think your original C code is flawed.  I tested it and it didn't work for me.  Usually QuickSort is implemented recursively but I see you have done it iteratively.  While it is not impossible to do it that way you might have an easier time porting a recursive C function to assembly.  Here is a basic recursive C implementation of quicksort for 10 decimals.  You might try porting this to assembly.

I hope this helps.  Good luck -Aaron

void sort(int array[], int begin, int end)
{
if (end > begin)
{
int pivot = array[begin];
int l = begin + 1;
int r = end;
while(l < r)
{
if (array[l] <= pivot)
{
l++;
}
else
{
r--;
int swap = array[l];
array[l] = array[r];
array[r] = swap;
}
}
l--;
int swap = array[begin];
array[begin] = array[l];
array[l] = swap;
sort(array, begin, l);
sort(array, r, end);
}
}

int _tmain(int argc, _TCHAR* argv[])
{
int a[10] = {0,9,6,7,8,1,2,3,5,4};
sort(a,0,10);
return 0;
}
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Assembly

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.