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

  1. ;***************************** SORT34.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_arrayd - bubble sort array of dwords
  13. ;  inputs:  ds:di - ptr to word array
  14. ;              dx - number of elements in array
  15. ;
  16. ;  Output:  if no carry array is sorted in decending order
  17. ;           carry = insufficient memory
  18. ;           
  19. ;  Note:  bubble_sort_arrayw uses 290 bytes for code
  20. ;*********************
  21. 
  22.  
  23.     public    bubble_sort_arrayd
  24. bubble_sort_arrayd    proc    far
  25.     apush    ax,bx,cx,dx,si
  26.     cld
  27.     cmp    dx,1
  28.     jbe    sort_exit        ;exit if not enough data to sort
  29.     sub    dx,1
  30. sn_loop1:
  31.     mov    cx,dx            ;setup length of loop
  32.     mov    bx,0            ;set exchange flag = 0
  33.     mov    si,di            ;start at top of buffer
  34. ;
  35. ; registers -  si=moving ptr
  36. ;
  37. sn_loop2:
  38.     mov    ax,word ptr ds:[si+2]
  39.     cmp    ax,word ptr ds:[si+6]
  40.     jb    no_swap
  41.     ja    do_swap
  42.     
  43.     mov    ax,word ptr ds:[si]    ;get next array value
  44.     cmp    ax,word ptr ds:[si+4]
  45.     jbe    no_swap            ;jmp if order ok
  46. do_swap:
  47.     mov    ax,word ptr ds:[si]    
  48.     xchg    ax,word ptr ds:[si+4]    ;swap order of array values
  49.     mov    word ptr ds:[si],ax
  50.  
  51.     mov    ax,word ptr ds:[si+2]
  52.     xchg    ax,word ptr ds:[si+6]
  53.     mov    word ptr ds:[si+2],ax
  54.     mov    bx,1            ;set exchange flag = 1
  55. no_swap:
  56.     add    si,4
  57.     loop    sn_loop2
  58.     cmp    bx,0            ;check if sort done
  59.     jne    sn_loop1
  60. sort_exit:
  61.     apop    si,dx,cx,bx,ax
  62.     ret
  63. bubble_sort_arrayd    endp
  64.  
  65. LIBSEG    ENDS
  66.     end
  67.