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

  1.          name      parent
  2.          title     'PARENT --- demonstrate EXEC call'
  3. ;
  4. ; PARENT.EXE --- demonstration of EXEC to run process
  5. ;
  6. ; Uses MS-DOS EXEC (Int 21H Function 4BH Subfunction 00H)
  7. ; to load and execute a child process named CHILD.EXE, 
  8. ; then displays CHILD's return code.
  9. ;
  10. ; Ray Duncan, June 1987
  11. ;
  12.  
  13. stdin   equ     0                       ; standard input
  14. stdout  equ     1                       ; standard output
  15. stderr  equ     2                       ; standard error
  16.  
  17. stksize equ     128                     ; size of stack
  18.  
  19. cr      equ     0dh                     ; ASCII carriage return
  20. lf      equ     0ah                     ; ASCII linefeed
  21.  
  22.  
  23. DGROUP  group   _DATA,_ENVIR,_STACK
  24.  
  25.  
  26. _TEXT   segment byte public 'CODE'      ; executable code segment
  27.  
  28.         assume  cs:_TEXT,ds:_DATA,ss:_STACK
  29.  
  30.  
  31. stk_seg dw      ?                       ; original SS contents
  32. stk_ptr dw      ?                       ; original SP contents 
  33.  
  34.  
  35. main    proc    far                     ; entry point from MS-DOS
  36.  
  37.         mov     ax,_DATA                ; set DS = our data segment
  38.         mov     ds,ax
  39.  
  40.                                         ; now give back extra memory
  41.                                         ; so child has somewhere to run...
  42.         mov     ax,es                   ; let AX = segment of PSP base
  43.         mov     bx,ss                   ; and BX = segment of stack base
  44.         sub     bx,ax                   ; reserve seg stack - seg psp 
  45.         add     bx,stksize/16           ; plus paragraphs of stack
  46.         mov     ah,4ah                  ; fxn 4AH = modify memory block
  47.         int     21h
  48.         jc      main1
  49.                                         ; display parent message ...
  50.         mov     dx,offset DGROUP:msg1   ; DS:DX = address of message
  51.         mov     cx,msg1_len             ; CX = length of message
  52.         call    pmsg
  53.  
  54.         push    ds                      ; save parent's data segment
  55.         mov     stk_seg,ss              ; save parent's stack pointer
  56.         mov     stk_ptr,sp
  57.  
  58.                                         ; now EXEC the child process...
  59.         mov     ax,ds                   ; set ES = DS
  60.         mov     es,ax
  61.         mov     dx,offset DGROUP:cname  ; DS:DX = child pathname
  62.         mov     bx,offset DGROUP:pars   ; EX:BX = parameter block
  63.         mov     ax,4b00h                ; function 4BH subfunction 00H
  64.         int     21h                     ; transfer to MS-DOS
  65.  
  66.         cli                             ; (for bug in some early 8088s)
  67.         mov     ss,stk_seg              ; restore parent's stack pointer
  68.         mov     sp,stk_ptr
  69.         sti                             ; (for bug in some early 8088s)
  70.         pop     ds                      ; restore DS = our data segment
  71.  
  72.         jc      main2                   ; jump if EXEC failed
  73.  
  74.                                         ; otherwise EXEC succeeded,
  75.                                         ; convert and display child's 
  76.                                         ; termination and return codes...
  77.         mov     ah,4dh                  ; fxn 4DH = get return code
  78.         int     21h                     ; transfer to MS-DOS
  79.         xchg    al,ah                   ; convert termination code
  80.         mov     bx,offset DGROUP:msg4a
  81.         call    b2hex
  82.         mov     al,ah                   ; get back return code
  83.         mov     bx,offset DGROUP:msg4b  ; and convert it
  84.         call    b2hex
  85.         mov     dx,offset DGROUP:msg4   ; DS:DX = address of message
  86.         mov     cx,msg4_len             ; CX = length of message
  87.         call    pmsg                    ; display it
  88.  
  89.         mov     ax,4c00h                ; no error, terminate program
  90.         int     21h                     ; with return code = 0
  91.  
  92. main1:  mov     bx,offset DGROUP:msg2a  ; convert error code
  93.         call    b2hex
  94.         mov     dx,offset DGROUP:msg2   ; display message 'Memory
  95.         mov     cx,msg2_len             ; resize failed...'
  96.         call    pmsg
  97.         jmp     main3
  98.  
  99. main2:  mov     bx,offset DGROUP:msg3a  ; convert error code
  100.         call    b2hex
  101.         mov     dx,offset DGROUP:msg3   ; display message 'EXEC
  102.         mov     cx,msg3_len             ; call failed...'
  103.         call    pmsg
  104.  
  105. main3:  mov     ax,4c01h                ; error, terminate program
  106.         int     21h                     ; with return code = 1
  107.  
  108. main    endp                            ; end of main procedure
  109.  
  110.  
  111. b2hex   proc    near                    ; convert byte to hex ASCII
  112.                                         ; call with AL = binary value
  113.                                         ;           BX = addr to store string
  114.         push    ax
  115.         shr     al,1
  116.         shr     al,1
  117.         shr     al,1
  118.         shr     al,1
  119.         call    ascii                   ; become first ASCII character
  120.         mov     [bx],al                 ; store it
  121.         pop     ax
  122.         and     al,0fh                  ; isolate lower 4 bits, which 
  123.         call    ascii                   ; become the second ASCII character
  124.         mov     [bx+1],al               ; store it
  125.         ret
  126. b2hex   endp
  127.  
  128.  
  129. ascii   proc    near                    ; convert value 00-0FH in AL
  130.         add     al,'0'                  ; into a "hex ASCII" character
  131.         cmp     al,'9'
  132.         jle     ascii2                  ; jump if in range 00-09H,
  133.         add     al,'A'-'9'-1            ; offset it to range 0A-0FH,
  134.  
  135. ascii2: ret                             ; return ASCII char. in AL
  136. ascii   endp
  137.  
  138.  
  139. pmsg    proc    near                    ; displays message on standard output
  140.                                         ; call with DS:DX = address,
  141.                                         ;              CX = length
  142.  
  143.         mov     bx,stdout               ; BX = standard output handle
  144.         mov     ah,40h                  ; function 40h = write file/device
  145.         int     21h                     ; transfer to MS-DOS
  146.         ret                             ; back to caller
  147.  
  148. pmsg    endp
  149.  
  150. _TEXT   ends
  151.  
  152.  
  153. _DATA   segment para public 'DATA'      ; static & variable data segment
  154.  
  155. cname   db      'CHILD.EXE',0           ; pathname of child process
  156.  
  157. pars    dw      _ENVIR                  ; segment of environment block
  158.         dd      tail                    ; long address, command tail
  159.         dd      fcb1                    ; long address, default FCB #1
  160.         dd      fcb2                    ; long address, default FCB #2
  161.  
  162. tail    db      fcb1-tail-2             ; command tail for child
  163.         db      'dummy command tail',cr
  164.                 
  165. fcb1    db      0                       ; copied into default FCB #1 in
  166.         db      11 dup (' ')            ; child's program segment prefix
  167.         db      25 dup (0)
  168.  
  169. fcb2    db      0                       ; copied into default FCB #2 in
  170.         db      11 dup (' ')            ; child's program segment prefix
  171.         db      25 dup (0)
  172.  
  173. msg1    db      cr,lf,'Parent executing!',cr,lf
  174. msg1_len equ    $-msg1
  175.  
  176. msg2    db      cr,lf,'Memory resize failed, error code='
  177. msg2a   db      'xxh.',cr,lf
  178. msg2_len equ    $-msg2
  179.  
  180. msg3    db      cr,lf,'EXEC call failed, error code='
  181. msg3a   db      'xxh.',cr,lf
  182. msg3_len equ    $-msg3
  183.  
  184. msg4    db      cr,lf,'Parent regained control!'
  185.         db      cr,lf,'Child termination type='
  186. msg4a   db      'xxh, return code='
  187. msg4b   db      'xxh.',cr,lf
  188. msg4_len equ    $-msg4
  189.  
  190. _DATA   ends
  191.  
  192.  
  193. _ENVIR  segment para public 'DATA'      ; example environment block
  194.                                         ; to be passed to child
  195.         
  196.         db      'PATH=',0               ; basic PATH, PROMPT,
  197.         db      'PROMPT=$p$_$n$g',0     ; and COMSPEC strings
  198.         db      'COMSPEC=C:\COMMAND.COM',0
  199.         db      0                       ; extra null terminates block
  200.  
  201. _ENVIR  ends    
  202.  
  203.  
  204. _STACK  segment para stack 'STACK'
  205.  
  206.         db      stksize dup (?)
  207.  
  208. _STACK  ends
  209.  
  210.  
  211.         end     main                    ; defines program entry point
  212.