Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4496
  • Last Modified:

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!!
0
waylonwhales
Asked:
waylonwhales
1 Solution
 
AaronReamsCommented:
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now