China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-12 10:07
中国DOS联盟论坛 » DOS学习入门 & 精彩文章 (教学室) » [Share]Memory Management in DOS Systems View 3,867 Replies 1
Original Poster Posted 2010-07-31 19:53 ·  中国 上海 移动
初级用户
★★
Credits 170
Posts 83
Joined 2007-11-30 16:44
18-year member
UID 104263
Gender Male
Status Offline
This article is the information collected, learned and organized online by myself when configuring the memory of DOS/Win3.x last year.
It was also posted on the forum last year, but after a few days, there was a problem with the forum database, it should have been rolled back, and the forum posts for a few days were lost.

When sorting out the files, I saw it and posted it again.

http://zuimeng.vicp.net/topic.asp?TOPIC_ID=1409

Origin of 1M memory:
  The first PC machine launched by IBM uses the 8088 chip. It has only 20 address lines, that is, its address space is 1MB.
  The designer of the PC machine uses the lower 640KB of 1MB as RAM for DOS and application programs to use, and the upper 384KB is reserved for systems such as ROM and video adapter cards. From then on, this boundary was established and has been used ever since.

0~640K: Used as RAM (random access memory).
641K~1M (384K): Reserved for ROM (read-only memory) or used as a buffer. In this 384K space, three memory spaces of 128K/192K/64K are divided in turn, where 128K is the display buffer, 64K is the system BIOS space, and the last 192K is reserved and empty.

*********************************************************************************

Later, with the increase in the size of programs, the increase in memory usage, and the appearance of new CPUs that can access larger memory spaces, the 1M space has become limited. Soon after 80286 was widely accepted, it was found that the 640K limit became an obstacle for large programs. The 286 has 24 address lines and can address a memory space of 16M, and the memory address space has exceeded the 1MB limit in the case of 8088. (The 80386 has 32 address lines and can address a memory space of 4G.)

There appeared an expanded memory (EMS: Expanded Memory), which allows programs to access the memory space above 640K. Expanded memory is an early standard for increasing memory, and can be expanded up to 32M at most. To use expanded memory, a special expanded memory board must be installed in the computer, and a management program for the expanded memory board must also be installed.

Because the address line of the I/O slot of the expanded memory only has 24 address lines to address 16M space (286 address line bits), which is not compatible with the 80386's later 32-bit addressing 4G space. In order to distinguish the expanded memory, the address space above 1M is called extended memory (XMS: eXtend Memory)

In microcomputers of 386 and above, there are two memory working modes, one is called real address mode or real mode, and the other is called protection mode. In real mode, the physical address still uses 20 bits, so the maximum addressing space is still 1MB to be compatible with 8086. The protection mode uses a 32-bit physical address, and the addressing range can reach 4GB. The DOS system works in real mode, and the memory space it manages is still 1MB, so it cannot directly use extended memory.

The difference between expanded memory and extended memory does not lie in the physical memory location, but in the method used to read and write it.

Since expanded memory was introduced before extended memory, most programs are designed to be able to use expanded memory, but not extended memory. But for program compatibility, extended memory simulates expanded memory for programs to use (this is also why himem.sys must be loaded first and then emm386.exe in dos). Since expanded memory is more troublesome to use, the expanded memory on the motherboard was eliminated soon after extended memory appeared.

Himem.sys is the driver program to manage extended memory
There are many drivers that comply with EMS (expanded memory), and commonly used ones are EMM386.EXE, QEMM, TurboEMS, 386MAX, etc. Both DOS and Windows provide EMM386.EXE.

In the first 1M address of the memory space, it is divided into: the first 640K is conventional memory (Conventional Memory) and the last 384K is upper memory (Upper Memory), and this 384K is also called the reserved area (see the origin of 1M memory above).

The reserved area has been clearly reserved for the system since the birth of the PC, and user programs cannot intervene. In order to make full use of the memory address space in this area, a piece of memory is divided out and named UMB (Upper Memory Blocks), which is called upper memory or upper memory block in Chinese, and is managed by the EMS (expanded memory) driver. (Personal guess: This is one of the reasons why EMS is not eliminated after XMS in DOS, and there is also compatibility.)

