home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / clipper / exoapi.zip / INT13.ASM < prev    next >
Assembly Source File  |  1993-06-05  |  6KB  |  219 lines

  1. ;*****************************************************************************
  2. ;** INT13.ASM
  3. ;**
  4. ;** This is a sample of calling real-mode interrupt functions with ExoSpace.
  5. ;**
  6. ;** by J. David Reynolds
  7. ;**
  8. ;** This code is released to the public domain.
  9. ;**
  10. ;** requires MASM 6.0 or above
  11. ;** tab spacing = 3
  12. ;**
  13. ;*****************************************************************************
  14.  
  15. .MODEL LARGE, C
  16. .286                                   ; safe to assume with ExoSpace
  17.  
  18. FARPTR         TYPEDEF     FAR PTR
  19.  
  20. INT13_READ     equ         2
  21. INT13_WRITE    equ         3
  22. INT13_VERIFY   equ         4
  23. INT13_FORMAT   equ         5
  24.  
  25. ; ExoSpace register structure for ExoRMInterrupt()
  26. EXOREGS STRUCT
  27.    wDS      WORD     ?
  28.    wES      WORD     ?
  29.    wDI      WORD     ?
  30.    wSI      WORD     ?
  31.    wBP      WORD     ?
  32.    wSP      WORD     ?
  33.    wBX      WORD     ?
  34.    wDX      WORD     ?
  35.    wCX      WORD     ?
  36.    wAX      WORD     ?
  37. EXOREGS ENDS
  38.  
  39. ; ExoSpace API functions
  40. _xalloclow        PROTO FAR C, _wBytes:WORD
  41. _xfreelow         PROTO FAR C, _pPtr:FARPTR
  42. ExoRealPtr        PROTO FAR C, _pPtr:FARPTR
  43. ExoRMInterrupt    PROTO FAR C, _wInt:WORD, _regInRegs:FARPTR, \
  44.                      _regOutRegs:FARPTR
  45.  
  46. .DATA
  47.  
  48. .CODE
  49.  
  50. ;*****************************************************************************
  51. ;** void Int13()
  52. ;**
  53. ;** This function is a direct replacement for the instruction:
  54. ;**   int   13h
  55. ;** for functions 2, 3, 4, and 5 only.
  56. ;**
  57. ;** Interrupt 13h is not supported by ExoSpace directly, so if one of its
  58. ;** functions is called which requires or returns a pointer to a buffer,
  59. ;** special handling is required.  If no pointers are involved, nothing extra
  60. ;** needs to be done.
  61. ;**
  62. ;** This sample replacement only handles functions 2, 3, 4, and 5.  These
  63. ;** are the read, write, verify, and format calls for floppy disks.  Some
  64. ;** other interrupt 13h functions, such as 8 (get current drive parameters),
  65. ;** also need special handling if they are called directly from protected
  66. ;** mode.
  67. ;**
  68. ;*****************************************************************************
  69. Int13 PROC FAR C
  70.  
  71.    LOCAL _wAX:WORD, \
  72.          _wBufLen:WORD
  73.  
  74.    LOCAL _pProtBuf:FARPTR, \
  75.          _pRealBuf:FARPTR, \
  76.          _pRealPtr:FARPTR
  77.  
  78.    LOCAL _regRegs:EXOREGS
  79.  
  80.  
  81.    ; only handles functions 2, 3, 4, and 5
  82.    .IF ( ( ah >= INT13_READ ) && ( ah <= INT13_FORMAT ) )
  83.  
  84.       ; save needed registers and pointers
  85.       mov   _wAX, ax                   ; save function number (in ah)
  86.  
  87.       mov   _regRegs.wAX, ax           ; save registers for int 13h call
  88.       mov   _regRegs.wCX, cx
  89.       mov   _regRegs.wDX, dx
  90.  
  91.       mov   WORD PTR _pProtBuf[2], es  ; save pointer to protected-mode buffer
  92.       mov   WORD PTR _pProtBuf[0], bx
  93.       mov   WORD PTR _pRealBuf[2], es  ; assume it's already in low memory
  94.       mov   WORD PTR _pRealBuf[0], bx
  95.  
  96.       ; see if buffer is already in low memory by getting a real-mode pointer
  97.       ; to buffer
  98.       INVOKE ExoRealPtr, _pProtBuf
  99.  
  100.       ; test for null pointer (meaning buffer is in extended memory)
  101.       .IF ( ( dx == 0 ) && ( ax == 0 ) )
  102.  
  103.          ; copy memory from protected-mode buffer to real-mode buffer
  104.          .IF ( BYTE PTR _wAX[1] == INT13_FORMAT )
  105.             mov   _wBufLen, 512
  106.          .ELSE
  107.             mov   ax, _wAX             ; al=number of sectors
  108.             shl   ax, 9                ; multiply by 512 to get bytes
  109.             mov   _wBufLen, ax
  110.          .ENDIF
  111.  
  112.          ; allocate low memory
  113.          INVOKE _xalloclow, _wBufLen
  114.  
  115.          ; test for null pointer (meaning allocation failed)
  116.          .IF ( ( dx == 0 ) && ( ax == 0 ) )
  117.             mov   ah, 0BBh             ; int 13h undefined error code
  118.             stc
  119.             jmp   ReturnPoint
  120.          .ELSE
  121.             mov   WORD PTR _pRealBuf[2], dx
  122.             mov   WORD PTR _pRealBuf[0], ax
  123.          .ENDIF
  124.  
  125.          ; only copy to real-mode buffer if writing or formatting
  126.          .IF ( ( BYTE PTR _wAX[1] == INT13_WRITE ) || \
  127.                ( BYTE PTR _wAX[1] == INT13_FORMAT ) )
  128.  
  129.             push  si
  130.             push  di
  131.             push  ds
  132.  
  133.             lds   si, _pProtBuf
  134.             les   di, _pRealBuf
  135.             mov   cx, _wBufLen
  136.             shr   cx, 1                ; always a multiple of 512 bytes
  137.             rep movsw
  138.  
  139.             pop   ds
  140.             pop   di
  141.             pop   si
  142.  
  143.          .ENDIF
  144.  
  145.          ; get real-mode pointer to buffer
  146.          INVOKE ExoRealPtr, _pRealBuf
  147.  
  148.       .ENDIF
  149.  
  150.       ; save real-mode pointer to buffer
  151.       mov   WORD PTR _pRealPtr[2], dx
  152.       mov   WORD PTR _pRealPtr[0], ax
  153.  
  154.       ; set up rest of register structure
  155.       mov   ax, WORD PTR _pRealPtr[2]
  156.       mov   _regRegs.wES, ax
  157.       mov   ax, WORD PTR _pRealPtr[0]
  158.       mov   _regRegs.wBX, ax
  159.  
  160.       ; call real-mode interrupt
  161.       INVOKE ExoRMInterrupt, 13h, ADDR _regRegs, ADDR _regRegs
  162.       push  ax                         ; flags are returned in ax
  163.       popf
  164.  
  165.       pushf                            ; save flags
  166.  
  167.       ; see if buffer was already in low memory
  168.       mov   dx, WORD PTR _pProtBuf[2]
  169.       mov   ax, WORD PTR _pProtBuf[0]
  170.       .IF ( ( dx != WORD PTR _pRealBuf[2] ) || \
  171.             ( ax != WORD PTR _pRealBuf[0] ) )
  172.  
  173.          ; only copy back to protected-mode buffer if reading
  174.          .IF ( BYTE PTR _wAX[1] == INT13_READ )
  175.  
  176.             push  ds
  177.             push  si
  178.             push  di
  179.  
  180.             lds   si, _pRealBuf
  181.             les   di, _pProtBuf
  182.             mov   cx, _wBufLen
  183.             shr   cx, 1                ; always a multiple of 512 bytes
  184.             rep movsw
  185.  
  186.             pop   di
  187.             pop   si
  188.             pop   ds
  189.  
  190.          .ENDIF
  191.  
  192.          ; free low memory
  193.          INVOKE _xfreelow, _pRealBuf
  194.  
  195.       .ENDIF
  196.  
  197.       mov   ax, _regRegs.wAX           ; save return register values
  198.       mov   cx, _regRegs.wCX
  199.       mov   dx, _regRegs.wDX
  200.       mov   es, WORD PTR _pProtBuf[2]
  201.       mov   bx, WORD PTR _pProtBuf[0]
  202.  
  203.       popf                             ; restore flags
  204.  
  205.    .ELSE
  206.  
  207.       ; is not function 2, 3, 4, or 5, so just call int 13h in protected mode
  208.       int   13h
  209.  
  210.    .ENDIF
  211.  
  212.    ReturnPoint:
  213.       ret
  214.  
  215.    Int13 ENDP
  216.  
  217. END
  218.  
  219.