Sunday 20 November 2011

How can I get more than 640KB of memory available to my DOS program? in C programming

How can I get more than 640KB of memory available to my DOS program?

When you find yourself in a memory-crunch situation, needing to use more than 640KB of memory in a DOS program, you can use a few good methods of getting more memory available. One way is to use disk swapping. Disk swapping means that you write data elements that are stored in memory to disk when you do not need them. After writing a data element (variable, array, structure, and so forth) to disk, you can free up the memory that was used by that data element (by using the free() function) and thus have more memory available to your program. When you need to use the data element that was swapped to disk, you can swap out another data element from memory to disk and read the previously swapped variable back in from disk. Unfortunately, this method requires a lot of coding and can be quite tedious to implement. 

Another good way to get more than 640KB of memory available to your DOS program is to use an alternative memory source—EMS (expanded memory) or XMS (extended memory). EMS and XMS, which refer to two ways of allocating memory above the 640KB region, are explained in separate paragraphs in the following text. EMS stands for Expanded Memory Specification. This is a method developed by Lotus, Intel, and Microsoft for accessing memory above the 1 megabyte region on IBM-compatible machines. Currently, two versions of this specification are used: LIM 3.2 and LIM 4.0. The newer version, LIM 4.0, overcomes some of the limitations of LIM 3.2. Expanded memory is enabled by the installation of an expanded memory manager (such as EMM386.EXE included with DOS). Your program makes calls to the expanded memory manager to request blocks of expanded memory. The expanded memory manager uses a technique called bank switching to move memory temporarily from above the 1 megabyte region to an empty region in the upper memory area between 640KB and 1 megabyte. Bank switching involves taking a memory allocation request from the application program and allocating 16KB of upper memory area at a time to keep track of memory that is addressed above the 1 megabyte region.

Extended memory is enabled by the installation of an extended memory manager (such as HIMEM.SYS included with DOS). Your program makes calls to the extended memory manager to request extended memory blocks (EMBs). No “bank switching” technique is used for requesting extended memory. Your program simply makes a function call to the extended memory manager to request a block of memory above the 1 megabyte region. Unfortunately, code cannot be executed above the 1 megabyte region under DOS; therefore, you cannot execute code stored in extended memory. Similarly, you cannot directly address data stored in extended memory, so many programmers like to set up a “buffer area” in conventional memory (below 640KB) to provide a swap area between conventional and extended memory.

The techniques used for expanded memory are older and somewhat outdated. Expanded memory was popular when DOS-based machines first came out that had add-on expanded memory boards attached. Using expanded memory techniques is somewhat slower than using extended memory techniques. In fact, many of today’s PC configurations eliminate expanded memory altogether by including the NOEMS flag in the EMM386.EXE entry of the config.sys file. Most modern programs have abandoned the older expanded memory techniques for the newer extended memory techniques. If your program needs to address above the 1 megabyte region, you should use extended memory rather than expanded memory. When you use extended memory, your programs will have greater stability and perform faster than if you had chosen to use expanded memory.

The specific steps of implementing extended and expanded memory are beyond the scope of this book.Explaining how to address memory with these techniques would probably require a separate chapter. Instead, you can obtain the EMS (Expanded Memory Specification) and XMS (Extended Memory Specification) documents directly from Microsoft or download them from a network service such as CompuServe. These documents detail the EMS and XMS application programming interface (API) and show you in detail how to use each technique.

Cross Reference:

XVIII.11: I get the message DGROUP: group exceeds 64KB during my link. What’s wrong?
XVIII.12: How can I keep my program from running out of memory?
XVIII.13: My program is too big to run under DOS. How can I make it fit?
XVIII.15: What is the difference between near and far?

No comments:

Post a Comment