Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Peter Norton Programmer's Guide - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

  This chapter's interface example shows how you can use an interrupt
  handler to process Ctrl-C keystrokes. The example consists of two
  assembly-language routines.

  The first routine, INT23Handler, gains control when DOS executes INT 23H
  in response to a Ctrl-C keystroke. This handler simply increments the
  value in a flag and then returns to DOS with an IRET instruction.

  Note how the flag _INT23Flag is addressed through the segment group
  DGROUP. In many languages, segments with different names are grouped
  together in one logical group so that they can all be addressed with the
  same segment register. In the case of Microsoft C, this group of segments
  is named DGROUP, and it includes the data segment (_DATA) used by the
  compiled C program.

  The second assembly-language routine, _Install() is designed to be called
  by a C program. This short routine calls a DOS interrupt 21H function that
  updates the interrupt 23H vector with the address of the interrupt
  handler. (The next few chapters contain more about this DOS function and
  about interrupt 21H services in general.)

  DGROUP          GROUP   _DATA

  _TEXT           SEGMENT byte public 'CODE'
                  ASSUME  cs:_TEXT,ds:DGROUP

  ;
  ; the interrupt 23H handler:

  INT23Handler    PROC    far
                  push    ds                      ; preserve all registers used
                  push    ax                      ; ... in this interrupt handler

                  mov     ax,seg DGROUP           ; set DS to the segment where
                  mov     ds,ax                   ; ... the flag is located
                  inc     word ptr _INT23flag     ; increment the flag

                  pop     ax                      ; restore regs and return
                  pop     ds
                  iret

  INT23Handler    ENDP

  ;
  ; the C-callable installation routine:

                  PUBLIC  _Install
  _Install        PROC    near

                  push    bp                      ; the usual C prologue
                  mov     bp,sp
                  push    ds                      ; preserve DS

                  push    cs                      ; set DS:DX to point to ...
                  pop     ds
                  mov     dx,offset INT23Handler  ; ... the interrupt handler
                  mov     ax,2523h                ; AH = DOS function number
                                                  ; AL = interrupt number
                  int     21h                     ; call DOS to update the ...
                                                  ; ... interrupt vector
                  pop     ds
                  pop     bp                      ; restore regs and return
                  ret

  _Install        ENDP

  _TEXT           ENDS
  ;
  ; the flag set by the interrupt 23H handler when Ctrl-C is pressed:

  _DATA           SEGMENT word public 'DATA'

                  PUBLIC  _INT23flag
  _INT23flag      DW      0                    ; flag (initial value = 0)
  _DATA           ENDS

  The snippet of C code that follows shows how you could use this interrupt
  23H handler in a program. This C program does nothing but wait for you to
  press Ctrl-C. When you do, the assembly-language interrupt 23H handler
  increments the flag. When the loop in the C program sees that the flag is
  nonzero, it displays a message and decrements the flag.

  extern int INT23flag;                         /* flag set when Ctrl-C is pressed */

  main()
  {
          int     KeyCode;


          Install();                            /* install the interrupt 23H handler */

          do
          {
            while( INT23flag > 0 )
            {
              printf( "\nCtrl-C was pressed" );   /* ... show a message ... */
              --INT23flag;                        /* ... and decrement the flag */
            }

            if( kbhit() )                         /* look for a keypress */
              KeyCode = getch();
            else
              KeyCode = 0;
          }
          while( KeyCode != 0x0D );               /* loop until Enter is pressed */
  }

  Although the C code is short, it suggests two important points. One is
  that you must give DOS the chance to detect a Ctrl-C keystroke each time
  you test your interrupt 23H flag. (Remember that DOS is guaranteed to
  check for Ctrl-C only when it reads or writes to a character input/output
  device.) In this program, C's kbhit() function calls DOS to check for
  keyboard activity and, at the same time, lets DOS check for Ctrl-C as
  well.

  Also, note how the interrupt handler increments the flag instead of merely
  setting it to "true" or "false." This lets the loop in the C program
  process rapid, successive interrupts without losing track of how many
  interrupts have occurred.

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson