home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 5 / ctrom5b.zip / ctrom5b / PROGRAM / DIVERSEN / DOS32V3B / STUB.ASM < prev    next >
Assembly Source File  |  1995-03-01  |  6KB  |  216 lines

  1. ;*****************************************************************************
  2. ; Filename: STUB.ASM
  3. ;   Author: Adam Seychell
  4. ;  Version: 1.00
  5. ;  Created: 1994.09
  6. ;  Updated: 1995.Feb.1
  7. ;   Note 1: This code has been optimized for size ( current size 290 bytes).
  8. ;   Note 2: After linking this program use the EXERID utility to remove
  9. ;           wasted header space.
  10. ;
  11. ; History and changes are documented at the end of this file.
  12. ;*****************************************************************************
  13. ;            Stub loader for DOS32 executibles.
  14. ;
  15. ; This stub loader attempts to loads and execute DOS32.EXE.
  16. ;  and is searched in the following order.
  17. ;
  18. ;  1) The directory in which the executible exists ( i.e this stub file )
  19. ;  2) Directories entries in the PATH environment varible.
  20. ;
  21. ;  The final EXE will be this stub EXE file and the 32bit executable image
  22. ;  appended together.
  23. ;
  24. ;*****************************************************************************
  25. .8086
  26.  
  27. Stub_Program  SEGMENT stack 'STACK'
  28. Assume DS:Stub_Program,CS:Stub_Program
  29.  
  30.  
  31. Stub_Loader:
  32.  
  33.  
  34.    ;
  35.    ;  Free program memory
  36.    ;
  37.         mov     bx,cs
  38.         mov     ax,sp
  39.         mov     cl,4
  40.         shr     ax,cl
  41.         inc     ax
  42.         add     bx,ax                           ; BX = top of stack paragraph
  43.         mov     ax,ds                           ; Sub PSP address
  44.         sub     bx,ax
  45.         mov     ah,4Ah
  46.         int     21h
  47.  
  48.         push    cs                      ; Load DS with data segment
  49.         pop     ds
  50.  
  51.  
  52.    ;
  53.    ; Fuxup rest of the Execute Parameter Table.  note: ES -> PSP segment
  54.    ;
  55.         mov     word ptr EXEC_Params[04h],es      ; command tail segment
  56.         mov     word ptr EXEC_Params[08h],es      ; FCB 1 segment
  57.         mov     word ptr EXEC_Params[0Ch],es      ; FCB 2 segment
  58.  
  59.  
  60.         mov     es,es:[2Ch]             ; Load ES with environment segment.
  61.  
  62.  
  63.    ;
  64.    ; Look for the 'PATH=' envrinment string;
  65.    ;
  66.         cld
  67.         xor     di,di
  68. Next_Var:
  69.         mov     si,Offset Path_String
  70.         mov     cx,5
  71.         cmp     byte ptr es:[di],0
  72.         je   Got_Envir
  73.         repe    cmpsb                   ; Compare DS:SI with ES:DI
  74.         jne NoPATH
  75.         mov     Path_Ofs,di           ; Save the PATH varible offset if Equal
  76. NoPATH: xor     al,al                   ; Goto next environment varible
  77.         mov     ch,-1
  78.         repne   scasb
  79.         jmp  Next_Var
  80.  
  81.  
  82. Got_Envir:                              ; ES:DI points to end of environment
  83.  
  84.  ; 
  85.  ; Copy .EXE path to the file name buffer
  86.  ;
  87.         cmp     byte ptr es:[di+4],':'
  88.         jne  Try_path_loop              ; Assume there's no path is no ':'
  89.         mov     si,Offset File_Name
  90. GetEXEpath:
  91.         mov     al,es:[di+3]
  92.         inc     di
  93.         mov     ds:[si],al
  94.         inc     si
  95.         cmp     al,'\'
  96.         jne    _J87
  97.          mov    bx,si           ; Save SI
  98. _J87:
  99.         and     al,al
  100.         jnz     GetEXEpath
  101.  
  102.         mov     si,bx
  103.         jmp     Exec_It
  104.  
  105. ;
  106. ; The main loop when trying to load and execute DOS32.EXE with each path
  107. ; entry in the 'PATH=...' environment string.
  108. ;
  109. Try_path_loop:
  110.         mov     di,Path_Ofs
  111.         And     di,di
  112.         jl   Load_error                 ; Error if no path varible was found.
  113.         cmp     al,2
  114.         je   Try_again                  ; Try again if file not found.
  115.         cmp     al,3
  116.         jne  Load_error                 ; Try again if path not found.
  117. Try_again:
  118.  
  119.  
  120.      ;
  121.      ; Copy a path entry to the file name buffer
  122.      ;
  123.         mov     si,Offset File_Name
  124. Copy_path:
  125.         mov     al,es:[di]
  126.         inc     di
  127.         cmp     al,';'
  128.         je done_pen
  129.         cmp     al,' '
  130.         je done_pen
  131.         and     al,al
  132.         jz  done_pen_End
  133.         mov     [si],al
  134.         inc     si
  135.         jmp     Copy_path
  136.  
  137. done_pen_End:
  138.          mov      di,-1
  139. done_pen:
  140.         mov     Path_Ofs,di
  141.  
  142.  
  143.      ;
  144.      ; Append the string 'DOS32.EXE' to the file name buffer
  145.      ;
  146.         cmp     Byte Ptr [si-1],'\'        ; If path dosn't contain a '\'
  147.         je Exec_It                         ; then put one it.
  148.          mov    Byte ptr [si],'\'
  149.          inc    si
  150. Exec_It:
  151.         push    es                         ; Save ES
  152.         push    cs
  153.         pop     es
  154.         mov     di,si
  155.         mov     si,Offset DOS32_EXE
  156.         mov     cx,10
  157.         rep     movsb
  158.  
  159.  
  160.    ;
  161.    ; Go and Load then Execute DOS32.EXE
  162.    ;
  163.         mov     bx,Offset EXEC_Params
  164.         mov     dx,Offset File_name
  165.         mov     ax,4B00h
  166.         int     21h
  167.         pop     es                    ; Restore ES
  168.         jc     Try_path_loop
  169.  
  170.         Mov     ah,4Dh                ; Get return code from DOS32 aplication
  171.         Int     21h
  172.  
  173. exit:
  174.         Mov     ah,4ch                ; Terminate to DOS
  175.         Int     21h
  176.  
  177.  
  178.  
  179.  
  180.  ;
  181.  ; Exit with an error message
  182.  ;
  183.  
  184. Load_error:
  185.         mov     dx,Offset Exec_Error_Mesg
  186.         mov     ah,9
  187.         int     21h
  188.         xor     al,al
  189.         jmp exit
  190.  
  191.  
  192. Path_String     DB        'PATH='
  193. Path_Ofs        DW        -1
  194. Exec_Error_Mesg DB        'Cannot find '
  195. DOS32_EXE       DB      'DOS32.EXE',0,'in path',10,13,36
  196. EXEC_Params     DW      0                ; childs Environemnt segment
  197.                 DW      80h,0           ; childs command tail seg:ofs
  198.                 DW      5Ch,0           ; FCB 1  seg:ofs
  199.                 DW      6Ch,?           ; FCB 2  seg:ofs
  200. align 2
  201. File_name       DB      128 DUP (?)     ; 128 chars for file the name.
  202.  
  203.                 DB      100     dup (?)                 ; The stack space
  204. The_Stack       DW ?
  205.  
  206. Stub_Program    ENDS
  207.  
  208. End  Stub_Loader
  209.  
  210. 1994.Nov.11 : Made the return code equal to the return code from
  211.               the DOS32 application. This would of caused problems
  212.               with applications that rely on the return code.
  213.  
  214. 1995.Feb.1  : Fixed bug in Execute parameter table. The segment
  215.               values were not set correctly.
  216.