home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / SPAWN.ASM < prev    next >
Encoding:
Assembly Source File  |  1990-06-07  |  5.9 KB  |  178 lines

  1. ;[]-----------------------------------------------------------------[]
  2. ;|      SPAWN.ASM -- Execute a program w/o Overlay                   |
  3. ;|                                                                   |
  4. ;|      Turbo-C Run Time Library        Version 3.0                  |
  5. ;|                                                                   |
  6. ;|      Copyright (c) 1987,1988,1990 by Borland International Inc.   |
  7. ;|      All Rights Reserved.                                         |
  8. ;[]-----------------------------------------------------------------[]
  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. SaveSP          dw      ?
  54. SaveSS          dw      ?
  55.  
  56.         public    __spawn
  57. __spawn        proc    near
  58.                 push    bp
  59.                 mov     bp, sp
  60.                 sub     sp, Fcb1
  61.                 push    si
  62.                 push    ds
  63.                 push    di
  64.                 push    es
  65.  
  66.         ;/* All we need to access with ES is in the stack */
  67.  
  68.                 push    ss
  69.                 pop     es
  70.  
  71.         ;/* Use envP and make with it a segment address */
  72.  
  73. IF      LDATA
  74.                 mov     ax, [bp+envP]
  75.                 mov     dx, [bp+envP+2]
  76. ELSE
  77.                 mov     ax, [bp+envP]
  78.                 mov     dx, ds
  79.                 or      ax, ax
  80.                 jz      SetEnvSeg
  81. ENDIF
  82.                 mov     cl, 4
  83.                 shr     ax, cl
  84.                 add     ax, dx
  85. SetEnvSeg       label   near
  86.                 mov     [bp-EnvSeg], ax
  87.  
  88.         ;/* Parse the command line for FCBs */
  89.  
  90.                 pushDS_
  91.                 LDS_    si, [bp+cmdP]
  92.                 mov     [bp-CmdPtr], si
  93.                 mov     [bp-CmdPtr+2], ds
  94.                 mov     ax, 2901h
  95.                 lea     di, [bp-Fcb1]
  96.                 mov     [bp-Fcb1Ptr], di
  97.                 mov     [bp-Fcb1Ptr+2], es
  98.                 inc     si
  99.                 int     21h             ;Build FCB 1 in PSP
  100.  
  101.                 mov     ax, 2901h
  102.                 lea     di, [bp-Fcb2]
  103.                 mov     [bp-Fcb2Ptr], di
  104.                 mov     [bp-Fcb2Ptr+2], es
  105.                 int     21h             ;Build FCB 2 in PSP
  106.                 popDS_
  107.  
  108.         ;/* DOS 2.xx likes DS to point to PSP, so copy pathP to PSP:80 which
  109.         ;/* allows us to move DS to point to the PSP
  110.  
  111.                 pushDS_
  112.                 push    es
  113. IFDEF   __HUGE__
  114.                 mov     ax, seg _psp@   ; Get segment of _psp variable
  115.                 mov     ds, ax          ; Setup DS to point to it
  116. ENDIF
  117.                 mov     es, _psp@       ; Get PSP segment
  118.                 LDS_    si, [bp+pathP]  ; Pathname of program to load
  119.                 mov     di, 80h         ; Point to argument area
  120.                 mov     cx, 7Fh         ; Limit length so we don't trash things
  121.                 cld                     ; Forward copy
  122. CopyPathP:      lodsb                   ; Get a char
  123.                 test    al, al          ; End of string yet ?
  124.                 jz      CopyPathPDone   ; Yes
  125.                 stosb                   ; Nope, copy character
  126.                 loop    CopyPathP       ; Next character
  127.                 mov     al, 0           ; Place null at end of string
  128. CopyPathPDone:  stosb
  129.                 pop     es
  130.                 popDS_
  131.  
  132.         ;/* Load and Execute the program */
  133.  
  134.                 push    bp              ; Save our context
  135.                 push    ds
  136.                 lea     bx, [bp-EnvSeg] ; Load descriptor
  137. IFDEF   __HUGE__
  138.                 mov     ax, seg _psp@   ; Get segment of _psp variable
  139.                 mov     ds, ax
  140. ENDIF
  141.                 mov     ds, _psp@       ; Pathname is in the PSP now
  142.                 mov     dx, 80h
  143.                 mov     ax, 4B00h       ; Load and Execute a program
  144.                 push    word ptr ss:[02Eh] ; DOS 2.xx destroy these 2 words
  145.                 push    word ptr ss:[030h]
  146.                 mov     SaveSS, ss      ; SS and SP also
  147.                 mov     SaveSP, sp
  148.                 int     21h
  149.         cli
  150.                 mov     ss, SaveSS      ; Restore our stack pointer
  151.                 mov     sp, SaveSP
  152.         sti
  153.                 pop     word ptr ss:[030h]
  154.                 pop     word ptr ss:[02Eh]
  155.                 pop     ds
  156.                 pop     bp
  157.  
  158.         ;/* If no error, get child exit code */
  159.  
  160.                 jnc     SpawnOk
  161.                 push    ax
  162.                 call    __IOERROR
  163.                 jmp     short SpawnExit
  164. SpawnOk         label   near
  165.                 mov     ah, 4dh
  166.                 int     21h
  167. SpawnExit       label   near
  168.                 pop     es
  169.                 pop     di
  170.                 pop     ds
  171.                 pop     si
  172.                 mov     sp, bp
  173.                 pop     bp
  174.                 ret
  175.         endp
  176. CSegEnd@
  177.         END
  178.