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-01 15:31
中国DOS联盟论坛 » DOS开发编程 & 发展交流 (开发室) » Question: How can programs under DOS use extended memory (Urgent) View 1,354 Replies 13
Original Poster Posted 2003-11-21 00:00 ·  中国 北京 北京化工大学教育网
初级用户
Credits 109
Posts 2
Joined 2003-11-21 00:00
22-year member
UID 13132
Gender Male
Status Offline
I'm doing embedded system development, using a PC104 board, with a 486 CPU, 16M of memory, and DOS as the operating system. I have now written a C program that uses the dynamic memory allocation function MALLOC to store data. I originally planned to store 10,000 data points, but because of memory limits, it can only store 4,000 data points. At the same time, the farmalloc function cannot be used. Could the experts here tell me how to solve this problem?
Floor 2 Posted 2003-12-04 00:00 ·  中国 台湾 中华电信
初级用户
Credits 168
Posts 16
Joined 2003-11-21 00:00
22-year member
UID 13141
Gender Male
Status Offline
Can it enter protected mode?
Floor 3 Posted 2004-03-01 00:00 ·  中国 上海 浦东新区 电信
初级用户
Credits 104
Posts 2
Joined 2004-03-01 00:00
22-year member
UID 18871
Gender Male
Status Offline
Floor 4 Posted 2004-03-01 00:00 ·  中国 上海 浦东新区 电信
初级用户
Credits 104
Posts 2
Joined 2004-03-01 00:00
22-year member
UID 18871
Gender Male
Status Offline
Floor 5 Posted 2004-03-03 00:00 ·  中国 湖南 长沙 电信
初级用户
★★
Credits 255
Posts 54
Joined 2003-10-24 00:00
22-year member
UID 11864
Gender Male
Status Offline
The poster above, what he wants is C-based, so why did you give him a C++ one?
Floor 6 Posted 2004-04-09 00:00 ·  加拿大 Bell
初级用户
Credits 110
Posts 5
Joined 2004-04-09 00:00
22-year member
UID 22088
Gender Male
Status Offline
I've thought about this problem too, it's just that what I know is QuickBASIC. One solution is to use the hard disk or virtual memory, write the large arrays into a data file, and write two subroutines, one for reading the file and one for writing the file. That way large arrays can be implemented.
Floor 7 Posted 2004-04-11 00:00 ·  中国 广东 珠海 电信
中级用户
★★
bbpc
Credits 340
Posts 107
Joined 2004-04-11 00:00
22-year member
UID 22257
Gender Male
Status Offline
It can be done in the following two ways
1. Enter protected mode, modify the segment size attribute, then exit back to real mode. That can use up to 4G of memory
2. Use it by loading himem.sys or emm386.exe
I've used both methods. The first one is a trick, the second is the common method. If needed I can give you the source code
x86!dos!
爱你就象老鼠爱大米
http://www.baby-pc.com/
Floor 8 Posted 2004-05-12 00:00 ·  中国 新疆 喀什地区 电信
初级用户
Credits 125
Posts 11
Joined 2004-05-09 00:00
22-year member
UID 23971
Gender Male
Status Offline
Could you send me a copy too,
I know that by using EMS (that is, the himeme.sys and emm386 you mentioned) it can use up to 16M,

>1、Enter protected mode, modify the segment size attribute, then exit back to real mode. That can use up to 4G of memory
Could you send me a copy of the source code? Thanks!

alhanhome@21cn.com
Floor 9 Posted 2004-05-14 00:00 ·  中国 辽宁 丹东 联通
中级用户
★★
Credits 316
Posts 74
Joined 2004-03-04 00:00
22-year member
UID 19167
Gender Male
Status Offline
XMS or EMS both map extended memory to a certain specific address segment in conventional memory,
if your program is not modified, it still cannot use extended memory
Floor 10 Posted 2004-05-20 00:00 ·  中国 北京 电信
初级用户
Credits 110
Posts 2
Joined 2004-05-12 00:00
22-year member
UID 24141
Gender Male
Status Offline
Could you send me a copy too, thanks
psjboy@tom.com
Floor 11 Posted 2004-05-20 00:00 ·  中国 北京 朝阳区 联通
初级用户
Credits 110
Posts 5
Joined 2004-05-20 00:00
22-year member
UID 24824
Gender Male
Status Offline
hhmmdd's program can access 4G of memory with a slight modification; it is just missing the check for the extended functions
If checking for XMS version not lower than 3, you need to use the extended function numbers 88H and 89H for function numbers 8 and 9
Floor 12 Posted 2004-05-23 00:00 ·  中国 湖北 武汉 电信
初级用户
Credits 114
Posts 5
Joined 2004-05-09 00:00
22-year member
UID 23954
Gender Male
Status Offline
2. Use up to 256M of memory by loading himem.sys or emm386.exe,
source code?
Could you send me one?
tiger205@163.net
Floor 13 Posted 2004-05-24 00:00 ·  中国 北京 联通
初级用户
Credits 110
Posts 5
Joined 2004-05-20 00:00
22-year member
UID 24824
Gender Male
Status Offline
This program only needs himem.sys, it has nothing to do with emm386.exe, and can access all memory (4G)

