Hello!

I made a small bubble sort function just to learn and made as a lib (.a) to import in a C program. I am learning assembly, so I would like some advices on my code, because I feel that I am not doing it right, but its working :p Its very hard to find assembly stuff on the net ^-^

Here is my code:

; BUBBLE SORT ********************************************************************

segment .text
   global bubbleSort:function

bubbleSort:

bubble1:

   mov     dl,0                        ; swaped = false
   mov     ecx,dword           ; array index
   mov     eax,dword            ; size = n
   sub     eax,1                       ; last index is n-1
   mov     ebx,4                       ; integers are 4 bytes long
   mul     ebx

bubble2:

   mov     ebx,             ; get the array
   cmp     ebx,               ; compare with array
   jle     noswap                      ; if less or equal no swaping
   mov     dl,1                        ; swaped = true
   push    edx                          ; saving edx
   mov     edx,dword          ; aux = array
   mov     dword,ebx          ; array = array
   mov     ,edx             ; array = aux
   pop     edx                          ; restoring edx

noswap:

   sub     eax,4                       ; --i
   jnz     bubble2                     ; if i == 0 jump
   cmp     dl,0                        ; if nothing was swapped the alg is finished
   jne     bubble1

endbubble:

   ret                                 ; ended


Any advices, some mistakes I am making, or wrong or bad practices?
I use nasm to compile it and ar to generate the lib file (.a)

Thank you!
Posted on 2010-01-02 11:14:03 by bruno
By rights, a C-callable function should preserve ebx. If you push it at the beginning and pop it at the end, this will move the offsets to your parameters, of course... Trashing ebx *could* cause your code (well, the calling code) to fail in some cases.

You could replace the "mul" with a shift. The "mul" is affecting edx - which isn't hurting you, but isn't really "intended"...

Not tested, but it looks good to me, outside of that little nit.

Best,
Frank

Posted on 2010-01-02 13:17:43 by fbkotler
Thank you for attention!

I am going to implement some algs in assembly to learn it better :)
Do you have any good references to assembly stuff? Its hard to find good stuff related to assembly on internet!
I would like to find some tips to not loose performance in my assembly code, like using a var from memory to do things instead of using a register (register are faster, rigth?) Does someone have good tips?
Posted on 2010-01-02 16:57:27 by bruno