Assembly: Bubble Sort

Discussion in 'Computer Science & Culture' started by davyne*dutchess, Jun 2, 2003.

Thread Status:
Not open for further replies.
  1. davyne*dutchess Registered Member

    Messages:
    7
    Group needs help on below assignment:

    Add a variable to the below bubble sort procedure that is set to 1 whenever a pair of values is exchanged within the inner loop. use the variable to exit the sort before its normal completion.
    extras:
    -place values into a table
    -run with 50 numbers (random array possibly)
    -run comparison drill

    ;---------------------------------------------------------------------------
    BubbleSort PROC USES eax ecx esi'
    pArray

    Please Register or Log in to view the hidden image!

    TR DWORD' ; ointer array
    Count

    Please Register or Log in to view the hidden image!

    WORD ; rray size

    ;
    'Sort an array of 32-bit signed integers in ascending
    ;order, using the bubble sort algorithm.
    ;Receives: pointer to array, array size
    ;Returns:nothing
    ;---------------------------------------------------------------------------

    mov ecx,Count
    dec ecx ; decrement count by 1

    L1: push ecx ; save outer loop count
    mov esi,pArray ; point to first value

    L2: mov eax, [esi] ; get array value
    cmp [esi+4],eax ; compare a pair of values
    jge L3 ; if [esi] <= [edi], don't exch
    xchg eax, [esi+4] ; exchange the pair
    mov [esi], eax
    L3: add esi, 4 ; move both pointers forward
    loop L2 ; inner loop

    pop ecx ; retrieve outer loop count
    loop L1 ; else repeat outer loop

    L4: ret
    BubbleSort ENDP
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. davyne*dutchess Registered Member

    Messages:
    7
    sorry about the mistake

    first half of procedure is:


    BubbleSort PROC USES eax ecx esi,
    pArray

    Please Register or Log in to view the hidden image!

    TR DWORD', ; pointer array
    Count

    Please Register or Log in to view the hidden image!

    WORD ; array size
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. SG-N Registered Senior Member

    Messages:
    1,051
    Code:
    ;---------------------------------------------------------------------------
    BubbleSort PROC USES eax ebx ecx esi'
       pArray:PTR DWORD'                        ; pointer array
       Count: DWORD                             ; array size
    
    ;Sort an array of 32-bit signed integers in ascending
    ;order, using the bubble sort algorithm.
    ;Receives: pointer to array, array size
    ;Returns:nothing
    ;---------------------------------------------------------------------------
    
       mov ecx,Count
       dec ecx                                  ; decrement count by 1
     
       L1: push ecx                             ; save outer loop count
           mov esi,pArray                       ; point to first value
           mov ebx, 0			    ; init the exit var
    
       L2: mov eax, [esi]                       ; get array value
           cmp [esi+4],eax                      ; compare a pair of values
           jge L3                               ; if [esi]  <= [edi],  don't exch
           xchg eax, [esi+4]                    ; exchange the pair
           mov [esi], eax			    
           cmp ebx,1			    ; if ebx = 1, exit
           je L4
    
       L3: add esi, 4                           ; move both pointers forward
           loop L2                              ; inner loop
    
           pop ecx                              ; retrieve outer loop count
           loop L1                              ; else repeat outer loop
    
       L4: ret
       BubbleSort  ENDP
    
    Here is one way to answer the first question, but is it a beginner course or is there a hidden trap I didn't saw? Anyway, I added a counter ebx that will stop the procedure as soon as one exchange will have been done. You can give it a name if you want (do as with Count). An optimised version will not use ebx : just delete the added lines and replace je L4 by jmp L4.
    On more thing that could be done : in my modifications, you could replace je L4 by a jump to pop ecx. Just have a look on what it would do... However, I don't remember if it would be a BubbleSort anymore.
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. davyne*dutchess Registered Member

    Messages:
    7
    Bub Help

    It's a beginner course, nothing hidden. But I'm sure this will get us on our way. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page