home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c329 / 2.img / EXAMPLES / M1.ASM < prev    next >
Encoding:
Assembly Source File  |  1989-12-18  |  4.7 KB  |  164 lines

  1.         TITLE   METHOD1
  2. ; Purpose: demonstrates interfacing between
  3. ; protected mode program and real mode program
  4. ; via interrupt.
  5.  
  6. ; We are using the following interrupt number because
  7. ; it seems not to conflict with other programs, but we
  8. ; could easily use a different number, if we wish.
  9. INTNUM EQU 48h
  10. NULL equ 0
  11.  
  12. print macro string
  13.         LOCAL   next,done
  14.         lea     si,string
  15. next:   lodsb
  16.         or      al,al
  17.         jz      done
  18.         mov     ah,0Eh
  19.         int     10h
  20.         jmp     next
  21. done:
  22. endm
  23.  
  24. _DATA   SEGMENT  WORD PUBLIC 'DATA'
  25. ; The calling program uses the AX register to pass an
  26. ; argument indicating which of several routines to call.
  27. ; The following macro defines a table of code pointers
  28. ; to those routines for which the argument acts as an index.
  29. ;
  30. code_table label byte
  31. HIGHEST_INDEX = 0
  32. IRP label_number, <0,1,2,3,4>
  33.         dw      p&label_number
  34.         HIGHEST_INDEX = label_number
  35. ENDM
  36.         dw      NULL
  37.  
  38. ; The following defines 1000 bytes of storage to be used
  39. ; as a buffer between the real and protected mode programs.
  40. data_buffer db 1000 dup (?)
  41.  
  42. ; The next three strings are used to verify that the calling
  43. ; program can indeed call real mode routines, by having
  44. ; routine p1 output msg1, etc.
  45. even
  46. msg1    db      'Message 1',0dh,0ah,0
  47. even
  48. msg2    db      'Message 2',0dh,0ah,0
  49. even
  50. msg3    db      'Message 3',0dh,0ah,0
  51. even
  52. msg4    db      'Goodbye',0
  53. end_msg4 equ $
  54. _DATA   ENDS
  55.  
  56. ; We use Phar Lap's Intermode stack. We leave the
  57. ; following segment definition in place, however,
  58. ; in case we decide to use our own.
  59. STACK SEGMENT WORD STACK 'STACK'
  60.         db 100 dup (?)
  61. end_stack label byte
  62. STACK ENDS
  63.  
  64. DGROUP  GROUP _DATA, STACK
  65.  
  66. _TEXT   SEGMENT  PARA PUBLIC 'CODE'
  67.         ASSUME  CS: _TEXT, DS: DGROUP, SS: DGROUP, ES: DGROUP
  68. ; The Interrupt Service Routine.
  69. ; Return value in ax. Destroys bx,cx.
  70. isr proc near
  71.         push    si              ; save si
  72.         mov     bx, DGROUP
  73.         mov     ds,bx           ; point ds at real mode data
  74.         cmp     ax,HIGHEST_INDEX
  75.         jbe     index_ok
  76.         stc
  77.         jmp     short exit
  78. index_ok:
  79.         lea     bx,code_table   ; get pointer to code table
  80.         shl     ax,1            ; index * size of code pointer
  81.         add     bx,ax           ; now, index into table
  82.         call    [bx]            ; call desired routine
  83. exit:
  84.         pop     si              ; restore si
  85. ; return value of 0 means ok, anything else means problem
  86.         sbb     ax,ax           ; return value in ax
  87.         iret
  88.  
  89. ; The first time the protected mode code calls the real
  90. ; mode program, it calls p0, which passes back the real
  91. ; mode address of the data buffer in bx,cx. The segment
  92. ; address goes to cx, the offset goes to bx. After this,
  93. ; both programs can communicate through the data buffer.
  94. ; The next three times the protected mode code calls the
  95. ; real mode program, it calls p1, p2, and p3, which output
  96. ; msg1, msg2, and msg3 respectively, to verify that the
  97. ; table-driven program is working.
  98. ; The fourth time the protected mode program calls, it
  99. ; moves a string into the data buffer, which is then
  100. ; printed by the real mode program. The real mode program
  101. ; then moves a different string into the buffer and returns.
  102. ; The protected mode program then prints out the new contents.
  103. ; This is to verify that both programs can communicate through
  104. ; the data buffer.
  105.  
  106. p0 label near
  107.         mov     cx,DGROUP
  108.         lea     bx,data_buffer
  109.         clc
  110.         ret
  111. p1 label near
  112.         print msg1
  113.         clc
  114.         ret
  115. p2 label near
  116.         print msg2
  117.         clc
  118.         ret
  119. p3 label near
  120.         print msg3
  121.         clc
  122.         ret
  123. p4 label near
  124.         print data_buffer
  125.         push    di
  126.         push    es
  127.         push    ds
  128.         pop     es
  129.         lea     si, msg4
  130.         lea     di,data_buffer
  131.         mov     cx,end_msg4-msg4
  132.         rep     movsb
  133.         pop     es
  134.         pop     di
  135.         clc
  136.         ret
  137. isr endp
  138.  
  139. ; The following code is used to initialize the Interrupt
  140. ; Service Routine and leave it resident. This code and
  141. ; all memory above it is given back to the operating
  142. ; system by the call to Int 21h, function 31h at the end
  143. ; of the code.
  144. ALIGN 16
  145. initialize proc near
  146. end_tsr label byte
  147.         lea     dx, isr
  148.         push    cs
  149.         pop     ds
  150.         mov     ah,25h
  151.         mov     al,INTNUM
  152.         int     21h
  153. ; go back to MS-DOS leaving ISR resident
  154.         lea     ax,end_tsr
  155.         mov     cl,4
  156.         shr     ax,cl          ; convert to paragraphs
  157.         mov     dx,_TEXT
  158.         add     dx,ax
  159.         mov     ax,3100h       ; terminate and stay resident
  160.         int     21h
  161. initialize endp
  162. _TEXT ENDS
  163. END initialize
  164.