home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / dos_ency / 14 / execsort.asm next >
Encoding:
Assembly Source File  |  1988-08-11  |  8.0 KB  |  193 lines

  1.         name    execsort
  2.         title   'EXECSORT --- demonstrate EXEC of filter'
  3.         .sall
  4. ;
  5. ; EXECSORT.ASM --- demonstration of use of EXEC to run the SORT
  6. ; filter as a child process, redirecting its input and output.
  7. ; This program requires the files SORT.EXE and MYFILE.DAT in
  8. ; the current drive and directory.
  9. ; Ray Duncan, June 1987
  10. ;
  11.  
  12. stdin   equ     0                       ; standard input
  13. stdout  equ     1                       ; standard output
  14. stderr  equ     2                       ; standard error
  15.  
  16. stksize equ     128                     ; size of stack
  17.  
  18. cr      equ     0dh                     ; ASCII carriage return
  19. lf      equ     0ah                     ; ASCII linefeed
  20.  
  21. jerr    macro   target                  ;; Macro to test carry flag
  22.         local   notset                  ;; and jump if flag set.
  23.         jnc     notset                  ;; Uses JMP DISP16 to avoid
  24.         jmp     target                  ;; branch out of range errors
  25. notset:
  26.         endm
  27.  
  28.  
  29. DGROUP  group   _DATA,_STACK            ; 'automatic data group'
  30.  
  31.  
  32. _TEXT   segment byte public 'CODE'      ; executable code segment
  33.  
  34.         assume  cs:_TEXT,ds:DGROUP,ss:_STACK
  35.  
  36.  
  37. stk_seg dw      ?                       ; original SS contents
  38. stk_ptr dw      ?                       ; original SP contents 
  39.  
  40.  
  41. main    proc    far                     ; entry point from MS-DOS
  42.  
  43.         mov     ax,DGROUP               ; set DS = our data segment
  44.         mov     ds,ax
  45.  
  46.                                         ; now give back extra memory so
  47.                                         ; child SORT has somewhere to run...
  48.         mov     ax,es                   ; let AX = segment of PSP base
  49.         mov     bx,ss                   ; and BX = segment of stack base
  50.         sub     bx,ax                   ; reserve seg stack - seg psp 
  51.         add     bx,stksize/16           ; plus paragraphs of stack
  52.         mov     ah,4ah                  ; fxn 4AH = modify memory block
  53.         int     21h                     ; transfer to MS-DOS
  54.         jerr    main1                   ; jump if resize block failed
  55.  
  56.                                         ; prepare stdin and stdout
  57.                                         ; handles for child SORT process
  58.  
  59.         mov     bx,stdin                ; dup the handle for stdin
  60.         mov     ah,45h
  61.         int     21h                     ; transfer to MS-DOS
  62.         jerr    main1                   ; jump if dup failed
  63.         mov     oldin,ax                ; save dup'd handle     
  64.  
  65.         mov     dx,offset DGROUP:infile ; now open the input file
  66.         mov     ax,3d00h                ; mode = read-only
  67.         int     21h                     ; transfer to MS-DOS
  68.         jerr    main1                   ; jump if open failed
  69.  
  70.         mov     bx,ax                   ; force stdin handle to
  71.         mov     cx,stdin                ; track the input file handle
  72.         mov     ah,46h
  73.         int     21h                     ; transfer to MS-DOS
  74.         jerr    main1                   ; jump if force dup failed
  75.  
  76.         mov     bx,stdout               ; dup the handle for stdout
  77.         mov     ah,45h
  78.         int     21h                     ; transfer to MS-DOS
  79.         jerr    main1                   ; jump if dup failed
  80.         mov     oldout,ax               ; save dup'd handle
  81.  
  82.         mov     dx,offset dGROUP:outfile ; now create the output file
  83.         mov     cx,0                    ; normal attribute
  84.         mov     ah,3ch
  85.         int     21h                     ; transfer to MS-DOS 
  86.         jerr    main1                   ; jump if create failed
  87.  
  88.         mov     bx,ax                   ; force stdout handle to
  89.         mov     cx,stdout               ; track the output file handle
  90.         mov     ah,46h
  91.         int     21h                     ; transfer to MS-DOS
  92.         jerr    main1                   ; jump if force dup failed
  93.  
  94.                                         ; now EXEC the child SORT, 
  95.                                         ; which will inherit redirected
  96.                                         ; stdin and stdout handles
  97.  
  98.         push    ds                      ; save EXECSORT's data segment
  99.         mov     stk_seg,ss              ; save EXECSORT's stack pointer
  100.         mov     stk_ptr,sp
  101.  
  102.         mov     ax,ds                   ; set ES = DS
  103.         mov     es,ax
  104.         mov     dx,offset DGROUP:cname  ; DS:DX = child pathname
  105.         mov     bx,offset DGROUP:pars   ; EX:BX = parameter block
  106.         mov     ax,4b00h                ; function 4BH, subfunction 00H
  107.         int     21h                     ; transfer to MS-DOS
  108.  
  109.         cli                             ; (for bug in some early 8088s)
  110.         mov     ss,stk_seg              ; restore execsort's stack pointer
  111.         mov     sp,stk_ptr
  112.         sti                             ; (for bug in some early 8088s)
  113.         pop     ds                      ; restore DS = our data segment
  114.  
  115.         jerr    main1                   ; jump if EXEC failed
  116.  
  117.         mov     bx,oldin                ; restore original meaning of
  118.         mov     cx,stdin                ; standard input handle for
  119.         mov     ah,46h                  ; this process
  120.         int     21h
  121.         jerr    main1                   ; jump if force dup failed
  122.  
  123.         mov     bx,oldout               ; restore original meaning
  124.         mov     cx,stdout               ; of standard output handle
  125.         mov     ah,46h                  ; for this process
  126.         int     21h
  127.         jerr    main1                   ; jump if force dup failed
  128.  
  129.         mov     bx,oldin                ; close dup'd handle of 
  130.         mov     ah,3eh                  ; original stdin
  131.         int     21h                     ; transfer to MS-DOS
  132.         jerr    main1                   ; jump if close failed
  133.  
  134.         mov     bx,oldout               ; close dup'd handle of 
  135.         mov     ah,3eh                  ; original stdout
  136.         int     21h                     ; transfer to MS-DOS
  137.         jerr    main1                   ; jump if close failed
  138.  
  139.                                         ; display success message
  140.         mov     dx,offset DGROUP:msg1   ; address of message
  141.         mov     cx,msg1_len             ; message length
  142.         mov     bx,stdout               ; handle for standard output
  143.         mov     ah,40h                  ; fxn 40H = write file or device
  144.         int     21h                     ; transfer to MS-DOS
  145.         jerr    main1
  146.         
  147.         mov     ax,4c00h                ; no error, terminate program 
  148.         int     21h                     ; with return code = 0
  149.  
  150. main1:  mov     ax,4c01h                ; error, terminate program
  151.         int     21h                     ; with return code = 1 
  152.  
  153. main    endp                            ; end of main procedure
  154.  
  155. _TEXT   ends
  156.  
  157.  
  158. _DATA   segment para public 'DATA'      ; static & variable data segment
  159.  
  160. infile  db      'MYFILE.DAT',0          ; input file for SORT filter
  161. outfile db      'MYFILE.SRT',0          ; output file for SORT filter
  162.  
  163. oldin   dw      ?                       ; dup of old stdin handle
  164. oldout  dw      ?                       ; dup of old stdout handle      
  165.  
  166. cname   db      'SORT.EXE',0            ; pathname of child SORT process
  167.  
  168. pars    dw      0                       ; segment of environment block
  169.                                         ; (0 = inherit parent's)
  170.         dd      tail                    ; long address, command tail
  171.         dd      -1                      ; long address, default FCB #1
  172.                                         ; (-1 = none supplied)
  173.         dd      -1                      ; long address, default FCB #2
  174.                                         ; (-1 = none supplied)
  175.  
  176. tail    db      0,cr                    ; empty command tail for child
  177.  
  178. msg1    db      cr,lf,'SORT was executed as child.',cr,lf
  179. msg1_len equ    $-msg1 
  180.  
  181. _DATA   ends
  182.  
  183.  
  184. _STACK  segment para stack 'STACK'
  185.  
  186.         db      stksize dup (?)
  187.  
  188. _STACK  ends
  189.  
  190.  
  191.         end     main                    ; defines program entry point
  192.