flat assembler
Message board for the users of flat assembler.

Index > Windows > bubble sort in fasm

Author
Thread Post new topic Reply to topic
david botero



Joined: 10 Feb 2005
Posts: 1
david botero 10 Feb 2005, 22:03
Does anybody have the code for the bubble sort in fasm?
Post 10 Feb 2005, 22:03
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 10 Feb 2005, 22:25
I have QuickSort if you like it - its quicker Very Happy
Code:
push [lo] [hi]
call quicksort
;Exit
quicksort:                  ;void quicksort (int[] a, int lo, int hi)
    mov eax,[esp+8]         ;{
    mov edx,[esp+4]         ;//  lo is the lower index, hi is the upper index
    lea ecx,[eax+edx]       ;//  of the region of array a that is to be sorted
    shr ecx,3               ;    int i=lo, j=hi, h;
    shl ecx,2
    mov ecx,[a+ecx]         ;    int x=a[(lo+hi)/2];
  .qc1:                     ;
    cmp [a+eax],ecx         ;    //  partition
    jnc .qc2                ;    do
    add eax,4
    jmp .qc1                ;    {
  .qc2:                     ;        while (a[i]<x) i++;
    cmp ecx,[a+edx]         ;        while (a[j]>x) j--;
    jnc .qc3                ;        if (i<=j)
    sub edx,4               ;        {
    jmp .qc2                ;            h=a[i]; a[i]=a[j]; a[j]=h;
  .qc3:                     ;            i++; j--;
    cmp edx,eax             ;        }
    jc  .qc4                ;    } while (i<=j);
    push [a+eax] [a+edx]    ;
    pop  [a+eax] [a+edx]    ;    //  recursion
    add eax,4               ;    if (lo<j) quicksort(a, lo, j);
    sub edx,4               ;    if (i<hi) quicksort(a, i, hi);
    cmp edx,eax             ;}
    jnc .qc1
  .qc4:
    cmp  [esp+8],edx
    jnc  .qc5
    push dword [esp+8]
    push edx
    call quicksort
    pop  edx
    pop  dword [esp+12]
  .qc5:
    cmp  eax,[esp+4]
    jnc  .qc6
    push eax
    push dword [esp+8]
    call quicksort
    pop  dword [esp+12]
    pop  eax
  .qc6:
ret
align 4
a  dd 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3 ;16
lo dd 0
hi dd 15*4   
    


btw there is a search function in these forums
there are only 3 matces to bubble and one of them yours - I think you can find the time to read them through. The last one is very educational.
Bubble proc by Matrix
Code:
bubblesort:
         lea     ebx,[edi+ecx*4]
         mov     eax,[edi]
.cmploop:sub     ebx,4
         cmp     eax,[ebx]
         jle     .again
         xchg    eax,[ebx]
.again:  cmp     ebx,edi
         jnz     .cmploop
stosd
loop bubblesort
ret 
    


P.S. bubble is so trivial that you could try your hand and make it yourself.
Post 10 Feb 2005, 22:25
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 13 Feb 2005, 17:37
Hello,
i think someone may find this useful,
it whould be a little more complicated to make this little mod with a quicksort

Code:
org 100h

arraysize=10

;mov edi,a
mov ecx,arraysize
;call bubblesort
;call seebubblesort

mov edi,a2
call bubblesort64 ; dd index, dd pointer

mov cx,arraysize*2
mov si,a2
call writea

int 20h;
;You end up with {1,1,2,3,3,3,4,5,5,5,6,7,8,9,9,9} in a

writea: ; si=dword string cx=elements
dec cx
.loop:
lodsd
mov ah,$e
add al,'0'
int 10h
mov al,','
int 10h
loop .loop
lodsd
mov ah,$e
add al,'0'
int 10h
bendline:
mov ax,$e0d
int 10h
mov ax,$e0a
int 10h
;ret
ret


;
; bubble sort
; input: edi = pointer to array, ecx = number of indexes
; output: edi = pointer to sorted array
; destroys:  eax, edx, eflags
;
seebubblesort:
pusha
mov cx,arraysize
mov si,a
call writea
popa
         lea     ebx,[edi+ecx*4]
     mov     eax,[edi]
.cmploop:sub   ebx,4
       cmp     eax,[ebx]
   jae     .again
      xchg    eax,[ebx]
.again:  cmp   ebx,edi
     jnz     .cmploop
stosd
loop seebubblesort
ret

bubblesort:
       lea     ebx,[edi+ecx*4]
     mov     eax,[edi]
.cmploop:sub   ebx,4
       cmp     eax,[ebx]
   jle     .again
      xchg    eax,[ebx]
.again:  cmp   ebx,edi
     jnz     .cmploop
stosd
loop bubblesort
ret

bubblesort64: ; dd index, dd pointer
pusha
mov cx,arraysize*2
mov si,a2
call writea
popa

  lea     ebx,[edi+ecx*8]
     mov     eax,[edi]
.cmploop:sub   ebx,8
       cmp     eax,[ebx]
   jle     .again
      xchg    eax,[ebx]
   push    dword [ebx+4] dword [edi+4]
         pop     dword [ebx+4] dword [edi+4]
.again:  cmp         ebx,edi
     jnz     .cmploop
    stosd
       add     edi,4
       loop    bubblesort64
ret


a  dd 9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0 ;20
a2  dd 9,0,8,1,7,2,6,3,5,4,4,5,3,6,2,7,1,8,0,9, 9,0,8,1,7,2,6,3,5,4,4,5,3,6,2,7,1,8,0,9 ;20


    

( the array sorter drags the idexes with the pointers, pointers to anything, hint: filenames )
Post 13 Feb 2005, 17:37
View user's profile Send private message Visit poster's website Reply with quote
Nikolay Petrov



Joined: 22 Apr 2004
Posts: 101
Location: Bulgaria
Nikolay Petrov 17 Mar 2005, 19:14
Wink
but qsort is better


Description:
Download
Filename: rnd_loto.rar
Filesize: 1.57 KB
Downloaded: 887 Time(s)

Post 17 Mar 2005, 19:14
View user's profile Send private message Reply with quote
mike.dld



Joined: 03 Oct 2003
Posts: 235
Location: Belarus, Minsk
mike.dld 17 Mar 2005, 22:27
Post 17 Mar 2005, 22:27
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
at0mic



Joined: 09 Mar 2005
Posts: 12
at0mic 18 Mar 2005, 14:27
when the sortengine input values have the same size or lenght its not so hard to code something but

if you will code engine that can sort zero terminated string(different data size) or data with specified data size which is not multiple dd but multiple bits size then the isue will be more complicated. Confused

the curious task is to build an array with strings null terminated
when all chars are packet to 5 bits size so the next font has 3 bits in the same byte just like the previous font. Zero mark is also 5 bit lenght. So there is no byte alignment or any other alignment.


Yes, I hear you think: This is easy because we can unpack the array to another one with byte alignment.So do it and test yourself.

best regards

_________________
at0mic!
Post 18 Mar 2005, 14:27
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.