home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / c_spec / execute / mydos.asm < prev    next >
Assembly Source File  |  1986-02-20  |  4KB  |  167 lines

  1.     TITLE    DOS INTERFACE FUNCTION
  2.     SUBTTL    Copyright 1985 by Allen I. Holub
  3.     NAME    DOS
  4.  
  5.     PAGE    58,132
  6. ;----------------------------------------------------------------------
  7. ;
  8. ; Data types: The REGS structure is defined as type allregs in mydos.h
  9. ;
  10.  
  11. REGS    STRUC        ; Structure used to transfer register
  12. AX_REG    DW    0    ; contents. Note that this structure is
  13. BX_REG    DW    0    ; a supperset of that defined in the
  14. CX_REG    DW    0    ; dos.h supplied by Lattice. This particular
  15. DX_REG    DW    0    ; structure is defined in \include\mydos.h
  16. SI_REG    DW    0
  17. DI_REG    DW    0
  18. ES_REG    DW    0
  19. CS_REG    DW    0
  20. SS_REG    DW    0
  21. DS_REG    DW    0
  22. REGS    ENDS
  23.  
  24. OFF    EQU    4    ; Offset to arguments
  25.  
  26. ;----------------------------------------------------------------------
  27. ;    Header for Microsoft C compiler
  28. ;
  29.  
  30. _TEXT    SEGMENT  BYTE PUBLIC 'CODE'
  31. _TEXT    ENDS
  32. CONST    SEGMENT  WORD PUBLIC 'CONST'
  33. CONST    ENDS
  34. _BSS    SEGMENT  WORD PUBLIC 'BSS'
  35. _BSS    ENDS
  36. _DATA    SEGMENT  WORD PUBLIC 'DATA'
  37. _DATA    ENDS
  38. DGROUP    GROUP    CONST,    _BSS,    _DATA
  39.     ASSUME  CS: _TEXT, DS: DGROUP, SS: DGROUP, ES: DGROUP
  40.  
  41. ;----------------------------------------------------------------------
  42. ;
  43. ; name:        mydos  --  Do a DOS function call
  44. ;
  45. ; synopsis:    #include <mydos.h>
  46. ;
  47. ;        status = mydos( regsp );
  48. ;        int    status  ;     /* returned status register   */
  49. ;        REGS    *regp   ;     /* pointer to register struct */
  50. ;
  51. ; description:    This function is a more useful version of the bdos()
  52. ;        function provided with the lattice C compiler. It
  53. ;        saves the existing machine state, replaces the
  54. ;        contents of all registers except CS & SS from the
  55. ;        structure pointed to by regp, does an int 21, loads
  56. ;        the registers back into the structure, restores the
  57. ;        machine state and returns.
  58. ;
  59. ; notes:    This function will only work with the SMALL model, since
  60. ;        it assumes a 16 bit pointer. The stack frame is
  61. ;        non-standard too (ie the BP holds the structure pointer
  62. ;        arg rather than the frame pointer).
  63. ;
  64.  
  65.     PUBLIC    _mydos
  66.  
  67. _TEXT    SEGMENT
  68.  
  69. _mydos    PROC    NEAR
  70.  
  71.     PUSH    BP        ; Save the old stack frame.
  72.     MOV    BP,SP        ;
  73.     MOV    BP,[BP+OFF]    ; BP = pointer to register structure.
  74.     
  75.     PUSH    BX        ; Save the current machine state. No
  76.     PUSH    CX        ; point in saving AX because it's
  77.     PUSH    DX        ; going to be used for the return
  78.     PUSH    SI        ; value.
  79.     PUSH    DI
  80.     PUSH    ES
  81.     PUSH    DS
  82.  
  83.     MOV    AX,[BP].AX_REG        ; Set up a new machine state
  84.     MOV    BX,[BP].BX_REG        ; using the contents of the
  85.     MOV    CX,[BP].CX_REG        ; REGS structure. Don't modify
  86.     MOV    DX,[BP].DX_REG        ; the SS or CS registers.
  87.     MOV    SI,[BP].SI_REG
  88.     MOV    DI,[BP].DI_REG
  89.     MOV    ES,[BP].ES_REG
  90.     MOV    DS,[BP].DS_REG
  91.  
  92.     PUSH    BP            ; Make the DOS call. Save the BP
  93.     INT    21H            ; out of irrational paranoia.
  94.     POP    BP
  95.  
  96.     MOV    [BP].AX_REG,AX        ; Now update the original structure
  97.     MOV    [BP].BX_REG,BX        ; to reflect the return values from
  98.     MOV    [BP].CX_REG,CX        ; the DOS call.
  99.     MOV    [BP].DX_REG,DX
  100.     MOV    [BP].SI_REG,SI
  101.     MOV    [BP].DI_REG,DI
  102.     MOV    [BP].ES_REG,ES
  103.     MOV    [BP].CS_REG,CS
  104.     MOV    [BP].DS_REG,DS
  105.     MOV    [BP].SS_REG,SS
  106.  
  107.     POP    DS
  108.     POP    ES
  109.     POP    DI        ; Restore the previous machine state
  110.     POP    SI
  111.     POP    DX
  112.     POP    CX
  113.     POP    BX
  114.  
  115.     LAHF            ; Return the flags:
  116.     MOV    AL,AH        ;    Move flags to LSB
  117.     XOR    AH,AH        ;    Clear high bytes
  118.  
  119.     POP    BP        ; Restore the previous stack frame's
  120.     RET            ; frame pointer and return.
  121.  
  122. _mydos    ENDP
  123. _TEXT    ENDS
  124.  
  125. ;----------------------------------------------------------------------
  126. ;
  127. ; name:        gregs  --  Initialize a REGS structure to the current
  128. ;               register contents.
  129. ;
  130. ; synopsis:    #include <mydos.h>
  131. ;
  132. ;        gregs( regp )
  133. ;        REGS    *regp   ;    /* pointer to register struct */
  134. ;
  135. ; description:    This function is used before before calling mydos().
  136. ;
  137. ; notes:    This function will only work with the SMALL model.
  138. ;
  139. ;
  140.     PUBLIC    _gregs
  141. ;
  142. _TEXT      SEGMENT
  143. ;
  144. _gregs    PROC    NEAR
  145.  
  146.     PUSH    BP           ; Save the old stack frame.
  147.     MOV    BP,SP           ;
  148.     MOV    BP,[BP+OFF]       ; BP = pointer to register structure.
  149.  
  150.     MOV    [BP].AX_REG,AX       ; Initialize the structure pointed
  151.     MOV    [BP].BX_REG,BX       ; to by bp.
  152.     MOV    [BP].CX_REG,CX
  153.     MOV    [BP].DX_REG,DX
  154.     MOV    [BP].SI_REG,SI
  155.     MOV    [BP].DI_REG,DI
  156.     MOV    [BP].ES_REG,ES
  157.     MOV    [BP].CS_REG,CS
  158.     MOV    [BP].DS_REG,DS
  159.     MOV    [BP].SS_REG,SS
  160.  
  161.     POP    BP            ; Restore the previous stack
  162.     RET                ; frame and return.
  163.  
  164. _gregs    ENDP
  165. _TEXT    ENDS
  166.     END
  167.