home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / sysutil / exec1.asm < prev    next >
Assembly Source File  |  1994-03-05  |  7KB  |  197 lines

  1. Date: Friday, 15 March 1985
  2. From: Harald Striepe
  3. Re:   Use of IBM PC-DOS \"EXEC\" function call
  4.  
  5. > Does anyone have any experience with the use of PCDOS functions
  6. > (INT 21H) I have been trying to figure out how to use the "Exec"
  7. > function 4Bh and have been having no success.
  8. > Would someone who has used this function call please let me know what 
  9. > the secret is.  A copy of a simple program using the call would 
  10. > help also.
  11. > Brian
  12.  
  13. The key is to release the memory not used by your calling program.
  14. MS-DOS appears to allocate all memory to the current application until
  15. it is properly notified. Below is an example program.  Note that this
  16. worked on a Rainbow, but should also function on any other PC with
  17. MS-DOS V2 or higher.
  18.  
  19. --------------------------- cut here ---------------------------------
  20.  
  21.  
  22. PAGE    56,132
  23. TITLE   Exec Call Test- Call COMMAND.COM SHELL to do Directory (.EXE file)
  24. NAME    EXEC1
  25.  
  26. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  27. ;                                                                        ;
  28. ;  DOS EXEC-CALL EXAMPLE                                                 ;
  29. ;  Loads COMMAND.COM,  and executes a directory on default drive.        ;
  30. ;                                                                        ;
  31. ;  Based on a shell example by Brian Markey.                             ;
  32. ;  Note: This example does not do any error checking!!!                  ;
  33. ;                                                                        ;
  34. ;  Author: H. Striepe                                            8/84    ;
  35. ;                                                                        ;
  36. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  37.  
  38.  
  39. ; EQUATES
  40.  
  41. MSDOS           EQU  021h       ;MS-DOS interrupt
  42. Print           EQU  009h       ;  func: print string terminated with $
  43. ModMem          EQU  04ah       ;        modify allocated memory
  44. Exec            EQU  04bh       ;        load and execute program
  45. TermProc        EQU  04ch       ;        terminate process
  46.  
  47. Stacklen        EQU  128        ;Size of program stack
  48.  
  49. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  50.  
  51. code    SEGMENT PARA 'codeseg'
  52.  
  53.         ASSUME  CS:code,SS:stack,DS:data,ES:nothing
  54.  
  55. Start:                                          ; Program entry point
  56.  
  57.         MOV     AX,SEG data                     ; load data seg pointer
  58.         MOV     DS,AX                           ;  .
  59.  
  60.         MOV     AH,Print                        ; Print "Before shell"
  61.     MOV    DX,OFFSET mess1            ;  .
  62.     INT    MSDOS                ;  ..
  63.  
  64. ; MS-DOS allocates all available memory to a program upon loading,
  65. ; we first have to deallocate all unused memory using function 4Ah.
  66. ; ES := point to PSB (default after load)
  67. ; BX := current program size in paragraphs
  68. ; Note: If on calling function 4Ah ES: is not equal to the PSB MS-DOS
  69. ;       allocated on loading, an ARENA TRASHED error will result.
  70.  
  71. ; ES: still points to PSB,  calculate memory required for calling 
  72. ; routine:
  73.  
  74.         MOV     BX,OFFSET end+256               ; code space+PSP
  75.         ADD     BX,stacklen                     ; + stackspace
  76.         ADD     BX,OFFSET lastloc+15            ; + data space
  77.         MOV     CX,4                            ;  in
  78.         SHR     BX,CL                           ;  paragraphs
  79.  
  80.  
  81.         XOR     AL,AL                           ; Deallocate unused memory
  82.         MOV     AH,Modmem                       ;  .
  83.         INT     MSDOS                           ;  ..
  84.  
  85. ; In case of error, carry would be set.
  86. ;   Error codes in AX:  7=> arena trashed
  87. ;                       8=> not enough memory
  88. ;                       9=> invalid block
  89.  
  90.         MOV     SI,2CH                          ; Get environment address
  91.         MOV     AX,ES:[SI]                      ;  from PSP+2CH
  92.         MOV     WORD PTR parmblk,AX             ;  .
  93.  
  94.         MOV     DX,OFFSET filenam               ; Set up exec call
  95.         PUSH    DS                              ;  .
  96.         POP     ES                              ;  ..
  97.         MOV     BX,OFFSET parmblk               ;  ...
  98.         XOR     AL,AL                           ;  ....
  99.         MOV     AH,Exec                         ;  .....
  100.  
  101.     PUSH    DS                ; Save machine state
  102.     PUSH    ES                ;  .
  103.         MOV     CS:savess,SS                    ;  ..
  104.         MOV     CS:savesp,SP                    ;  ...
  105.  
  106.     INT    MSDOS                ; Shell to DOS
  107.  
  108. ; In case of error, carry would be set.
  109. ;   Error codes in AX:  1=> invalid function
  110. ;                       2=> file not found
  111. ;                       8=> not enough memory
  112. ;                      10=> bad environment
  113. ;                      11=> bad format
  114.  
  115.         MOV     SP,CS:savesp                    ; Restore machine state
  116.     MOV    SS,CS:savess            ;  .
  117.         POP     ES                              ;  ..
  118.         POP     DS                              ;  ...
  119.  
  120.         MOV     AH,Print                        ; Print "After shell"
  121.     MOV    DX,OFFSET mess2            ;  .
  122.     INT    MSDOS                ;  ..
  123.  
  124.         MOV     AH,TermProc                     ; Terminate process
  125.         XOR     AL,AL                           ;  .  no error...
  126.         INT     MSDOS                           ;  ..
  127.  
  128. savess    DW    ?                ; Holders for SS:SP
  129. savesp  DW      ?                               ;  .
  130.  
  131. end:
  132.  
  133. code    ENDS
  134.  
  135. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  136.  
  137. stack   SEGMENT PARA STACK 'stackseg'
  138.  
  139.         DB      Stacklen DUP (?)                ; Stack
  140. TOS     LABEL   BYTE                            ;  .
  141.  
  142. stack   ENDS
  143.  
  144. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  145.  
  146. data    SEGMENT PARA 'dataseg'
  147.  
  148. mess1    DB    'Before shell',0DH,0AH,'$'    ; Program messages
  149. mess2    DB    'After shell',0DH,0AH,'$'    ;  ..
  150. filenam DB      '\command.com',0                ; Load filename
  151. parmblk    DW    00                ; Parameter block
  152.         DD      comline                         ;  .
  153.         DD      fcb_one                         ;  ..
  154.         DD      fcb_two                         ;  ...
  155.  
  156. comline DB      07H,'/C dir ',0DH               ; Command line
  157.  
  158. ; The following are two full fcb's
  159.  
  160.                 db 0ffh         ;flag byte, extended fcb
  161.                 db 5 dup (0)    ;reserved
  162.                 db (0)          ;attribute
  163. fcb_one         db 0            ;drive number
  164.                 db "        "   ;filename
  165.         db "   "    ;extension
  166.         dw 0        ;current block
  167.         dw 0        ;record size
  168.         dd 0        ;file size
  169.         dw 0        ;date of last write
  170.         dw 0        ;time of last write
  171.         db 8 dup (0)    ;reserved
  172.                 db 0            ;current record
  173.         dd 0        ;relative record
  174.  
  175.                 db 0ffh         ;flag byte, extended fcb
  176.                 db 5 dup (0)    ;reserved
  177.                 db (0)          ;attribute
  178. fcb_two         db 0            ;drive number
  179.                 db "        "   ;filename
  180.         db "   "    ;extension
  181.         dw 0        ;current block
  182.         dw 0        ;record size
  183.         dd 0        ;file size
  184.         dw 0        ;date of last write
  185.         dw 0        ;time of last write
  186.         db 8 dup (0)    ;reserved
  187.                 db 0            ;current record
  188.         dd 0        ;relative record
  189.  
  190. lastloc LABEL   BYTE                            ; End of program
  191.  
  192. data    ENDS
  193.  
  194.         END     start
  195.