home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / msysjour / vol07 / 08 / cqa / loadhi.asm
Assembly Source File  |  1992-10-20  |  8KB  |  167 lines

  1. ;; LOADHI demonstrates how a memory-resident program run under
  2. ;; MS-DOS version 5 may load itself high if upper memory blocks
  3. ;; are available.  The commands
  4. ;;      MASM LOADHI /ML;
  5. ;;      LINK LOADHI;
  6. ;;      EXE2BIN LOADHI.EXE LOADHI.COM
  7. ;; generate a COM-formatted executable from the source file.
  8.  
  9. SET_LINK        equ     1                       ;Set upper memory link
  10. FF_HIGH_ONLY    equ     40h                     ;First-fit/high-only code
  11. UMB_REQUIRED    equ     400h                    ;Paragraphs needed to load
  12.  
  13. LOADEXEC        struct
  14. Env             dw      0                       ;Environment block segment
  15. Tail            dd      ?                       ;Command tail address
  16. FCB_1           dd      ?                       ;First FCB address
  17. FCB_2           dd      ?                       ;Second FCB address
  18. LOADEXEC        ends
  19.  
  20. code            segment
  21.                 assume  cs:code
  22.                 org     100h
  23. Begin:          jmp     Init
  24.  
  25. ; *** Code that will remain in memory after the program has terminated goes here ;; If the program is loaded high, Init calls MS-DOS Function 31h to
  26. ;; become resident in memory.  If the program is loaded low and it is
  27. ;; the first instance of the program in memory, Init attempts to load
  28. ;; and execute a second instance of the program in upper memory.  If
  29. ;; the program is loaded low and it is the second instance, Init
  30. ;; terminates the program.
  31.                 assume  cs:code,ds:code
  32.  
  33. Init            proc    near
  34.                 mov     ax,cs                   ;Branch if the program
  35.                 cmp     ax,0A000h               ;was not loaded high
  36.                 jb      LoadedLow
  37.  
  38.                 ; *** Perform initializations here ***
  39.                 ; *** (revector interrupts, etc.)  ***
  40.  
  41.                 mov     dx,offset Init + 15     ;Compute number of
  42.                 mov     cl,4                    ;paragraphs of memory
  43.                 shr     dx,cl                   ;needed after termination
  44.                 mov     ax,3100h                ;Terminate and stay
  45.                 int     21h                     ;resident
  46. ;
  47. ; If this is the second instance of this program in memory, terminate now.
  48. LoadedLow:      mov     es,ds:[16h]             ;Load parent╒s PSP address
  49.                 mov     di,100h                 ;Set DI equal to 100h
  50.                 mov     si,di                   ;Set SI equal to DI
  51.                 mov     cx,100h                 ;Set counter for 256 bytes
  52.                 cld                             ;Clear direction flag
  53.                 repe    cmpsb                   ;Compare 256 bytes
  54.                 jne     SetLink                 ;Branch ifthey╒re not equal
  55.                 jmp     ErrorExit3              ;Exit if they are
  56. ;
  57. ; Set the upper memory link and change the allocation strategy to high-only.
  58. SetLink:        call    SaveMemState            ;Save the memory state
  59.                 mov     ax,5803h                ;Link upper and
  60.                 mov     bx,SET_LINK             ;conventional memory
  61.                 int     21h
  62.                 mov     dx,offset Message2      ;Branch if call failed
  63.                 jc      ErrorExit2
  64.  
  65.                 mov     ax,5801h                ;Set allocation strategy
  66.                 mov     bx,FF_HIGH_ONLY         ;to first-fit/high-only
  67.                 int     21h
  68.  
  69.                 mov     ah,48h                  ;Find the size of the
  70.                 mov     bx,0FFFFh               ;largest free UMB
  71.                 int     21h
  72.  
  73.                 mov     dx,offset Message3      ;Branch if the largest free
  74.                 cmp     bx,UMB_REQUIRED         ;UMB is not large enough to
  75.                 jb      ErrorExit1              ;hold the program
  76. ;
  77. ; EXEC another instance of the program.
  78.                 mov     es,ds:[2Ch]             ;Get env segment in ES
  79.                 xor     di,di                   ;Point DI to segment base
  80.                 xor     ax,ax                   ;Zero AX
  81.  
  82. TryAgain:       scasw                           ;Scan for two zero bytes
  83.                 jz      ZeroesFound             ;Branch if they were found
  84.                 dec     di                      ;Otherwise decrement DI and
  85.                 jmp     TryAgain                ;try again
  86.  
  87. ZeroesFound:    add     di,2                    ;Point DI to program name
  88.                 mov     dx,di                   ;Transfer the address to DX
  89.                 mov     ax,cs                   ;Get PSP address in AX
  90.                 mov     es,ax                   ;Transfer address to ES
  91.  
  92.                 mov     word ptr pb.Tail,80h    ;Load parameters block
  93.                 mov     word ptr pb.Tail[2],ax  ;with pointers to data
  94.                 mov     word ptr pb.FCB_1,5Ch   ;objects
  95.                 mov     word ptr pb.FCB_1[2],ax
  96.                 mov     word ptr pb.FCB_2,6Ch
  97.                 mov     word ptr pb.FCB_2[2],ax
  98.                 mov     bx,offset pb            ;ES:BX -> Parameter block
  99.  
  100.                 push    ds                      ;Save DS
  101.                 mov     ds,ds:[2Ch]             ;Point DS to env segment
  102.                 assume  ds:nothing              ;DS:DX -> Program name
  103.  
  104.                 mov     ax,4B00h                ;Load and execute the program
  105.                 int     21h
  106.  
  107.                 pop     ds                      ;Restore DS
  108.                 assume  ds:code
  109.                 jc      LoadFailed              ;Branch if EXEC failed
  110.  
  111.                 mov     ah,4Dh                  ;Get the return code
  112.                 int     21h
  113.                 or      al,al                   ;Branch if the return
  114.                 jnz     LoadFailed              ;code is not zero
  115.  
  116.                 mov     ah,09h                  ;Display message saying
  117.                 mov     dx,offset Message1      ;the program was loaded
  118.                 int     21h                     ;high
  119.                 call    RestoreMemState         ;Restore the memory state
  120.                 mov     ax,4C00h                ;Terminate this instance
  121.                 int     21h                     ;of the program
  122.  
  123. LoadFailed:     mov     dx,offset Message4      ;Point DX to error message
  124. ErrorExit1:     call    RestoreMemState         ;Restore the memory state
  125. ErrorExit2:     mov     ah,09h                  ;Display the error message
  126.                 int     21h
  127. ErrorExit3:     mov     ax,4C01h                ;Terminate with a return
  128.                 int     21h                     ;code of 1
  129. Init            endp
  130.  
  131. ;; SaveMemState saves the current memory allocation strategy and
  132. ;; upper memory link flag.
  133.  
  134. SaveMemState    proc    near
  135.                 mov     ax,5800h                ;Save the current memory
  136.                 int     21h                     ;allocation strategy
  137.                 mov     Strategy,ax
  138.                 mov     ax,5802h                ;Save the current upper
  139.                 int     21h                     ;memory link flag
  140.                 mov     byte ptr LinkFlag,al
  141.                 ret                             ;Return to caller
  142. SaveMemState    endp
  143. ;; RestoreMemState restores the memory allocation strategy and
  144. ;; upper memory link flag saved earlier.
  145. RestoreMemState proc    near
  146.                 mov     ax,5801h                ;Restore the memory
  147.                 mov     bx,Strategy             ;allocation strategy
  148.                 int     21h
  149.                 mov     ax,5803h                ;Restore the upper
  150.                 mov     bx,LinkFlag             ;memory link flag
  151.                 int     21h
  152.                 ret                             ;Return to caller
  153.  
  154. RestoreMemState endp
  155.  
  156. Message1        db      ╥The program was successfully loaded high╙,13,10,╙$╙
  157. Message2        db      ╥There are no UMBs to load the program into╙,13,10,╙$╙
  158. Message3        db      ╥Insufficient upper memory╙,13,10,╙$╙
  159. Message4        db      ╥Attempt to load the program high failed╙,13,10,╙$╙
  160.  
  161. Strategy        dw      ?                       ;Memory allocation strategy
  162. LinkFlag        dw      0                       ;Upper memory link flag
  163. pb              LOADEXEC <>                     ;EXEC parameter block
  164.  
  165. code            ends
  166.                 end     Begin
  167.