From the origin of 1M memory above, it can be seen that the maximum UMB is 192KB, but the size of UMB can be adjusted through the EMS driver (device=emm386.exe ram=xxxx-xxxx). After talking about UMB, let's talk about HIGH.

The address space above 1M in the memory space is all in the XMS management area, but the first 64K space after 1MB is divided out and named high memory area HMA (High Memory Area). The physical memory of HMA is obtained from extended memory. Therefore, to use HMA, there must be physical extended memory. In addition, the establishment and use of HMA also require the support of the XMS driver HIMEM.SYS, so HMA can only be used after HIMEM.SYS is loaded.

Why is there this HMA?? Because in real mode, the address of the memory unit can be recorded as:
Segment address: offset within segment
  Usually written in hexadecimal as XXXX:XXXX. The actual physical address is formed by shifting the segment address left by 4 bits and adding it to the offset within the segment. If all bits of the address are 1, it is FFFF:FFFF. Its actual physical address is: FFF0 + FFFF = 10FFEF, which is about 1088KB (16 bytes less), which has exceeded the 1MB range and entered the extended memory area.

*********************************************************************************

From the above, HIMEM.SYS manages the XMS space, and EMM386.EXE manages the EMS space. But EMS and XMS overlap in the memory space management above 1MB, and XMS has a larger area than EMS, so generally EMM386.exe is not used to manage the area above 1MB. But EMM386.exe can manage and utilize UMB, so generally in config.sys use device=emm386.exe noems (provide access to the upper memory area, but prevent access to extended memory.) Or simply do not load the emm386.exe program, and the UMB space is not large either.

DOS can only directly use 640K memory (DOS applications can only run in conventional memory), that is, basic memory (or conventional memory), and must rely on other memory management programs to use more memory. The DOS system has two built-in commands, himem.sys and emm386.exe, which are the most commonly used memory management programs.

The memory between 640K and 1M is the system reserved area, and there is upper memory (UMB) space in the reserved area.
The memory space above 1M is called extended memory XMS (or expanded memory EMS);


himem.sys is responsible for managing extended memory (XMS).
emm386.exe is responsible for managing upper memory (UMB), and simulates expanded memory (EMS) in extended memory (XMS) for some software to use.

#Virtual 16M EMS in XMS.
device=emm386 ram 16000

Note: EMM386.exe requires himem.sys to be installed first, so it must be ensured that the configuration command for installing himem.sys is before emm386.exe.


The reserved area memory refers to the 384K memory above conventional memory. Programs generally cannot use this memory area, but EMM386.exe can activate part of the reserved area memory, and it allows users to load some device drivers and user programs into the upper memory using Devicehigh or LH (that is, loadhigh). dos=high,umb also loads part of DOS into the upper memory. Here, umb is the abbreviation of upper memory block (Upper Memory Block).

Add dos=high,umb in config.sys, which will move the system files of DOS into the upper memory, leaving more basic memory for other software to use. Note that if the user does not install emm386.exe, the umb parameter is not meaningful. Similarly, if himem.sys is not installed, the high parameter is also not meaningful.

DOS: Used for the configuration of the DOS system, such as whether to use HMA (high memory area) and UMB (upper memory), etc.
Usage: DOS=
Meaning: HIGH and LOW mean using HMA or not using HMA, UMB and NOUMB mean using UMB or not using UMB.

*********************************************************************************

Basic memory: also called conventional memory, 0~640K
Reserved area memory: 641K~1MB (384K), including upper memory (UMB), and can use emm386.exe to use the UMB area.
Upper memory: UMB, an area of TSR and auxiliary drivers in the upper memory

HMA high memory: the first 64K high memory after 1M (HMA, managed by himem.sys)

XMS extended memory: memory space area after 1M.
EMS expanded memory: memory space area after 1M, appeared before xms.


