home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR4 / XLIB30.ZIP / EXAMP2B.C < prev    next >
C/C++ Source or Header  |  1993-12-20  |  2KB  |  61 lines

  1. /*This program features a function which retrieves a dword from any specified
  2. location in extended memory.  The function illustrates inline protected-mode
  3. assembly.  TASM is used for assembly rather than the internal assembler
  4. because TASM can handle 32-bit instructions.  Since this program addresses
  5. extended memory which has not been allocated, it may fail with a page fault
  6. if page protection is being enforced.*/
  7.  
  8. #pragma inline                       /*Invoke TASM*/
  9. #include <stdio.h>
  10. #include <xlibb.h>
  11.  
  12. long far getextmem(long address);
  13.  
  14. int goterr = 0;                      /*Error flag*/
  15.  
  16. void main(void)
  17. {
  18.   long l, xaddress;
  19.  
  20.   l = INITXLIB();                    /*Initialize XLIB*/
  21.   if(l != 0)                         /*See if an error occurred*/
  22.   {
  23.     printf("Library initialization error:  %lX\n",l);
  24.     return;
  25.   }
  26.  
  27.   xaddress = 0x100000;               /*Read the first dword in 2ond meg*/
  28.   l = getextmem(xaddress);
  29.   if(goterr != 0)                    /*See if an error occurred*/
  30.   {
  31.     printf("Inline mode-switch error:  %lX\n",l);
  32.     return;
  33.   }
  34.   printf("[%lX] = %lX\n",xaddress,l);
  35. }
  36.  
  37. long far getextmem(long address)
  38. {
  39.   asm{
  40.     .386                            /*Enable 32-bit assembly*/
  41.     mov       eax,address
  42.     call      far ptr INLINEPM      /*Switch to 16-bit protected mode*/
  43.     jc        error                 /*Error code returned in ax*/
  44.     mov       ds,es:FLATDSEL        /*Switch to flat data model*/
  45.     push      dword ptr [eax]       /*Read the requested address*/
  46.     pop       ax                    /*Return result in dx:ax*/
  47.     pop       dx
  48.     call      dword ptr es:INLINERMPTR  /*Switch to real-mode by calling*/
  49.     jmp       done                      /*far pointer in XLIB.  A direct*/
  50.                                         /*far call would load CS with an*/
  51.                                         /*invalid value*/
  52.   }
  53. error:
  54.   asm{
  55.     xor       dx,dx                 /*Return error code in dx:ax*/
  56.     inc       goterr
  57.     .286                            /*Restore 16-bit assembly*/
  58.   }
  59. done:
  60. }
  61.