home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / dosexec.zip / ROOT.ASM < prev   
Assembly Source File  |  1989-06-07  |  5KB  |  220 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.OVL, 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 
  46.             ; block
  47.     int    21h    ; transfer to MS-DOS
  48.     jc    main1    ; jump if resize failed
  49.  
  50.             ; display message 'Root
  51.             ; 'segment executing...'
  52.     mov    dx,offset DGROUP:msg1    ; DS:DX = address of message
  53.     mov    cx,msg1_len    ; CX = length of message
  54.     call    pmsg
  55.  
  56.             ; allocate memory for overlay
  57.     mov    bx,1000h    ; get 64 KB (4096 paragraphs)
  58.     mov    ah,48h    ; fxn 48h, allocate mem block
  59.     int    21h    ; transfer to MS-DOS
  60.     jc    main2    ; jump if allocation failed
  61.  
  62.     mov    pars,ax    ; set load address for 
  63.             ; overlay
  64.     mov    pars+2,ax    ; set relocation segment for
  65.             ; overlay
  66.     mov    word ptr entry+2,ax    ; set segment of entry point
  67.  
  68.     push    ds    ; save root's data segment
  69.     mov    stk_seg,ss    ; save root's stack pointer
  70.     mov    stk_ptr,sp
  71.  
  72.             ; now use EXEC to load 
  73.             ; overlay
  74.     mov    ax,ds    ; set ES = DS
  75.     mov    es,ax
  76.     mov    dx,offset DGROUP:oname    ; DS:DX = overlay pathname
  77.     mov    bx,offset DGROUP:pars    ; EX:BX = parameter block
  78.     mov    ax,4b03h    ; function 4BH, subfunction 
  79.             ; 03H
  80.     int    21h    ; transfer to MS-DOS
  81.  
  82.     cli        ; (for bug in some early 
  83.             ; 8088s)
  84.     mov    ss,stk_seg    ; restore root's stack 
  85.             ; pointer
  86.     mov    sp,stk_ptr
  87.     sti        ; (for bug in some early 8088s)
  88.     pop    ds    ; restore DS = our data 
  89.             ; segment
  90.  
  91.     jc    main3    ; jump if EXEC failed
  92.  
  93.             ; otherwise EXEC succeeded...
  94.     push    ds    ; save our data segment
  95.     call    dword ptr entry    ; now call the overlay
  96.     pop    ds    ; restore our data segment
  97.  
  98.             ; display message that root
  99.             ; segment regained control...
  100.     mov    dx,offset DGROUP:msg5    ; DS:DX = address of message
  101.     mov    cx,msg5_len    ; CX = length of message
  102.     call    pmsg    ; display it
  103.  
  104.     mov    ax,4c00h    ; no error, terminate program
  105.     int    21h    ; with return code = 0
  106.  
  107. main1:    mov    bx,offset DGROUP:msg2a    ; convert error code
  108.     call    b2hex
  109.     mov    dx,offset DGROUP:msg2    ; display message 'Memory
  110.     mov    cx,msg2_len    ; resize failed...'
  111.     call    pmsg
  112.     jmp    main4
  113.  
  114. main2:    ov    bx,offset DGROUP:msg3a    ; convert error code
  115.     call    b2hex
  116.     mov    dx,offset DGROUP:msg3    ; display message 'Memory
  117.     mov    cx,msg3_len    ; allocation failed...'
  118.     call    pmsg
  119.     jmp    main4
  120.  
  121. main3:    mov    bx,offset DGROUP:msg4a    ; convert error code
  122.     call    b2hex
  123.     mov    dx,offset DGROUP:msg4    ; display message 'EXEC
  124.     mov    cx,msg4_len    ; call failed...'
  125.     call    pmsg
  126.  
  127. main4:    mov    ax,4c01h    ; error, terminate program
  128.     int    21h    ; with return code = 1
  129.  
  130. main    endp        ; end of main procedure
  131.  
  132.  
  133. b2hex    proc    near    ; convert byte to hex ASCII
  134.             ; call with AL = binary value
  135.             ; BX = addr to store string
  136.     push    ax
  137.     shr    al,1
  138.     shr    al,1
  139.     shr    al,1
  140.     shr    al,1
  141.     call    ascii    ; become first ASCII 
  142.             ; character
  143.     mov    [bx],al    ; store it
  144.     pop    ax
  145.     and    al,0fh    ; isolate lower 4 bits, which
  146.     call    ascii    ; become the second ASCII 
  147.             ; character
  148.     mov    [bx+1],al    ; store it
  149.     ret
  150. b2hex    endp
  151.  
  152.  
  153. ascii    proc    near    ; convert value 00-0FH in AL
  154.     add    al,'0'    ; into a "hex ASCII" character
  155.     cmp    al,'9'
  156.     jle    ascii2    ; jump if in range 00-09H,
  157.     add    al,'A'-'9'-1    ; offset it to range 0A-0FH,
  158. ascii2:    ret         ; return ASCII char. in AL.
  159. ascii    endp
  160.  
  161.  
  162. pmsg    proc    near    ; displays message on 
  163.             ; standard output
  164.             ; call with DS:DX = address,
  165.             ;              CX = length
  166.  
  167.     mov    bx,stdout    ; BX = standard output handle
  168.     mov    ah,40h    ; function 40H = write 
  169.             ; file/device
  170.     int    21h    ; transfer to MS-DOS
  171.     ret        ; back to caller
  172.  
  173. pmsg    ndp
  174.  
  175. _TEXT    ends
  176.  
  177.  
  178. _DATA    segment para public 'DATA'    ; static & variable data 
  179.             ; segment
  180.  
  181. oname    db    'OVERLAY.OVL',0    ; pathname of overlay file
  182.  
  183. pars    dw    0    ; load address (segment) for 
  184.             ; file
  185.     dw    0    ; relocation (segment) for 
  186.             ; file
  187.  
  188. entry    dd    0    ; entry point for overlay
  189.  
  190. msg1    db    cr,lf,'Root segment executing!',cr,lf
  191. msg1_len equ     $-msg1
  192.  
  193. msg2    db    cr,lf,'Memory resize failed, error code='
  194. msg2a    db    'xxh.',cr,lf
  195. msg2_len equ     $-msg2
  196.  
  197. msg3    db    cr,lf,'Memory allocation failed, error code='
  198. msg3a    db    'xxh.',cr,lf
  199. msg3_len equ     $-msg3
  200.  
  201. msg4    db    cr,lf,'EXEC call failed, error code='
  202. msg4a    db    'xxh.',cr,lf
  203. msg4_len equ     $-msg4
  204.  
  205. msg5    db    cr,lf,'Root segment regained control!',cr,lf
  206. msg5_len equ     $-msg5
  207.  
  208. _DATA    ends
  209.  
  210.  
  211. _STACK    segment para stack 'STACK'
  212.  
  213.     db    stksize dup (?)
  214.  
  215. _STACK  ends
  216.  
  217.  
  218.     end    main    ; defines program entry point
  219.  
  220.