Sometimes the reserved area memory is also called upper memory.

http://zuimeng.vicp.net/Attachment/dosmemjg.jpg
This picture is in the attachment

Area division in conventional memory (640K):
1. Interrupt vector area: address range is 00-3FFH, occupying 1KB, also called interrupt vector table area.
2. BIOS data area: range is 400~4FFH, occupying 256 bytes, for the special program BIOS to store data.
3. System data area: address range is 500~6FFH, occupying 512 bytes, used for the operating system to store information
       Small area, store initialization information when the computer starts.
4. Application memory area: range is 700~9FFFH, occupying 638KB, usually used as the memory area for running programs.

*********************************************************************************

There is a very good tool in DOS: MemMaker, which can help you free up more basic memory (640K).
In config.sys, you can use the DEVICEHIGH command to load the driver into UMB, and in autoexec.bat, you can use the LH command to load the driver into UMB.

MS KB: Overview of Memory-Management Functionality in MS-DOS (Overview of MS-DOS memory management)
http://support.microsoft.com/kb/95555/en-us

*********************************************************************************
SUMMARY

This article provides an overview of how expanded memory conforming to the Expanded Memory Specification (EMS) and extended memory conforming to the Extended Memory Specification (XMS) is created and managed in MS-DOS versions 5.0 and later by the device drivers HIMEM.SYS and EMM386.EXE. It also discusses how MS-DOS is loaded into the high memory area (HMA) and manages upper memory blocks (UMBs).

NOTE: Before reading this article, you may want to review the sections about memory management, HIMEM.SYS, and EMM386.EXE in your printed MS-DOS documentation. If you are using MS-DOS 6.0, 6.2, 6.21, or 6.22, refer to the online help. For example, type help emm386.exe at the MS-DOS command prompt.

The information in this article is organized as follows:


·Old Extended Memory Allocation Methods: Interrupt 15h and VDisk Headers
·HIMEM.SYS Creates XMS Memory
·A20 and the HMA
·DOS=HIGH Asks MS-DOS to Run in the HMA
·EMM386.EXE Uses XMS to Create EMS and/or UMBs
·DOS=UMB Asks MS-DOS to Manage UMBs
·DEVICEHIGH=<filename>
·LH (LOADHIGH)
·DEVICEHIGH AND LOADHIGH WITH /L: AND /S
·MEM Reports on Memory MS-DOS Is Managing
·How MS-DOS Uses Extended or Expanded Memory
·Available References

NOTE: Only the section "How MS-DOS Uses Extended or Expanded Memory" applies to MS-DOS versions 3.x and 4.x.

MORE INFORMATION

Old Extended-Memory Allocation Methods: Interrupt 15h & VDisk Headers
In the past, there were two ways for programs to allocate extended memory: top-down (using Interrupt 15) and bottom-up (using VDisk headers).

Interrupt 15h is a ROM BIOS service that includes several extensions to the original PC ROM BIOS, including the means to find out how much RAM (conventional plus extended) is on the system. A program uses this service to find out how much extended memory there is, then "hooks" Interrupt 15h and reports to any other programs that there is <n>K less memory available, effectively slicing <n>K of extended memory off the top. By doing this, the program has allocated its own extended memory from the top of the memory pool.

Bottom-up memory allocation works by checking for a header at the start of extended memory stating that <n>K of RAM is in use. If a header is there, the program checks <n>K further for another header. If no header exists, the program puts in its own header. These headers are called VDisk headers because the original IBM DOS RAM drive utility (VDISK.SYS) uses this method.

A drawback to these methods is that after memory has been allocated to a program, deallocating it is usually not possible. The XMS was designed to make allocating and deallocating extended memory easier for all involved.

HIMEM.SYS Creates XMS Memory
HIMEM.SYS implements all of the XMS except the optional UMB portion. HIMEM.SYS versions earlier than 3.0 are XMS 2.0 compliant and recognize up to 16 MB of RAM; versions 3.0 and later (first included with Windows 3.10 and MS-DOS 6.0) are XMS 3.0 compliant and recognize up to 4 GB.

