home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 5 / ctrom5b.zip / ctrom5b / PROGRAM / ASM / ALIB30A / SORT.ASM < prev    next >
Assembly Source File  |  1994-11-15  |  4KB  |  180 lines

  1. ;*****************************   SORT.ASM  **********************************
  2. PAGE  70,132
  3. comment 
  4.                                 SORT
  5.                              -------------
  6.  
  7.      Purpose:
  8.      --------
  9.  
  10.      Sort disk file using the "merge sort" algorithm.
  11.  
  12.      Using sort
  13.      ----------
  14.      SORT <file> <starting column> <length of sort field>
  15.  
  16.      SORT reads the given file, sorts the records using the specified
  17.      column.  One line ending with cr/lf is assumed to be one record.
  18.      
  19.      Example:  To sort all TEST.DAT using columns 1-5.
  20.  
  21.                SORT test.dat 1 5
  22.  
  23.  
  24.      Compiling
  25.      ---------
  26.  
  27.      The commands needed to build sort.EXE using MASM are:
  28.         masm sort;
  29.         link sort,sort,,alib.lib;
  30. 
  31.      
  32.     include    mac.inc
  33.     include    common.inc
  34. ;-----------------------------------------------------------------------------
  35.     extrn    library_setup:far
  36. ;;    extrn    lib_error_handler:far
  37.     extrn    library_terminate:far    
  38.     extrn    parse_first:far
  39.     extrn    parse_next:far
  40.     extrn    merge_sort_file:far
  41.     extrn    message:far
  42. ;------------------------------------------------------------------------------
  43. code        segment para public 'CODE'
  44.         assume    cs:code, ds:code
  45. ;-----------------------------------------------------------------------------
  46. ;
  47.  
  48. pspseg        dw    0
  49. lib_info_ptr    dw    0
  50. lib_info_seg    dw    0
  51.  
  52. file_name    db    40 dup (0)
  53. starting_column    dw    0
  54. field_length    dw    0
  55.  
  56.         dw    300 dup (?)        ;stack
  57. stack_        dw    0
  58. ;-----------------------------------------------------------------------------
  59. start:
  60.     cli
  61.     mov    cs:pspseg,es    ;save PSP segment
  62.     mov    ax,cs        ;get CODE segment
  63.     mov    ss,ax
  64.     mov    ds,ax
  65.     mov    es,ax
  66.     mov    sp,offset stack_
  67.     sti
  68.     
  69. ; next, release memory beyond the end of the program
  70. ; The  definition for ZSEG marks the
  71. ; end of the program's code, data and stack area.
  72. ; When linking be sure ZSEG is at the end of the program.
  73.  
  74.     mov    ax,zseg
  75.  
  76.     mov    bx,cs:pspseg        ;
  77.     mov    es,bx
  78.     sub    bx,ax
  79.     neg    bx            ; size of program in paragraphs
  80.     mov    ah,4Ah            ; resize memory block
  81.     int    21h
  82.  
  83.     mov    ax,cs
  84.     mov    es,ax
  85. ;
  86. ; check if enough memory free to run program
  87. ;
  88.     mov    ax,pspseg        ;pass psp segment to setup
  89.     mov    bx,8            ;number of floating point variables
  90.     call    library_setup
  91.     mov    lib_info_ptr,si        ;save ptr to library info block
  92.     mov    lib_info_seg,es         ; see COMMON.INC or LIBRARY_SETUP
  93.     cmp    ax,128
  94.     jae    got_enough_mem        ;jmp if 128k of memory avail
  95. ;;    mov    al,7
  96. ;;    mov    ah,fatal_return
  97. ;;    call    lib_error_handler
  98.     jmp    exitx
  99.     
  100. got_enough_mem:
  101.     push    ds
  102.     pop    es
  103.  
  104.     mov    di,offset file_name
  105.     call    parse_first
  106.     cmp    bh,7
  107.     jne    parm_err
  108.  
  109.     mov    di,offset starting_column
  110.     call    parse_next
  111.     cmp    ax,0
  112.     je    parm_err
  113.     dec    ax
  114.     mov    starting_column,ax
  115.     cmp    bh,8
  116.     jne    parm_err
  117.  
  118.     mov    di,offset field_length
  119.     call    parse_next
  120.     cmp    ax,0
  121.     je    parm_err
  122.     cmp    ax,50
  123.     ja    parm_err
  124.     mov    field_length,ax
  125.     cmp    bh,8
  126.     jne    parm_err
  127.  
  128.     mov    di,offset file_name
  129.     mov    bx,0            ;variable len records
  130.     mov    al,0ah            ;end of record char
  131.     mov    dx,starting_column
  132.     mov    ah,byte ptr field_length
  133.     call    merge_sort_file    
  134.     jmp    exitx
  135.  
  136. error_msg    label    byte
  137.  db 'Parameter error,  SORT must be called as follows:',0dh,0ah
  138.  db '  ',0dh,0ah
  139.  db 'SORT <file> <first sort column>  <length of sort field>',0dh,0ah
  140.  db '  ',0dh,0ah
  141.  db 'Press any key to continue'
  142.  db 0
  143.  
  144. parm_err:
  145.     mov    al,0            ;message ends with zero
  146.     mov    bh,9            ;rows in box
  147.     mov    bl,60            ;columns in box
  148.     mov    dx,040ah        ;box position row/column
  149.     mov    si,offset error_msg
  150.     mov    bp,msg_save_win+msg_restore_win+msg_anykey+msg_ram+msg_disp
  151.     call    message
  152.         
  153.     
  154. ; normal program exit
  155.  
  156. exitx:    mov    ax,1
  157.     call    library_terminate
  158.     mov    ax,4C00h
  159.     int    21h
  160.  
  161. code        ends
  162.  
  163. ;-------------------------------------------------------------------------
  164. ;
  165. ; This segment definition is needed so linker will put the LIBSEG here
  166. ; before the ZSEG.  We want ZSEG to be last so memory allocation will
  167. ; work correctly.
  168. ;
  169. LIBSEG           segment byte public 'LIB'
  170. LIBSEG    ENDS
  171. ;-------------------------------------------------------------------------
  172. ; zseg must be at the end of the program for memory allocation from
  173. ; DOS.
  174. ;
  175. zseg    segment    para public 'ZZ'
  176.  
  177. zseg    ends
  178.  
  179.         end    start
  180.