Introduction to XMS Memory


This is a very old article written by me (around 2001, I guess) and it is just here for record.

What is XMS?

As a DOS based application programmer, you might be aware of the fact that out of your entire RAM, only 640 KBs are available for use for your program. This is enough for quite some applications; but when you are programming a game or a program which uses lot of memory, you will find out that your application cannot find the memory needed. Then you might be thinking, what about the rest of the RAM I have? Where is it? How to access it in DOS? Then here’s your answer. Use XMS with Newlib.

XMS stands for eXtended Memory System; in which you can use all your RAM using XMS Manager calls. First thing that you have to check is whether there is XMS support present in your computer. If you are running Windows, then XMS support is present. But, if you are using DOS(no windows), then you have to load the XMS driver at system boot up.

How to use XMS?

You have seen all the features that XMS can give to you. Now, you need to know how to use XMS. It is pretty simple. It might sound different at first. But, if you have experience working with files, then this will be a piece of cake. First thing I should tell you is that the Extended Memory cannot be directly accessed and needs to have a driver. Then, accessing XMS memory is done in blocks. Block is a part of XMS memory in which you can store data. The blocks have a fixed size which is specified while allocating it. The size is given in KiloBytes and not bytes. However, the blocks can be resized by a function specified later.

So, what do you mean by accessing memory in Blocks? It means that when you access XMS memory, you have to specify a length which will be equal to the number of bytes you want to store. The XMS memory is catagorised in Blocks with a label(handle) of its own. Just as file handles are used for reading and writing to files, XMS handles are used here to read and write in XMS blocks. The handle will be given to you when you allocate a block of XMS memory. All the data you want to store in XMS, must be stored in the block through its handle.

So, now you know that you need a handle to access the extended memory blocks. These blocks have variable length; i.e. you define their length; and this is done at the time of allocating a block. You can allocate a XMS block for use with the help of a Newlib function XMSAllocate. Then, you can use this block with the help of the functions MovetoXMS, MovefromXMS and MoveinXMS. After using the block, you have to deallocate the block using the function XMSDeallocate or if you want to resize a block for greater or lesser length, you can use function XMSReallocate.

That’s it. You can use the Newlib functions to access XMS very easily. If you did not understand anything above, read it again. Nobody is perfect. After you have understood the concept of XMS clearly, you can move on to study each function in detail at the XMS reference page.