// --- xms.h

#ifndef _Head_XMS_H_
#define _Head_XMS_H_

#ifndef FarPtr
#define FarPtr(seg,ofs) ((void _seg *)(seg) + (void near *)(ofs))
#define GetSeg(p) ((unsigned)(void _seg *)(void far *)(p))
#define GetOfs(p) ((unsigned)(p))
#endif

class far TXmsMem
{
public:
typedef struct
{
unsigned long Size; //must be EVEN number
int SourHandle; //0: Conventional Memory
unsigned long SourOffset; //Offset/Conv. Mem. Address
int DestHandle; //0: Conventional memory
unsigned long DestOffset; //Offset/Conv. Mem. Address
}XMMStrcut;

int far DriverAvailable(void); //return true if xms driver installed
int far ExtendedVersion(void); //return true if > 64M memory supported

unsigned far GetVersion(void); //return BCD XMS Version
unsigned far GetRevision(void); //return BCD XMS Driver Revision
unsigned long far GetBlockFreeXMS(void); //(kb) ErrCode -> _XMS_Err
unsigned long far GetTotalFreeXMS(void); //(kb) ErrCode -> _XMS_Err

int far AllocXMS(int far *XMSHandle, unsigned long kbyte); //(kb) 0:error, ErrCode -> _XMS_Err
int far FreeXMS(int XMSHandle); //0:error, ErrCode -> _XMS_Err

//Size must be an EVEN number
//return 0 -> Error, ErrCode -> _XMS_Err
int far MemCpy(int hD, unsigned long oD, int hS, unsigned long oS, unsigned long Size);

int far LockXMSBlock(int Handle, long *Addr);
int far UnLockXMSBlock(int Handle);

far TXmsMem();

private:
static void far (*_XMS_Proc)(void);
static unsigned far _XMS_Version;
int far fXMSDriverInstalled(void);
void far fInitXMSProc(void);
};
extern TXmsMem far xms;

#endif


//--- xms.cpp


#include "xms.h"

void far (*TXmsMem::_XMS_Proc)(void) = 0;
unsigned far TXmsMem::_XMS_Version = 0x0000;
TXmsMem far xms;

far TXmsMem::TXmsMem()
{
if(!DriverAvailable())
{
if(fXMSDriverInstalled())
fInitXMSProc();
if(DriverAvailable())
_XMS_Version=GetVersion();
}
}

int far TXmsMem:riverAvailable(void)
{
return _XMS_Proc!=0;
}

int far TXmsMem::ExtendedVersion(void)
{
return _XMS_Version>=0x0300;
}

int far TXmsMem::fXMSDriverInstalled(void) //called only in XMS.CPP, not a public proc.
{
asm mov ax,0x4300
asm int 0x2f
return _AL==0x80;
}

void far TXmsMem::fInitXMSProc(void)
{
unsigned int Sgmt,Ofst;
asm mov ax,0x4310
asm int 0x2f
asm mov Ofst,bx
asm mov bx,es
asm mov Sgmt,bx
_XMS_Proc=(void far(*)(void)) FarPtr(Sgmt,Ofst);
}

unsigned far TXmsMem::GetVersion(void)
{
asm xor ah,ah
_XMS_Proc();
return _AX;
}

unsigned far TXmsMem::GetRevision(void)
{
asm xor ah,ah
_XMS_Proc();
return _BX;
}

