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

  1.         ;************************************************************;
  2.         ;                                                            ;
  3.         ;          Function 4BH: Load and Execute Program            ;
  4.         ;                                                            ;
  5.         ;          int execute(pprogname,pcmdtail)                   ;
  6.         ;              char *pprogname,*pcmdtail;                    ;
  7.         ;                                                            ;
  8.         ;          Returns 0 if program loaded, ran, and             ;
  9.         ;          terminated successfully, otherwise returns        ;
  10.         ;          error code.                                       ;
  11.         ;                                                            ;
  12.         ;************************************************************;
  13.  
  14. sBegin  data
  15. $cmdlen =       126
  16. $cmd    db      $cmdlen+2 dup (?) ; Make space for command line, plus
  17.                                 ; 2 extra bytes for length and
  18.                                 ; carriage return.
  19.  
  20. $fcb    db      0               ; Make dummy FCB.
  21.         db      'dummy   fcb'
  22.         db      0,0,0,0
  23.  
  24.                                 ; Here's the EXEC parameter block:
  25. $epb    dw      0               ; 0 means inherit environment.
  26.         dw      dataOFFSET $cmd ; Pointer to cmd line.
  27.         dw      seg dgroup
  28.         dw      dataOFFSET $fcb ; Pointer to FCB #1.
  29.         dw      seg dgroup
  30.         dw      dataOFFSET $fcb ; Pointer to FCB #2.
  31.         dw      seg dgroup
  32. sEnd    data
  33. sBegin  code
  34.  
  35. $sp     dw      ?               ; Allocate space in code seg
  36. $ss     dw      ?               ; for saving SS and SP.
  37.  
  38. Assumes ES,dgroup
  39.  
  40. cProc   execute,PUBLIC,<ds,si,di>
  41. parmDP  pprogname
  42. parmDP  pcmdtail
  43. cBegin
  44.         mov     cx,$cmdlen      ; Allow command line this long.
  45.         loadDP  ds,si,pcmdtail  ; DS:SI = pointer to cmdtail string.
  46.         mov     ax,seg dgroup:$cmd    ; Set ES = data segment.
  47.         mov     es,ax
  48.         mov     di,dataOFFSET $cmd+1  ; ES:DI = pointer to 2nd byte of
  49.                                       ; our command-line buffer.
  50. copycmd:
  51.         lodsb                   ; Get next character.
  52.         or      al,al           ; Found end of command tail?
  53.         jz      endcopy         ; Exit loop if so.
  54.         stosb                   ; Copy to command buffer.
  55.         loop    copycmd
  56. endcopy:
  57.         mov     al,13
  58.         stosb                   ; Store carriage return at
  59.                                 ; end of command.
  60.         neg     cl
  61.         add     cl,$cmdlen      ; CL = length of command tail.
  62.         mov     es:$cmd,cl      ; Store length in command-tail buffer.
  63.  
  64.         loadDP  ds,dx,pprogname ; DS:DX = pointer to program name.
  65.         mov     bx,dataOFFSET $epb ; ES:BX = pointer to parameter
  66.                                    ; block.
  67.  
  68.         mov     cs:$ss,ss       ; Save current stack SS:SP (since
  69.         mov     cs:$sp,sp       ; EXEC function destroys stack).
  70.         mov     ax,4b00h        ; Set function code.
  71.         int     21h             ; Ask MS-DOS to load and execute
  72.                                 ; program.
  73.         cli                     ; Disable interrupts.
  74.         mov     ss,cs:$ss       ; Restore stack.
  75.         mov     sp,cs:$sp
  76.         sti                     ; Enable interrupts.
  77.         jb      ex_err          ; Branch on error.
  78.         xor     ax,ax           ; Return 0 if no error.
  79. ex_err:
  80. cEnd
  81. sEnd    code
  82.  
  83.  
  84.  
  85.         ;************************************************************;
  86.         ;                                                            ;
  87.         ;   Function 4BH: Load an Overlay Program                    ;
  88.         ;                                                            ;
  89.         ;   int load_overlay(pfilename,loadseg)                      ;
  90.         ;       char *pfilename;                                     ;
  91.         ;       int  loadseg;                                        ;
  92.         ;                                                            ;
  93.         ;   Returns 0 if program has been loaded OK,                 ;
  94.         ;   otherwise returns error code.                            ;
  95.         ;                                                            ;
  96.         ;   To call an overlay function after it has been            ;
  97.         ;   loaded by load_overlay(), you can use                    ;
  98.         ;   a far indirect call:                                     ;
  99.         ;                                                            ;
  100.         ;   1. FTYPE (far *ovlptr)();                                ;
  101.         ;   2. *((unsigned *)&ovlptr + 1) = loadseg;                 ;
  102.         ;   3. *((unsigned *)&ovlptr) = offset;                      ;
  103.         ;   4. (*ovlptr)(arg1,arg2,arg3,...);                        ;
  104.         ;                                                            ;
  105.         ;   Line 1 declares a far pointer to a                       ;
  106.         ;   function with return type FTYPE.                         ;
  107.         ;                                                            ;
  108.         ;   Line 2 stores loadseg into the segment                   ;
  109.         ;   portion (high word) of the far pointer.                  ;
  110.         ;                                                            ;
  111.         ;   Line 3 stores offset into the offset                     ;
  112.         ;   portion (low word) of the far pointer.                   ;
  113.         ;                                                            ;
  114.         ;   Line 4 does a far call to offset                         ;
  115.         ;   bytes into the segment loadseg                           ;
  116.         ;   passing the arguments listed.                            ;
  117.         ;                                                            ;
  118.         ;   To return correctly, the overlay  must end with a far    ;
  119.         ;   return instruction.  If the overlay is                   ;
  120.         ;   written in Microsoft C, this can be done by              ;
  121.         ;   declaring the overlay function with the                  ;
  122.         ;   keyword "far".                                           ;
  123.         ;                                                            ;
  124.         ;************************************************************;
  125.  
  126. sBegin  data
  127.                                 ; The overlay parameter block:
  128. $lob    dw      ?               ; space for load segment;
  129.         dw      ?               ; space for fixup segment.
  130. sEnd    data
  131.  
  132. sBegin  code
  133.  
  134. cProc   load_overlay,PUBLIC,<ds,si,di>
  135. parmDP  pfilename
  136. parmW   loadseg
  137. cBegin
  138.         loadDP  ds,dx,pfilename ; DS:DX = pointer to program name.
  139.         mov     ax,seg dgroup:$lob ; Set ES = data segment.
  140.         mov     es,ax
  141.         mov     bx,dataOFFSET $lob ; ES:BX = pointer to parameter
  142.                                    ; block.
  143.         mov     ax,loadseg      ; Get load segment parameter.
  144.         mov     es:[bx],ax      ; Set both the load and fixup
  145.         mov     es:[bx+2],ax    ; segments to that segment.
  146.  
  147.         mov     cs:$ss,ss       ; Save current stack SS:SP (because
  148.         mov     cs:$sp,sp       ; EXEC function destroys stack).
  149.         mov     ax,4b03h        ; Set function code.
  150.         int     21h             ; Ask MS-DOS to load the overlay.
  151.         cli                     ; Disable interrupts.
  152.         mov     ss,cs:$ss       ; Restore stack.
  153.         mov     sp,cs:$sp
  154.         sti                     ; Enable interrupts.
  155.         jb      lo_err          ; Branch on error.
  156.         xor     ax,ax           ; Return 0 if no error.
  157. lo_err:
  158. cEnd
  159. sEnd    code
  160.