On loading, HIMEM.SYS determines the amount of extended memory available. Unless it was loaded with the /INT15= option, HIMEM.SYS sets out to allocate all available extended memory for use as XMS memory (note the actual allocation of all the available extended memory does not occur until a program makes an XMS function call). HIMEM.SYS reserves the HMA with a VDisk header and hooks Interrupt 15h. Programs that want to use extended memory (other than the HMA) without using XMS can do so until a program actually requests XMS.

After a program actually asks for XMS memory, HIMEM.SYS uses its Interrupt 15h hook to notify programs that only the amount of extended memory specified by /INT15=xxxx, minus the HMA (64K), is available for use. Memory available through /INT15=xxxx is physically located above the HMA; XMS- managed RAM is physically located above any /INT15=xxxx RAM.

A20 and the HMA
The HMA is defined as FFFF:0010-FFFF:FFFF on 80286 and higher systems that have physical addressable RAM at these addresses. This area can be addressed in real mode (8086 emulation) on 80286 and higher systems if the 21st address line (A20) is enabled, which produces 64K-16 bytes of additional usable RAM. On an 8086 or an 80x86 with the A20 line disabled, FFFF:0010 "wraps around" and is the same as 0000:0000.

Turning this line on and off is accomplished using the keyboard port and is hardware dependent. HIMEM.SYS includes a number of "A20 handlers" for different machines. The XMS allocates and deallocates the HMA as one block, that is, only one program can use it at a time. MS-DOS 5.0 and later can run in the HMA if DOS=HIGH is in the CONFIG.SYS file.

DOS=HIGH Asks MS-DOS to Run in the HMA
If DOS=LOW or no DOS= command is in the CONFIG.SYS file, MS-DOS and its data are initialized and loaded into their final place in low memory before the DEVICE= and DEVICEHIGH= commands are processed.

If the DOS=HIGH command is in the CONFIG.SYS file, MS-DOS data (which must remain low for compatibility) is loaded into its final place in conventional memory. After each device driver is initialized, a check is made to determine if an XMS driver has been installed. If so, and if the HMA is available, MS-DOS is moved into the HMA. If not, MS-DOS keeps checking after each DEVICE= command, and then begins processing the INSTALL= commands.

If a DOS=HIGH command exists but MS-DOS hasn't loaded high (if no XMS driver was loaded or the HMA wasn't available), MS-DOS reports "HMA not available/loading DOS low" and loads itself into conventional memory above all the installable device drivers and/or terminate-and-stay-resident (TSR) programs loaded with INSTALL=.

The transient portion of COMMAND.COM remains in conventional memory whether MS-DOS is loaded high or low. Most of the COMMAND.COM resident portion, any software code pages, and the disk buffers (usually) also load high if DOS=HIGH is in the CONFIG.SYS file.

EMM386.EXE Uses XMS to Create EMS and/or UMBs
EMM386.EXE is a device driver for 80386 and higher systems with XMS memory. EMM386.EXE uses XMS memory to create and manage EMS memory and/or XMS upper memory blocks (UMBs). EMS is available to programs through the EMS 4.0 interface; UMBs are available through the XMS interface. When providing UMBs, EMM386.EXE answers only requests to allocate or deallocate UMBs; all other XMS memory is managed by HIMEM.SYS.

On loading, EMM386.EXE shows a report of its activity. Among other things, this report includes:

·Whether EMS memory is being provided, and, if so:
  ·Amount of EMS available
  ·Address of EMS page frame segment
·If UMBs are being provided, the following is also shown:
  ·Amount of UMBs available through the XMS
  ·Largest UMB available through the XMS
  ·Address of upper memory starting segment
This report is also available at the MS-DOS command prompt by running EMM386.EXE.

NOTE: EMM386.EXE versions 4.45 and later do not display this information at startup UNLESS the /VERBOSE switch is used.