unsigned long far TXmsMem::GetBlockFreeXMS(void) //return Size (kb); if Error, ErrCode -> _XMS_Err
{
if(ExtendedVersion())
{
unsigned FreeHi, FreeLo;
asm mov ah, 0x88
asm xor bl, bl
_XMS_Proc();
asm mov FreeLo, ax //OPSIZ:
asm db 0x66,0xc1,0xe8,0x10 //SHR EAX 10H
asm mov FreeHi, ax
return ((long)FreeHi< _XMS_Err
{
if(ExtendedVersion())
{
unsigned FreeHi, FreeLo;
asm mov ah, 0x88
asm xor bl, bl
_XMS_Proc();
asm mov FreeLo, dx //OPSIZ:
asm db 0x66,0xc1,0xea,0x10 //SHR EDX 10H
asm mov FreeHi, dx
return ((long)FreeHi< Success, 0 -> Error, ErrCode -> _XMS_Err
{
int Handle, AllocErr;
unsigned kbhi = kbyte>>16;
unsigned kblo = kbyte&0x0000ffff;

if(ExtendedVersion())
{
asm mov ah, 0x89
asm mov dx, kbhi //OPSIZ:
asm db 0x66,0xc1,0xe2,0x10 //SHL EDX, 10H
asm mov dx, kblo
_XMS_Proc();
asm mov AllocErr,ax
//asm mov _XMS_Err,bl
asm mov Handle,dx
*XMSHandle=Handle;
return AllocErr;
}
else
{
asm mov ah, 9
asm mov dx, kblo
_XMS_Proc();
asm mov AllocErr,ax
//asm mov _XMS_Err,bl
asm mov Handle,dx
*XMSHandle=Handle;
return AllocErr;
}
}

int far TXmsMem::FreeXMS(int XMSHandle) //return 1 -> Success, 0 -> Error, ErrCode -> _XMS_Err
{
asm mov ah,0x0a
asm mov dx,XMSHandle
_XMS_Proc();
//asm mov _XMS_Err,bl
return _AX;
}

int far TXmsMem::MemCpy(int hD, unsigned long oD, int hS, unsigned long oS, unsigned long Size)
{
//Size must be an EVEN number //return 1 -> Success, 0 -> Error, ErrCode -> _XMS_Err
XMMStrcut XMSS;
int XMSSSgmt, XMSSOfst;
void far (*XMS_Proc)(void)=_XMS_Proc;

XMSS.Size=Size; //must be EVEN number
XMSS.SourHandle=hS;
XMSS.SourOffset=oS;
XMSS.DestHandle=hD;
XMSS.DestOffset=oD;
XMSSSgmt=GetSeg(&XMSS);
XMSSOfst=GetOfs(&XMSS);

asm push ds
asm push es
asm mov si,XMSSSgmt
asm mov ds,si
asm mov si,XMSSOfst
asm mov ah,0x0b
XMS_Proc();
asm pop es
asm pop ds
//asm mov _XMS_Err,bl
return _AX;
}

int far TXmsMem::LockXMSBlock(int Handle, long *Addr)
{
unsigned int HighWord,LowWord,ErrCode;
asm mov ah,0x0c
asm mov dx,Handle
_XMS_Proc();
asm mov ErrCode,ax
asm mov HighWord,dx
asm mov LowWord,bx
//asm mov _XMS_Err,bl
*Addr=((unsigned long)HighWord<<16)+(unsigned long)LowWord;
return ErrCode;
}

int far TXmsMem::UnLockXMSBlock(int Handle)
{
asm mov ah,0x0d
asm mov dx,Handle
_XMS_Proc();
//asm mov _XMS_Err,bl
return _AX;
}


//-- Usage:

int XmsHandle;
if(!xms.AllocXMS(&XmsHandle,260)) //allocate memory
{
//error
}

//copy data (access XMS)

char Buf;

//Copy Bytes bytes from Buf to the memory location in XmsHandle starting at byte XMSOffset
xms.MemCpy(XmsHandle,XMSOffset, 0,(long)Buf, Bytes);

//Copy Bytes bytes from XmsHandle starting at XMSOffset to address Buf
xms.MemCpy(0,(long)Buf, XmsHandle,XMSOffset, Bytes);

//If the handle for the memory copy function is zero, the offset is the conventional memory address
//If the handle for the memory copy function is nonzero, the offset is the starting position in extended memory accessed through this handle

xms.FreeXMS(XmsHandle); //free memory, XMS memory must be freed when exiting the program
//If you forget to free it when the program exits, only a reboot can free it
Floor 14 Posted 2005-08-21 22:01 ·  中国 北京 航天信息网
新手上路
Credits 2
Posts 2
Joined 2005-08-21 21:51
20-year member
UID 41841
Gender Male
Status Offline
Use it by loading himem.sys or emm386.exe does anyone have C source code
Forum Jump: