home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c329 / 2.img / EXAMPLES / M2.ASM < prev    next >
Encoding:
Assembly Source File  |  1990-01-23  |  7.2 KB  |  164 lines

  1.         TITLE    METHOD2
  2. ; Purpose: demonstrates interfacing between
  3. ; protected mode program and real mode program
  4. ; by shelling from protected mode to real mode.
  5.  
  6. codeseg segment                         ;
  7.         assume  cs:codeseg              ;
  8.                                         ;
  9.          rds     dw (?)                 ;
  10.          res     dw (?)                 ;
  11.          rss     dw (?)                 ;
  12.          rsp     dw (?)                 ;
  13.          rbp     dw (?)                 ;
  14.          rdi     dw (?)                 ;
  15.          rsi     dw (?)                 ;
  16.          rax     dw (?)                 ;
  17.          rbx     dw (?)                 ;
  18.          rcx     dw (?)                 ;
  19.          rdx     dw (?)                 ;
  20.          rfl     dw (?)                 ;
  21.          rseg    dw (?)                 ;
  22.          roff    dw (?)                 ;
  23.          psp     dw (?)                 ;
  24.          pss     dw (?)                 ;
  25.                                         ;
  26. ; ------ install ---------------------- ;
  27. ;
  28. ; install is passed two arguments. One is the address of
  29. ; the real mode buffer, passed through BASIC from the
  30. ; protected mode program. The other is the BASIC array,
  31. ; which will serve as a data transfer area.
  32. ;
  33. ; The real mode buffer is loaded with the address of the
  34. ; data transfer area and the address of a return
  35. ; instruction.
  36. ; NOTE => It is important to observe the following two
  37. ; rules:
  38. ;  1. install MUST be called from the main program area
  39. ;  2. install MUST NOT be called more than once.
  40. ;
  41. ; -------------------------------------
  42. ;
  43.                                         ;
  44. install proc    far                     ;
  45.         public  install                 ;
  46.                                         ;
  47. ; start by saving the state of the processor into RAM.
  48.                                         ;
  49.          mov     cs:rax,ax              ; save ax
  50.          mov     cs:rbx,bx              ; save bx
  51.          mov     cs:rcx,cx              ; save cx
  52.          mov     cs:rdx,dx              ; save dx
  53.          mov     cs:rdi,di              ; save di
  54.          mov     cs:rsi,si              ; save si
  55.          lahf                           ;
  56.          mov     cs:rfl,ax              ; save flags
  57.          mov     ax,ds                  ;
  58.          mov     cs:rds,ax              ; save ds
  59.          mov     ax,es                  ;
  60.          mov     cs:res,ax              ; save es
  61.          mov     ax,ss                  ;
  62.          mov     cs:rss,ax              ; save ss
  63.          mov     ax,sp                  ;
  64.          mov     cs:rsp,ax              ; save sp
  65.          mov     ax,bp                  ;
  66.          mov     cs:rbp,ax              ; save bp
  67.          push    bp                     ;
  68.          mov     bp,sp                  ;
  69.          mov     ax,[bp+4]              ; save portion of
  70.          mov     cs:rseg,ax             ;       stack
  71.          mov     ax,[bp+2]              ;
  72.          mov     cs:roff,ax             ;
  73.          mov     ax,cs:rds              ;
  74.          mov     es,ax                  ;
  75.                                         ;
  76. ; Put address of return instruction into the buffer passed
  77. ; through BASIC from the protected mode program.
  78.                                         ;
  79.          mov     di,[bp+8]              ; address of 2nd arg
  80.          mov     ax,es:[di]             ; ds now defined
  81.          mov     ds,ax                  ; by calling routine
  82.          mov     ax,cs                  ; this code segment
  83.          mov     ds:[2],ax              ;
  84.          mov     ax,offset retinst      ; return instruction
  85.          mov     ds:[0],ax              ;
  86.                                         ;
  87. ; Put the address of the data transfer array into the
  88. ; same buffer.
  89.                                         ;
  90.         mov     ax,cs:rds               ;
  91.         mov     ds:[6],ax               ;
  92.         mov     ax,[bp+6]               ;
  93.         mov     ds:[4],ax               ;
  94.         mov     ah,31h               ; request to terminate
  95.         int     21h                  ; and stay resident
  96.                                         ;
  97. ; All future calls to the BASIC program start here.
  98.                                         ;
  99. retinst:                                ;
  100.         push    cs                      ;
  101.         pop     ds                      ;
  102.                                         ;
  103. ; Save the stack so we can recover it and return to
  104. ; protected mode.
  105.                                        ;
  106.         mov     ax,sp                  ;
  107.         mov     psp,ax                 ; save sp
  108.         mov     ax,ss                  ;
  109.         mov     pss,ax                 ; save ss
  110.         mov     ax,rss                 ;
  111.                                        ;
  112. ; Restore the processor, and part of the stack, as it was
  113. ; defined by BASIC.
  114.                                         ;
  115.         mov     ss,ax                   ; restore BASIC
  116.         mov     ax,rsp                  ;    stack segment
  117.         mov     sp,ax                   ; restore BASIC
  118.         add     sp,4                    ;    stack pointer
  119.         push    cs:rseg                 ; and portion of stack
  120.                                         ;
  121.         push    cs:roff                 ;
  122.         mov     ax,rbp                  ; restore BASIC
  123.         mov     bp,ax                   ;      base pointer
  124.         mov     ax,res                  ; restore BASIC
  125.         mov     es,ax                   ; extra segment
  126.         mov     ax,rds                  ; must be last restore
  127.         mov     ds,ax                   ; restore BASIC
  128.         mov     ax,cs:rfl               ;      data segment
  129.         sahf                            ; restore flags
  130.         mov     ax,cs:rax               ; restore ax
  131.         mov     bx,cs:rbx               ; restore bx
  132.         mov     cx,cs:rcx               ; restore cx
  133.         mov     dx,cs:rdx               ; restore dx
  134.         mov     di,cs:rdi               ; restore di
  135.         mov     si,cs:rsi               ; restore si
  136.                                         ;
  137. ; At this point, the return instruction delivers us to the
  138. ; main BASIC program, at the instruction following the call
  139. ; to install.
  140.                                         ;
  141.         ret     4                       ; ss:sp points at
  142.                                         ; BASIC code
  143. install endp                            ;
  144.                                         ;
  145. ; ------ goback ------------------------
  146. ; goback returns to the calling protected mode program
  147. ; after the BASIC program has provided a service. It
  148. ; can be called from any level of the BASIC program.
  149. ;
  150. ; --------------------------------------
  151. ;
  152.                                         ;
  153. goback  proc    far                     ;
  154.          public  goback                 ;
  155.          mov     ax,psp                 ; restore stack to
  156.          mov     sp,ax                  ; what it was when
  157.          mov     ax,pss                 ; protected mode
  158.          mov     ss,ax                  ; program called.
  159.          ret                            ; return
  160. goback endp                             ;
  161. codeseg ends                            ;
  162.          end                            ;
  163.  
  164.