home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / dos_ency / 10 / root.asm < prev   
Encoding:
Assembly Source File  |  1988-08-11  |  7.7 KB  |  204 lines

  1.          name      root
  2.          title     'ROOT --- demonstrate EXEC overlay'
  3. ;
  4. ; ROOT.EXE --- demonstration of EXEC for overlays
  5. ;
  6. ; Uses MS-DOS EXEC (Int 21H Function 4BH Subfunction 03H)
  7. ; to load an overlay named OVERLAY.EXE, calls a routine
  8. ; within the OVERLAY, then recovers control and terminates.
  9. ;
  10. ; Ray Duncan, June 1987
  11. ;
  12.  
  13. stdin   equ     0                       ; standard input
  14. stdout  equ     1                       ; standard output
  15. stderr  equ     2                       ; standard error
  16.  
  17. stksize equ     128                     ; size of stack
  18.  
  19. cr      equ     0dh                     ; ASCII carriage return
  20. lf      equ     0ah                     ; ASCII linefeed
  21.  
  22.  
  23. DGROUP  group   _DATA,_STACK
  24.  
  25.  
  26. _TEXT   segment byte public 'CODE'      ; executable code segment
  27.  
  28.         assume  cs:_TEXT,ds:_DATA,ss:_STACK
  29.  
  30.  
  31. stk_seg dw      ?                       ; original SS contents
  32. stk_ptr dw      ?                       ; original SP contents
  33.  
  34.  
  35. main    proc    far                     ; entry point from MS-DOS
  36.  
  37.         mov     ax,_DATA                ; set DS = our data segment
  38.         mov     ds,ax
  39.  
  40.                                         ; now give back extra memory
  41.         mov     ax,es                   ; AX = segment of PSP base
  42.         mov     bx,ss                   ; BX = segment of stack base
  43.         sub     bx,ax                   ; reserve seg stack - seg psp
  44.         add     bx,stksize/16           ; plus paragraphs of stack
  45.         mov     ah,4ah                  ; fxn 4AH = modify memory block
  46.         int     21h                     ; transfer to MS-DOS
  47.         jc      main1                   ; jump if resize failed
  48.  
  49.                                         ; display message 'Root
  50.                                         ; 'segment executing...'
  51.         mov     dx,offset DGROUP:msg1   ; DS:DX = address of message
  52.         mov     cx,msg1_len             ; CX = length of message
  53.         call    pmsg
  54.  
  55.                                         ; allocate memory for overlay
  56.         mov     bx,1000h                ; get 64 KB (4096 paragraphs)
  57.         mov     ah,48h                  ; fxn 48h, allocate mem block
  58.         int     21h                     ; transfer to MS-DOS
  59.         jc      main2                   ; jump if allocation failed
  60.  
  61.         mov     pars,ax                 ; set load address for overlay
  62.         mov     pars+2,ax               ; set relocation segment for overlay
  63.         mov     word ptr entry+2,ax     ; set segment of entry point
  64.  
  65.         push    ds                      ; save root's data segment
  66.         mov     stk_seg,ss              ; save root's stack pointer
  67.         mov     stk_ptr,sp
  68.  
  69.                                         ; now use EXEC to load overlay
  70.         mov     ax,ds                   ; set ES = DS
  71.         mov     es,ax
  72.         mov     dx,offset DGROUP:oname  ; DS:DX = overlay pathname
  73.         mov     bx,offset DGROUP:pars   ; EX:BX = parameter block
  74.         mov     ax,4b03h                ; function 4BH, subfunction 03H
  75.         int     21h                     ; transfer to MS-DOS
  76.  
  77.         cli                             ; (for bug in some early 8088s)
  78.         mov     ss,stk_seg              ; restore root's stack pointer
  79.         mov     sp,stk_ptr
  80.         sti                             ; (for bug in some early 8088s)
  81.         pop     ds                      ; restore DS = our data segment
  82.  
  83.         jc      main3                   ; jump if EXEC failed
  84.  
  85.                                         ; otherwise EXEC succeeded...
  86.         push    ds                      ; save our data segment
  87.         call    dword ptr entry         ; now call the overlay
  88.         pop     ds                      ; restore our data segment
  89.  
  90.                                         ; display message that root
  91.                                         ; segment regained control...
  92.         mov     dx,offset DGROUP:msg5   ; DS:DX = address of message
  93.         mov     cx,msg5_len             ; CX = length of message
  94.         call    pmsg                    ; display it
  95.  
  96.         mov     ax,4c00h                ; no error, terminate program
  97.         int     21h                     ; with return code = 0
  98.  
  99. main1:  mov     bx,offset DGROUP:msg2a  ; convert error code
  100.         call    b2hex
  101.         mov     dx,offset DGROUP:msg2   ; display message 'Memory
  102.         mov     cx,msg2_len             ; resize failed...'
  103.         call    pmsg
  104.         jmp     main4
  105.  
  106. main2:  mov     bx,offset DGROUP:msg3a  ; convert error code
  107.         call    b2hex
  108.         mov     dx,offset DGROUP:msg3   ; display message 'Memory
  109.         mov     cx,msg3_len             ; allocation failed...'
  110.         call    pmsg
  111.         jmp     main4
  112.  
  113. main3:  mov     bx,offset DGROUP:msg4a  ; convert error code
  114.         call    b2hex
  115.         mov     dx,offset DGROUP:msg4   ; display message 'EXEC
  116.         mov     cx,msg4_len             ; call failed...'
  117.         call    pmsg
  118.  
  119. main4:  mov     ax,4c01h                ; error, terminate program
  120.         int     21h                     ; with return code = 1
  121.  
  122. main    endp                            ; end of main procedure
  123.  
  124.  
  125. b2hex   proc    near                    ; convert byte to hex ASCII
  126.                                         ; call with AL = binary value
  127.                                         ; BX = addr to store string
  128.         push    ax
  129.         shr     al,1
  130.         shr     al,1
  131.         shr     al,1
  132.         shr     al,1
  133.         call    ascii                   ; become first ASCII character
  134.         mov     [bx],al                 ; store it
  135.         pop     ax
  136.         and     al,0fh                  ; isolate lower 4 bits, which
  137.         call    ascii                   ; become the second ASCII character
  138.         mov     [bx+1],al               ; store it
  139.         ret
  140. b2hex   endp
  141.  
  142.  
  143. ascii   proc    near                    ; convert value 00-0FH in AL
  144.         add     al,'0'                  ; into a "hex ASCII" character
  145.         cmp     al,'9'
  146.         jle     ascii2                  ; jump if in range 00-09H,
  147.         add     al,'A'-'9'-1            ; offset it to range 0A-0FH,
  148. ascii2: ret                             ; return ASCII char. in AL.
  149. ascii   endp
  150.  
  151.  
  152. pmsg    proc    near                    ; displays message on standard output
  153.                                         ; call with DS:DX = address,
  154.                                         ;              CX = length
  155.  
  156.         mov     bx,stdout               ; BX = standard output handle
  157.         mov     ah,40h                  ; function 40H = write file/device
  158.         int     21h                     ; transfer to MS-DOS
  159.         ret                             ; back to caller
  160.  
  161. pmsg    endp
  162.  
  163. _TEXT   ends
  164.  
  165.  
  166. _DATA   segment para public 'DATA'      ; static & variable data segment
  167.  
  168. oname   db      'OVERLAY.OVL',0         ; pathname of overlay file
  169.  
  170. pars    dw      0                       ; load address (segment) for file
  171.         dw      0                       ; relocation (segment) for file
  172.  
  173. entry   dd      0                       ; entry point for overlay
  174.  
  175. msg1    db      cr,lf,'Root segment executing!',cr,lf
  176. msg1_len equ    $-msg1
  177.  
  178. msg2    db      cr,lf,'Memory resize failed, error code='
  179. msg2a   db      'xxh.',cr,lf
  180. msg2_len equ    $-msg2
  181.  
  182. msg3    db      cr,lf,'Memory allocation failed, error code='
  183. msg3a   db      'xxh.',cr,lf
  184. msg3_len equ    $-msg3
  185.  
  186. msg4    db      cr,lf,'EXEC call failed, error code='
  187. msg4a   db      'xxh.',cr,lf
  188. msg4_len equ    $-msg4
  189.  
  190. msg5    db      cr,lf,'Root segment regained control!',cr,lf
  191. msg5_len equ    $-msg5
  192.  
  193. _DATA   ends
  194.  
  195.  
  196. _STACK  segment para stack 'STACK'
  197.  
  198.         db      stksize dup (?)
  199.  
  200. _STACK  ends
  201.  
  202.  
  203.         end     main                    ; defines program entry point
  204.