home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / msj / msjv4_5 / ems / example.asm < prev    next >
Encoding:
Assembly Source File  |  1989-06-13  |  6.5 KB  |  272 lines

  1. ;=============================================================================
  2. ;                   Sample program for EMS Toolbox Routines
  3. ;                          written by Douglas Boling
  4. ;                       (c) 1989 Microsoft Corporation
  5. ;=============================================================================
  6.  
  7.          DOSSEG
  8.          .MODEL    small
  9.          .STACK    512
  10.  
  11.          .DATA
  12. EMShandle    dw    0            ;EMS handle
  13. pageframe_seg    dw    0
  14.  
  15. active        db    0            ;Flag to indicate int9 active
  16. key_code    db    0            ;Code of key pressed
  17. cursor_pos    dw    0            ;Saved position of cursor
  18.  
  19. save_index    dw    0
  20. save_area    db    100 dup (0)
  21. save_area_end    =    $
  22.  
  23.         .CODE
  24.          EXTRN    GetEMSMem:PROC
  25.          EXTRN    SaveEMSMem:PROC
  26.          EXTRN    RestoreEMSMem:PROC
  27.  
  28. int09h        dd    0            ;Saved interrupt 9 vector
  29.  
  30. ;----------------------------------------------------------------------------
  31. ;Keyboard Interrupt.  This routine checks for the hotkey then saves or
  32. ;  restores the video page as necessary.
  33. ;----------------------------------------------------------------------------
  34. kbdint        proc    far
  35.         assume    cs:_text,ds:nothing,es:nothing
  36.         push    ax
  37.         mov    ah,2            ;Get keyboard shift state
  38.         int    16h
  39.         cmp    al,08h            ;See if ALT key pressed
  40.         jne    goto_kbdbios
  41.         in    al,60h            ;Read keyboard port
  42.         cmp    al,13h            ;See if 'R' key pressed
  43.         je     kbd0
  44.         cmp    al,2Eh            ;See if 'C' key pressed
  45.         jne    goto_kbdbios
  46. kbd0:
  47.         push    ds
  48.         push    ax
  49.         mov    ax,@data        ;Point DS to data segment.
  50.         mov    ds,ax
  51.         pop    ax
  52.         assume    ds:_data
  53.         mov    key_code,al        ;Save key value (C or R)
  54.         cmp    active,0
  55.         je     kbd1
  56.         pop    ds
  57. goto_kbdbios:
  58.         pop    ax
  59.         jmp    cs:int09h
  60. kbd1:
  61.         inc    active
  62.         sti
  63.         push    bx            ;Save registers
  64.         push    cx
  65.         push    dx
  66.         push    di
  67.         push    si
  68.         push    es
  69.         mov    di,@data
  70.         mov    es,di                  ;Point to save area
  71.         mov    di,offset save_area
  72.         mov    cx,offset save_area_end - offset save_area
  73.         call    SaveEMSMem        ;Save state of EMS driver
  74.         or    ah,ah              ;Check for error, if error,
  75.         jne    kbd_exit            ;  exit.
  76.         mov    save_index,dx
  77.         mov    ax,4400h        ;EMS Map page 0
  78.         xor    bx,bx            ;Logical page 0
  79.         mov    dx,EMShandle
  80.         int    67h
  81.         or    ah,ah              ;Check for error, if error,
  82.         jne    kbd_restore             ;  exit.
  83.  
  84.         mov    ah,0fh            ;Get display mode
  85.         int    10h            ;Check for valid modes 2,3,7
  86.         cmp    al,7
  87.         je     kbd2
  88.         cmp    al,2
  89.         jb    kbd_restore
  90.         cmp    al,3
  91.         ja    kbd_restore
  92. kbd2:
  93.         push    ax
  94.         mov    ah,3            ;Save cursor position
  95.         int    10h
  96.         mov    cursor_pos,dx
  97.         pop    ax
  98.  
  99.         mov    es,pageframe_seg
  100.         xor    di,di            ;Saved scr at offset 0
  101.         cmp    key_code,2eh        ;See if capture function
  102.         je    kbd3
  103.         mov    di,1000h        ;Save current scr at 1000h
  104. kbd3:
  105.         call    save_scr        ;Save current screen
  106.         call    kb_reset        ;Reset keyboard controller
  107.  
  108.         cmp    key_code,2eh        ;See if capture function
  109.         je    kbd_restore        ;If so, were done.
  110.  
  111.         push    ds
  112.         mov    ds,pageframe_seg
  113.         xor    si,si
  114.         call    restore_scr        ;Display saved screen
  115.         pop    ds
  116. kb4:
  117.         xor    ah,ah            ;Get key
  118.         int    16h
  119.         cmp    al,27            ;Check for ESC key. If found
  120.         jne    kb4                     ;  exit. Else get another key.
  121.  
  122.         push    ds                  ;Restore original screen
  123.         mov    ds,pageframe_seg
  124.         mov    si,1000h
  125.         call    restore_scr
  126.         pop    ds
  127.  
  128. kbd_restore:
  129.         mov    si,offset save_area     ;Restore EMS mapping context
  130.         mov    dx,save_index
  131.         call    RestoreEMSMem
  132.         mov    ah,2            ;Set cursor position
  133.         mov    dx,cursor_pos
  134.         int    10h
  135. kbd_exit:
  136.         mov    active,0        ;Clear active flag
  137.         pop    es
  138.         pop    si
  139.         pop    di
  140.         pop    dx
  141.         pop    cx
  142.         pop    bx
  143. kbd_exit1:
  144.         pop    ds
  145.         pop    ax
  146.         iret
  147. kbdint    endp
  148.  
  149. ;----------------------------------------------------------------------------
  150. ;Save screen.
  151. ;----------------------------------------------------------------------------
  152. save_scr    proc    near
  153.         stosw                           ;Save video mode
  154.         mov    ax,1234h        ;Buffer initialized flag
  155.         stosw
  156.         mov    cx,25            ;25 rows
  157.         xor    dx,dx
  158. saves1:
  159.         push    cx
  160.         mov    cx,80            ;80 columns
  161.         xor    dl,dl
  162. saves2:
  163.         mov    ah,2            ;Set cursor position
  164.         int    10h
  165.         mov    ah,8            ;Read char and attribute
  166.         int    10h
  167.         stosw                ;Store in EMS buffer
  168.         inc    dl            ;Point to next column
  169.         loop    saves2
  170.         pop    cx
  171.         inc    dh            ;Point to next row
  172.         loop    saves1
  173.         ret
  174. save_scr        endp
  175.  
  176. ;----------------------------------------------------------------------------
  177. ;Restore screen.
  178. ;----------------------------------------------------------------------------
  179. restore_scr     proc    near
  180.         lodsw                ;Get video mode
  181.         xor    ah,ah            ;Set mode
  182.         cmp    word ptr [si],1234h    ;See if initialized
  183.         jne    restore_exit
  184.         int    10h
  185.         inc    si
  186.         inc    si
  187.         mov    cx,25            ;25 rows
  188.         xor    dx,dx            ;Start at row 0
  189. restores1:
  190.         push    cx
  191.         mov    cx,80            ;80 columns
  192.         xor    dl,dl            ;Start at column 0
  193. restores2:
  194.         push    cx
  195.         mov    ah,2            ;Set cursor position
  196.         int    10h
  197.         lodsw                ;Get char and attr from buffer
  198.         mov    bl,ah            ;Copy attribute
  199.         mov    cx,1            ;Write 1 character
  200.         mov    ah,9            ;Write char and attribute
  201.         int    10h
  202.         inc    dl            ;Point to next column
  203.         pop    cx
  204.         loop    restores2
  205.         pop    cx
  206.         inc    dh            ;Point to next row
  207.         loop    restores1
  208. restore_exit:
  209.         ret
  210. restore_scr     endp
  211.  
  212. ;-----------------------------------------------------------------------------
  213. ;KB_RESET resets the keyboard and signals end-of-interrupt to the 8259
  214. ;-----------------------------------------------------------------------------
  215. kb_reset    proc near
  216.         assume    cs:_text
  217.         in     al,61h            ;get control port value
  218.         mov    ah,al             ;save it in AH
  219.         or     al,80h            ;set bit 7
  220.         out    61h,al            ;send reset value
  221.         mov    al,ah             ;get original value
  222.         out    61h,al            ;send to enable keyboard
  223.         cli                      ;suspend interrupts
  224.         mov    al,20h            ;get EOI value
  225.         out    20h,al            ;send EOI to 8259
  226.         sti                      ;enable interrupts
  227.         ret
  228. kb_reset    endp
  229.  
  230.  
  231. ;----------------------------------------------------------------------------
  232. ;Initialization. Reserve EMS memory and hook into keyboard interrupt.
  233. ;----------------------------------------------------------------------------
  234. main:        mov    ax,@data        ;Set DS to data segment
  235.         mov    ds,ax
  236.  
  237.              mov    bx,1            ;Get 1 page
  238.         mov    si,-1
  239.         call    GetEMSMem        ;Allocate EMS memory
  240.         or    ah,ah
  241.         jne    ems_error
  242.         mov    EMShandle,dx        ;Save EMS handle
  243.         mov    pageframe_seg,bx    ;Save EMS page frame
  244.  
  245.         push    es
  246.         mov    ax,3509h        ;Get and Set int 9 vector
  247.         int    21h
  248.         mov    word ptr int09h,bx
  249.         mov    word ptr int09h[2],es
  250.         mov    ax,2509h
  251.         push    ds
  252.         push    cs
  253.         pop    ds
  254.         mov    dx,offset kbdint
  255.          int    21h
  256.         pop    ds
  257.         pop    es
  258.  
  259.         mov    dx,ds
  260.         add    dx,10h
  261.         mov    bx,es
  262.         sub     dx,bx
  263.         mov    ax,3100h        ;TSR
  264.         int    21h
  265. ems_error:
  266.         mov    al,ah            ;Copy EMS return code
  267.         mov    ah,4ch            ;terminate with return code
  268.         int    21h
  269.  
  270.         END    main
  271.  
  272.