home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / caway349.zip / BIN / CALLBACK.ASM < prev    next >
Assembly Source File  |  1996-06-17  |  4KB  |  187 lines

  1. ;
  2. ;A demo of using FarCallReal to pass control from protected mode to real mode
  3. ;and then use a Call-Back to pass control from real mode to protected mode.
  4. ;
  5.     .386            ;This order of .386 and .model
  6.     .model small        ;ensures use32 segments.
  7.     .stack 1024        ;should be enough for most.
  8.  
  9. ;    option oldstructs        ;MASM 6.1 needs this.
  10.  
  11.     include cw.inc        ;define CauseWay API
  12.  
  13.     .code
  14.  
  15. ;-------------------------------------------------------------------------------
  16. Start    proc    near
  17.     mov    ax,DGROUP        ;Make our data addressable.
  18.     mov    ds,ax
  19. ;
  20. ;Get a Call-Back so that we can pass control from real mode to protected mode.
  21. ;
  22.     push    ds
  23.     mov    ax,ds        ;Point to the real mode register
  24.     mov    es,ax        ;structure.
  25.     mov    edi,offset Real2ProtectedRegs
  26.     mov    ax,cs        ;Point to the protected mode
  27.     mov    ds,ax        ;recipient of the Call-Back.
  28.     mov    esi,offset Real2ProtectedEntry
  29.     sys    GetCallBack        ;allocate a Call-Back.
  30.     pop    ds
  31.     jc    callback_error
  32. ;
  33. ;Put the real mode address in a useful place.
  34. ;
  35.     push    ds
  36.     mov    ax,_REAL
  37.     mov    ds,ax
  38.     assume ds:_REAL
  39.     mov    word ptr [Real2Protected],dx
  40.     mov    word ptr [Real2Protected+2],cx
  41.     assume ds:DGROUP
  42.     pop    ds
  43. ;
  44. ;We need some DOS memory to copy the real mode code into.
  45. ;
  46.     mov    bx,((RealEnd-RealStart)/16)+1
  47.     sys    GetMemDOS
  48.     jc    memory_error
  49.     mov    RealSegment,ax
  50.     mov    RealSelector,dx
  51. ;
  52. ;Copy our real mode code into the DOS memory allocated.
  53. ;
  54.     push    ds
  55.     mov    es,dx
  56.     mov    edi,0
  57.     mov    esi,edi
  58.     mov    ax,_REAL
  59.     mov    ds,ax
  60.     mov    ecx,RealEnd-RealStart
  61.     rep    movsb
  62.     pop    ds
  63. ;
  64. ;Pass control to the real mode code.
  65. ;
  66.     push    ds
  67.     pop    es
  68.     mov    edi,offset Protected2RealRegs
  69.     mov    ax,RealSegment
  70.     mov    Real_CS[edi],ax
  71.     mov    ax,offset RealModeEntry
  72.     mov    Real_IP[edi],ax
  73.     sys    FarCallReal
  74. ;
  75. ;Now we've proved our point we can exit.
  76. ;
  77.     jmp    exit
  78. ;
  79. ;Wasn't enough DOS memory so print a message and exit.
  80. ;
  81. memory_error:    mov    edx,offset MemoryMessage
  82.     jmp    error_print
  83. ;
  84. ;Wasn't a free Call-Back so print a message and exit.
  85. ;
  86. callback_error: mov    edx,offset CallBackMessage
  87.     jmp    error_print
  88. ;
  89. ;Print a message.
  90. ;
  91. error_print:    mov    ah,9
  92.     int    21h
  93. ;
  94. ;Back to the real world.
  95. ;
  96. exit:    mov    ax,4c00h
  97.     int    21h
  98. Start    endp
  99.  
  100. ;-------------------------------------------------------------------------------
  101. ;
  102. ;This gets control from real mode so do something to let the world know it
  103. ;happened.
  104. ;
  105. Real2ProtectedEntry proc far
  106. ;
  107. ;Need to adjust the real mode CS:IP and SP first.
  108. ;
  109.     mov    ax,[esi]        ;get stacked offset.
  110.     mov    es:Real_IP[edi],ax
  111.     mov    ax,2[esi]
  112.     mov    es:Real_CS[edi],ax
  113.     add    es:Real_SP[edi],4
  114. ;
  115. ;Now save all the registers ready for exit.
  116. ;
  117.     pushad
  118.     push    ds
  119.     push    es
  120.     push    fs
  121.     push    gs
  122. ;
  123. ;The real mode code could have passed values to this code via the registers. A
  124. ;copy of the real mode registers is in the struc pointed to by ES:EDI. When we
  125. ;exit the real mode registers will be reloaded from this struc which allows
  126. ;values to be passed back as well.
  127. ;
  128.     mov    ax,DGROUP        ;Make our data addressable.
  129.     mov    ds,ax
  130. ;
  131. ;Print a message to let us know we got here.
  132. ;
  133.     mov    ah,9
  134.     mov    edx,offset ProtModeMessage
  135.     int    21h
  136. ;
  137. ;Retrieve the registers.
  138. ;
  139.     pop    gs
  140.     pop    fs
  141.     pop    es
  142.     pop    ds
  143.     popad
  144. ;
  145. ;Return to the real mode caller.
  146. ;
  147.     iretd
  148. Real2ProtectedEntry endp
  149.  
  150.  
  151.     .data
  152. ;-------------------------------------------------------------------------------
  153. RealSegment    dw ?
  154. RealSelector    dw ?
  155. Real2ProtectedRegs db size RealRegsStruc dup (?)
  156. Protected2RealRegs db size RealRegsStruc dup (?)
  157. MemoryMessage    db "Not enough DOS memory to run.",13,10,"$"
  158. CallBackMessage db "No free Call-Back.",13,10,"$"
  159. ProtModeMessage db "Hello world from protected mode via a Call-Back.",13,10,"$"
  160.  
  161. ;-------------------------------------------------------------------------------
  162. ;
  163. ;This segment gets copied into conventional DOS memory.
  164. ;
  165. _REAL    segment para public 'data' use16
  166.     assume cs:_REAL, ds:_REAL
  167. RealStart    equ    $
  168. ;
  169. ;This needs to be far so we can use FarCallReal to address it.
  170. ;
  171. RealModeEntry    proc    far
  172.     push    cs        ;Make our data addressable. This
  173.     pop    ds        ;is valid in real mode.
  174.     mov    dx,offset RealModeMessage
  175.     mov    ah,9
  176.     int    21h
  177.     call    dword ptr [Real2Protected] ;Call protected mode code.
  178.     retf
  179. Real2Protected    dd ?
  180. RealModeMessage db "Hello world from real mode",13,10,"$"
  181. RealModeEntry    endp
  182. ;
  183. RealEnd    equ    $
  184. _REAL    ends
  185.  
  186.     end    Start
  187.