Excerpt from

Programming the 80286, 80386, 80486, and Pentium-Based Personal Computer

© 1995 by Barry B. Brey

14-2 MEMORY MANAGEMENT DRIVERS

The expanded and extended memory systems cannot be accessed from DOS without the installation of memory management drivers or programs. There are quite a few of these available, yet one of the more common ones is described. The EMS (expanded memory system) driver was the first made available with the addition of expanded memory systems to early personal computers. Later, the XMS (extended memory system) driver was added with the advent of the 80286 microprocessor to control its extended memory system. Recently the VCPI (virtual control program interface) has been added to control the memory system in the 80386 and newer microprocessors and their protected mode memory systems. Note that VCPI services are provided by DOS version 6.X as part of the EMM386.EXE program. Finally, DPMI (DOS Protected mode interface) provides the best control if WINDOWS is available. WINDOWS includes the DPMI interface if the application is executed from the DOS applications box from inside WINDOWS.

EMS

The EMS (expanded memory system) memory manager divides the upper memory area or systems area into page frames that begin at any 4K byte boundary and are 64K bytes in length. A page frame is divided into four pages that are each 16K bytes in length. This area of memory is known as expanded memory because it also is used in early 8088 based personal computers to expand the memory system. This memory expansion standard, called LIM 4.0 (Lotus/Intel/Microsoft), uses INT 67H for accessing and controlling the pages of memory. The EMS manager found in most system is EMM386.EXE designed to provide memory management for the 80386 or newer microprocessor. Note that although EMS seems dated, it is still used by many video games that are executed by the personal computer. Eventually this memory manager should disappear, and it is strongly recommended that the XMS manager should be used in its place.

Figure 14-1 shows the area of memory managed by EMM386.EXE. The page frame is any 64K byte contiguous section of system memory. In the illustration location D0000H-DFFFFH is chosen for the page frame. The 64K byte page frame is divided into four 16K byte pages. The EMM386.EXE program uses the microprocessor's internal hardware paging mechanism to assign extended memory to each page located in the page frame. In older systems, the memory manager activated hardware memory pages on a plug-in memory board using a bank swapping technique.

Is EMM386.EXE installed?

Before expanded memory is used by a program, it must be known that EMM386.EXE or some other expanded memory manager is installed. To find if the expanded memory manager is installed:

  1. Open a file with the name EMMXXXX0 using the DOS INT 21H open function (3DH). If there is a carry upon return, the manager is not installed.
  2. If an EMS manager is installed, see if it is ready to accept output information by using the IOCTL (I/O control) function 07H. If the return value is not a -1, the memory manager is not present.

If both tests are passed, use INT 67H to access the manager and expanded memory. If not, indicate that the memory manager is not present.

EXAMPLE 14-4  

                    ;a program that tests for the presence of an extended memory
                    ;manager and expanded memory.  If present, the program displays
                    ;the version number, page frame address, total expanded memory,
                    ;and available expanded memory.
                    ;
                          .MODEL SMALL
                          .386
0000                      .DATA
0000 45 4D 4D 58 58 EMM   DB   'EMMXXXX0',0       ;EMM driver name
     58 58 30 00
0009 0D 0A 0A 4E 6F ERR   DB   13,10,10,'No EMM driver present.$'
     20 45 4D 4D 20 
     64 72 69 76 65 
     72 20 70 72 65 
     73 65 6E 74 2E 
     24
0023 0D 0A 0A 45 4D MES1  DB   13,10,10,'EMM version number $'
     4D 20 76 65 72 
     73 69 6F 6E 20
     6E 75 6D 62 65 
     72 20 24
003A 0D 0A 50 61 67 MES2  DB   13,10,'Page frame address = $'
     65 20 66 72 61 
     6D 65 20 61 64 
     64 72 65 73 73 
     20 3D 20 24
0052 0D 0A 54 6F 74 MES3  DB   13,10,'Total expanded memory = $'
     61 6C 20 65 78 
     70 61 6E 64 65 
     64 20 6D 65 6D 
     6F 72 79 20 3D 
     20 24
006D 0D 0A 41 76 61 MES4  DB   13,10,'Available expanded memory = $'
     69 6C 61 62 6C 
     65 20 65 78 70 
     61 6E 64 65 64 
     20 6D 65 6D 6F 
     72 79 20 3D 20
     24
0000                      .CODE
                          .STARTUP
0010 E8 0069              CALL ISEMS              ;see if EMS driver present
0013 73 09                JNC  MAIN1              ;if EMM driver present
0015 B4 09                MOV  AH,9
0017 BA 0009 R            MOV  DX,OFFSET ERR      ;display no EMM driver
001A CD 21                INT  21H
001C EB 5A                JMP  MAIN2              ;end it
001E                MAIN1:
001E B4 09                MOV  AH,9
0020 BA 0023 R            MOV  DX,OFFSET MES1
0023 CD 21                INT  21H                ;display EMM version number
                                        
0025 B4 46                MOV  AH,46H
0027 CD 67                INT  67H                ;get version number
                                        
