Link to home
Start Free TrialLog in
Avatar of tjodolf
tjodolf

asked on

Convert c code into assembly code

Hey. I hope you can help me with converting some c code into assembly code, i have tried much now, and i still don't get it. What would help me alot, would be some comments for each line in the assembly code, so i better can understand to convert process...

Here is the code:

/* Standard quicksort. */
void quicksort(int a[], int left, int right)
{
      int lp = left - 1;
      int rp = right;
      int v = a[right]; /* the partioning element */

      if (right <= left)
            /* Array has one or null elements to sort */
            return;

      /* No elements to the left of lp are greater than the partioning elemnt. */
      while (1) {
            while (a[++lp] < v)
                  ;

            while (v < a[--rp])
                  /* In case the partioning element is the smallest in the array */
                  if (rp == left)
                        break;

            if (lp >= rp)
                  break;

            /* Deadlock, switch elements and continue */
            swap(&a[lp], &a[rp]);
      }
      /* This completes the partioning */
      swap(&a[lp], &a[right]);

      /* Now every element to the left of a[lp] are smaller than a[lp],
       * and every element to the right is larger. */

      quicksort(a, left, lp - 1);
      quicksort(a, lp + 1, right);
}

static void swap(int *x, int *y)
{
      int temp = *x;
      *x = *y;
      *y = temp;
}



Hope you can help.

Terje.
Avatar of grg99
grg99

A question:  Why do you want to do this?   It's hard and the benefits are few.  

 Most compiler nowdays can generate very good code, especially for this kind of simple stuff, just some if()s and calls.   In assembly you'd be doing pretty much the same thing.  There's no advantage to the usual trick of keeping values in registers (because of the recursion, you have to push/pop all of them anyway), or unrolling complex loops (there arent any), or using SIMD instructions (not much data to process in parallel here).

One sure way is to use a C compiler and capture its output (-S option IIRC).








ASKER CERTIFIED SOLUTION
Avatar of Dawaffleman
Dawaffleman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DanRollins
No ASM version of this will be any faster than optimized code generated by a C compiler.
-- Dan
and infact, C code can usually be faster, because it is easier to be complicated.  You can do tricks like  heapsorting small partitions because once the item count gets small, the algorithmic advantages of quicksort give way to the fewer, simpler, perhaps even more cache friendly heapsort functions.

You can do it in assembly too, but its that much longer, and that much more work to hand optimize.

Also, C compilers can take advantage of its knowledge of the computer.  It can see when a LEA can be used to speed up a pointer access.  It can see where a stall occurs, and often re-order things to sneak around it, all invisible to you.  A perfect example is in MIPS.  In MIPS, a branch can take quite a while, so it offers a delayed branch, where the processor executes the instruction immediatly after the branch, no matter whether or not it takes the branch.  This is usually filled in (by the compiler), with a NOP, but if it can execute the first instruciton of one branch, without affecting the outcome of the second, it will put it in there.
Handling that in assembly is a pain in the butt.

but if you want asm anyway, use the S switch to produce assembly code.
eg,
gcc -o abc -S -O3 xyz.c (for gcc)
tcc -S xyz.c
 
IIRC you have /Ox switch in Vc++ to optimize the code.  
  -Ox in gcc.

regards manish
Um,  I'm a bit peevd about your accepting that particular answer.  There's not a chance the answer is particularly faster than C compiler generated code, it doesnt use any special asm-like tricks, like loop unrolling or in-line function expansion, or MMX instructions, or block move instructions.  Not to mention it's basically not correct, as you can't use global variables for a function that is recursive.  It just won't work.  The whole thing has to be redone, using stack-based variables and parameters.



i think it was a good attempt by me do do what the author wanted. it may not be perfect but he asked for assembly code translated from C++ code and that is what i give him. if it doesnt work properly id be willing to rework it. besides he didnt want it to be neccissarily faster...
True.  We always assume that the Asker must want speed... but another valid reason for this request would be to learn ASM and there may be other reasons.

tjodolf,
Would you care to post a comment to us?  Or do you think we are mindless automata to do your bidding?

-- Dan