DOS=UMB Asks MS-DOS to Manage UMBs
The DOS=UMB command asks MS-DOS to allocate any UMBs available through the XMS to itself. MS-DOS then makes UMBs available through its own memory- management services in Interrupt 21h, including:

Function Description Version
-------------------------------------------------------
48h Allocate memory 2.0
49h Free allocated memory 2.0
4Ah Set memory block size 2.0
5800h Get allocation strategy 2.0
5801h Set allocation strategy 2.0
5802h Get upper-memory link status 5.0
5803h Set upper-memory link status 5.0

If DOS=UMB is in the CONFIG.SYS file, EMM386.EXE reports that 0 (zero) UMBs are available from the MS-DOS command prompt, and any program that attempts to use UMBs through the XMS services is unable to find them.

Users can determine whether MS-DOS has any UMBs available by using the MEM /C command. Programs can use Interrupt 21h, Function 5803h to determine if UMBs exist.

DEVICEHIGH=<filename>
The DEVICEHIGH= command asks that the device driver file be loaded into an MS-DOS UMB if there is one available that is big enough. If there isn't, the driver is loaded into conventional memory and executed; no error is displayed by MS-DOS.

LH (LOADHIGH)
The LH (or LOADHIGH) command tells MS-DOS to load the program file into an MS-DOS UMB if there is one available that is big enough. If there is not, the program is loaded into conventional memory and executed; no error is displayed by MS-DOS.

DEVICEHIGH AND LOADHIGH WITH /L: AND /S
If you are using MS-DOS version 6.0, 6.2, 6.21, or 6.22, you can use the /L and /S switches to control the memory regions into which a device driver or program is loaded.

DEVICEHIGH ]=

The /L: switch specifies the memory region(s) a program can be loaded into. If the /L: switch is not specified, MS-DOS loads the program into the largest free UMB and all other UMB regions are available to the program. Use the MEM /F command to view the available memory regions. (Region 0 is conventional memory.)

The /S switch shrinks the UMB to the minimum size (minsize) while the program is loading. This switch can only be used in conjunction with the /L: switch and affects only UMBs for which a minimum size was specified.

The following is an example:

LOADHIGH /L:1,12194;2,34213 /S C:\PROGDIR\PROGRAM.EXE
DEVICEHIGH /L:2,12048 =C:\DOS\SETVER.EXE



This command loads PROGRAM.EXE and restricts it to load into memory regions 1 and 2. The program has access to only 12,194 bytes in region 1 and only 34,213 bytes in region 2.

NOTE: Rather than attempting to manually configure the memory regions programs load into, MS-DOS 6.0, 6.2, 6.21, and 6.22 users are encouraged to run the MemMaker memory optimization program. To run MemMaker, type memmaker at the MS-DOS command prompt.

MEM Reports on Memory MS-DOS Is Managing
If MS-DOS is managing UMBs (DOS=UMB), MEM /C or MEM /D includes the UMA in its report. Any areas not being managed by MS-DOS are labeled "SYSTEM" in this report. For UMB information, type mem /c | more at the MS-DOS command prompt. (Note that if you are using MS-DOS 6.0 or later, you can type mem /c /p to view the memory report one screen at a time.)

If MS-DOS is not managing the UMBs, MEM does not report on the UMA. Other UMB managers typically have some means to determine what is loading high. Check the documentation for your UMB manager for details.

MEM also includes information about extended memory (determined using the Interrupt 15/VDisk header interface), the XMS (reported through the XMS 2.0 or 3.0 interface), and the EMS (reported through the EMS 3.2 or 4.0 interface) for your convenience.

How MS-DOS Uses Extended or Expanded Memory
As a whole, MS-DOS does not use extended or expanded memory for general usage or for loading applications.

However, some MS-DOS utilities and drivers use extended or expanded memory for data areas. In addition, MS-DOS versions 5.0 and later load most of the MS-DOS kernel, command interpreter, code pages (if used), and disk buffers into the HMA, which is the first 64K of extended memory.

