home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / library / masmsubs / runprog.asm < prev    next >
Assembly Source File  |  1987-07-27  |  3KB  |  122 lines

  1.  
  2.     title    Run a program from within another program
  3.  
  4.  
  5.     name    RUNPROG
  6.  
  7.  
  8. save    macro    p1,p2,p3,p4,p5,p6,p7,p8,p9
  9.     irp    y,<p1,p2,p3,p4,p5,p6,p7,p8,p9>
  10.     ifnb    <y>
  11.     push    y
  12.     endif
  13.     endm
  14. restore macro
  15.     irp    z,<p9,p8,p7,p6,p5,p4,p3,p2,p1>
  16.     ifnb    <z>
  17.     pop    z
  18.     endif
  19.     endm
  20.     endm
  21.     endm
  22.  
  23.  
  24. cseg    segment    byte    public    'code'
  25.  
  26.  
  27.     assume    cs:cseg,ds:nothing
  28.  
  29.  
  30.     public    run_program
  31.  
  32.     comment    `
  33.  
  34. Routine to execute a program from within a program.
  35.  
  36. Calling stack:
  37.     <sp+00> -> dword pointer to ASCIZ file name
  38.     <sp+04> -> dword pointer to command line to pass to the program.
  39.            First byte of the string is assumed to be the length
  40.            of the string, and string must be terminated by a 0dh
  41.            (carriage return) character.
  42.  
  43. Return:
  44.     AX contains the return code of the program.
  45.  
  46. `
  47. run_program    proc    near
  48.  
  49.  
  50. FCB1Body    equ    byte ptr [bp]
  51. FCB2Body    equ    byte ptr [bp+16]
  52. EnvSeg        equ    word ptr [bp+32]
  53. CmdLinePtrO    equ    word ptr [bp+34]
  54. CmdLinePtrS    equ    word ptr [bp+36]
  55. FCB1PtrO    equ    word ptr [bp+38]
  56. FCB1PtrS    equ    word ptr [bp+40]
  57. FCB2PtrO    equ    word ptr [bp+42]
  58. FCB2PtrS    equ    word ptr [bp+44]
  59. ProgramName    equ    dword ptr [bp+64]
  60. CommandLine    equ    dword ptr [bp+68]
  61.     push    bp            ; Save incoming fp
  62.     save    bx,cx,dx,si,di,ds,es    ; Save the world (except for return)
  63.     mov    cs:OldSS,ss        ; Save the stack
  64.     mov    cs:OldSP,sp
  65.     sub    sp,46            ; Make room for temporaries
  66.     mov    bp,sp            ; And get a pointer to them
  67.     lea    ax,FCB1Body        ; Get pointer to first FCB
  68.     mov    FCB1PtrS,ss        ; Put pointers to default FCBs on stack
  69.     mov    FCB1PtrO,ax
  70.     mov    FCB2PtrS,ss
  71.     lea    ax,FCB2Body
  72.     mov    FCB2PtrO,ax
  73.     lds    si,CommandLine        ; Get pointer to command line
  74.     mov    CmdLinePtrO,si        ; Put it on the stack
  75.     mov    CmdLinePtrS,ds
  76.     push    ss            ; Get pointer to FCBs
  77.     pop    es
  78.     lea    di,FCB1Body
  79.     mov    ax,2901h        ; Function to parse file name
  80.     int    21h            ; Get first default FCB
  81.     lea    di,FCB2Body        ; Point to second FCB
  82.     mov    ax,2901h        ; Function again
  83.     int    21h            ; Parse it
  84.     mov    ah,30h            ; Get the DOS version
  85.     int    21h
  86.     cmp    al,3            ; Is it 3.x?
  87.     mov    ax,5200h        ; "Get PSP" function for 2.x (undoc)
  88.     jne    Not3x            ; No, use different function
  89.     add    ah,10h            ; "Get PSP" function for 3.x
  90. Not3x:
  91.     int    21h            ; Get our PSP segment
  92.     mov    ds,bx            ; Get it where we can use it
  93.     mov    ax,ds:2ch        ; Get OUR environment
  94.     mov    EnvSeg,ax        ; Put it in parameter block
  95.     push    ss            ; Get segment of parameter block
  96.     pop    es
  97.     lea    bx,EnvSeg        ; Point to parameter block
  98.     lds    dx,ProgramName        ; Get pointer to program name
  99.     mov    ax,4b00h        ; EXEC function to execute program
  100.     int    21h            ; Call DOS to do it
  101.     mov    ss,cs:OldSS        ; Get our old stack back. Note that
  102.     mov    sp,cs:OldSP        ;  this takes care of the temps, too.
  103.     jc    run_err            ; If CF set, return that as error
  104.     mov    ax,4d00h        ; Function to get termination code
  105.     int    21h            ; Get from DOS
  106. run_err:
  107.     restore                ; Get all the registers back
  108.     pop    bp            ; Get old fp back
  109.     ret    8            ; Return to caller
  110. ;
  111. ; Local storage for SS & SP. Since the only segment we can be sure of is CS,
  112. ;  we have to break all the rules and keep it with the code.
  113. ;
  114. OldSS    dw    0
  115. OldSP    dw    0
  116.  
  117. run_program    endp
  118.  
  119. cseg    ends
  120.  
  121.     end
  122.