Simple quicksort assembly program

Posted on 2004-11-30
Last Modified: 2012-08-14
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(--j>i && pivot<a[j])
    if (i>=j)
    t = a[i];
  t = a[i];

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]

add %fp, aS, %aR
mov 0, %lowR
mov 9, %highR

sll %highR, 2, %l3
ld [%aR + %l3], %pivotR
mov %lowR, %iR
mov %highR, %jR

sll %iR, 2, %l3
ld [%aR + %l3], %l3
cmp %l3, %pivotR
bl quicksortLoop
add %iR, 1, %iR

sll %jR, 2, %l5
ld [%aR + %l5], %l5
cmp %pivotR, %l5
bl quicksortLoop2
sub %jR, 1, %jR

cmp %iR, %jR
bge test
sll %iR, 2, %l3
ld [%aR + %l3], %l3
mov %tR, %l3
mov %l3, %l5
mov %l5, %tR

mov %tR, %l3
sll %highR, 2, %l6
ld [%aR + %l6], %l6
mov %l3, %l6
mov %l6, %tR


Thank you very much for your time!!
Question by:waylonwhales
    1 Comment
    LVL 8

    Accepted Solution

    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)
                            int swap = array[l];
                            array[l] = array[r];
                            array[r] = swap;
                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};
          return 0;

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    What is a Logic Library? 2 503
    Show semaphore name in Pascal 12 623
    LNK2017 error with MASM64 2 1,536
    Application Trololo: "Do you want to GPF?" 4 511
    When we have a dead host and we lose all connections to the ESXi, and we need to find a way to move all VMs from that dead ESXi host.
    Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now