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

  1. ;***************************** SORT32.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. ; bubble_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. exchange_flag    db     0
  33.  
  34.     public    bubble_sort
  35. bubble_sort    proc    near
  36.     cld
  37.     cmp    cs:index_length,4
  38.     jbe    sort_exit
  39.     mov    cs:sort_column,dx
  40.     add    dx,cx
  41.     mov    cs:last_sort_column,dx
  42.     mov    dx,cs:sort_column
  43. sn_loop1:
  44.     mov    bp,0
  45.     mov    cs:exchange_flag,0
  46.     mov    bx,bp
  47.     add    bx,4
  48. ;
  49. ; registers -  bp - index ptr with lowest name  (si points at name)
  50. ;              bx - index of challenger         (di points at challenger name)
  51. ;
  52. sn_loop2:
  53.     cmp    word ptr ds:[bx],-1    ;check if done
  54.     je    sn1_done
  55.     mov    si,word ptr ds:[bp]    ;point si at lowest text string
  56.     add    si,dx            ;move to sort column
  57.     mov    ax,word ptr ds:[bp+2]    ;get lenght of si string
  58.     cmp    ax,cs:last_sort_column
  59.     jb    next_challenger        ;jmp if incomplete sort field
  60.     mov    di,word ptr ds:[bx]    ;point di at challenger
  61.     add    di,dx            ;move to sort column
  62.     mov    ax,word ptr ds:[bx+2]
  63.     cmp    ax,cs:last_sort_column
  64.     jb    put_on_top        ;jmp if incomplete sort field
  65.     mov    cx,cs:sort_field_len
  66.     push    ds            ;save index seg
  67.     push    es            ;set
  68.     pop    ds            ;  ds=es
  69.     repe    cmpsb            ;check the strings
  70.     pop    ds            ;restore index seg
  71.     jbe    next_challenger
  72. put_on_top:
  73.     mov    ax,word ptr ds:[bp]    ;get old lowest
  74.     xchg    ax,word ptr ds:[bx]    ;put at challenger's space
  75.     mov    word ptr ds:[bp],ax    ;put challenger at top
  76.  
  77.     mov    ax,word ptr ds:[bp+2]
  78.     xchg    ax,word ptr ds:[bx+2]
  79.     mov    word ptr ds:[bp+2],ax
  80.     mov    cs:exchange_flag,1
  81. next_challenger:
  82.     add    bx,4
  83.     add    bp,4
  84.     jmp    sn_loop2
  85. sn1_done:
  86.     cmp    cs:exchange_flag,0
  87.     jne    sn_loop1
  88. sort_exit:    
  89.     ret
  90. bubble_sort    endp
  91.  
  92. LIBSEG    ENDS
  93.     end
  94.