home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 5 / ctrom5b.zip / ctrom5b / PROGRAM / ASM / ALIB30B / SORT33.ASM < prev    next >
Assembly Source File  |  1994-11-12  |  1KB  |  54 lines

  1. ;***************************** SORT33.ASM ***********************************
  2.  
  3. LIBSEG           segment byte public "LIB"
  4.         assume cs:LIBSEG , ds:nothing
  5.  
  6. ;----------------------------------------------------------------------------
  7. .xlist
  8.     include  mac.inc
  9. .list
  10. comment 
  11. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(  SORT   )
  12. ; bubble_sort_arrayw - bubble sort array of words
  13. ;  inputs:  ds:di - ptr to word array
  14. ;              dx - number of elements in array
  15. ;
  16. ;  Output:  array is sorted in decending order
  17. ;           
  18. ;  Note:  bubble_sort_arrayw uses 43 bytes for code
  19. ;*********************
  20. 
  21.  
  22.     public    bubble_sort_arrayw
  23. bubble_sort_arrayw    proc    far
  24.     apush    ax,bx,cx,dx,si
  25.     cld
  26.     cmp    dx,1
  27.     jbe    sort_exit        ;exit if not enough data to sort
  28.     dec    dx
  29. sn_loop1:
  30.     mov    cx,dx            ;setup length of loop
  31.     xor    bx,bx            ;set exchange flag = 0
  32.     mov    si,di            ;start at top of buffer
  33. ;
  34. ; registers -  si=moving ptr
  35. ;
  36. sn_loop2:
  37.     lodsw                ;get next array value
  38.     cmp    ax,word ptr ds:[si]
  39.     jbe    no_swap            ;jmp if order ok
  40.     xchg    ax,word ptr [si]    ;swap order of array values
  41.     mov    word ptr [si-2],ax
  42.     inc    bx            ;set exchange flag = 1
  43. no_swap:
  44.     loop    sn_loop2
  45.     cmp    bx,0            ;check if sort done
  46.     jne    sn_loop1
  47. sort_exit:
  48.     apop    si,dx,cx,bx,ax
  49.     ret
  50. bubble_sort_arrayw    endp
  51.  
  52. LIBSEG    ENDS
  53.     end
  54.