0029 8A E0                MOV  AH,AL              ;get minor version
002B 80 E4 0F             AND  AH,0FH
002E C0 E8 04             SHR  AL,4               ;get major version
0031 05 3030              ADD  AX,3030H           ;make ASCII
0034 50                   PUSH AX
0035 8A D0                MOV  DL,AL              ;display version number
0037 B4 06                MOV  AH,6
0039 CD 21                INT  21H
003B B2 2E                MOV  DL,'.'
003D CD 21                INT  21H
003F 5A                   POP  DX
0040 8A D6                MOV  DL,DH
0042 CD 21                INT  21H
0044 B4 09                MOV  AH,9
0046 BA 003A R            MOV  DX,OFFSET MES2
0049 CD 21                INT  21H                ;page frame address
004B B4 41                MOV  AH,41H
004D CD 67                INT  67H                ;get page frame address
004F E8 0049              CALL DISPH              ;display hex address
0052 B4 09                MOV  AH,9
0054 BA 0052 R            MOV  DX,OFFSET MES3
0057 CD 21                INT  21H                ;total expanded memory
0059 B4 42                MOV  AH,42H
005B CD 67                INT  67H                ;get number of pages
005D 8B C2                MOV  AX,DX
005F B9 4000              MOV  CX,4000H
0062 F7 E1                MUL  CX                 ;find total
0064 E8 0052              CALL DISPD              ;display decimal
0067 B4 09                MOV  AH,9
0069 BA 006D R            MOV  DX,OFFSET MES4
006C CD 21                INT  21H                ;available expanded memory
006E 8B C3                MOV  AX,BX
0070 B9 4000              MOV  CX,4000H
0073 F7 E1                MUL  CX
0075 E8 0041              CALL DISPD
0078                MAIN2:
                          .EXIT
                    ;
                    ;the ISEMS procedure tests to determine of EMM is loaded.
                    ;***return parameter***
                    ;if carry = 1, EMM is not installed
                    ;if carry = 0, EMM is installed
                    ;
007C                ISEMS PROC NEAR
                                        
007C B8 3D00              MOV  AX,3D00H
007F BA 0000 R            MOV  DX,OFFSET EMM
0082 CD 21                INT  21H                ;open file
0084 72 0E                JC   ISEMS1             ;if EMM not there
0086 8B D8                MOV  BX,AX              ;save handle
0088 B8 4407              MOV  AX,4407H           ;access IOCTL function 7
008B CD 21                INT  21H
008D 72 05                JC   ISEMS1             ;if error
008F 3C FF                CMP  AL,-1
0091 74 01                JE   ISEMS1             ;if present
0093 F9                   STC
0094                ISEMS1:
0094 9C                   PUSHF                   ;save carry
0095 B4 3E                MOV  AH,3EH             ;close file
0097 CD 21                INT  21H
0099 9D                   POPF
009A C3                   RET
                                        
009B                ISEMS ENDP
                    ;
                    ;the DISPH is places the contents of BX as a 4 digit
                    ;hexadecimal number.
                    ;                  
009B                DISPH PROC NEAR
                                        
009B B5 04                MOV  CH,4
009D                DISPH1:
009D C1 C3 04             ROL  BX,4               ;get digit
00A0 8A D3                MOV  DL,BL
00A2 80 E2 0F             AND  DL,0FH
00A5 80 C2 30             ADD  DL,30H             ;convert to ASCII
                          .IF DL > 39H
00AD 80 C2 07                 ADD  DL,7           ;if a letter add 7 more
                          .ENDIF
00B0 B4 06                MOV  AH,6               ;display contents of DL
00B2 CD 21                INT  21H
00B4 FE CD                DEC  CH                 ;count off 4
00B6 75 E5                JNZ  DISPH1             ;repeat 4 times
00B8 C3                   RET

00B9                DISPH ENDP
                    ;
                    ;the DISPD procedure displays the contents of EAX in decimal
                    ;with commas.
                    ;
00B9                DISPD PROC NEAR
                                        
00B9 53                   PUSH BX                 ;save BX
00BA BD 002C              MOV  BP,','
00BD 66| C1 E2 10         SHL  EDX,16
00C1 66| 25 FFFF0000AND   EAX,0FFFF0000H
00C7 66| 03 C2            ADD  EAX,EDX
00CA B9 0003              MOV  CX,3               ;comma counter
00CD BB FFFF              MOV  BX,-1
00D0 53                   PUSH BX                 ;indicate end
00D1 66| BB 0000000A      MOV  EBX,10             ;division factor
00D7                DISPD1:
00D7 66| 33 D2            XOR  EDX,EDX
00DA 66| F7 F3            DIV  EBX
00DD 80 C2 30             ADD  DL,30H             ;make ASCII
00E0 52                   PUSH DX
00E1 66| 83 F8 00         CMP  EAX,0
00E5 74 08                JE   DISPD2             ;if done
00E7 E2 EE                LOOP DISPD1             ;do three times
00E9 55                   PUSH BP                 ;save comma
00EA B9 0003              MOV  CX,3
00ED EB E8                JMP  DISPD1
00EF                DISPD2:
00EF B4 06                MOV  AH,6               ;display decimal digit
00F1 5A                   POP  DX
00F2 80 FA FF             CMP  DL,-1
00F5 74 04                JE   DISPD3
00F7 CD 21                INT  21H
00F9 EB F4                JMP  DISPD2
00FB                DISPD3:
00FB 5B                   POP  BX                 ;restore BX
00FC C3                   RET
                                        
00FD                DISPD ENDP
                          END

To illustrate this sequence refer to Example 14-4, which tests for the presence of EMS management and then uses INT 67H to get the version number, page frame address, total EMS memory, and available EMS memory. For a list of the INT 67H calls, refer to Appendix A. You must install EMM386.EXE with expanded memory for this example to display the presence of expanded memory. An example is DEVICE=EMM386.EXE 1024, which installs 1M byte of expanded memory from the CONFIG.SYS file. Notice how the ISEMS procedure tests for the presence of EMM and how the DISPH and DISPD procedures display hexadecimal or decimal data.

Using expanded memory in a program.

Now that a check for expanded memory is made, the use of expanded memory is investigated. Example 14-5 shows how 512K bytes of expanded memory is accessed and filled with 00H. Although this is not a practical program, it does illustrate the access to a considerable amount of expanded memory for an application.

return to publication list