The table below outlines which MS-DOS utilities use extended memory without an extended memory driver, extended memory through an XMS driver, or expanded memory through a Lotus-Intel-Microsoft (LIM) EMS driver.

These utilities load into conventional memory but use nonconventional memory for data areas. Because different versions of many utilities that ship with MS-DOS also ship with Microsoft Windows, the Windows versions are included in this table. Microsoft recommends that you do not mix versions of these drivers that are included with different products.

Non-XMS
Conv. Extended XMS LIM 3.2/4.0 EMS
------------------------------------------
RAMDRIVE.SYS 3.3 Yes Yes No Yes
RAMDRIVE.SYS 4.x Yes Yes No Yes
RAMDRIVE.SYS Win30 Yes No Yes Yes
RAMDRIVE.SYS 5.x Yes No Yes Yes
RAMDRIVE.SYS Win31 Yes No Yes Yes
RAMDRIVE.SYS 6.0-6.22 Yes No Yes Yes

SMARTDRV.SYS 4.x No Yes No Yes
SMARTDRV.SYS Win30 No No Yes Yes
SMARTDRV.SYS 5.x No No Yes Yes
SMARTDRV.EXE All No No Yes No

HIMEM.SYS (1) All No Yes Provider No

EMM386.SYS(2) 4.x No Yes No Provider
EMM386.SYS(3) Win30 No No Yes Provider
EMM386.EXE(3) All No No Yes(4) Provider

(1) Uses extended memory to provide XMS memory
(2) Uses extended memory to provide EMS memory
(3) Uses XMS memory to emulate EMS memory
(4) MS-DOS 5.0 and later EMM386.EXE can also be configured to
provide UMBs according to the XMS. This causes EMM386.EXE
to be a provider of the UMB portion of the XMS.

In MS-DOS versions 4.x, you can place the disk buffers in expanded memory by using the /X switch; however, this is not recommended because of problems that may arise. SMARTDRV can provide much of the same performance enhancements and is preferable to using BUFFERS /X. For more information about BUFFERS /X, query in the Microsoft Knowledge Base on the following words:
buffers and ems and /x
Loading the disk buffers in EMS memory is not supported in MS-DOS versions 5.0 and later. These versions support loading the MS-DOS kernel and, if there is room, the disk buffers into the HMA. MS-DOS accesses the HMA through the XMS protocol.

Available References
The official LIM EMS specification, "Lotus/Intel/Microsoft Expanded Memory Specification Version 4.0," is available from Intel by calling (800) 538- 3373.

The official XMS specification, "Extended Memory Specification Version 3.0," is available free from Microsoft. To obtain the specification, see the "Instructions for Downloading" section below.

The official MS-DOS program interface documentation, "Microsoft MS-DOS Programmer's Reference," is available from Microsoft Press (by calling 677-7377), or internationally through Penguin Books.


--------------------------------------------------------------------------------

APPLIES TO
Microsoft MS-DOS 3.1
Microsoft MS-DOS 3.2 Standard Edition
Microsoft MS-DOS 3.21 Standard Edition
Microsoft MS-DOS 3.3 Standard Edition
Microsoft MS-DOS 3.3a
Microsoft MS-DOS 4.0 Standard Edition
Microsoft MS-DOS 4.01 Standard Edition
Microsoft MS-DOS 5.0 Standard Edition
Microsoft MS-DOS 5.0a
Microsoft MS-DOS 6.0 Standard Edition
Microsoft MS-DOS 6.2 Standard Edition
Microsoft MS-DOS 6.21 Standard Edition
Microsoft MS-DOS 6.22 Standard Edition
Attachments
DOSMEMJG.PNG
Floor 2 Posted 2010-08-05 15:59 ·  中国 湖南 常德 电信
银牌会员
★★★
Credits 1,384
Posts 709
Joined 2005-10-29 22:22
20-year member
UID 44271
Status Offline
The article is good, give it a thumbs up. Just that I don't understand English, heh heh...
Forum Jump: