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

  1. ;***************************** SORT13.ASM ***********************************
  2.  
  3. LIBSEG           segment byte public "LIB"
  4.         assume cs:LIBSEG , ds:nothing
  5.  
  6. ;----------------------------------------------------------------------------
  7. .xlist
  8.     include  mac.inc
  9.     include  common.inc
  10.  
  11. .list
  12. comment 
  13. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(  SORT   )
  14. selection_sort_arrayw  - sort word array (integers)
  15. ;  inputs:  ds/es:si - ptr to array of words
  16. ;                 dx - number of items in array (range 0-32767)
  17. ;           direction flag is in CLD state.
  18. ;
  19. ;  Output:  array is sorted in decending order
  20. ;           Registers  ax,bx,cx,dx,si,di are modified
  21. ;
  22. ;  Note:   code size is 29 bytes.
  23. ;*********************
  24. 
  25.  
  26.     public    selection_sort_arrayw
  27.  
  28. do_swap:    lea    bx,ds:[di-2]    ;get  [di-2]
  29.         mov    ax,ds:[bx]    ;get new smallest
  30.         dec    cx        ;do not put "jmp" here, too slow
  31.         jcxz    tail        ;jmp if one pass done
  32. loop1:        scasw            ;compare
  33.         ja    do_swap        ;jmp if new smallest
  34.         loop    loop1        ;jmp if more data
  35. tail:        xchg    ax,ds:[si-2]    ;put smallest
  36.         mov    [bx],ax        ;  at top
  37.  
  38. selection_sort_arrayw    proc    far
  39.         mov    bx,si        ;set smallest ptr -bx-
  40.         lodsw            ;get smallest
  41.         mov    di,si        ;set moving compare ptr
  42.         dec    dx        ;adjust loop count
  43.         mov    cx,dx
  44.         jg    loop1        ;loop till done
  45.         retf            
  46. selection_sort_arrayw    endp
  47.  
  48. LIBSEG    ENDS
  49.     end
  50.