home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / XLIB20.ZIP / XLIB.DOC < prev    next >
Text File  |  1993-10-11  |  117KB  |  2,984 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.                              XLIB PROGRAMMER'S MANUAL
  31.                                    VERSION 2.0
  32.  
  33.                               (DOS Extender Library)
  34.  
  35.                                 TechniLib Company
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.                     Copyright 1993, by TechniLib (TM) Company
  60.                                All Rights Reserved
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                           TERMS OF USE AND DISTRIBUTION
  68.  
  69.  
  70.        XLIB is a shareware product; therefore, unregistered copies of XLIB are
  71.   made available free of charge so that potential purchasers will have the
  72.   opportunity to examine and test the software before committing payment.
  73.   Distribution of unregistered copies of XLIB to other potential users is also
  74.   permitted and appreciated.  However, usage and distribution of XLIB must
  75.   conform to the following conditions.  In the following statement, the term
  76.   "commercial distribution," includes shareware distribution.
  77.  
  78.   1) XLIB and accompanying software must be distributed together in copies of
  79.   the original archive provided by TechniLib.  Neither the archive nor
  80.   individual files therein may be modified.
  81.  
  82.   2) The XLIB archive may be distributed in combination with other shareware
  83.   products; however, the XLIB archive may not be distributed with other
  84.   commercially distributed software without written consent of TechniLib.
  85.  
  86.   3) Copies of XLIB which have been used to develop software for commercial
  87.   distribution must be registered before such software is marketed.  Copies of
  88.   XLIB which have been used to develop noncommercial software must be registered
  89.   if such software is to be regularly used either by the developer or others.
  90.  
  91.   4) Commercially distributed software must embed XLIB procedures in the
  92.   software code.  Files contained in the XLIB archive may not be placed in the
  93.   distribution media.
  94.  
  95.   5) XLIB is designed to offer a set of services to other executable code.  XLIB
  96.   may not be used to develop software for commercial distribution which will
  97.   essentially offer any of these same services to other executable code.
  98.   Exceptions to this condition require written consent of TechniLib.
  99.  
  100.   6) Rights afforded by registering a single copy of XLIB pertain only to a
  101.   single computer.
  102.  
  103.   7) XLIB may be registered for a fee of $35.00 per copy.  Accompany payment
  104.   with the registration form included in the XLIB archive.  Registrants will be
  105.   entitled to the most recent version of the XLIB archive.
  106.  
  107.  
  108.                               DISCLAIMER OF WARRANTY
  109.  
  110.  
  111.        XLIB AND ALL ACCOMPANYING SOFTWARE AND LITERATURE ARE DISTRIBUTED WITH
  112.   THE EXCLUSION OF ANY AND ALL IMPLIED WARRANTIES, AND WITH THE EXCLUSION OF
  113.   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  TechniLib
  114.   SHALL HAVE NO LIABILITY FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  115.   RESULTING FROM THE USE OF XLIB OR ACCOMPANYING MATERIALS.  The user assumes
  116.   the entire risk of using this software.
  117.  
  118.  
  119.                     Copyright 1993, by TechniLib (TM) Company
  120.                                All Rights Reserved
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.                                 TABLE OF CONTENTS
  128.  
  129.  
  130.   CHAPTERS
  131.                                                                       Page
  132.   1.  Introduction                                                       1
  133.   2.  XLIB Conventions and Structure                                     4
  134.   3.  XLIB Initialization and Termination                                6
  135.   4.  Mode Switching                                                     9
  136.   5.  Inline Mode Switching                                             13
  137.   6.  Interrupt Management                                              16
  138.   7.  Memory Management                                                 22
  139.   8.  File Management                                                   25
  140.   9.  Descriptor Management                                             29
  141.   10. Using XLIB in High-Level Language Libraries                       30
  142.  
  143.  
  144.   TABLES
  145.                                                                       Page
  146.   1.  XLIB Segments and Selectors by Public Symbol                       5
  147.   2.  CALLPM/ENTERPM Register Storage Locations by Public Symbol        11
  148.   3.  CALLRM Register Storage Locations by Public Symbol                12
  149.   4.  XLIB File Control Block Structure                                 25
  150.  
  151.  
  152.   EXAMPLES
  153.                                                                       Page
  154.   1.  Simple Mode Switching Under XLIB                                   3
  155.   2.  Using INLINEPM/INLINERM in C                                      13
  156.   3.  Calling Protected-Mode Libraries From BASIC                       30
  157.  
  158.  
  159.   APPENDICES
  160.                                                                       Page
  161.   A. Description of XLIB Public Data                                    34
  162.   B. XLIB Error Codes                                                   37
  163.   C. DPMI 1.0 Error Codes                                               39
  164.   D. XMS Error Codes                                                    40
  165.   E. Calling Protected-Mode Libraries From C                            41
  166.   F. Reporting Problems With XLIB                                       43
  167.   G. The SWITCHPM and SWITCHRM Procedures                               44
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.                                  1. Introduction
  175.  
  176.  
  177.        XLIB is an assembly language library which may be used with assembly
  178.   language programs to greatly simplify protected-mode programming under the
  179.   Microsoft DOS operating system.  Implementation of protected-mode procedures
  180.   will often require no more than placing code in the proper segment and making
  181.   two calls to XLIB procedures.  XLIB also simplifies protected-mode programming
  182.   in C using inline assembly.
  183.        XLIB is designed for the Intel 386, 486, and Pentium processors.  XLIB
  184.   fully utilizes the 32-bit processing powers of these chips and makes these
  185.   powers available to the user.  The compactness of XLIB follows largely from
  186.   the fact that much of it is written in 32-bit code.
  187.        XLIB is used to produce extended DOS applications.  DOS is unfortunately
  188.   limited by the fact that it is a real-mode operating system intended to manage
  189.   only real-mode programs.  Real-mode programs cannot use memory addresses
  190.   requiring more than 20 bits, or use memory offsets requiring more than 16
  191.   bits.  Such programs are further limited by the fact that 32-bit instructions
  192.   execute awkwardly in real-mode.  When the processor is in real-mode, it will
  193.   expect all 32-bit instructions to be preceded by at least one prefix.  Each of
  194.   these prefixes consumes one byte of memory and requires at least one clock to
  195.   execute.  Such limitations do not exist in 32-bit protected mode.  Extended
  196.   DOS applications overcome the limitations of DOS with their ability to execute
  197.   in both real and protected modes.  DOS services can be utilized from real mode
  198.   while the 32-bit processing power of the CPU can be utilized from protected
  199.   mode.
  200.        There are presently several 32-bit operating systems available on the
  201.   market.  Such systems include IBM OS/2, Microsoft Windows NT, and UNIX.  These
  202.   systems can manage programs which operate exclusively in protected mode.  Such
  203.   programs are generally easier to develop than extended DOS applications.
  204.   However, extended DOS applications have an advantage in that they can execute
  205.   under all of these operating systems because all of these systems can emulate
  206.   DOS.
  207.        Of course DOS will eventually become obsolete.  Future programs will
  208.   likely operate exclusively in protected mode using the flat memory model.  The
  209.   memory models supported by XLIB approximate the flat model; therefore, code
  210.   written for XLIB will require little modification when being transported to
  211.   flat-model operating environments.  Indeed, many procedures will require no
  212.   modification whatsoever.  Moreover, XLIB includes flat-model descriptors which
  213.   may be used to execute genuine flat-model code.  XLIB does not load and
  214.   relocate such code; however, it does provide all the necessary tools to
  215.   develop such procedures.
  216.        XLIB procedures handle important tasks such as mode switching between
  217.   real and protected modes, memory management under protected mode, protected-
  218.   mode interrupt management, and protected-mode file management.  XLIB includes
  219.   routines to perform these tasks in the absence of a protected-mode interface,
  220.   or in the presence of the Virtual Control Program Interface (VCPI), or the DOS
  221.   Protected Mode Interface (DPMI, version .9 or higher).  XLIB can also manage
  222.   extended memory through the Extended Memory Specification (XMS).
  223.        Upon initialization, XLIB will examine the operating environment for the
  224.   presence of DPMI, VCPI, and XMS, and then configure itself accordingly.  The
  225.   client program may therefore perform calls to XLIB procedures with few
  226.   concerns as to the environment in which it is executing.
  227.  
  228.  
  229.  
  230.  
  231.                                         1
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.        XLIB relieves the programmer of descriptor table management by supplying
  239.   a set of predefined segments along with their associated descriptors and
  240.   selectors.  Many protected-mode procedures will require no modification for
  241.   XLIB other than being placed in the proper segment.  XLIB provides a single
  242.   32-bit segment for protected-mode routines.  This segment may be larger than
  243.   64K, but must reside in conventional memory so that DOS can load it.  However,
  244.   code within this segment may access data throughout the address space.  XLIB
  245.   does allow user-defined descriptors; however, these should be needful only in
  246.   very rare cases.
  247.        The following program illustrates the simplicity with which protected-
  248.   mode execution may be initiated and terminated with XLIB.  The program was
  249.   written with the Microsoft Assembler (MASM).  It first initializes XLIB by
  250.   calling a procedure called INITXLIB.  After confirming that initialization is
  251.   successful, the program then transfers control to a 32-bit protected-mode
  252.   procedure which prints a message to the screen.  Control is transferred by
  253.   placing the protected-mode target address on the stack and then calling an
  254.   XLIB procedure named CALLPM (call protected mode).  CALLPM will expect the
  255.   target procedure to be contained in a segment called TSEG.  The protected-mode
  256.   procedure in TSEG returns control to real or virtual 8086 (V86) mode simply by
  257.   executing the RET instruction.
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.                                         2
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.   Example 1:  Simple Mode Switching Under XLIB
  303.   -----------------------------------------------------------------------------
  304.                  .MODEL        LARGE,PASCAL
  305.                  .386P
  306.  
  307.                  INCLUDE        XLIB.INC       ;Include XLIB public symbols
  308.                  INCLUDELIB     XLIB.LIB       ;Link with XLIB.LIB
  309.  
  310.                  .STACK         1024
  311.                  .CODE
  312.                  .STARTUP
  313.                  CALL           INITXLIB       ;Initialize XLIB
  314.                  OR             EAX,EAX        ;EAX = 0 if successful
  315.                  JZ             INITDONE
  316.                  .EXIT          0              ;Initialization failed
  317.   INITDONE:      PUSHD          OFFSET DEMOPROC
  318.                  CALL           CALLPM         ;Execute DEMOPROC in protected
  319.                  .EXIT          0
  320.  
  321.   ;Protected-mode routines must be placed in following segment:
  322.   TSEG           SEGMENT PARA PUBLIC USE32 'CODE'
  323.                  ASSUME CS:TSEG, SS:TSEG, DS:TSEG, ES:TSEG, FS:DSEG, GS:DGROUP
  324.  
  325.   ;Protected-mode routine to print message to the screen using DOS function.
  326.   DEMOPROC       PROC NEAR
  327.                  MOV            EBX,OFFSET PMMSG
  328.                  MOV            AH,02H
  329.   MSGLOOP:       MOV            DL,CS:[EBX]    ;32-bit offset!!!!!
  330.                  OR             DL,DL
  331.                  JZ             EXIT
  332.                  INT            21H            ;Print character with DOS
  333.                  INC            EBX
  334.                  JMP            MSGLOOP
  335.   EXIT:          RET                           ;Go back to real or V86 mode
  336.   PMMSG          DB  "In 32-bit protected mode!!!  "
  337.                  DB  "Returning to real mode.",10,13,0
  338.   DEMOPROC       ENDP
  339.  
  340.   TSEG           ENDS
  341.                  END
  342.   -----------------------------------------------------------------------------
  343.  
  344.  
  345.        The framework presented in the above program is extremely simple;
  346.   nonetheless, it will meet the demands of many protected-mode programs.  Most
  347.   protected-mode programs will require no further modifications other than a few
  348.   calls to XLIB extended memory management procedures.
  349.        XLIB was developed and tested under Microsoft DOS version 6.0 using
  350.   Microsoft Assembler (MASM) version 6.1a, Microsoft LINK version 5.31.009, and
  351.   Microsoft LIB version 3.20.01.  MASM parameters were set to c, W2, and WX.
  352.   LINK parameters were set to BATCH, CPARM:1, FAR, NOPACKF, and PACKC.  XLIB has
  353.   also been tested under Microsoft Windows 3.1, Qualitas 386MAX version 6.02,
  354.   Quarterdeck QEMM version 6.02, Quarterdeck QDPMI version 1.01, Quarterdeck
  355.   DESQview version 2.42, and IBM OS/2 version 2.1.
  356.  
  357.  
  358.  
  359.                                         3
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.                         2. XLIB Conventions and Structure
  367.  
  368.  
  369.        XLIB public procedures and public data are explained in the following
  370.   chapters.  A detailed explanation of most XLIB public data is also included in
  371.   Appendix A.  This chapter sets forth rules which will be generally applicable
  372.   to all XLIB data and procedures.
  373.        Though it is sometimes necessary for XLIB to distinguish between real
  374.   mode and virtual 8086 mode; this document uses the term "real mode" to include
  375.   virtual 8086 mode.
  376.        All public real-mode procedures and 16-bit protected-mode procedures in
  377.   XLIB are located in a segment called CSEG.  The user may also place code in
  378.   CSEG but is rarely required to do so.  All public XLIB procedures in CSEG have
  379.   far returns.
  380.        All public 32-bit protected-mode procedures in XLIB are located in a 32-
  381.   bit segment called TSEG and have near returns.  XLIB will also expect the user
  382.   to place all 32-bit procedures in TSEG and will expect these procedures to
  383.   have near returns.  This policy is adopted to achieve approximation to the
  384.   flat model.
  385.        Nearly all XLIB protected-mode procedures are 32-bit routines.  The only
  386.   exceptions to this rule are the inline mode-switch procedures discussed in
  387.   Chapter 5.  This policy is implemented to approximate the flat model.  There
  388.   are very few circumstances in which 16-bit protected mode is preferable to 32-
  389.   bit protected mode.  One can generally increase program speed and reduce
  390.   program size by writing the code for 32-bit segments.
  391.        TSEG may be larger than 64K provided that certain rules are observed.
  392.   XLIB adheres to all of the necessary rules.  First, only 32-bit protected-mode
  393.   code should be placed in TSEG.  The processor will not generally be able to
  394.   execute real-mode code in this segment because the offsets will be 32-bit
  395.   values.  Second, real-mode code should never write to or read from TSEG.  Such
  396.   instructions will also require 32-bit offsets.  Modifications to TSEG should
  397.   be done from a TSEG protected-mode procedure.  Finally, segment constants
  398.   should never be encoded in TSEG.  For example, the symbols CSEG, TSEG, DSEG,
  399.   and DGROUP should not be found in TSEG.  DOS will not be able to perform
  400.   relocation edits on these constants if they are in a segment larger than 64K.
  401.   To read these values from TSEG, initialize memory locations in a 16-bit
  402.   segment to the values and then read the memory locations.  Memory locations in
  403.   DSEG have already been initialized for XLIB segments (see Appendix A).
  404.   Microsoft LINK will issue a warning for code segments exceeding 64K; however,
  405.   this warning may be safely ignored provided that the above rules are observed.
  406.        All XLIB procedures may be called with interrupts enabled and will return
  407.   with interrupts enabled provided that they were enabled upon call.
  408.        All XLIB public data is contained in a 16-bit segment called DSEG.  The
  409.   user may also place data in DSEG but is rarely required to do so.
  410.        XLIB uses the PASCAL calling and naming convention.  The PASCAL
  411.   convention is equivalent to the BASIC/FORTRAN convention.  C programmers must
  412.   adapt XLIB procedures and symbols with declarations which specify the PASCAL
  413.   convention.  The header file XLIB.H contains such declarations.
  414.        XLIB routines which can encounter error conditions will always return
  415.   XLIB error codes in AX.  A list of such error codes is presented in Appendix
  416.   B.  In most cases, DX or the high word of EAX will be returned with specific
  417.   information about the error, such as DPMI, XMS, or DOS error codes.  DPMI
  418.   error codes are presented in Appendix C.  XMS error codes are in Appendix D.
  419.  
  420.  
  421.  
  422.  
  423.                                         4
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.        Selectors for all XLIB segments are placed in public WORD locations in
  431.   segment DSEG.  The following table gives the name of each predefined selector
  432.   along with its associated segment name and description:
  433.  
  434.  
  435.   Table 1:  XLIB Segments and Selectors by Public Symbol
  436.   -----------------------------------------------------------------------------
  437.   Selector Name    Segment Name      Description
  438.   -------------    ------------      -----------
  439.   CSEGSEL          CSEG              XLIB 16-bit code segment
  440.   CSEGDSEL         CSEG              Data selector to CSEG
  441.   TSEGSEL          TSEG              32-bit code segment
  442.   TSEGDSEL         TSEG              Data selector to TSEG
  443.   DSEGSEL          DSEG              XLIB data segment
  444.   FLATSEL          .                 Flat-model code
  445.   FLATDSEL         .                 Flat-model data
  446.   DGROUPSEL        DGROUP            DGROUP data group
  447.   SCRNSEL          .                 Screen data
  448.   MAINCSSEL        .                 CS selector for main caller
  449.   MAINSSSEL        .                 SS selector for main caller
  450.   MAINDSSEL        .                 DS selector for main caller
  451.   MAINESSEL        .                 ES selector for main caller
  452.   ILCSSEL          .                 Inline CS selector
  453.   ILSSSEL          .                 Inline SS selector
  454.   ILDSSEL          .                 Inline DS selector
  455.   -----------------------------------------------------------------------------
  456.  
  457.  
  458.        The flat-model and TSEG descriptors have limit FFFFFFFFH.  All other
  459.   descriptors have limit FFFFH.  The screen descriptor has base set to B8000H
  460.   for color monitors and B0000H for monochrome monitors.  MAINCSSEL, MAINSSEL,
  461.   MAINDSSEL, and MAINESSEL are selectors to descriptors which have base
  462.   addresses matching the contents of CS, SS, DS, and ES as of the call to
  463.   INITXLIB.  ILCSSEL, ILSSSEL, and ILDSSEL are selectors used by the inline
  464.   mode-switch procedures (see Chapter 5).  The base addresses of the
  465.   corresponding descriptors are adjusted dynamically.
  466.        All data segments are writable.  The data descriptors have their big bits
  467.   set; consequently, implicit stack instructions will use ESP rather then SP.
  468.   All code segments are readable and nonconforming.  Descriptor privilege levels
  469.   and requested privilege levels are set to zero unless DPMI is installed.
  470.   Privilege levels under DPMI will generally be set to three.
  471.        The values contained in the above selectors will be different under DPMI
  472.   than other environments.  Moreover, DPMI selector values can differ in
  473.   different environments and under different hosts.  The user should therefore
  474.   always read selector values from these locations.
  475.        Since selectors are stored in DSEG, the user must never lose track of the
  476.   DSEG selector.  This could prove a problem in interrupt handlers where no
  477.   assumptions can be made as to register contents.  Consequently, XLIB places a
  478.   copy of the DSEG selector in TSEG where it can always be found.  It is stored
  479.   under WORD symbol CSDSEGSEL and may be read with CS override.  For example
  480.   from TSEG code you can always load DS with DSEGSEL using MOV DS,CS:CSDSEGSEL.
  481.        XLIB never uses selectors past DGROUPSEL in Table 1.  The user may
  482.   therefore redefine the associated descriptors if desired.  Selector values
  483.   however should not be changed.
  484.  
  485.  
  486.  
  487.                                         5
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.                       3. XLIB Initialization and Termination
  495.  
  496.  
  497.        XLIB procedures apart from those presented in this chapter should be
  498.   called only after XLIB has been initialized by calling INITXLIB.  This routine
  499.   will examine the operating environment for the presence of DPMI, VCPI, and
  500.   XMS.  It will then perform extensive code modifications upon XLIB to
  501.   accommodate the resident software.  INITXLIB is to be called only once within
  502.   a program.  Subsequent calls have no effect.
  503.        If XLIB finds that neither DPMI nor VCPI are present, then XLIB will
  504.   completely handle all mode switching and interrupt management.  If XLIB finds
  505.   that XMS is absent also, then XLIB will handle all extended memory management.
  506.   If XLIB finds that both DPMI and VCPI are present, then it will configure
  507.   itself for DPMI by default.  However, the default may be changed by setting
  508.   bit 0 of IFLAGS (initialization flags) before calling INITXLIB.  If this bit
  509.   is set, then VCPI is given priority over DPMI.  IFLAGS is a public WORD
  510.   location in DSEG.
  511.        INITXLIB will probably attempt to allocate some conventional memory
  512.   through DOS.  Since high-level language modules and assembly language modules
  513.   often claim all available DOS memory, INITXLIB may fail for lack of available
  514.   DOS memory.  This problem can be averted with assembly language modules by
  515.   linking with the CPARM:1 parameter.  This forces the module to claim no more
  516.   DOS memory than is necessary.  High-level language modules should call
  517.   XLIBMEMREQ (XLIB memory requirements) to obtain conventional memory
  518.   requirements and then release at least this much memory.  This process is
  519.   illustrated for Microsoft BASIC in Chapter 10.  C does not allocate all DOS
  520.   memory and therefore does not require such special action.
  521.        If both VCPI and DPMI are present, then conventional memory requirements
  522.   will depend upon which of these interfaces is to be chosen by INITXLIB.  In
  523.   such cases, XLIBMEMREQ will return DPMI memory requirements if bit 0 of IFLAGS
  524.   is clear, and will return VCPI requirements otherwise.  Therefore, this bit
  525.   should be set to the appropriate value by the user before calling XLIBMEMREQ.
  526.   DPMI conventional memory requirements may be obtained by calling DPMIMEMREQ.
  527.   These will vary from host to host.  VCPI conventional memory requirements may
  528.   be obtained by calling VCPIMEMREQ.  VCPI requirements are the same for all
  529.   servers.
  530.        One of the principle function of XLIB is of course to make protected-mode
  531.   interfaces and memory management interfaces transparent to the client program.
  532.   However, there are special cases where a program should be informed as to
  533.   which interfaces are implemented.  In such cases, the procedure XLIBCONFIG
  534.   (XLIB configuration) may be called to determine the how XLIB has been
  535.   configured by INITXLIB.
  536.        XLIB is terminated with INT 21H function 4C (DOS termination) issued
  537.   either from real or protected mode.
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.                                         6
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.   Detailed Specifications
  559.  
  560.  
  561.   INITXLIB (Initialize XLIB)
  562.   Purpose:  Check for presence of XMS, DPMI, and VCPI, then configure XLIB to
  563.   operate with the installed interfaces.
  564.   CPU Mode:  Real
  565.   Registers at Call:  None
  566.   Return Registers:
  567.      AX = 0 if successful, in which event DX and EAX are zero as well.  The
  568.   configured interfaces may be identified by calling XLIBCONFIG (see below).
  569.      AX <> 0 if unsuccessful.  An XLIB error code is returned in AX.  A specific
  570.   error code is returned in DX and in the high word of EAX (EAH).  If AX = 11H
  571.   or 12H, then DX and EAH will equal a DOS error code.  If AX = DPMI error, then
  572.   DX and EAH will equal a DPMI 1.0 error code (if provided by host).  If AX =
  573.   VCPI error, then DX and EAH are returned as zero.
  574.   Details:
  575.      If both DPMI and VCPI are present, then XLIB will be configured for DPMI if
  576.   the zero bit of IFLAGS is clear.  If this bit is set, then XLIB will be
  577.   configured for VCPI.  The bit is clear by default.
  578.      This routine will likely attempt to allocate DOS memory.  The amount of DOS
  579.   memory XLIB will attempt to allocate can be obtained by calling XLIBMEMREQ
  580.   (see below).
  581.      Descriptors are created for the segments loaded in CS, SS, DS, and ES as of
  582.   call to this routine.  The selectors for these descriptors are MAINCSSEL,
  583.   MAINSSSEL, MAINDSSEL, and MAINESSEL.  These descriptors are never used by
  584.   other XLIB procedures.  They are provided to augment development of protected-
  585.   mode libraries.  Protected-mode library routines may access the stack and data
  586.   of the main program through these descriptors.
  587.      INITXLIB should be called only once within a program.  Subsequent calls are
  588.   returned with no action.  XLIB is terminated by INT 21H function 4C (DOS
  589.   termination) issued from either real or protected mode.
  590.  
  591.   XLIBMEMREQ (XLIB Memory Requirements)
  592.   Purpose:  Find XLIB conventional memory requirements.
  593.   CPU Mode:  Real
  594.   Registers at Call:  None
  595.   Return Registers:
  596.      Sign bit of DX clear if successful.  Memory requirements in bytes are
  597.   returned in DX:AX.
  598.      Sign bit of DX set if unsuccessful.  An error code is returned in AX
  599.   (always a DOS error code).
  600.   Details:
  601.      DX:AX is adjusted upward to an integer multiple of 16.
  602.      If both DPMI and VCPI are present, then XLIBMEMREQ will assume that DPMI
  603.   will be used if bit 0 of IFLAGS is clear (the default); otherwise, VCPI is
  604.   assumed.  No additional conventional memory is needed if both DPMI and VCPI
  605.   are absent.
  606.      This routine will return DX:AX = 0 if XLIB contains free internal memory in
  607.   sufficient quantity to meet conventional memory demands.
  608.      This routine obtains DPMI requirements by calling DPMIMEMREQ (see below)
  609.   and VCPI requirements by calling VCPIMEMREQ.
  610.  
  611.  
  612.  
  613.  
  614.  
  615.                                         7
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.   DPMIMEMREQ  (DPMI Memory Requirements)
  623.   Purpose:  Find DPMI conventional memory requirements.
  624.   CPU Mode:  Real
  625.   Registers at Call:  None
  626.   Return Registers:  DX:AX = conventional memory requirements.
  627.   Details:
  628.      DX:AX is adjusted upward to an integer multiple of 16.
  629.      This routine does not assume the presence of DPMI.  It will return DX:AX =
  630.   0 if DPMI is absent.
  631.      This routine will return DX:AX = 0 if XLIB contains free internal memory in
  632.   sufficient quantity to meet the conventional memory demands of DPMI.
  633.  
  634.   VCPIMEMREQ (VCPI Memory Requirements)
  635.   Purpose:  Find VCPI conventional memory requirements.
  636.   CPU Mode:  Real
  637.   Registers at Call:  None
  638.   Return Registers:  DX:AX = conventional memory requirements.
  639.   Details:
  640.      DX:AX is adjusted upward to an integer multiple of 16.
  641.      This routine simply loads DX:AX with a constant.
  642.  
  643.   XLIBCONFIG (XLIB Configuration)
  644.   Purpose:  Get XLIB configuration.
  645.   CPU Mode:  Real
  646.   Registers at Call:  None
  647.   Return Registers:  AX = 0 if protected-mode structures are not initialized;
  648.   otherwise, AX = XLIB configuration.  The value of lower nibble identifies the
  649.   protected-mode host/server.  If 1 then DPMI is installed.  If 2 then VCPI is
  650.   installed.  If 3 then XLIB handles mode switches.  Bit 4 is set if XMS is
  651.   installed.
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.                                         8
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.                                 4. Mode Switching
  687.  
  688.  
  689.        As illustrated in Example 1, CALLPM may be used to transfer control to
  690.   32-bit protected mode in segment TSEG.  When execution is returned to real
  691.   mode, segment registers, ESP, system flags, and control flags are restored to
  692.   their original values.
  693.        Execution may also be transferred to protected mode in TSEG with the
  694.   ENTERPM (enter protected mode) procedure.  This procedure is specially
  695.   designed to accommodate mixed-language programming with high-level languages
  696.   operating in real mode.  High-level language modules may be linked with
  697.   libraries containing protected-mode procedures.  These procedures may then be
  698.   called from high-level code.  However, such procedures must generally be
  699.   careful to restore register state.  ENTERPM restores register state as
  700.   required by Microsoft high-level languages.  In particular, ENTERPM restores
  701.   all registers except EAX, EDX, and the status flags.  EAX and EDX are not
  702.   restored because these are typically used by high-level languages for return
  703.   values.  ENTERPM otherwise functions exactly as CALLPM.
  704.        Both CALLPM and ENTERPM save register state as of call.  CALLPM and
  705.   ENTERPM will also save and restore the state of the floating point unit (FPU)
  706.   if requested.  FPU save/restore can be enabled by setting bit 2 of OFLAGS
  707.   (operation flags).  The bit is clear by default.  OFLAGS is a public WORD in
  708.   DSEG.
  709.        FPU state is saved with the FSAVE instruction executed from real mode.
  710.   This instruction resets the FPU; consequently, the FPU control word must be
  711.   redefined.  XLIB will therefore load FPUCW (FPU control word) to the FPU
  712.   control word after performing FSAVE.  FPUCW is a public WORD location in DSEG.
  713.        After entering protected mode through CALLPM or ENTERPM, control would
  714.   typically be returned to real mode with the RET instruction.  However, control
  715.   may also be returned to real mode by jumping to either RETPM or EXITPM.  These
  716.   are both procedures in TSEG.  They will successfully return control to real
  717.   mode regardless of the stack state.
  718.        RETPM returns control to the real-mode caller of CALLPM/ENTERPM after
  719.   restoring segment registers, ESP, system flags, and control flags.  EXITPM
  720.   returns control to the real mode caller after restoring all registers except
  721.   EAX, EDX, and the status flags.
  722.        The return address placed on the stack by CALLPM is actually a near
  723.   return to RETPM.  Likewise, ENTERPM places a near return to EXITPM.  CALLPM
  724.   and ENTERPM are otherwise identical procedures.
  725.        Once within protected mode, far procedures in real mode can be called
  726.   using CALLRM.  CALLRM may be called only by protected-mode procedures in TSEG.
  727.        XLIB contains two hardware interrupt handlers that are typically
  728.   activated upon entry to protected mode.  These handlers are fully explained in
  729.   Chapter 6.  The first handler is hooked to the keyboard interrupt.  This
  730.   handler manages hot key detection.  The second handler is hooked to the FPU
  731.   interrupt and is designed to handle FPU exceptions.
  732.  
  733.  
  734.  
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.                                         9
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.   Detailed Specifications
  751.  
  752.  
  753.   CALLPM (Call Protected Mode)
  754.   Purpose:  Call a protected-mode procedure in TSEG with near return.
  755.   CPU Mode:  Real
  756.   Registers at Call:  SS:ESP = 32-bit protected-mode target offset.
  757.   Return Registers:  Returns through RETPM.  See RETPM for details.
  758.   Details:
  759.      All CPU registers except EAX and EDX are saved at locations presented in
  760.   Table 2.  The stack is saved after the return address and argument have been
  761.   popped.
  762.      The target receives SS = TSEGDSEL with 1000H free bytes on the stack.  The
  763.   return address on the stack is a near return to RETPM.  The target receives by
  764.   default:  DS = FLATDSEL, ES = TSEGDSEL, FS = DSEGSEL, and GS = DGROUPSEL.
  765.   Other registers, including the status flags, are received at values as of
  766.   call.
  767.      If bit 2 of OFLAGS is set, then the FPU state is also saved; the FPU is
  768.   initialized, and FPUCW is loaded to the control word.
  769.      XLIB hardware interrupt handlers are enabled (see Chapter 6).  However, if
  770.   bit 1 of OFLAGS is set, then the XLIB interrupt handler for the FPU will not
  771.   be enabled.
  772.      If an FPU exception occurs after CALLPM, and if the FPU exception handler
  773.   is enabled, then protected mode will be exited through EXITPM rather than
  774.   RETPM.  If FPU save/restore is not enabled, then real-mode will receive an
  775.   initialized FPU with control word set to the value existing as of the
  776.   exception.
  777.      The user may change the stack after the mode switch.
  778.      DS, ES, FS, and GS are actually loaded from:  PMDS, PMES, PMFS, and PMGS.
  779.   These are public WORD locations in DSEG and are initialized to the default
  780.   selectors by INITXLIB.  The user however may change these selectors to any
  781.   legal values after initialization.
  782.  
  783.   RETPM (Return From Protected Mode)
  784.   Purpose:  Return control to real mode with partial register restoration.
  785.   CPU Mode:  Protected
  786.   Registers at Call:  None
  787.   Return Registers:  No return
  788.   Details:
  789.      RETPM switches to real mode and then restores all segment registers, ESP,
  790.   system flags, and control flags to values as of call to either CALLPM or
  791.   ENTERPM.  XLIB hardware interrupt handlers are disabled (see Chapter 6).
  792.   Control is then transferred to the real-mode return address as of call to
  793.   CALLPM/ENTERPM.
  794.      If bit 2 of OFLAGS is set, then RETPM also restores FPU state.
  795.      RETPM will successfully execute regardless of stack state.
  796.  
  797.   ENTERPM (Enter Protected Mode)
  798.   Purpose:  Call a protected mode procedure in TSEG with near return.
  799.   CPU Mode:  Real
  800.   Registers at Call:  SS:ESP = 32-bit protected-mode target offset.
  801.   Return Registers:  Returns through EXITPM.  See EXITPM for details.
  802.   Details:  This routine executes exactly as CALLPM except that a near return to
  803.   EXITPM is placed on the stack rather than to RETPM.
  804.  
  805.  
  806.  
  807.                                         10
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.   EXITPM (Exit Protected Mode)
  816.   Purpose:  Return control to real mode with general register restoration.
  817.   CPU Mode:  Protected
  818.   Registers at Call:  None
  819.   Return Registers:  No return
  820.   Details:
  821.      EXITPM switches to real mode and then restores all registers except EAX,
  822.   EDX, and status flags to values as of call to either CALLPM or ENTERPM.  XLIB
  823.   hardware interrupt handlers are disabled (see Chapter 6).  Control is then
  824.   transferred to the real-mode return address as of call to CALLPM/ENTERPM.
  825.      If bit 2 of OFLAGS is set, then EXITPM also restores FPU state.
  826.      EXITPM will successfully execute regardless of stack state.
  827.      The FPU exception handler performs a jump to EXITPM upon occurrence of any
  828.   unmasked FPU exception.
  829.  
  830.  
  831.   Table 2:  CALLPM/ENTERPM Register Storage Locations by Public Symbol
  832.   -----------------------------------------------------------------------------
  833.   Register           Symbol              Symbol Type
  834.   --------           ------              -----------
  835.   EBX                ORGEBX              DWORD
  836.   ECX                ORGECX              DWORD
  837.   ESI                ORGESI              DWORD
  838.   EDI                ORGEDI              DWORD
  839.   EBP                ORGEBP              DWORD
  840.   ESP                ORGESP              DWORD
  841.   EFLAGS             ORGEFLAGS           DWORD
  842.   SS                 ORGSS               WORD
  843.   DS                 ORGDS               WORD
  844.   ES                 ORGES               WORD
  845.   FS                 ORGFS               WORD
  846.   GS                 ORGGS               WORD
  847.   FPU State          ORGFPU              BYTE[94]
  848.   -----------------------------------------------------------------------------
  849.  
  850.  
  851.   CALLRM (Call a Real-Mode Procedure)
  852.   Purpose:  Call a real-mode routine with far return from protected mode.
  853.   CPU Mode:  Protected
  854.   Registers at Call:  SS:ESP = far real-mode target address (four bytes).
  855.   Return Registers:  All segment registers and ESP are restored.  Other
  856.   registers, including status flags, are received with values as of the real-
  857.   mode RET instruction.
  858.   Details:
  859.      This is a near procedure in TSEG.  It must therefore be called from TSEG.
  860.      Segment registers and ESP are saved at locations presented in Table 3.  The
  861.   stack is saved after popping the return address and argument.
  862.      The target receives the XLIB real-mode stack (SS = DSEG) with 200H free
  863.   bytes.  By default, the target receives DS = DGROUP, ES = DSEG, FS = DSEG, and
  864.   GS = DSEG.  Other registers, including status flags, are received at values as
  865.   of call.
  866.      Code called by this routine should not perform calls to XLIB procedures
  867.   other than SWITCHPM and SWITCHRM (see Appendix G).
  868.  
  869.  
  870.  
  871.                                         11
  872.  
  873.  
  874.  
  875.  
  876.  
  877.  
  878.      DS and ES are actually loaded from RMDS and RMES.  These are public WORD
  879.   locations in DSEG and are initialized to the default values.  However, the
  880.   user may change these values if desired.
  881.      This procedure does not change values in OFLAGS; consequently, XLIB
  882.   hardware interrupt handlers remain enabled in real mode if they were enabled
  883.   as of call (see Chapter 6).
  884.      FPU exceptions in real mode are handled the same as in protected mode;
  885.   however, system software is less apt to be left in regular state after real-
  886.   mode exceptions.  FPU instructions should therefore be executed in protected
  887.   mode where possible.
  888.  
  889.  
  890.   Table 3:  CALLRM Register Storage Locations by Public Symbol
  891.   -----------------------------------------------------------------------------
  892.   Register           Symbol              Symbol Type
  893.   --------           ------              -----------
  894.   ESP                CALLESP             DWORD
  895.   SS                 CALLSS              WORD
  896.   DS                 CALLDS              WORD
  897.   ES                 CALLES              WORD
  898.   FS                 CALLFS              WORD
  899.   GS                 CALLGS              WORD
  900.   -----------------------------------------------------------------------------
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930.  
  931.  
  932.  
  933.  
  934.  
  935.                                         12
  936.  
  937.  
  938.  
  939.  
  940.  
  941.  
  942.                              5. Inline Mode Switching
  943.  
  944.  
  945.        XLIB includes two routines to perform mode switching in C programs using
  946.   inline assembly.  These procedures are very versatile and simple.  A third
  947.   routine is included to facilitate calls from 16-bit segments to 32-bit near
  948.   procedures in TSEG.
  949.        Call INLINEPM to switch to protected mode.  Descriptors are automatically
  950.   created for CS, SS, and DS.  These registers are returned containing selectors
  951.   to the respective descriptors.  ES is returned containing DSEGSEL.  ES may be
  952.   used to load other XLIB selectors to segment registers.
  953.        Call INLINERM to return to real-mode.  This function should be called
  954.   from the same segment as the previous call to INLINEPM and should be using the
  955.   same stack segment.  INLINERM restores segment registers to values which
  956.   existed as of the call to INLINEPM.
  957.        The following Microsoft C 7.0 program illustrates the usage of these
  958.   procedures.  The program contains a C subroutine called "getextmem" which uses
  959.   INLINEPM and INLINERM to retrieve a DWORD from extended memory.
  960.        Observe that since the inline assembly code is in a 16-bit segment,
  961.   prefixes must be used with 32-bit instructions.  Also observe that INLINERM is
  962.   called indirect through a supplied pointer in DSEG called INLINERMPTR.  This
  963.   is done to ensure that the intersegment call loads CS with the protected-mode
  964.   selector for CSEG (CSEGSEL) rather than with the segment constant.
  965.        This program may fail if an attempt is made to access logical addresses
  966.   which are either protected or undefined in the page tables.
  967.  
  968.  
  969.   Example 2:  Using INLINEPM/INLINERM in C
  970.   -----------------------------------------------------------------------------
  971.   #include <stdio.h>
  972.   #include <xlib.h>
  973.   #define som _emit 0x66               /* switch operand mode */
  974.   #define sam _emit 0x67               /* switch address mode */
  975.   long __far getextmem(long address);
  976.  
  977.   int goterr = 0;                      /* error flag */
  978.  
  979.   main()
  980.   {
  981.     long l;
  982.     l = INITXLIB();                    /* initialize XLIB */
  983.     if(l != 0)
  984.     {
  985.       printf("Library initialization error:  %lX\n",l);
  986.       return 0;
  987.     }
  988.     l = getextmem(0x100000);           /* read first address in 2ond meg */
  989.     if(goterr != 0)
  990.     {
  991.       printf("Inline mode-switch error:  %lX\n",l);
  992.       return 0;
  993.     }
  994.     printf("%lX\n",l);
  995.   }
  996.  
  997.  
  998.  
  999.                                         13
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005.  
  1006.  
  1007.   long __far getextmem(long address)
  1008.   {
  1009.     __asm
  1010.     {
  1011.       som                             ;mov  eax,[bp+6]
  1012.       mov       ax,[bp+6]             ;  ""
  1013.       call      INLINEPM
  1014.       jc        error                 ;Error code in ax
  1015.       mov       ds,es:FLATDSEL
  1016.       sam                             ;push dword ptr [eax]
  1017.       som                             ;  ""
  1018.       _emit     0ffh                  ;  ""
  1019.       _emit     030h                  ;  ""
  1020.       pop       ax
  1021.       pop       dx
  1022.       call      es:INLINERMPTR
  1023.       jmp       done
  1024.   error:
  1025.       xor       dx,dx                 ;Return error code in dx:ax
  1026.       inc       goterr
  1027.   done:
  1028.     }
  1029.   }
  1030.   -----------------------------------------------------------------------------
  1031.  
  1032.  
  1033.   Detailed Specifications
  1034.  
  1035.  
  1036.   INLINEPM (Inline Protected-Mode)
  1037.   Purpose:  Return to real-mode caller in 16-bit protected mode.
  1038.   CPU Mode:  Real
  1039.   Registers at Call:  None
  1040.   Return Registers:
  1041.      CF clear if successful.  Descriptors are created for CS, SS, and DS.  These
  1042.   descriptors have base addresses matching the calling contents of the
  1043.   respective segment registers.  The segment registers are returned containing
  1044.   selectors to these descriptors.  These selectors are also stored in DSEG at
  1045.   the public WORD locations ILCSSEL, ILSSSEL, and ILDSSEL.  See Chapter 2 for
  1046.   further details as to descriptor specifications.  ES, FS, and GS are returned
  1047.   containing DSEGSEL.  Protected mode receives other registers, except the
  1048.   status flags, at values as of call.
  1049.      CF set if unsuccessful.  The processor will still be in real mode.
  1050.   Unsuccessful execution can occur only under DPMI.  EAX is returned with an
  1051.   error code.  AX = XLIB error code.  The high word of EAX will equal a DPMI 1.0
  1052.   error code (if provided by host).  Other registers, except the status flags,
  1053.   are unchanged.
  1054.   Details:
  1055.      INLINEPM stores segment registers at the same locations used by CALLPM and
  1056.   ENTERPM (Table 1).  INLINERM (see below) then restores these registers.  C
  1057.   will require that other registers be preserved also; however, the user is
  1058.   responsible for managing these.
  1059.  
  1060.  
  1061.  
  1062.  
  1063.                                         14
  1064.  
  1065.  
  1066.  
  1067.  
  1068.  
  1069.  
  1070.      XLIB hardware interrupt handlers are not activated by this procedure (see
  1071.   Chapter 6).  The keyboard handler may be enabled by the user if hot key
  1072.   detection is needful; however, the FPU exception handler should never be
  1073.   enabled.  Therefore, hot key detection should be enabled by setting both bits
  1074.   0 and 1 in OFLAGS.
  1075.      SP is preserved through the mode switch; however, the high word of ESP is
  1076.   set to zero.  The high word must be cleared since SS is set to a descriptor
  1077.   having FFFFH limit and having its big bit set.
  1078.      If multiple calls are made to INLINEPM with the same values in CS, SS, and
  1079.   DS, then descriptors are created only on the first call.  Subsequent calls
  1080.   will therefore execute more quickly.
  1081.  
  1082.   INLINERM (Inline Real-Mode)
  1083.   Purpose:  Return to 16-bit protected-mode caller in real mode.
  1084.   CPU Mode:  16-bit protected mode
  1085.   Registers at Call:  CS and SS must equal values as of return from INLINEPM.
  1086.   Return Registers:  Segment registers are restored to values existing as of
  1087.   former call to INLINEPM.  Real mode receives other registers, except status
  1088.   flags, at values as of call.
  1089.   Details:
  1090.      Since INLINERM will be called intersegment, caution must be taken that CS
  1091.   is loaded with CSEGSEL and not CSEG.  XLIB provides a far pointer to this
  1092.   procedure called INLINERMPTR which may be used to execute the call.
  1093.      INLINERM and CALL32 are the only 16-bit protected-mode procedures in XLIB.
  1094.   They are also the only protected-mode procedures having far returns.
  1095.  
  1096.   CALL32 (Call 32-bit Protected-Mode)
  1097.   Purpose:  Call a 32-bit protected-mode near procedure in segment TSEG from 16-
  1098.   bit protected-mode.
  1099.   CPU Mode:  16-bit protected mode
  1100.   Registers at Call:  SS:ESP =  32-bit protected-mode target offset.
  1101.   Return Registers:  All registers, including status flags, are returned with
  1102.   values existing as of the 32-bit RET instruction.
  1103.   Details:
  1104.      CALL32 is a far procedure; therefore, caution must be taken that calls to
  1105.   CALL32 load CS with CSEGSEL instead of CSEG.  XLIB provides a far pointer to
  1106.   this procedure in DSEG called CALL32PTR which may be used to execute the call.
  1107.      CALL32 and INLINERM are the only 16-bit protected-mode procedures in XLIB.
  1108.   They are also the only protected-mode procedures having far returns.
  1109.      This procedure does not alter the state of OFLAGS.
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127.                                         15
  1128.  
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134.                              6. Interrupt Management
  1135.  
  1136.  
  1137.        Interrupt management is the most complicated task performed by XLIB.
  1138.   Accordingly, this chapter is the most difficult section of the user's manual.
  1139.   In general, this chapter may be ignored for programs which do not install
  1140.   interrupt handlers, do not require hot key detection, and do not use floating
  1141.   point operations.
  1142.  
  1143.  
  1144.   XLIB Interrupt Handlers
  1145.  
  1146.  
  1147.        XLIB handles nearly all interrupts occurring in protected mode by
  1148.   shifting to real mode and calling the currently installed real-mode interrupt
  1149.   handlers.  In all but three cases, XLIB uses inherited real-mode handlers.
  1150.   XLIB installs its own handlers only for the DOS interrupt (INT 21H), the
  1151.   keyboard interrupt (INT 9), and the FPU interrupt (INT 75H).
  1152.        The DOS interrupt handler (INT 21H) intercepts all DOS calls and
  1153.   determines if termination is requested (AH = 4CH).  If not, then the interrupt
  1154.   is immediately cascaded to DOS.  If so, then XLIB performs housecleaning
  1155.   before relaying the request to DOS.  Under DPMI, XLIB will restore all
  1156.   interrupt vectors and release all allocated descriptors.  The DPMI host
  1157.   automatically releases all allocated memory.  Under other configurations, XLIB
  1158.   will reset all real-mode interrupt vectors and release all allocated extended
  1159.   memory.
  1160.        INT 21H function 4CH may be executed in either real mode or protected
  1161.   mode.  DPMI hosts will expect this function to be executed from protected
  1162.   mode; consequently, XLIB will switch to protected mode before relaying the
  1163.   request to DPMI.  XLIB also installs a protected-mode handler for INT 21H
  1164.   under DPMI.  This is to ensure that XLIB will see the termination request
  1165.   before the DPMI host.
  1166.        The keyboard interrupt handler is intended to facilitate termination of
  1167.   protected-mode procedures with a keypress.  When a user-defined hot key is
  1168.   pressed, the keyboard interrupt handler will modify a flag variable.  This
  1169.   flag variable may then be polled periodically in the main thread of execution.
  1170.   The keyboard interrupt handler is a real-mode routine.
  1171.        The inconvenience of having to poll the flag variable is unfortunately
  1172.   necessary.  It is not generally safe to terminate within a hardware interrupt
  1173.   handler, particularly in protected-mode environments.  A hardware interrupt
  1174.   generated by a keypress may have interrupted system software in the middle of
  1175.   a system maintenance operation.  Termination in such cases would likely leave
  1176.   the system in an irregular and potentially unstable state.
  1177.        Matters are further complicated in most protected-mode environments.  For
  1178.   example, a DPMI host will trap all hardware interrupts before cascading them
  1179.   to interrupt handlers.  If control is not returned to the host with the IRET
  1180.   instruction, then the host will be left in an irregular state.  Moreover, the
  1181.   trapping procedure will likely switch stacks before relaying the interrupt to
  1182.   the handler; consequently, the handler cannot determine the final return
  1183.   address and therefore cannot change this address to a termination procedure.
  1184.   These complications will nearly always occur when hardware interrupts are
  1185.   virtualized.  Virtual 8086 mode interrupts will almost certainly be
  1186.   virtualized in either VCPI-based or DPMI-based environments.
  1187.  
  1188.  
  1189.  
  1190.  
  1191.                                         16
  1192.  
  1193.  
  1194.  
  1195.  
  1196.  
  1197.  
  1198.        When a key is pressed, the keyboard interrupt handler will examine the
  1199.   key to determine if it is the hot key.  If not, then the interrupt is cascaded
  1200.   to the inherited real-mode handler.  If so, then a hot key flag is recorded to
  1201.   a DWORD whose linear address is recorded at CCODEPTR (condition code pointer).
  1202.   The hot key is not cascaded.  CCODEPTR is a public DWORD in DSEG.  The hot key
  1203.   flag is listed among XLIB error codes in Appendix B.
  1204.        CCODEPTR initially contains the linear address to public DWORD location
  1205.   CCODE which is in DSEG.  Therefore, the hot key flag would be written to CCODE
  1206.   by default.  CCODEPTR may be changed by the user; however, it must point to an
  1207.   address in conventional memory.
  1208.        The hot key specification is stored in DSEG at a public WORD location
  1209.   called HOTKEY.  The lower byte of HOTKEY contains the scan code of the hot key
  1210.   while the upper byte specifies the state of the shift keys.  Bit 8 specifies
  1211.   the state of SHIFT; bit 9 specifies CTRL, and bit 10 specifies ALT.  All other
  1212.   bits are ignored.  Set bits require that the designated shift key be pressed.
  1213.   The default setting for HOTKEY is zero.  This setting disables hot key
  1214.   detection since no key has a zero scan code.
  1215.        The XLIB interrupt handler for FPU exceptions performs three major
  1216.   functions upon the occurrence of any FPU exception that is unmasked in the FPU
  1217.   control word.  First, an error code is loaded to EAX and is also recorded at
  1218.   the linear address in CCODEPTR.  Next, the FPU is initialized with FNINIT (the
  1219.   FPU control word is preserved).  Third, control is transferred to EXITPM to
  1220.   return to real mode.  The high word stored in EAX and the condition code will
  1221.   be the FPU status word.  This word may be examined to determine the nature of
  1222.   the exception.  The FPU interrupt handler is a real-mode routine.
  1223.        The response of the FPU to exception conditions is largely determined by
  1224.   the settings in the FPU control word.  If FPU save/restore is enabled, then
  1225.   the FPU control word will be set to FPUCW upon execution of CALLPM/ENTERPM.
  1226.   The default value for FPUCW is 0332H.  This sets rounding control to nearest,
  1227.   precision control to 64 bits, and unmasks exceptions for overflow, zero
  1228.   divide, and invalid operations.  Exceptions for underflow, precision, and
  1229.   denormalized operations are masked, and are therefore handled internally by
  1230.   the FPU.  FPUCW may be modified by the user.
  1231.        As explained above, the machine may be left in an unstable state after a
  1232.   program has been terminated from within a hardware interrupt handler.  This
  1233.   can also be the case for the FPU interrupt handler.  However, it will be safe
  1234.   to continue execution after FPU exceptions under clean configurations.  This
  1235.   follows because interrupts are not virtualized and because the exception will
  1236.   never be generated in the operating system (DOS does not use the FPU).  Nor
  1237.   should there be any problem with continuing execution after an exception in
  1238.   protected-mode under VCPI.  This follows since protected-mode interrupts
  1239.   cannot be virtualized under VCPI.  Exceptions occurring in Virtual 8086 mode
  1240.   or under DPMI protected mode may however leave the machine in an irregular
  1241.   state.  Reboot may be necessary in these cases.
  1242.        All tested DPMI hosts appear to be restored to normal state by execution
  1243.   of INT 21H function 4CH (DOS termination).  However, the DPMI specifications
  1244.   offer no guarantees to this approach.  FPU exceptions in virtual 8086 mode are
  1245.   generally no problem in single-task environments; however, they will
  1246.   frequently prove problematic under a multitasker such as DESQview.
  1247.        Bit 0 of OFLAGS is used by XLIB to simultaneously enable or disable all
  1248.   of its own hardware interrupt handlers.  Setting the bit enables the handlers.
  1249.   XLIB sets this bit upon calls to CALLPM/ENTERPM and clears the bit upon
  1250.   return.  All interrupts are immediately cascaded to the inherited real-mode
  1251.   handlers when the bit is clear.  Therefore, FPU exceptions are handled by XLIB
  1252.  
  1253.  
  1254.  
  1255.                                         17
  1256.  
  1257.  
  1258.  
  1259.  
  1260.  
  1261.  
  1262.   only after calls to CALLPM/ENTERPM.  Accordingly, hot key detection is enabled
  1263.   only after calls to CALLPM/ENTERPM.  The user may set the bit under other
  1264.   circumstances; however, XLIB will not be able to properly handle FPU
  1265.   exceptions in these cases.  The bit should be set by the user only when hot
  1266.   key detection is desired and when it is certain that the FPU exception handler
  1267.   will not be invoked.
  1268.        The FPU exception handler may be permanently disabled by setting bit 1 in
  1269.   OFLAGS.  If this bit is set, then the FPU interrupt handler simply cascades
  1270.   the interrupt to the inherited real-mode handler.  XLIB will set the bit
  1271.   during initialization if an FPU is not present; it is otherwise cleared.
  1272.        Real-mode software interrupts which receive or return values in segment
  1273.   registers cannot be used within protected mode because the deflection routine
  1274.   will restore selectors to segment registers upon completion of the interrupt.
  1275.   To use such software interrupts, one must switch to real mode through CALLRM;
  1276.   issue the interrupt, and then transfer the segment registers to other
  1277.   registers or to memory before returning to protected mode.
  1278.        Certain software interrupts use status flags for return flags, typically
  1279.   to signal error conditions.  This is particularly the case for DOS interrupts.
  1280.   The deflection routine will pass these alterations to the code which issued
  1281.   the interrupt.
  1282.  
  1283.  
  1284.   Installation of Interrupt Handlers
  1285.  
  1286.  
  1287.        The user may install real-mode interrupt handlers in usual fashion.  Such
  1288.   handlers will also receive interrupts occurring in protected mode because
  1289.   either XLIB or the DPMI host will deflect all protected-mode interrupts.  If
  1290.   XLIB deflects the interrupt, then the handler will receive SS = DSEG with ESP
  1291.   set to 100H free bytes.  Stack sizes under DPMI will depend upon the host, but
  1292.   must contain a minimum of 200H free bytes to meet DPMI specifications.
  1293.        The DOS routines to get and set interrupt vectors (INT 21H functions 35H
  1294.   and 25H) receive and return values through segment registers; consequently,
  1295.   they cannot be used in protected mode.  Instead, use the XLIB procedures
  1296.   PMGETRMIV and PMSETRMIV (protected mode - get/set real-mode interrupt vector).
  1297.        The user may install a protected-mode interrupt handler from real mode by
  1298.   calling SETPMIV (set protected-mode interrupt vector).  The current protected-
  1299.   mode interrupt vector may be obtained by calling GETPMIV.
  1300.        From protected mode, interrupt vectors can be managed with PMSETPMIV and
  1301.   PMGETPMIV.  These procedures have the same specifications as SETPMIV and
  1302.   GETPMIV.
  1303.        Interrupt handlers installed with SETPMIV/PMSETPMIV are never disabled by
  1304.   XLIB and will therefore always be active under protected-mode execution.
  1305.   These handlers will not be active under real-mode execution in the absence of
  1306.   DPMI.  That is, real-mode interrupts are never deflected to protected-mode
  1307.   handlers in such environments.  However if DPMI is active, then all hardware
  1308.   interrupts (IRQs 0-15) and the software interrupts:  1CH (BIOS timer tick),
  1309.   23H (DOS CTRL C), and 24H (DOS critical error) are deflected from real mode to
  1310.   the installed protected-mode handler.  Therefore the protected-mode handler
  1311.   always receives the interrupt first.  If the handler cascades the interrupt,
  1312.   then the real-mode handler will receive it next.  If the user has not
  1313.   installed protected-mode handlers for these interrupts, then they are serviced
  1314.   by default handlers in the DPMI host.  The default handlers typically deflect
  1315.   the interrupts to the real-mode handlers.
  1316.  
  1317.  
  1318.  
  1319.                                         18
  1320.  
  1321.  
  1322.  
  1323.  
  1324.  
  1325.  
  1326.        If the programmer wishes to install a protected-mode interrupt handler
  1327.   for a hardware interrupt or for INT 1CH, INT 23H, or INT 24H, then
  1328.   consideration must be given to the fact that treatment of these interrupts
  1329.   will differ under different protected-mode configurations.  As noted above,
  1330.   the DPMI host will always send these interrupts to the protected-mode handler
  1331.   regardless of the CPU mode in which the interrupt occurred.  If DPMI is not
  1332.   installed, then protected-mode handlers receive control only under protected-
  1333.   mode interrupts.  Therefore, if the protected-mode handler is to receive real-
  1334.   mode interrupts under such configurations, the programmer must install a real-
  1335.   mode handler to perform the deflection.
  1336.        XLIB includes a procedure called DEFLECTPM which can be used within a
  1337.   real-mode interrupt handler to deflect control to a protected-mode handler.
  1338.   DEFLECTPM functions only under VCPI and XLIB mode switching.  If DPMI is
  1339.   installed, then the procedure returns with no action.  The intent of this
  1340.   procedure is to enable simulation of DPMI treatment of hardware interrupts,
  1341.   INT 1CH, INT 23H, and INT 24H.
  1342.        Observe that if the real-mode handler deflects to the protected-mode
  1343.   handler, then the latter should not cascade the interrupt since an infinite
  1344.   loop would result.  This follows because the initial protected-mode handler
  1345.   deflects to the real-mode handler.
  1346.        Were one to use DEFLECTPM in a real-mode handler for the interrupts named
  1347.   above, then the protected-mode handler will receive all interrupts regardless
  1348.   of the protected-mode configuration.  This occurs naturally under DPMI.
  1349.   DEFLECTPM ensures that it will occur under other configurations.  Observe that
  1350.   under DPMI, the real-mode handler will never be executed.
  1351.        It is sometimes important that interrupt handlers execute in shortest
  1352.   possible CPU time.  This would typically be the case for handlers of the
  1353.   communication ports.  Since mode switching is time consuming, such handlers
  1354.   should be installed for the CPU mode which is expected to receive the most
  1355.   interrupts.
  1356.        If DPMI is installed, then it is possible for multiple clients to operate
  1357.   in a single virtual machine.  In such cases, DPMI will always send hardware
  1358.   interrupts to the primary client (the most recently installed client in the
  1359.   virtual machine).
  1360.        Under DPMI, all protected-mode handlers for hardware interrupts and
  1361.   software interrupts 0-7 will receive control with interrupts disabled.  Since
  1362.   DPMI virtualizes the interrupt flag, the IRET instruction may not reenable
  1363.   interrupts.  Consequently, all handlers for these interrupts should execute
  1364.   STI before executing IRET.  Other protected-mode interrupts do not affect the
  1365.   interrupt flag.
  1366.        All real-mode interrupt handlers will receive control with interrupts
  1367.   disabled regardless of the protected-mode configuration.  All protected-mode
  1368.   handlers will receive control with interrupts disabled under VCPI or XLIB mode
  1369.   switching.  However, if DPMI is installed, then protected-mode software
  1370.   interrupts apart from 0-7 will receive the virtual interrupt flag at its value
  1371.   as of the INT instruction.  That is, DPMI does not alter the interrupt flag in
  1372.   these cases.
  1373.        Hardware interrupts IRQ 0 through IRQ 7 are typically assigned to
  1374.   interrupt numbers 08H through 0FH, while IRQs 8 through 15 are typically
  1375.   assigned interrupt numbers 70H through 77H.  However, IRQs are remapped in
  1376.   some operating environments, typically to facilitate exception handling.  The
  1377.   current mappings may be loaded from IRQ0INTNO (IRQ 0 interrupt number) and
  1378.   IRQ8INTNO.  These are public BYTE locations in DSEG.  They should be read only
  1379.   after the call to INITXLIB.
  1380.  
  1381.  
  1382.  
  1383.                                         19
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.        DESQview does remap hardware interrupts; however, its interrupt handlers
  1391.   for the new locations generally transfer control to the addresses at the
  1392.   conventional vectors.  DESQview must be started with a command-line switch if
  1393.   it is to accommodate certain hardware interrupts.  In particular, the FPU
  1394.   interrupt will not function properly under DESQview unless DESQview is started
  1395.   with DV /HW:75:C.
  1396.  
  1397.  
  1398.   Detailed Specifications
  1399.  
  1400.  
  1401.   PMGETRMIV (Protected Mode - Get Real-Mode Interrupt Vector)
  1402.   Purpose:  Retrieve address of real-mode interrupt handler from protected mode.
  1403.   CPU Mode:  Protected
  1404.   Registers at Call:  AL = interrupt number.
  1405.   Return Registers:  Handler address returned in CX:DX.
  1406.   Details:  The DOS routine for this purpose (INT 21H function 35H) is not
  1407.   useful because it returns a value in ES.
  1408.  
  1409.   PMSETRMIV (Protected Mode - Set Real-Mode Interrupt Vector)
  1410.   Purpose:  Set address of real-mode interrupt handler from protected mode.
  1411.   CPU Mode:  Protected
  1412.   Registers at Call:  AL = interrupt number, CX:DX = address of handler.
  1413.   Return Registers:  None
  1414.   Details:
  1415.      The DOS routine for this purpose (INT 21H function 25H) is not useful
  1416.   because it requires an argument in DS.
  1417.      Real-mode interrupt handlers will also be called when interrupts occur in
  1418.   protected mode provided that the protected-mode interrupt handler cascades the
  1419.   interrupt.  The default protected-mode handlers do in fact cascade all
  1420.   interrupts.
  1421.      XLIB never disables handlers installed by this procedure.
  1422.  
  1423.   GETPMIV (Get Protected-Mode Interrupt Vector)
  1424.   Purpose:  Retrieve address of protected-mode interrupt handler from interrupt
  1425.   descriptor table.
  1426.   CPU Mode:  Real
  1427.   Registers at Call:  AL = interrupt number.
  1428.   Return Registers:  Handler address returned in CX:EDX (CX is a selector).
  1429.   Details:  This routine does not return addresses of CPU exception handlers
  1430.   under DPMI.  Use DPMI functions directly for this purpose.
  1431.  
  1432.   SETPMIV (Set Protected-Mode Interrupt Vector)
  1433.   Purpose:  Set address of protected-mode interrupt handler in interrupt
  1434.   descriptor table.
  1435.   CPU Mode:  Real
  1436.   Registers at Call:  AL = interrupt number.  Handler address in CX:EDX (CX is a
  1437.   selector).
  1438.   Return Registers:  EAX = 0 if successful.  EAX = error code if unsuccessful.
  1439.   AX = XLIB error code.  If DPMI is installed then the high word of EAX will
  1440.   contain a DPMI 1.0 error code (if provided by host).
  1441.   Details:
  1442.      XLIB never disables handlers installed by this procedure.
  1443.  
  1444.  
  1445.  
  1446.  
  1447.                                         20
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.      Protected-mode interrupt handlers never receive interrupts occurring in
  1455.   real mode unless DPMI is active.  Under DPMI all hardware interrupts (IRQs 0-
  1456.   15) and software interrupts 1CH, 23H, and 24H are deflected from real mode to
  1457.   the installed protected-mode handler.  The protected-mode handler therefore
  1458.   receives control of the interrupt first.  It may cascade the interrupt if so
  1459.   desired, in which event, the real-mode handler receives the interrupt next. If
  1460.   no protected-mode handler has been installed, then DPMI generally deflects the
  1461.   interrupt to the real-mode handler.
  1462.      All protected-mode handlers will receive control with interrupts disabled
  1463.   unless DPMI is installed.  Under DPMI, protected-mode software interrupts
  1464.   apart from 0-7 do not alter the state of the virtual interrupt flag.
  1465.      Protected-mode handlers under DPMI for hardware interrupts and software
  1466.   interrupts 0-7 should execute STI before IRET to ensure that the virtual
  1467.   interrupt flag is enabled.
  1468.      If multiple DPMI clients are running in the same virtual machine, then the
  1469.   primary client (most recently installed client) always receives hardware
  1470.   interrupts.
  1471.      If DPMI is installed, then protected-mode interrupt vectors should be reset
  1472.   to original values before termination.
  1473.      This routine should not be used to install CPU exception handlers under
  1474.   DPMI.  DPMI functions should be used for this purpose.
  1475.  
  1476.   PMGETPMIV (Protected Mode - Get Protected-Mode Interrupt Vector)
  1477.   Purpose:  Retrieve address of protected-mode interrupt handler from interrupt
  1478.   descriptor table.
  1479.   CPU Mode:  Protected
  1480.   Details:  This routine is the protected-mode version of GETPMIV.  See GETPMIV
  1481.   for specifications.
  1482.  
  1483.   PMSETPMIV (Protected Mode - Set Protected-Mode Interrupt Vector)
  1484.   Purpose:  Set address of protected-mode interrupt handler in interrupt
  1485.   descriptor table.
  1486.   CPU Mode:  Protected
  1487.   Details:  This routine is the protected-mode version of SETPMIV.  See SETPMIV
  1488.   for specifications.
  1489.  
  1490.   DEFLECTPM (Deflect to Protected-Mode)
  1491.   Purpose:  Call a protected-mode interrupt handler.
  1492.   CPU Mode:  Real
  1493.   Registers at Call:  SS:ESP = interrupt number (2 bytes)
  1494.   Return Registers:  All registers except segment registers and ESP are returned
  1495.   at values as of the protected-mode IRET instruction.
  1496.   Details:
  1497.      This procedure can be used only under VCPI and XLIB mode switching.  It
  1498.   returns with no action under DPMI.  The routine is intended to simulate DPMI
  1499.   treatment of hardware interrupts, INT 1CH, INT 23H, and INT24H.  The routine
  1500.   may be called from a real-mode interrupt handler to deflect the interrupt to
  1501.   the protected-mode handler.
  1502.      The protected-mode handler should not cascade the interrupt; otherwise, an
  1503.   infinite loop will result.
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.                                         21
  1512.  
  1513.  
  1514.  
  1515.  
  1516.  
  1517.  
  1518.                                7. Memory Management
  1519.  
  1520.  
  1521.        XLIB supplies memory management procedures for both real and protected
  1522.   modes.  These procedures are configured at initialization to work with the
  1523.   currently resident memory management interfaces.
  1524.        Conventional memory may be allocated and released in real mode through
  1525.   DOS in usual fashion (INT 21H functions 48H and 49H).  However, DOS functions
  1526.   may not work properly in protected mode.  Therefore, use the XLIB routines
  1527.   PMGETDOSMEM and PMFREEDOSMEM for such requests.  PMFREEDOSMEM can also be used
  1528.   to find the amount of available DOS memory.
  1529.        The real-mode extended memory management procedures are GETMEM, FREEMEM,
  1530.   and RESETMEM.  GETMEM is used to allocate a block of extended memory.  FREEMEM
  1531.   may then be used to release the block.  RESETMEM releases all previously
  1532.   allocated blocks at once.  GETMEM may also be used to find the amount of
  1533.   available extended memory.
  1534.        The protected-mode memory management procedures are PMGETMEM, PMFREEMEM,
  1535.   and PMRESETMEM.  These procedures function exactly as the corresponding real-
  1536.   mode procedures:  GETMEM, FREEMEM, and RESETMEM.
  1537.        XLIB will seek extended memory through XMS only if it is present and if
  1538.   both DPMI and VCPI are absent.  If either protected-mode interface is present,
  1539.   then all extended memory will be allocated through the configured interface.
  1540.        XLIB will not use XMS to allocate memory from the high memory area (HMA)
  1541.   or from upper memory blocks (UMBs).  XLIB will however allocate from the HMA
  1542.   when it has full responsibility for extended memory management (DPMI, VCPI,
  1543.   and XMS are all absent).  XLIB never issues calls under the Expanded Memory
  1544.   Specification (EMS).
  1545.  
  1546.  
  1547.   Detailed Specifications
  1548.  
  1549.  
  1550.   PMGETDOSMEM (Protected Mode - Get DOS Memory)
  1551.   Purpose:  Allocate DOS memory block.
  1552.   CPU Mode:  Protected
  1553.   Registers at Call:  EAX = desired size of block in bytes.
  1554.   Return Registers:
  1555.      EAX = 0 if successful.  A block handle is returned in EBX.  The number of
  1556.   allocated bytes is returned in ECX.  The linear address of allocated block is
  1557.   returned in EDX.
  1558.      EAX = error code if unsuccessful.  AX = XLIB error code.  The high word of
  1559.   EAX (EAH) will be set to a DOS error code.  If DPMI is active, then EAH will
  1560.   be a DPMI error code (codes are supplied by DPMI .9 and up).
  1561.   Details:
  1562.      The block will always be paragraph aligned and will have size equal to an
  1563.   integer multiple of 16.
  1564.      Call with EAX = 0 to get largest available DOS memory block (not total free
  1565.   memory) in ECX (EAX, EBX, and EDX are preserved).
  1566.      If DPMI is active, then the handle is actually a selector with base address
  1567.   set to the linear address of the block.  If DPMI is not active, then the
  1568.   handle will be the segment of the block.
  1569.      In real mode, DOS memory may be allocated directly from DOS (INT 21H
  1570.   function 48H); however, this call will likely fail under DPMI protected mode.
  1571.  
  1572.  
  1573.  
  1574.  
  1575.                                         22
  1576.  
  1577.  
  1578.  
  1579.  
  1580.  
  1581.  
  1582.   PMFREEDOSMEM (Protected Mode - Free DOS Memory)
  1583.   Purpose:  Release previously allocated DOS memory block.
  1584.   CPU Mode:  Protected
  1585.   Registers at Call:  EAX = block handle.
  1586.   Return Registers:  EAX = 0 if successful; otherwise, EAX = error code.  AX =
  1587.   XLIB error.  The high word of EAX (EAH) will be a DOS error code.  If DPMI is
  1588.   active, then EAH will equal a DPMI error code (codes are supplied by DPMI .9
  1589.   and up).
  1590.   Details:  In real mode, DOS memory may be released directly by DOS (INT 21H
  1591.   function 49H); however, this call will likely fail under DPMI protected mode.
  1592.  
  1593.   GETMEM (Get Memory)
  1594.   Purpose:  Allocate extended memory block.
  1595.   CPU Mode:  Real
  1596.   Registers at Call:  EAX = desired size of block in bytes.
  1597.   Return Registers:
  1598.      EAX = 0 if successful.  A block handle is returned in EBX.  The number of
  1599.   allocated bytes is returned in ECX.  The logical address of allocated block is
  1600.   returned in EDX.
  1601.      EAX = error code if unsuccessful.  AX = XLIB error code.  If DPMI is
  1602.   active, then the high word of EAX (EAH) will be a DPMI 1.0 error code (if
  1603.   provided by host).  If XMS is active, then EAH = XMS error code.
  1604.   Details:
  1605.      The page size for extended memory allocations is contained in PAGESIZE.
  1606.   PAGESIZE is a DWORD in DSEG and should be read after initialization.  The
  1607.   blocks will have addresses that are PAGESIZE aligned and will have sizes equal
  1608.   to an integer multiple of PAGESIZE.  PAGESIZE will equal:  1024 for XMS, 4096
  1609.   for VCPI, 4096 for most DPMI hosts, and 16 in the absence of a memory manager.
  1610.      If XMS is present in conjunction with either DPMI or VCPI, no extended
  1611.   memory will be requested through XMS.  All extended memory will be requested
  1612.   through the active protected-mode interface.
  1613.      XMS is never used to allocate from the HMA or from UMBs.  XLIB will however
  1614.   allocate from the HMA in the absence of a memory management interface.
  1615.      Call with EAX = 0 to get largest available extended memory block (not total
  1616.   free memory) in ECX (EBX and EDX are preserved).  This call can also return
  1617.   with an error condition in EAX.
  1618.  
  1619.   FREEMEM (Free Memory)
  1620.   Purpose:  Release previously allocated extended memory block.
  1621.   CPU Mode:  Real
  1622.   Registers at Call:  EAX = block handle.
  1623.   Return Registers:  EAX = 0 if successful; otherwise, EAX = error code.  AX =
  1624.   XLIB error code.  If DPMI is active, then the high word of EAX (EAH) will be a
  1625.   DPMI 1.0 error code (if provided by host).  If XMS is active, then EAH = XMS
  1626.   error code.
  1627.   Details:  FREEMEM does not release page tables allocated under VCPI.  Call
  1628.   RESETMEM for this purpose.
  1629.  
  1630.   RESETMEM (Reset Memory)
  1631.   Purpose:  Release all previously allocated extended memory.
  1632.   CPU Mode:  Real
  1633.   Registers at Call:  None
  1634.   Return Registers:  EAX = 0 if successful; otherwise, EAX = error code.  AX =
  1635.   XLIB error code.  If DPMI is active, then the high word of EAX (EAH) will be a
  1636.  
  1637.  
  1638.  
  1639.                                         23
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.  
  1646.   DPMI 1.0 error code (if provided by host).  If XMS is active, then EAH = XMS
  1647.   error code.
  1648.   Details:
  1649.      GETMEM will automatically allocate page tables as needed under VCPI.
  1650.   RESETMEM will release such tables.
  1651.      If DPMI is not installed, then RESETMEM will be called upon execution of
  1652.   INT 21H function 4C (DOS termination).  DPMI hosts reset extended memory
  1653.   automatically.
  1654.  
  1655.   PMGETMEM  (Protected Mode - Get Memory)
  1656.   Purpose:  Allocate extended memory block while in protected mode.
  1657.   CPU Mode:  Protected
  1658.   Details:  This routine is the protected-mode version of GETMEM.  See GETMEM
  1659.   for specifications.
  1660.  
  1661.   PMFREEMEM  (Protected Mode - Free Memory)
  1662.   Purpose:  Free previously allocated extended memory block while in protected
  1663.   mode.
  1664.   CPU Mode:  Protected
  1665.   Details:  This routine is the protected-mode version of FREEMEM.  See FREEMEM
  1666.   for specifications.
  1667.  
  1668.   PMRESETMEM (Protected Mode - Reset Memory)
  1669.   Purpose:  Free all previously allocated extended memory while in protected
  1670.   mode.
  1671.   CPU Mode:  Protected
  1672.   Details:  This routine is the protected-mode version of RESETMEM.  See
  1673.   RESETMEM for specifications.
  1674.  
  1675.  
  1676.  
  1677.  
  1678.  
  1679.  
  1680.  
  1681.  
  1682.  
  1683.  
  1684.  
  1685.  
  1686.  
  1687.  
  1688.  
  1689.  
  1690.  
  1691.  
  1692.  
  1693.  
  1694.  
  1695.  
  1696.  
  1697.  
  1698.  
  1699.  
  1700.  
  1701.  
  1702.  
  1703.                                         24
  1704.  
  1705.  
  1706.  
  1707.  
  1708.  
  1709.  
  1710.                                 8. File Management
  1711.  
  1712.  
  1713.        XLIB file management procedures are low-level routines with powerful
  1714.   capabilities.  These routines can load files to extended memory or save
  1715.   extended memory to files.  They can read and write files either sequentially
  1716.   or randomly.
  1717.        All XLIB file management routines will receive and return values in a
  1718.   contiguous block of memory called a "file control block" (not to be confused
  1719.   with DOS file control blocks).  The file control block must be located in
  1720.   conventional memory and must have the form presented in Table 4.
  1721.  
  1722.  
  1723.   Table 4:  XLIB File Control Block Structure
  1724.   ------------------------------------------------------------------------------
  1725.   Field Name   Field Type        Field Description
  1726.   ----------   ----------        -----------------
  1727.   CONDCODE     DWORD             Condition code from file operation
  1728.   FNAME        BYTE[68]          File path and name (zero terminated string)
  1729.   FHANDLE      WORD              File handle assigned by DOS
  1730.   FPTRMODE     WORD              File pointer mode
  1731.   FPTR         DWORD             File pointer
  1732.   BLKADR       DWORD             Memory source/destination address
  1733.   BLKSIZE      DWORD             Size of transfer block in bytes
  1734.   BUFADR       DWORD             Buffer address (conventional memory address)
  1735.   BUFSIZE      WORD              Buffer size in bytes
  1736.   CONTROL      WORD              Control word
  1737.   ------------------------------------------------------------------------------
  1738.  
  1739.        CONDCODE is used to return error codes.  CONDCODE should be situated at
  1740.   the starting address of the control block.
  1741.        FNAME is a zero-terminated ASCII string defining the file path and name.
  1742.   There cannot be more than 67 characters in this string, excluding the
  1743.   termination character.
  1744.        BLKADR and BLKSIZE define the source/destination memory block for the
  1745.   transfer.  This block may be in either conventional or extended memory.
  1746.   BLKADR is a linear address.
  1747.        XLIB uses DOS to access the disk.  DOS cannot read or write to extended
  1748.   memory; consequently, a conventional memory buffer must be set up for the DOS
  1749.   transfers.  File management routines shift to protected mode to perform
  1750.   transfers between the buffer and the source/destination memory.  BUFADR and
  1751.   BUFSIZE define the conventional memory buffer.  BUFADR is a linear address.
  1752.        For fastest transfers, the memory block and the buffer should be DWORD
  1753.   aligned and should have sizes equal to an integer multiple of 16.
  1754.        FPTR and FPTRMODE specify the file pointer setting to be used before
  1755.   transfers to or from the disk.  FPTRMODE specifies how FPTR is to be
  1756.   interpreted.  The following values are valid for FPTRMODE:
  1757.  
  1758.  
  1759.   FPTRMODE    FPTR Interpretation
  1760.   0           Unsigned offset from the beginning of the file
  1761.   1           Signed offset from the current file pointer
  1762.   2           Signed offset from the end of the file
  1763.   3           FPTR is ignored.  Use current file pointer (sequential mode)
  1764.  
  1765.  
  1766.  
  1767.                                         25
  1768.  
  1769.  
  1770.  
  1771.  
  1772.  
  1773.  
  1774.  
  1775.  
  1776.        CONTROL is not used in the present version of XLIB.  Set all bits in
  1777.   CONTROL to zero.
  1778.        In assembly language or C, the file control block would typically be
  1779.   defined by a structure.  In BASIC, the file control block can be defined with
  1780.   a user defined type.
  1781.        Values are transferred to and from all file routines in EAX and in the
  1782.   file control block.  All routines should be called with the linear address of
  1783.   the file control block in EAX.  All routines return with two copies of the
  1784.   error code; one in EAX and one in the condition code of the file control
  1785.   block.  A zero error code indicates successful execution.
  1786.        Since these routines perform disk operations, special precautions should
  1787.   be taken to ensure that parameters in the file control block are properly
  1788.   defined before performing calls.  In particular, one should always make sure
  1789.   that the source/destination memory block and the conventional memory buffer
  1790.   are properly defined.  A safe rule is to simply set the buffer size to zero
  1791.   because this forces XLIB to supply a buffer when opening or creating the file.
  1792.  
  1793.  
  1794.   Detailed Specifications
  1795.  
  1796.  
  1797.   XCREATE (Create File)
  1798.   Purpose:  Create and open a new file of specified name in specified directory.
  1799.   CPU Mode:  Real
  1800.   Registers at Call:  EAX = linear address of file control block.
  1801.   Control Block at Call:  FNAME = file path and name.
  1802.   Return Registers:  EAX = error code.  AX = XLIB error code.  If a DOS error
  1803.   occurred, then the high word of EAX will be set to the DOS error code.
  1804.   Control Block at Return:  CONDCODE = error code.  If CONDCODE = 0, then
  1805.   FHANDLE = file handle assigned by DOS.  If the procedure is called with
  1806.   BUFSIZE = 0, then XLIB will set BUFADR and BUFSIZE to its own internal buffer.
  1807.   Details:
  1808.      If the file already exists, then it will be truncated to zero length.
  1809.      The size and location of the internal buffer will depend upon how XLIB was
  1810.   initialized.  If DPMI is active, then the buffer will be slightly larger than
  1811.   2K; otherwise, the buffer will be slightly larger than 1K.  The linear address
  1812.   and size of the buffer may be obtained from FILEBUFADR (DWORD), and
  1813.   FILEBUFSIZE (WORD) in DSEG.
  1814.      Files created by this routine will be given both read and write access.
  1815.      This routine uses INT 21H function 3CH to create the file.
  1816.  
  1817.   XOPEN (Open File)
  1818.   Purpose:  Open existing file of specified name in specified directory.
  1819.   CPU Mode:  Real
  1820.   Registers at Call:  EAX = linear address of file control block.
  1821.   Control Block at Call:  FNAME = file path and name.
  1822.   Return Registers:  EAX = error code.  AX = XLIB error code.  If a DOS error
  1823.   occurred, then the high word of EAX will be set to the DOS error code.
  1824.   Control Block at Return:  CONDCODE = error code.  If CONDCODE = 0, then
  1825.   FHANDLE = file handle assigned by DOS.  If the procedure is called with
  1826.   BUFSIZE = 0, then XLIB will set BUFADR and BUFSIZE to its own internal buffer.
  1827.   Details:
  1828.  
  1829.  
  1830.  
  1831.                                         26
  1832.  
  1833.  
  1834.  
  1835.  
  1836.  
  1837.  
  1838.      The file is opened for both read and write access.
  1839.      The size and location of the internal buffer will depend upon how XLIB was
  1840.   initialized.  If DPMI is active, then the buffer will be slightly larger than
  1841.   2K; otherwise, the buffer will be slightly larger than 1K.  The linear address
  1842.   and size of the buffer may be obtained from FILEBUFADR (DWORD), and
  1843.   FILEBUFSIZE (WORD) in DSEG.
  1844.      This routine uses INT 21H function 3DH to open the file.
  1845.  
  1846.   XCLOSE (Close File)
  1847.   Purpose:  Close previously opened file.
  1848.   CPU Mode:  Real
  1849.   Registers at Call:  EAX = linear address of file control block.
  1850.   Control Block at Call:  FHANDLE = file handle.
  1851.   Return Registers:  EAX = error code.  AX = XLIB error code.  If a DOS error
  1852.   occurred, then the high word of EAX will be set to the DOS error code.
  1853.   Control Block at Return:  CONDCODE = error code.
  1854.   Details:  This routine uses INT 21H function 3EH to close the file.
  1855.  
  1856.   XSAVE (Save File)
  1857.   Purpose:  Create file with contents equal to specified memory block.
  1858.   CPU Mode:  Real
  1859.   Registers at Call:  EAX = linear address of file control block.
  1860.   Control Block at Call:  FNAME = file path and name.  BLKADR/BLKSIZE = address
  1861.   and size of memory block to provide file contents.  BUFADR/BUFSIZE = address
  1862.   and size of conventional memory buffer.
  1863.   Return Registers:  EAX = error code.  AX = XLIB error code.  If a DOS error
  1864.   occurred, then the high word of EAX will be set to the DOS error code.
  1865.   Control Block at Return:  CONDCODE = error code.
  1866.   Details:
  1867.      The file cannot already be open.  The file is both created and closed by
  1868.   this routine.
  1869.      This routine will replace any previously existing file named FNAME.
  1870.      BLKADR/BLKSIZE may define a conventional memory block provided that this
  1871.   block is not overlapped by BUFADR/BUFSIZE.
  1872.      This routine transfers the source memory to the file through the buffer.
  1873.   Transfers from buffer to disk are accomplished with INT 21H function 40H.
  1874.  
  1875.   XLOAD (Load File)
  1876.   Purpose:  Load file contents to specified memory block.
  1877.   CPU Mode:  Real
  1878.   Registers at Call:  EAX = linear address of file control block.
  1879.   Control Block at Call:  FNAME = file path and name.  BLKADR/BLKSIZE = address
  1880.   and size of memory block to receive file contents.  BUFADR/BUFSIZE = address
  1881.   and size of conventional memory buffer.
  1882.   Return Registers:  EAX = error code.  AX = XLIB error code.  If a DOS error
  1883.   occurred, then the high word of EAX will be set to the DOS error code.
  1884.   Control Block at Return:  CONDCODE = error code.  If CONDCODE = 0, then
  1885.   BLKSIZE = actual number of bytes transferred.
  1886.   Details:
  1887.      The file cannot already be open.  The file is both opened and closed by
  1888.   this routine.
  1889.      The value of BLKSIZE as of call is interpreted as an upper limit on the
  1890.   number of bytes to transfer.  The entire file is loaded provided that it does
  1891.   not contain more than BLKSIZE bytes.
  1892.  
  1893.  
  1894.  
  1895.                                         27
  1896.  
  1897.  
  1898.  
  1899.  
  1900.  
  1901.  
  1902.      BLKADR/BLKSIZE may define a conventional memory block provided that this
  1903.   block is not overlapped by BUFADR/BUFSIZE.
  1904.      This routine uses INT 21H function 3FH to transfer the disk contents to the
  1905.   buffer.  It then transfers the buffer contents to the destination memory.
  1906.  
  1907.   XWRITE (Write to File)
  1908.   Purpose:  Write specified memory block to specified location in open file.
  1909.   CPU Mode:  Real
  1910.   Registers at Call:  EAX = linear address of file control block.
  1911.   Control Block at Call:  FHANDLE = file handle.  FPTR/FPTRMODE = file pointer
  1912.   setting for beginning of transfer.  BLKADR/BLKSIZE = address and size of
  1913.   memory block to provide file contents.  BUFADR/BUFSIZE = address and size of
  1914.   conventional memory buffer.
  1915.   Return Registers:  EAX = error code.  AX = XLIB error code.  If a DOS error
  1916.   occurred, then the high word of EAX will be set to the DOS error code.
  1917.   Control Block at Return:  CONDCODE = error code.
  1918.   Details:
  1919.      The file must be opened with XOPEN or XCREATE before using this routine.
  1920.      BLKADR/BLKSIZE may define a conventional memory block provided that this
  1921.   block is not overlapped by BUFADR/BUFSIZE.
  1922.      This routine uses INT 21H function 42H to set the file pointer.  The source
  1923.   memory is then transferred through the buffer to disk.  Transfers from buffer
  1924.   to disk are accomplished with INT 21H function 40H.
  1925.      Sequential transfers should set FPTRMODE = 3 for fastest execution.
  1926.  
  1927.   XREAD (Read From File)
  1928.   Purpose:  Write to specified memory block from specified location in open
  1929.   file.
  1930.   CPU Mode:  Real
  1931.   Registers at Call:  EAX = linear address of file control block.
  1932.   Control Block at Call:  FHANDLE = file handle.  FPTR/FPTRMODE = file pointer
  1933.   setting for beginning of transfer.  BLKADR/BLKSIZE = address and size of
  1934.   memory block to receive file contents.  BUFADR/BUFSIZE = address and size of
  1935.   conventional memory buffer.
  1936.   Return Registers:  EAX = error code.  AX = XLIB error code.  If a DOS error
  1937.   occurred, then the high word of EAX will be set to the DOS error code.
  1938.   Control Block at Return:  CONDCODE = error code.
  1939.   Details:
  1940.      The file must be opened with XOPEN or XCREATE before using this routine.
  1941.      BLKADR/BLKSIZE may define a conventional memory block provided that this
  1942.   block is not overlapped by BUFADR/BUFSIZE.
  1943.      This routine uses INT 21H function 42H to set the file pointer.  The file
  1944.   contents are then transferred to the destination memory through the buffer.
  1945.   The file contents are transferred to the buffer using INT 21H function 3FH.
  1946.      Sequential transfers should set FPTRMODE = 3 for fastest execution.
  1947.  
  1948.  
  1949.  
  1950.  
  1951.  
  1952.  
  1953.  
  1954.  
  1955.  
  1956.  
  1957.  
  1958.  
  1959.                                         28
  1960.  
  1961.  
  1962.  
  1963.  
  1964.  
  1965.  
  1966.                              9. Descriptor Management
  1967.  
  1968.  
  1969.        All selectors in Table 1 up to DGROUPSEL are used by XLIB procedures;
  1970.   consequently, the corresponding descriptors should never be changed.  However,
  1971.   descriptors for the other selectors may be modified.  XLIB includes a
  1972.   procedure called SETDESC (set descriptor) to facilitate such modifications.  A
  1973.   second routine called PMSETDESC is the protected-mode version of SETDESC.
  1974.        Descriptors corresponding to the inline selectors should not be changed
  1975.   in programs which also use the inline mode-switch procedures.
  1976.  
  1977.  
  1978.   Detailed Specifications
  1979.  
  1980.  
  1981.   SETDESC (Set Descriptor)
  1982.   Purpose:  Change a descriptor in the local descriptor table.
  1983.   CPU Mode:  Real
  1984.   Registers at Call:  BX = selector.  EDX:EAX = the new descriptor.
  1985.   Return Registers:  EAX = error code.  AX = XLIB error code.  If DPMI is
  1986.   installed, then the high word of EAX will equal a DPMI 1.0 error code (if
  1987.   provided by host).  EDX may be returned with some modifications to the access
  1988.   rights bits.
  1989.   Details:
  1990.      The access rights bits in EDX will be edited before installation of the
  1991.   descriptor.  In particular:  The application bit will be set to indicate an
  1992.   application segment (rather than a system segment).  Reserved bits will be
  1993.   given proper settings.  The descriptor privilege level will be set to the
  1994.   appropriate value.  If the descriptor corresponds to a code segment, then the
  1995.   descriptor will also be marked as readable and nonconforming.
  1996.      Segment registers which are loaded with the current value of the descriptor
  1997.   will not necessarily be reloaded when the descriptor is changed.
  1998.  
  1999.   PMSETDESC (Protected Mode - Set Descriptor)
  2000.   Purpose:  Change a descriptor in the local descriptor table.
  2001.   CPU Mode:  Protected
  2002.   Details:  This routine is the protected-mode version of SETDESC.  See SETDESC
  2003.   for details.
  2004.  
  2005.  
  2006.  
  2007.  
  2008.  
  2009.  
  2010.  
  2011.  
  2012.  
  2013.  
  2014.  
  2015.  
  2016.  
  2017.  
  2018.  
  2019.  
  2020.  
  2021.  
  2022.  
  2023.                                         29
  2024.  
  2025.  
  2026.  
  2027.  
  2028.  
  2029.  
  2030.                  10. Using XLIB in High-Level Language Libraries
  2031.  
  2032.  
  2033.        The following program illustrates the usage of XLIB in libraries called
  2034.   from Microsoft BASIC 7.0.  The library contains a protected-mode procedure
  2035.   which sums the elements in a single precision array created within BASIC.  The
  2036.   general methodology here is recommended for developing assembly language
  2037.   libraries.
  2038.        Since BASIC cannot call a 32-bit segment, a real-mode interface procedure
  2039.   must be placed in a 16-bit segment to receive the BASIC call and then transfer
  2040.   execution to 32-bit protected mode.  The interface procedure is call SUMARRAY
  2041.   while the 32-bit protected-mode procedure which actually sums the array
  2042.   elements is called SUMARRAY32.
  2043.        BASIC must pass certain arguments to the library procedures.  These
  2044.   include the array address and the number of elements to be summed.  These
  2045.   arguments could be passed on the stack; however, such approach proves awkward
  2046.   since the stack must be changed when entering protected mode.  Consequently,
  2047.   BASIC places all arguments in a contiguous block of memory called a "control
  2048.   block," and then passes only the address of the control block to the library.
  2049.   BASIC constructs the control block with a user-defined type.
  2050.        The first four bytes of the control block are reserved for placement of
  2051.   error codes by the library procedures.  Also, the address of the control block
  2052.   is placed in CCODEPTR so that any FPU exceptions will be signalled in the
  2053.   error code location as well.  Were an FPU exception to occur, the FPU
  2054.   interrupt handler will immediately transfer control to EXITPM which will shift
  2055.   to real-mode, restore registers, and return control to the BASIC caller.
  2056.        The library also contains a real-mode function called LINADR which may be
  2057.   called by BASIC to convert segment addresses to linear addresses.
  2058.        An example of this same program for Microsoft C 7.0 is included in
  2059.   Appendix E.
  2060.  
  2061.  
  2062.   Example 3:  Calling Protected-Mode Libraries From BASIC
  2063.   -----------------------------------------------------------------------------
  2064.                             +++++++++++++++++++++++++
  2065.                             + ASSEMBLY CODE LIBRARY +
  2066.                             +++++++++++++++++++++++++
  2067.  
  2068.  
  2069.   ;The following library should be combined with XLIB.LIB using the Microsoft
  2070.   ;LINK and LIB utilities.  If BASIC is to be executed from the QBX
  2071.   ;environment, then a quick library must be loaded with the environment.  See
  2072.   ;BASIC documentation for instructions.
  2073.  
  2074.  
  2075.                  .MODEL         LARGE,PASCAL
  2076.                  .386P
  2077.  
  2078.                  INCLUDE        XLIB.INC
  2079.  
  2080.  
  2081.  
  2082.  
  2083.  
  2084.  
  2085.  
  2086.  
  2087.                                         30
  2088.  
  2089.  
  2090.  
  2091.  
  2092.  
  2093.  
  2094.   CSEG           SEGMENT PARA PUBLIC USE16 'CODE'
  2095.                  ASSUME CS:CSEG, DS:DSEG
  2096.  
  2097.   ;Function to calculate linear address from segment address on stack.
  2098.   ;Returns linear address in DX:AX.
  2099.   LINADR         PROC FAR PUBLIC,
  2100.                  SEGADR:DWORD                  ;Segment address of variable
  2101.                  XOR            EAX,EAX        ;Clear high words
  2102.                  XOR            EDX,EDX
  2103.                  MOV            AX,WORD PTR SEGADR[0]
  2104.                  MOV            DX,WORD PTR SEGADR[2]
  2105.                  SHL            EDX,4          ;Calculate linear address
  2106.                  ADD            EDX,EAX
  2107.                  MOV            AX,DX
  2108.                  SHR            EDX,16         ;Return linear address in DX:AX
  2109.                  RET
  2110.   LINADR         ENDP
  2111.  
  2112.   ;Structure defining control block for SUMARRAY.
  2113.   ARRAYDATA      STRUCT
  2114.     CONDCODE     DWORD          0              ;Condition code
  2115.     N            DWORD          0              ;Number of elements to sum
  2116.     ADDRESS      DWORD          0              ;Address of first element
  2117.     SUM          DWORD          0              ;Sum of array elements
  2118.   ARRAYDATA      ENDS
  2119.  
  2120.   ;Real-mode interface to SUMARRAY32.  Segment address of control block having
  2121.   ;structure ARRAYDATA should be on the stack.
  2122.   SUMARRAY       PROC FAR PUBLIC,
  2123.                  CBSEGADR:DWORD                ;Control block segment address
  2124.                  PUSH           DS
  2125.                  PUSHW          DSEG
  2126.                  POP            DS
  2127.                  XOR            EAX,EAX        ;Clear high words
  2128.                  XOR            EDX,EDX
  2129.                  MOV            AX,WORD PTR CBSEGADR[2]
  2130.                  MOV            DX,WORD PTR CBSEGADR[0]
  2131.                  SHL            EAX,4          ;Calculate linear address
  2132.                  ADD            EAX,EDX
  2133.                  MOV            CCODEPTR,EAX   ;Reset condition code address
  2134.                  POP            DS             ;Pop calling DS
  2135.                  PUSHD          OFFSET SUMARRAY32
  2136.                  CALL           ENTERPM        ;Execute SUMARRAY32 in protected
  2137.                  RET
  2138.   SUMARRAY       ENDP
  2139.  
  2140.   CSEG           ENDS
  2141.  
  2142.  
  2143.  
  2144.  
  2145.  
  2146.  
  2147.  
  2148.  
  2149.  
  2150.  
  2151.                                         31
  2152.  
  2153.  
  2154.  
  2155.  
  2156.  
  2157.  
  2158.   TSEG           SEGMENT PARA PUBLIC USE32 'CODE'
  2159.                  ASSUME CS:TSEG, SS:TSEG, DS:TSEG, ES:TSEG, FS:DSEG, GS:DGROUP
  2160.  
  2161.   ;Sum the elements of a single precision array.  Array parameters are stored
  2162.   ;in a control block having structure of ARRAYDATA.  The linear address of the
  2163.   ;control block is stored at CCODEPTR.  An error code of -1 is returned in the
  2164.   ;condition code of the control block if the number of array elements is zero.
  2165.   ;XLIB places an error code in the control block if an FPU exception occurs
  2166.   ;while calculating the sum.  This error code will have the FPU status word in
  2167.   ;the high word and the XLIB FPU error code in the low word.  Observe that this
  2168.   ;routine will be called with DS = FLATDSEL (flat-model data descriptor) and
  2169.   ;FS = DSEGSEL (DSEG data descriptor).
  2170.   SUMARRAY32     PROC NEAR
  2171.                  MOV            EBX,FS:CCODEPTR               ;Get control block
  2172.                  MOV            EDX,ARRAYDATA.ADDRESS[EBX]    ;Get array address
  2173.                  MOV            ESI,ARRAYDATA.N[EBX]          ;Get N
  2174.                  SUB            ESI,1
  2175.                  JB             NODATA                        ;Error:  N = 0
  2176.                  FLDZ                                         ;Initialize sum
  2177.   SUMLOOP:       FADD           DWORD PTR [EDX+4*ESI]
  2178.                  SUB            ESI,1
  2179.                  JAE            SUMLOOP
  2180.                  FSTP           ARRAYDATA.SUM[EBX]            ;Save sum
  2181.                  RET
  2182.   NODATA:        MOV            ARRAYDATA.CONDCODE[EBX],-1    ;Record error code
  2183.                  RET
  2184.   SUMARRAY32     ENDP
  2185.  
  2186.   TSEG           ENDS
  2187.                  END
  2188.  
  2189.  
  2190.                               +++++++++++++++++++++
  2191.                               + BASIC MAIN MODULE +
  2192.                               +++++++++++++++++++++
  2193.  
  2194.  
  2195.   'The following Microsoft BASIC 7.0 program should be linked with the above
  2196.   'library.  The BASIC program first initializes XLIB.  Next, it creates a
  2197.   'single precision array.  A control block for SUMARRAY is then constructed
  2198.   'and the call to SUMARRAY is executed.  Finally, the condition code in the
  2199.   'control block is inspected and results are printed.
  2200.  
  2201.   DEFINT A-Z
  2202.  
  2203.   'Declare XLIB procedures
  2204.   DECLARE FUNCTION XLIBMEMREQ& ()
  2205.   DECLARE FUNCTION INITXLIB& ()
  2206.   DECLARE FUNCTION XLIBCONFIG% ()
  2207.  
  2208.   'Declare procedures in the library linked with XLIB
  2209.   DECLARE FUNCTION LINADR& (SEG VARIABLE AS ANY)
  2210.   DECLARE SUB SUMARRAY (SEG VARIABLE AS ANY)
  2211.  
  2212.  
  2213.  
  2214.  
  2215.                                         32
  2216.  
  2217.  
  2218.  
  2219.  
  2220.  
  2221.  
  2222.   'Structure for the control block
  2223.   TYPE ARRAYDATA
  2224.     CONDCODE AS LONG      'Location to receive any error codes
  2225.     N AS LONG             'Number of elements to be summed
  2226.     ADDRESS AS LONG       'Linear address of the array
  2227.     SUM AS SINGLE         'Location for array sum
  2228.   END TYPE
  2229.  
  2230.   'Check XLIBCONFIG to see if XLIB has already been initialized.  If not then
  2231.   'call XLIBMEMREQ to find amount of conventional memory needed by XLIB and
  2232.   'release at least this amount with the BASIC SETMEM function.  XLIBMEMREQ
  2233.   'returns with sign bit of DX set if an error occurred.  The error is then
  2234.   'identified by AX.  XLIB will not be terminated upon completion of this
  2235.   'program in the Microsoft QBX environment; therefore, initialization is
  2236.   'required only once within the environment.
  2237.   IF XLIBCONFIG = 0 THEN
  2238.     TEMP& = XLIBMEMREQ
  2239.     IF TEMP& >= 0& THEN
  2240.       IF TEMP& > 0 THEN TEMP& = SETMEM(-TEMP& - 16&)
  2241.       TEMP& = INITXLIB                 'INITXLIB error code returned in TEMP&
  2242.     ELSE
  2243.       TEMP& = TEMP& AND &H7FFFFFFF     'Mask sign bit to leave error code only
  2244.     END IF
  2245.     IF TEMP& THEN
  2246.       PRINT "Library initialization error:  "; HEX$(TEMP&)
  2247.       END
  2248.     END IF
  2249.   END IF
  2250.  
  2251.   DIM A(100) AS SINGLE
  2252.   DIM AD AS ARRAYDATA
  2253.  
  2254.   FOR I = 0 TO 100            'Assign numbers to array
  2255.     A(I) = I
  2256.   NEXT I
  2257.  
  2258.   AD.CONDCODE = 0&            'Clear the error code
  2259.   AD.N = 50&                  'Sum first 50 elements
  2260.   AD.ADDRESS = LINADR(A(0))   'Calculate and record linear address of A(0)
  2261.  
  2262.   CALL SUMARRAY(AD)
  2263.  
  2264.   IF AD.CONDCODE THEN
  2265.     PRINT "Error: ";HEX$(AD.CONDCODE)
  2266.   ELSE
  2267.     PRINT "Sum: ";AD.SUM      'Should equal 1225
  2268.   ENDIF
  2269.  
  2270.   END
  2271.   -----------------------------------------------------------------------------
  2272.  
  2273.  
  2274.  
  2275.  
  2276.  
  2277.  
  2278.  
  2279.                                         33
  2280.  
  2281.  
  2282.  
  2283.  
  2284.  
  2285.  
  2286.                    Appendix A: Description of XLIB Public Data
  2287.  
  2288.  
  2289.        The following is a summary of most public symbols located in the XLIB
  2290.   data segment DSEG.  This summary excludes the symbols presented in tables one
  2291.   through three.  All XLIB symbols conform to the PASCAL naming convention.
  2292.  
  2293.   Symbol:  CALL32PTR (CALL32 Pointer)
  2294.   Symbol Type:  DWORD
  2295.   Default Setting:  Far 16-bit protected-mode address of CALL32 procedure
  2296.   Description:  This location is a pointer to the CALL32 procedure and is
  2297.   included to facilitate intersegment calls.  The contents of the location
  2298.   should not be changed.
  2299.  
  2300.   Symbols:  CCODEPTR/CCODE (Condition Code Pointer/Condition Code)
  2301.   Symbol Types:  DWORD/DWORD
  2302.   Default Settings:  CCODEPTR = linear address of CCODE.  CCODE = 0.
  2303.   Descriptions:  XLIB interrupt handlers will place flags in the condition code
  2304.   to signal the occurrence of the interrupt.  Flags are placed for FPU
  2305.   exceptions and hot key presses.  CCODEPTR initially contains the linear
  2306.   address of CCODE.  CCODEPTR may be changed by the user, but must point to a
  2307.   DWORD in conventional memory.  The user is responsible for initializing the
  2308.   condition code.
  2309.  
  2310.   Symbols:  CSEGVAL, TSEGVAL, DSEGVAL, DGROUPVAL (Segment Values)
  2311.   Symbol Types:  WORD
  2312.   Default Settings:  CSEG, TSEG, DSEG, DGROUP
  2313.   Descriptions:  These are memory locations initialized to the respective
  2314.   segment values.  Code in TSEG should not contain segment constants since DOS
  2315.   may not be able to handle them in relocation edits.  Read these locations to
  2316.   get segment values.  User segments should be handled the same way.  These
  2317.   locations should not be changed.
  2318.  
  2319.   Symbols:  FILEBUFADR/FILEBUFSIZE (File Buffer Specifications)
  2320.   Symbol Types:  DWORD/WORD
  2321.   Default Settings:  Varies with operating environment
  2322.   Descriptions:  FILEBUFADR contains the linear address of the internal file
  2323.   buffer in XLIB.  FILEBUFSIZE contains the size of the buffer in bytes.  This
  2324.   buffer is used only by the file management routines.  The size and location of
  2325.   the buffer will depend upon the operating environment.  If DPMI is active,
  2326.   then the buffer will be slightly larger than 2K; it is otherwise slightly
  2327.   larger than 1K.  These location should be read only after initialization.
  2328.  
  2329.   Symbol:  FPUCW (Floating Point Unit Control Word)
  2330.   Symbol Type:  WORD
  2331.   Default Setting:  0332H
  2332.   Description:  FPUCW is optionally loaded to the FPU control word by CALLPM and
  2333.   ENTERPM.  The default sets rounding control to nearest, precision control to
  2334.   64 bits, and unmasks exceptions for overflow, zero divide, and invalid
  2335.   operations.  Exceptions for underflow, precision, and denormalized operations
  2336.   are masked, and are therefore handled internally by the FPU.  Set bit 2 of
  2337.   OFLAGS to enable FPU save/restore and load of FPUCW.
  2338.  
  2339.  
  2340.  
  2341.  
  2342.  
  2343.                                         34
  2344.  
  2345.  
  2346.  
  2347.  
  2348.  
  2349.  
  2350.   Symbol:  HOTKEY (Hot Key)
  2351.   Symbol Type:  WORD
  2352.   Default Setting:  0H
  2353.   Description:  HOTKEY specifies the hot key for the keyboard interrupt handler.
  2354.   The low byte of HOTKEY specifies the scan code for the key.  The upper byte
  2355.   specifies the state of the shift keys.  Bit 8 specifies SHIFT; bit 9 specifies
  2356.   CTLR, and bit 10 specifies ALT.  Set bits mean that the designated key must be
  2357.   pressed.  All other bits are ignored.  When the hot key is pressed, the XLIB
  2358.   keyboard interrupt handler will record the hot key flag at the DWORD whose
  2359.   linear address is stored at CCODEPTR.  The default setting for HOTKEY is 0.
  2360.   This setting effectively disables hot key detection since no key has a zero
  2361.   scan code.
  2362.  
  2363.   Symbol:  IFLAGS (Initialization Flags)
  2364.   Symbol Type:  WORD
  2365.   Default Setting:  0
  2366.   Description:  IFLAGS is used by INITXLIB to control the initialization
  2367.   process.  Bit 0 of IFLAGS determines DPMI/VCPI priority in the event that both
  2368.   interfaces are present.  If this bit is clear then DPMI will be installed in
  2369.   such cases.  The other bits are reserved.
  2370.  
  2371.   Symbol:  INLINERMPTR (INLINERM Pointer)
  2372.   Symbol Type:  DWORD
  2373.   Default Setting:  Far 16-bit protected-mode address of INLINERM procedure
  2374.   Description:  This location is a pointer to the INLINERM procedure and is
  2375.   included to facilitate intersegment calls.  The contents of the location
  2376.   should not be changed.
  2377.  
  2378.   Symbols:  IRQ0INTNO/IRQ8INTNO (IRQ X Interrupt Number)
  2379.   Symbol Types:  BYTE/BYTE
  2380.   Default Settings:  Varies with operating environment
  2381.   Descriptions:  Specifies the interrupt number assigned to IRQ X.  IRQs 0
  2382.   through 7 and IRQs 8 through 15 are assigned to contiguous interrupt numbers.
  2383.   These locations are valid only after call to INITXLIB.  Typically, IRQ 0 is
  2384.   assigned to interrupt 8, and IRQ 8 is assigned to interrupt 70H; however,
  2385.   these assignments may have been changed by system software.
  2386.  
  2387.   Symbol:  OFLAGS (Operation Flags)
  2388.   Symbol Type:  WORD
  2389.   Default Setting:  Varies with operating environment
  2390.   Description:  OFLAGS controls post-initialization operation of XLIB.
  2391.      Setting bit 0 enables XLIB hardware interrupt handlers.  These handlers
  2392.   will continue to receive interrupts but will always cascade them when the bit
  2393.   is clear.  XLIB sets this bit only at calls to CALLPM and ENTERPM and then
  2394.   clears the bit upon return.  When the bit is clear, hot key detection is
  2395.   disabled, and the XLIB FPU interrupt handler is disabled.
  2396.      Setting bit 1 causes all FPU interrupts to be cascaded to the inherited
  2397.   real-mode interrupt handler.  This bit is initialized by INITXLIB.  It is set
  2398.   if no FPU is present; it is otherwise cleared.
  2399.      Setting bit 2 enables FPU save/restore in CALLPM and ENTERPM.  Setting this
  2400.   bit also causes load of FPUCW to the FPU control word.  The bit is clear by
  2401.   default.
  2402.  
  2403.  
  2404.  
  2405.  
  2406.  
  2407.                                         35
  2408.  
  2409.  
  2410.  
  2411.  
  2412.  
  2413.  
  2414.   Symbol:  PAGESIZE (Page Size)
  2415.   Symbol Type:  DWORD
  2416.   Default Setting:  Varies with operating environment
  2417.   Description:  This memory location contains the minimum unit (in bytes) for
  2418.   extended memory allocation.  PAGESIZE is initialized by INITXLIB.  It will
  2419.   contain 4096 for VCPI, 1024 for XMS, and 16 for clean configurations.  Values
  2420.   can vary under DPMI but will typically equal 4096.  Extended memory requests
  2421.   are rounded up to the nearest integer multiple of PAGESIZE.  Extended memory
  2422.   blocks will be PAGESIZE aligned.
  2423.  
  2424.   Symbols:  PMDS, PMES, PMFS, PMGS (Protected-Mode Segments)
  2425.   Symbol Types:  WORD
  2426.   Default Settings:  FLATDSEL, TSEGDSEL, DSEGSEL, DGROUPSEL
  2427.   Descriptions:  These memory locations are loaded to data segment registers by
  2428.   CALLPM and ENTERPM before transferring control to the protected-mode target.
  2429.   These locations are respectively loaded to DS, ES, FS, and GS.  The contents
  2430.   of these locations may be changed to any legal selectors after the call to
  2431.   INITXLIB.
  2432.  
  2433.   Symbols:  RMDS, RMES (Real-Mode Segments)
  2434.   Symbol Types:  WORD
  2435.   Default Settings:  DGROUP, DSEG
  2436.   Descriptions:  These memory locations are loaded to data segment registers by
  2437.   CALLRM before transferring control to the real-mode target.  These locations
  2438.   are respectively loaded to DS and ES.  The contents of these locations may be
  2439.   changed if desired.
  2440.  
  2441.  
  2442.  
  2443.  
  2444.  
  2445.  
  2446.  
  2447.  
  2448.  
  2449.  
  2450.  
  2451.  
  2452.  
  2453.  
  2454.  
  2455.  
  2456.  
  2457.  
  2458.  
  2459.  
  2460.  
  2461.  
  2462.  
  2463.  
  2464.  
  2465.  
  2466.  
  2467.  
  2468.  
  2469.  
  2470.  
  2471.                                         36
  2472.  
  2473.  
  2474.  
  2475.  
  2476.  
  2477.  
  2478.                            Appendix B: XLIB Error Codes
  2479.  
  2480.  
  2481.        XLIB error codes are always returned in AX.  In many cases, the high word
  2482.   of EAX will be returned with specific information about the error, such as
  2483.   XMS, DPMI, or DOS error codes.
  2484.        Although error codes are not provided in the DPMI .9 specification, many
  2485.   DPMI .9 hosts do return DPMI 1.0 error codes.  DPMI 1.0 error codes may in
  2486.   fact be DOS error codes returned to the DPMI host by DOS.  If the sign bit
  2487.   (bit 15) of the error code is clear, then the error code was issued by DOS.
  2488.  
  2489.  
  2490.   Condition Code Flags
  2491.   01H     FPU exception
  2492.   02H     Hot key pressed
  2493.  
  2494.   General Errors
  2495.   10H     Interface not available
  2496.   11H     Unable to identify operating environment
  2497.   12H     DOS memory allocation failure
  2498.   13H     DOS memory release error
  2499.   14H     Failed to enable A20
  2500.   15H     Insufficient logical address space
  2501.   16H     Insufficient number of extended memory block handles
  2502.   17H     Bad extended memory block handle
  2503.   18H     Bad selector
  2504.   19H     Unable to create file
  2505.   1AH     Unable to open file
  2506.   1BH     Unable to read file
  2507.   1CH     Unable to write file
  2508.   1DH     Unable to set file pointer
  2509.   1EH     Unable to close file
  2510.   1FH     Disk full
  2511.  
  2512.   Errors Occurring Under DPMI (See Appendix C for codes returned by DPMI)
  2513.   20H     Protected mode initialization failure
  2514.   21H     Descriptor allocation error
  2515.   22H     Descriptor installation error
  2516.   23H     Unable to switch protected mode interrupt vector
  2517.   24H     Insufficient extended memory error
  2518.   25H     Extended memory allocation error
  2519.   26H     Extended memory release error
  2520.   27H     DPMI DOS memory allocation error
  2521.   28H     DPMI DOS memory release error
  2522.   29H     Unable to set descriptor base address
  2523.  
  2524.   Errors Occurring Under XMS (See Appendix D for codes returned by XMS)
  2525.   30H     Unable to measure available extended memory
  2526.   31H     Insufficient extended memory error
  2527.   32H     Extended memory allocation error
  2528.   33H     Unable to lock extended memory
  2529.   34H     Unable to unlock extended memory
  2530.   35H     Extended memory release error
  2531.  
  2532.  
  2533.  
  2534.  
  2535.                                         37
  2536.  
  2537.  
  2538.  
  2539.  
  2540.  
  2541.  
  2542.   Errors Occurring Under VCPI
  2543.   40H     Error in determining protected mode entry point
  2544.   41H     Unable to determine physical address of DOS memory page
  2545.   42H     Unable to determine hardware interrupt mappings
  2546.   43H     Insufficient extended memory error
  2547.   44H     Unable to determine number of free extended memory pages
  2548.   45H     Extended memory allocation error
  2549.   46H     Extended memory release error
  2550.  
  2551.  
  2552.  
  2553.  
  2554.  
  2555.  
  2556.  
  2557.  
  2558.  
  2559.  
  2560.  
  2561.  
  2562.  
  2563.  
  2564.  
  2565.  
  2566.  
  2567.  
  2568.  
  2569.  
  2570.  
  2571.  
  2572.  
  2573.  
  2574.  
  2575.  
  2576.  
  2577.  
  2578.  
  2579.  
  2580.  
  2581.  
  2582.  
  2583.  
  2584.  
  2585.  
  2586.  
  2587.  
  2588.  
  2589.  
  2590.  
  2591.  
  2592.  
  2593.  
  2594.  
  2595.  
  2596.  
  2597.  
  2598.  
  2599.                                         38
  2600.  
  2601.  
  2602.  
  2603.  
  2604.  
  2605.  
  2606.                          Appendix C: DPMI 1.0 Error Codes
  2607.  
  2608.  
  2609.        DPMI 1.0 error codes may in fact be DOS error codes returned to the DPMI
  2610.   host by DOS.  If the sign bit (bit 15) of the error code is clear, then the
  2611.   error code was issued by DOS.
  2612.  
  2613.  
  2614.   Number   Explanation
  2615.   8001H    Unsupported function
  2616.   8002H    Invalid state for requested operation
  2617.   8003H    System integrity would be endangered
  2618.   8004H    Deadlock situation detected by host
  2619.   8005H    Serialization request cancelled
  2620.   8010H    Resource unavailable
  2621.   8011H    Host unable to allocate descriptor
  2622.   8012H    Linear memory unavailable
  2623.   8013H    Physical memory unavailable
  2624.   8014H    Backing store unavailable
  2625.   8015H    Callback specifications cannot be allocated
  2626.   8016H    Cannot allocate handle
  2627.   8017H    Lock count limits exceeded
  2628.   8018H    Resource owned exclusively by another client
  2629.   8019H    Resource already shared by another client
  2630.   8021H    Invalid value
  2631.   8022H    Invalid selector
  2632.   8023H    Invalid handle
  2633.   8024H    Invalid callback
  2634.   8025H    Invalid linear address
  2635.   8026H    Request not supported by hardware
  2636.  
  2637.  
  2638.  
  2639.  
  2640.  
  2641.  
  2642.  
  2643.  
  2644.  
  2645.  
  2646.  
  2647.  
  2648.  
  2649.  
  2650.  
  2651.  
  2652.  
  2653.  
  2654.  
  2655.  
  2656.  
  2657.  
  2658.  
  2659.  
  2660.  
  2661.  
  2662.  
  2663.                                         39
  2664.  
  2665.  
  2666.  
  2667.  
  2668.  
  2669.  
  2670.                            Appendix D: XMS Error Codes
  2671.  
  2672.  
  2673.   Number   Explanation
  2674.   80H      Function not implemented
  2675.   81H      VDISK was detected
  2676.   82H      An A20 error occurred
  2677.   8EH      General driver error
  2678.   8FH      Unrecoverable driver error
  2679.   90H      HMA does not exist
  2680.   91H      HMA is already in use
  2681.   92H      Attempt to allocate less than HMAMIN of HMA
  2682.   93H      HMA is not allocated
  2683.   94H      A20 is still enabled
  2684.   A0H      All extended memory is allocated
  2685.   A1H      All available handles are allocated
  2686.   A2H      Invalid handle
  2687.   A3H      Source handle is invalid
  2688.   A4H      Source offset is invalid
  2689.   A5H      Destination handle is invalid
  2690.   A6H      Destination offset is invalid
  2691.   A7H      Length is invalid
  2692.   A8H      Move has an invalid overlap
  2693.   A9H      Parity error
  2694.   AAH      Block is not locked
  2695.   ABH      Block is locked
  2696.   ACH      Block lock count overflow
  2697.   ADH      Lock failed
  2698.   B0H      Only a smaller upper memory block (UMB) is available
  2699.   B1H      No UMB's are available
  2700.   B2H      UMB segment number is invalid
  2701.  
  2702.  
  2703.  
  2704.  
  2705.  
  2706.  
  2707.  
  2708.  
  2709.  
  2710.  
  2711.  
  2712.  
  2713.  
  2714.  
  2715.  
  2716.  
  2717.  
  2718.  
  2719.  
  2720.  
  2721.  
  2722.  
  2723.  
  2724.  
  2725.  
  2726.  
  2727.                                         40
  2728.  
  2729.  
  2730.  
  2731.  
  2732.  
  2733.  
  2734.                Appendix E: Calling Protected-Mode Libraries From C
  2735.  
  2736.  
  2737.        This appendix contains a C version of the BASIC program presented in
  2738.   Example 3.  Microsoft C version 7.0 is used to create a float array.  C then
  2739.   calls a protected-mode assembly language procedure in a library to sum the
  2740.   elements of the array.  The assembly language library is presented in Example
  2741.   3.  C calls a 16-bit procedure called SUMARRAY  This procedure then transfers
  2742.   control to a 32-bit protected-mode procedure called SUMARRAY32.  The latter
  2743.   procedure performs the actual calculations.  Parameters defining the array are
  2744.   placed in a contiguous block of memory defined by a C structure.  C passes the
  2745.   address of this structure to the library.  The first four bytes in the
  2746.   structure are reserved for error codes.  The linear address of the structure
  2747.   is placed in CCODEPTR so that any FPU exceptions will be recorded by the FPU
  2748.   interrupt handler in the error code location.  The SUMARRAY32 procedure will
  2749.   also record an error if the parameter defining the number of elements to be
  2750.   summed is zero.
  2751.        The C code is somewhat simpler than the corresponding BASIC code because
  2752.   DOS memory does not have to be released prior to calling INITXLIB.  This
  2753.   follows because C does not claim all DOS memory as does BASIC.
  2754.        C is more powerful than BASIC in that it can access data under external
  2755.   symbols whereas BASIC cannot.  Access to XLIB public data is made possible in
  2756.   C by including the header file called XLIB.H.  This file makes all XLIB public
  2757.   data and public real-mode procedures visible to C.  It also contains
  2758.   declarations which adapt the PASCAL conventions of XLIB.
  2759.  
  2760.  
  2761.   -----------------------------------------------------------------------------
  2762.                               +++++++++++++++++++++
  2763.                               +   C MAIN MODULE   +
  2764.                               +++++++++++++++++++++
  2765.  
  2766.  
  2767.   /* The following Microsoft C 7.0 program should be linked with the assembly
  2768.   language library in Example 3.  Combine the library with XLIB.LIB using the
  2769.   Microsoft LIB utility.  The C program first initializes XLIB.  Next, it
  2770.   creates a float array.  A control block for SUMARRAY is then constructed
  2771.   and the call to SUMARRAY is executed.  Finally, the condition code in the
  2772.   control block is inspected and results are printed. */
  2773.  
  2774.   #include <stdio.h>
  2775.   #include <xlib.h>
  2776.  
  2777.   extern long __far __pascal LINADR(void __far *ptr);
  2778.   extern void __far __pascal SUMARRAY(void __far *ptr);
  2779.  
  2780.   struct arraydata
  2781.   {
  2782.     long condcode;
  2783.     long n;
  2784.     long address;
  2785.     float sum;
  2786.   } ad;
  2787.  
  2788.  
  2789.  
  2790.  
  2791.                                         41
  2792.  
  2793.  
  2794.  
  2795.  
  2796.  
  2797.  
  2798.   float a[101];
  2799.  
  2800.   main()
  2801.   {
  2802.     int i;
  2803.     long temp;
  2804.  
  2805.     temp = INITXLIB();
  2806.     if (temp != 0)
  2807.     {
  2808.       printf("Initialization Error:  %lX\n",temp);
  2809.       return 0;
  2810.     }
  2811.  
  2812.     for(i = 0; i <= 100; i++)
  2813.       a[i] = i;
  2814.  
  2815.     ad.condcode = 0;
  2816.     ad.n = 50;
  2817.     ad.address = LINADR(a);
  2818.  
  2819.     SUMARRAY(&ad);
  2820.     if (ad.condcode != 0)
  2821.     {
  2822.       printf("Error:  %lX\n",ad.condcode);
  2823.       return 0;
  2824.     }
  2825.     printf("Sum:  %f\n",ad.sum);
  2826.   }
  2827.   -----------------------------------------------------------------------------
  2828.  
  2829.  
  2830.  
  2831.  
  2832.  
  2833.  
  2834.  
  2835.  
  2836.  
  2837.  
  2838.  
  2839.  
  2840.  
  2841.  
  2842.  
  2843.  
  2844.  
  2845.  
  2846.  
  2847.  
  2848.  
  2849.  
  2850.  
  2851.  
  2852.  
  2853.  
  2854.  
  2855.                                         42
  2856.  
  2857.  
  2858.  
  2859.  
  2860.  
  2861.  
  2862.                      Appendix F: Reporting Problems With XLIB
  2863.  
  2864.  
  2865.        All efforts to isolate and report problems with XLIB will be appreciated.
  2866.   The following steps will greatly facilitate bug-tracking:
  2867.  
  2868.   1) Ensure that your own program always checks the error codes returned by XLIB
  2869.   procedures.  These codes will likely resolve the problem.  If not, then make
  2870.   note of the code.
  2871.  
  2872.   2) Attempt to execute your program under DPMI, VCPI, and in the absence of
  2873.   both.  If the problem relates to memory management, then also attempt to
  2874.   execute your program in the presence of XMS but in the absence of DPMI and
  2875.   VCPI, then attempt to execute in the absence of all three interfaces.  It will
  2876.   generally be found that the problem occurs only under a specific interface.
  2877.   If so, then note the interface under which the problem occurs.
  2878.  
  2879.   3) If the problem occurs only under one interface, then attempt to execute
  2880.   your program under different implementations of the interface.  For example, a
  2881.   DPMI host is contained in Windows 3.1, 386MAX, QDPMI, and OS/2 2.x.  Try
  2882.   executing your program under each host and make note of the results.  Problems
  2883.   occurring only under one host are generally indicative of bugs in the host
  2884.   rather than XLIB.
  2885.  
  2886.   4) Try different options on your compiler, assembler, and linker.  It is
  2887.   sometimes the case that code is not processed properly under some options.
  2888.  
  2889.   5) Report problems to:
  2890.  
  2891.   Dr. David Pyles
  2892.   TechniLib Company
  2893.   P.O. Box 6818
  2894.   Jackson, Ms. 39282
  2895.   (601) 372-7433
  2896.  
  2897.  
  2898.  
  2899.  
  2900.  
  2901.  
  2902.  
  2903.  
  2904.  
  2905.  
  2906.  
  2907.  
  2908.  
  2909.  
  2910.  
  2911.  
  2912.  
  2913.  
  2914.  
  2915.  
  2916.  
  2917.  
  2918.  
  2919.                                         43
  2920.  
  2921.  
  2922.  
  2923.  
  2924.  
  2925.  
  2926.                  Appendix G: The SWITCHPM and SWITCHRM Procedures
  2927.  
  2928.  
  2929.        SWITCHPM and SWITCHRM are the primitive mode-switch routines used by
  2930.   nearly all XLIB procedures requiring execution in both real and protected
  2931.   modes.  They are made public for users who need to perform mode switching
  2932.   tasks not provided by XLIB.  These routines do not conform to the general
  2933.   conventions followed by other XLIB procedures; consequently, they are
  2934.   presented in an appendix.
  2935.        There are special situations in which CALLPM and ENTERPM may not be
  2936.   suitable for performing mode switches.  For example, suppose one has a
  2937.   protected-mode hardware interrupt handler that is intended to service
  2938.   interrupts occurring in both real and protected modes.  Unless DPMI is
  2939.   installed, the user will have to write a second handler to deflect real-mode
  2940.   interrupts to the protected-mode handler.  CALLPM and ENTERPM could not be
  2941.   used in the event that the interrupt occurred in a procedure called from
  2942.   protected mode will CALLRM.  This follows because CALLPM and ENTERPM are not
  2943.   reentrant.  Instead, the user must perform the mode switches either with
  2944.   DEFLECTPM or with SWITCHPM and SWITCHRM.  A second example could occur where
  2945.   the user needs to perform mode switches without the overhead of CALLPM and
  2946.   ENTERPM.  SWITCHPM and SWITCHRM perform mode switches in minimum CPU time.
  2947.        Both SWITCHPM and SWITCHRM are near procedures in CSEG; therefore, they
  2948.   must be called from this segment.  SWITCHPM returns to the caller in 16-bit
  2949.   protected mode.  SWITCHRM returns to the caller in real mode.  Both procedures
  2950.   must be called with a stack in DSEG.  Do not use XLIB stacks for this purpose.
  2951.        SWITCHPM returns with CS = CSEGSEL and with all other segments equal to
  2952.   DSEGSEL.  All other registers, except the status flags, are preserved.
  2953.        SWITCHRM returns with CS = CSEG and with SS, DS, and ES set to DSEG.  FS
  2954.   and GS are undefined.  All other registers, except the status flags, are
  2955.   preserved.
  2956.  
  2957.  
  2958.  
  2959.  
  2960.  
  2961.  
  2962.  
  2963.  
  2964.  
  2965.  
  2966.  
  2967.  
  2968.  
  2969.  
  2970.  
  2971.  
  2972.  
  2973.  
  2974.  
  2975.  
  2976.  
  2977.  
  2978.  
  2979.  
  2980.  
  2981.  
  2982.  
  2983.                                         44
  2984.