home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / INTR.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  4.2 KB  |  162 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - intr.cas
  3.  *
  4.  * function(s)
  5.  *        intr - alternate 8086 software interrupt interface
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18.  
  19. #pragma inline
  20. #include <asmrules.h>
  21. #include <dos.h>
  22.  
  23.  
  24. /*-----------------------------------------------------------------------*
  25.  
  26. Name        intr - alternate 8086 software interrupt interface
  27.  
  28. Usage        #include <dos.h>
  29.         void intr(int intr_num, struct REGPACK *preg);
  30.  
  31. Prototype in    dos.h
  32.  
  33. Description    The intr function is an alternate interface for
  34.         executing software interrupts. It generates an 8086 software
  35.         interrupt specified by the argument intr_num.
  36.  
  37.         intr copies register values from the REGPACK structure
  38.         preg into the registers before executing the software
  39.         interrupt. After the software interrupt completes, intr
  40.         copies the current register values into preg. The flags
  41.         are preserved.
  42.  
  43.         The arguments passed to intr are as follows:
  44.  
  45.           intr_num    the interrupt number to be executed
  46.  
  47.           preg        the address of a structure containing
  48.                 (a) the input registers before the call
  49.                 (b) the value of the registers after the
  50.                     interrupt call.
  51.  
  52.         The REGPACK structure preg (described in dos.h) has the
  53.         following format :
  54.  
  55.           struct  REGPACK
  56.           {
  57.           unsigned  r_ax, r_bx, r_cx, r_dx;
  58.           unsigned  r_bp, r_si, r_di, r_ds, r_es, r_flags;
  59.           };
  60.  
  61. Return value    No value is returned. The REGPACK structure preg
  62.         contains the value of the registers after the interrupt
  63.         call.
  64.  
  65.  
  66. *------------------------------------------------------------------------*/
  67. #pragma option -r+
  68. void intr(int int_type, struct REGPACK *preg)
  69. {
  70.     void    (far * Vector)(void);
  71.     char    Code[14];
  72.  
  73. /*    #define WhereIsBP    -(sizeof(Code) + sizeof(Vector) + 12)    */
  74.  
  75. #define WhereIsBP    -34
  76.  
  77.     /* Save caller context */
  78.  
  79. asm    push    bp
  80. asm    push    ds
  81. asm    pushf
  82.  
  83.     /* Prepare Interrupt call */
  84.  
  85. asm    lea    cx, Code
  86. asm    mov    word ptr Vector, cx
  87. asm    mov    word ptr Vector+2, ss
  88.  
  89. asm    mov    word ptr Code, 06E8Bh
  90. asm    mov    byte ptr Code+2, WhereIsBP
  91. asm    mov    byte ptr Code+3, 0CDh
  92. asm    mov    ax, int_type
  93. asm    mov    byte ptr Code+4, al
  94. asm    cmp    al, 025h
  95. asm    jb    NormalIntr
  96. asm    cmp    al, 026h
  97. asm    ja    NormalIntr
  98. asm    mov    byte ptr Code+5, 036h
  99. asm    mov    word ptr Code+6, 00068Fh
  100. asm    mov    word ptr Code+8, cx
  101. asm    mov    byte ptr Code+10, 0CAh
  102. asm    mov    word ptr Code+11, 2
  103. asm    jmp    SetRegs
  104.  
  105. asm    popf_proc proc near
  106. asm    iret            /* this proc does a bullet-proof popf */
  107. asm    popf_proc endp
  108.  
  109. NormalIntr:
  110. asm    mov    byte ptr Code+5, 0CAh
  111. asm    mov    word ptr Code+6, 2
  112.  
  113.     /* Set registers with register structure content */
  114.  
  115. SetRegs:
  116. asm    LDS_    di, preg
  117. asm    push    ds
  118. asm    push    di
  119.  
  120. asm    mov    ax,[di].r_ax
  121. asm    mov    bx,[di].r_bx
  122. asm    mov    cx,[di].r_cx
  123. asm    mov    dx,[di].r_dx
  124. asm    push    word ptr [di].r_bp  /* BP will be loaded before int xx */
  125. asm    mov    si,[di].r_si
  126. asm    mov    es,[di].r_es
  127. asm    lds    di,[di].r_di
  128.  
  129.     /* Call the interrupt routine */
  130.  
  131.     (* Vector)();
  132.  
  133.     /* Set register structure with registers */
  134.  
  135. asm    push    ds
  136. asm    push    di
  137. asm    push    bp
  138. asm    pushf
  139.  
  140. asm    mov    bp,sp
  141. asm    lds    di,[bp+8]     /* DS:DI points to the reg structure   */
  142. asm    mov    [di].r_ax,ax
  143. asm    mov    [di].r_bx,bx
  144. asm    mov    [di].r_cx,cx
  145. asm    mov    [di].r_dx,dx
  146. asm    mov    [di].r_si,si
  147. asm    mov    [di].r_es,es
  148. asm    pop    [di].r_flags    /* flags                 */
  149. asm    pop    [di].r_bp    /* BP                     */
  150. asm    pop    [di].r_di    /* DI                     */
  151. asm    pop    [di].r_ds    /* DS                     */
  152.  
  153. asm    add    sp,4          /* Remove saved DS:DI             */
  154.  
  155. asm    push    cs
  156. asm    call    popf_proc    /* pop flags */
  157.  
  158. asm    pop    ds
  159. asm    pop    bp
  160. }
  161. #pragma option -r.
  162.