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

  1. ;***************************** SORT12.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.     extrn    sort_column:word
  12.     extrn    last_sort_column:word
  13.     extrn    sort_field_len:word
  14.     extrn    index_length:word
  15. .list
  16. comment 
  17. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(  SORT   )
  18. ; selection_sort - sort indexed buffer. 
  19. ;  inputs:  ds:0  - ptr to index struc. <word buf_ptr>, <word record_length>
  20. ;           es:   - buffer seg,(index has offsets)
  21. ;              sort_field_len - length of sort field
  22. ;              dx - starting column of sort field
  23. ;
  24. ;  Output:  index structure is sorted in decending order
  25. ;
  26. ;  Notes:   The index must have at least one entry.
  27. ;           Short or empty records are placed at top of index without
  28. ;           attempting to sort.
  29. ;
  30. ;*********************
  31. 
  32. indx_seg    dw    0
  33. buf_seg        dw    0
  34.  
  35.     public    selection_sort
  36. selection_sort    proc    near
  37.     cld
  38.     cmp    cs:index_length,4
  39.     jbe    sort_exit
  40.     mov    bp,0
  41.     mov    cs:sort_column,dx
  42.     add    dx,cx
  43.     mov    cs:last_sort_column,dx
  44.  
  45.     mov    cs:indx_seg,ds
  46.     mov    cs:buf_seg,es
  47.  
  48.     mov    dx,bp
  49. sn_loop1:
  50.     mov    bp,dx
  51.     mov    bx,dx
  52. ;
  53. ; dx = top of outside loop index ptr
  54. ; bp = current smallest index ptr
  55. ; bx+4 = next challenger for smallest
  56. ;
  57. sn_loop2a:
  58.     mov    si,word ptr ds:[bp]    ;point si at lowest text string
  59.     add    si,cs:sort_column    ;move to sort column
  60.     mov    ax,word ptr ds:[bp+2]    ;get lenght of si string
  61.     cmp    ax,cs:last_sort_column
  62.     jb    make_bp_top         ;jmp if incomplete sort field
  63.  
  64. sn_loop2b:
  65.     add    bx,4            ;move to challenger
  66.     cmp    word ptr ds:[bx],-1
  67.     je    make_bp_top        ;jmp if inside loop2 done
  68.     mov    di,word ptr ds:[bx]    ;point di at challenger
  69.     add    di,cs:sort_column    ;move to sort column
  70.     mov    ax,word ptr ds:[bx+2]
  71.     cmp    ax,cs:last_sort_column
  72.     jb    force_bx_top        ;jmp if incomplete sort field
  73.     
  74.     mov    cx,cs:sort_field_len
  75.     mov    ds,cs:buf_seg
  76.     push    si
  77.     repe    cmpsb
  78.     pop    si
  79.     mov    ds,cs:indx_seg
  80.     jbe    sn_loop2b        ;jmp if ignoring challenger
  81. make_bx_top:
  82.     mov    bp,bx
  83.     jmp    sn_loop2a        ;go setup new challenger
  84. ;
  85. ; put bx on top and break out of inner loop, move on
  86. ;
  87. force_bx_top:
  88.     mov    bp,bx
  89. ;
  90. ; inner loop2 has completed one pass, set new smallest and bump -dx-
  91. ; dx=top ptr  bp=new smallest  bx=scratch
  92. ;
  93. make_bp_top:
  94.     mov    bx,dx
  95.     mov    ax,word ptr ds:[bp]    ;get old lowest
  96.     xchg    ax,word ptr ds:[bx]    ;put at challenger's space
  97.     mov    word ptr ds:[bp],ax    ;put challenger at top
  98.  
  99.     mov    ax,word ptr ds:[bp+2]
  100.     xchg    ax,word ptr ds:[bx+2]
  101.     mov    word ptr ds:[bp+2],ax
  102. sn_done2:
  103.     add    dx,4
  104.     cmp    word ptr ds:[bp+4],-1
  105.     jne    sn_loop1        ;if not at end go do it again
  106.         
  107. sort_exit:    
  108.     ret
  109. selection_sort    endp
  110.  
  111. LIBSEG    ENDS
  112.     end
  113.