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 */

      /* 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)

            if (lp >= rp)

            /* 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.

Who is Participating?
;  first you need to start off and declare your variables and your proccessor/model
; i use model medium because it is good for small projects and easy to work with
; ill use tasm code but it should be very easy to convert to masm (or nasm)

.model medium
.386                          ;386 instructions should be sufficient
.stack 500h                ; a good size stack for recursions might need to make this bigger
.data                         ; declair your data section (if you are using tasm, otherwise it is different)
                                ; put all of your variables here that you will use throught the program
lp          dw    ?          ;you use    (variable name)    (size)    (value)    form
rp          dw   ?          ;use question mark to set as undefined
v           dw   ?           ; you might want to use db (declare byte) here instead of dw (declare word)
right      dw   ?
left        dw     ?
x           dw    ?
y           dw    ?
temp     dw    ?
a           dw    ? dup (?)   ; this is an array with ? sets of ? (i dont know if this will work right, it  
                                     ; might be safer to put 100 dup (?) just so the assembler knows how
                                     ; many blocks there are, but you can try it either way.
;functions here-----------------------------------------------------------------------------------

quicksort proc                 ; name of procedure followed by 'proc'

                                     ;im not sure exactly how your code works so i will try my best to convert it
                                     ; in asm you dont need to pass your variables because they are all global
                                     ;(i think) so you are changing the actual value of the variable (its like
                                     ; passing it by reference) so if you dont want that to happen use separate
                                     ;variables just in your functions. heres the rest of the function:
                                     ;initializing variables here
mov ax,left                     ; form:  (move) (destination),(source)
sub ax,1                         ;form:   (sub) (destination),(source)  outcome is dest=dest-source
mov lp,ax                        ; these three steps=     lp = left - 1

mov ax,right                   ;you cant move memory to memory in assembly so it needs to go
mov rp,ax                      ;through a register first, like i did here

mov si,offset a         ;this is how you get a pointer. it points to a in memory (si is a good pointer
add si,[right*2]        ;register) now we add a + right * sizeof word (which is 2)
mov v,si                   ;si had a[right] in it and moved it to variable 'v'

if1:                         ;just to make it look better not neccissary here
mov ax,right            ;i compare using a register and a memory i think you can do mem & mem tho
cmp ax,left              ;this will set flags based on the outcome of the comparison
jle return                 ;this jumps if lessthan or equal, to return. sometimes you need jbe instead

mainloop:              ;while(1) always returns true correct? so this is an endless loop

while2:                   ;ill name the while loops to make it easier to comprehend
inc lp                      ;increase lp by 1 (++lp)
mov si,offset a
add si,[lp*2]           ;a[++lp]
cmp si,v                  ; < v
jge return                ;if a[++lp]<v we want to continue otherwise we will jump to the end  

dec rp                    ;--rp    we are on the next while loop now
mov si,offset a
add si,[rp*2]          ;a[--rp]
cmp v,si
jge while2                    ;if v>a[--rp] than skip this part here and return to while 2, otherwise do it

mov ax,rp
cmp ax,left             ;see if rp==left
je endofwhile         ;this will simulate a break statement and will jump to the end of the while
                            ;statements (i think this is how you want it)
if3:                        ;fall through to if statment number 3
mov ax,lp
cmp ax,rp             ;if ax>= rp then 'break'
jge endofwhile

mov si,offset a
add si,[lp*2]
mov x,si                ;you have to set up the variables before you call the function
mov si,offset a
add si,[rp*2]
mov y,si               ;now the proc is ready to be called because x has a[lp] and y has a[rp]
call swap              ;if all that is good we will call swap. notice to use a procedure you type
                           ; (call) (procedure name)
jmp mainloop        ;jump to main loop (while) if everything was ok

mov si,offset a
add si,[lp*2]
mov x,si                ;you have to set up the variables before you call the function
mov si,offset a
add si,[right*2]
call swap

                            ;when you first call quicksort you have to set the variables up in this fashion:
mov ax,left
mov left,ax            ;sets up second parameter
dec lp                   ;lp-1
mov ax,lp
mov right, ax         ;third parameter is set up now, so we can call the proc now
                            ;we dont need to set up the first parameter because that would be pointless
                            ;to set an array to an array just copy your array to the 'a' array if you have to  
call quicksort

mov ax,right
mov right,ax            ;set up 3rd parameter
inc lp                      ;lp+1
mov ax,lp
mov left,ax              ;set up and ready to go
call quicksort

return:                   ;point of returning to the program
ret                         ; this pops the cs:ip off the stack to return to the caller
endp                      ;says this is end of procedure (function)

;next procedure here-----------------------------------------

swap proc

mov ax,[x]       ;move to ax whats pointed at by x
mov temp,ax    ;temp = x
mov bx,[y]
mov [x],bx       ;move y to what x is pointing to (im not sure if you need to brackets)
mov ax,temp
mov [y],ax      ;move temp to what y points at

endp             ;end of swap procedure

i hope this works good for you, you might need a few minor adjustments tho. your code was a little confusing and i think you might have made a few mistakes in it, but that might be just me. i wasnt really sure where your while statments were SUPPOSED to end so i made a guess of what i thought was supposed to happen there.
if it doesnt work comment your C++ code more and tell me what the assembler errors were so i can fix it hopefully :)

one thing is for sure, this is a difficult way to do things and you should only do it if its absolutly neccissary ofr speed or otherwise.
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).

No ASM version of this will be any faster than optimized code generated by a C compiler.
-- Dan
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
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.

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

-- Dan
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.

All Courses

From novice to tech pro — start learning today.