home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / CLIBSRC3.ZIP / SPAWN.ASM < prev    next >
Encoding:
Assembly Source File  |  1992-06-10  |  6.2 KB  |  194 lines

  1. ;[]-----------------------------------------------------------------[]
  2. ;|      SPAWN.ASM -- Execute a program w/o Overlay                   |
  3. ;[]-----------------------------------------------------------------[]
  4.  
  5. ;
  6. ;       C/C++ Run Time Library - Version 5.0
  7. ;       Copyright (c) 1987, 1992 by Borland International
  8. ;       All Rights Reserved.
  9.  
  10.         INCLUDE RULES.ASI
  11.  
  12. ;       Segments Definitions
  13.  
  14. Header@
  15.  
  16. ;       External References
  17.  
  18. extrn           __IOERROR:near
  19.  
  20. ExtSym@         _psp, WORD, __CDECL__
  21.  
  22.         SUBTTL  _Spawn Function
  23.         PAGE
  24. ;/*                                                     */
  25. ;/*-----------------------------------------------------*/
  26. ;/*                                                     */
  27. ;/* int _spawn(pathP,cmdP,envP);                        */
  28. ;/*     char    *pathP;                                 */
  29. ;/*     char    *cmdP;                                  */
  30. ;/*     char    *envP;                                  */
  31. ;/*                                                     */
  32. ;/*-----------------------------------------------------*/
  33. ;/*                                                     */
  34.  
  35. pathP           equ     4
  36.  
  37. IF      LDATA
  38. cmdP            equ     pathP + 4
  39. envP            equ     cmdP + 4
  40. ELSE
  41. cmdP            equ     pathP + 2
  42. envP            equ     cmdP + 2
  43. ENDIF
  44.  
  45. Fcb2Ptr         equ     4
  46. Fcb1Ptr         equ     Fcb2Ptr + 4
  47. CmdPtr          equ     Fcb1Ptr + 4
  48. EnvSeg          equ     CmdPtr + 2
  49. Fcb2            equ     EnvSeg + 16
  50. Fcb1            equ     Fcb2 + 16
  51.  
  52. CSeg@
  53.  
  54.  
  55. SaveSP          dw      ?
  56. SaveSS          dw      ?
  57.  
  58.                 public  __spawn
  59. __spawn         proc    near
  60.                 push    bp
  61.                 mov     bp, sp
  62.                 sub     sp, Fcb1
  63.                 push    si
  64.                 push    ds
  65.                 push    di
  66.                 push    es
  67.  
  68.         ;/* All we need to access with ES is in the stack */
  69.  
  70.                 push    ss
  71.                 pop     es
  72.  
  73.         ;/* Use envP and make with it a segment address */
  74.  
  75. IF      LDATA
  76.                 mov     ax, [bp+envP]
  77.                 mov     dx, [bp+envP+2]
  78. ELSE
  79.                 mov     ax, [bp+envP]
  80.                 mov     dx, ds
  81.                 or      ax, ax
  82.                 jz      SetEnvSeg
  83. ENDIF
  84.                 mov     cl, 4
  85.                 shr     ax, cl
  86.                 add     ax, dx
  87. SetEnvSeg       label   near
  88.                 mov     [bp-EnvSeg], ax
  89.  
  90.         ;/* Parse the command line for FCBs */
  91.  
  92.                 pushDS_
  93.                 LDS_    si, [bp+cmdP]
  94.                 mov     [bp-CmdPtr], si
  95.                 mov     [bp-CmdPtr+2], ds
  96.                 mov     ax, 2901h
  97.                 lea     di, [bp-Fcb1]
  98.                 mov     [bp-Fcb1Ptr], di
  99.                 mov     [bp-Fcb1Ptr+2], es
  100.                 inc     si
  101.                 int     21h             ;Build FCB 1 in PSP
  102.  
  103. FindWhite       label   near
  104.                 mov     al,[si]
  105.                 cmp     al,20h         ; Space
  106.                 je      NextFCB
  107.                 cmp     al,09h         ; Tab
  108.                 je      NextFCB
  109.                 cmp     al,0dh         ; Carriage Return
  110.                 je      NextFCB
  111.                 inc     si
  112.                 jmp     FindWhite
  113.  
  114. NextFCB         label   near
  115.                 mov     ax, 2901h
  116.                 lea     di, [bp-Fcb2]
  117.                 mov     [bp-Fcb2Ptr], di
  118.                 mov     [bp-Fcb2Ptr+2], es
  119.                 int     21h             ;Build FCB 2 in PSP
  120.                 popDS_
  121.  
  122.         ;/* DOS 2.xx likes DS to point to PSP, so copy pathP to PSP:80 which
  123.         ;/* allows us to move DS to point to the PSP
  124.  
  125.                 pushDS_
  126.                 push    es
  127. IFDEF   __HUGE__
  128.                 mov     ax, seg _psp@   ; Get segment of _psp variable
  129.                 mov     ds, ax          ; Setup DS to point to it
  130. ENDIF
  131.                 mov     es, _psp@       ; Get PSP segment
  132.                 LDS_    si, [bp+pathP]  ; Pathname of program to load
  133.                 mov     di, 80h         ; Point to argument area
  134.                 mov     cx, 7Fh         ; Limit length so we don't trash things
  135.                 cld                     ; Forward copy
  136. CopyPathP:      lodsb                   ; Get a char
  137.                 test    al, al          ; End of string yet ?
  138.                 jz      CopyPathPDone   ; Yes
  139.                 stosb                   ; Nope, copy character
  140.                 loop    CopyPathP       ; Next character
  141.                 mov     al, 0           ; Place null at end of string
  142. CopyPathPDone:  stosb
  143.                 pop     es
  144.                 popDS_
  145.  
  146.         ;/* Load and Execute the program */
  147.  
  148.                 push    bp              ; Save our context
  149.                 push    ds
  150.                 lea     bx, [bp-EnvSeg] ; Load descriptor
  151. IFDEF   __HUGE__
  152.                 mov     ax, seg _psp@   ; Get segment of _psp variable
  153.                 mov     ds, ax
  154. ENDIF
  155.                 mov     ds, _psp@       ; Pathname is in the PSP now
  156.                 mov     dx, 80h
  157.                 mov     ax, 4B00h       ; Load and Execute a program
  158.                 push    word ptr ss:[02Eh] ; DOS 2.xx destroy these 2 words
  159.                 push    word ptr ss:[030h]
  160.                 mov     SaveSS, ss      ; SS and SP also
  161.                 mov     SaveSP, sp
  162.                 int     21h
  163.                 cli
  164.                 mov     ss, SaveSS      ; Restore our stack pointer
  165.                 mov     sp, SaveSP
  166.                 sti
  167.                 pop     word ptr ss:[030h]
  168.                 pop     word ptr ss:[02Eh]
  169.                 pop     ds
  170.                 pop     bp
  171.  
  172.         ;/* If no error, get child exit code */
  173.  
  174.                 jnc     SpawnOk
  175.                 push    ax
  176.                 call    __IOERROR
  177.                 jmp     short SpawnExit
  178. SpawnOk         label   near
  179.                 mov     ah, 4dh
  180.                 int     21h
  181. SpawnExit       label   near
  182.                 pop     es
  183.                 pop     di
  184.                 pop     ds
  185.                 pop     si
  186.                 mov     sp, bp
  187.                 pop     bp
  188.                 ret
  189.                 endp
  190. CSegEnd@
  191.         END
  192.