首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >16位DOS中的XMS分配

16位DOS中的XMS分配
EN

Stack Overflow用户
提问于 2010-12-01 11:01:45
回答 3查看 1.4K关注 0票数 3

请原谅我的亡灵尝试,但我实际上需要为16位DOS (!)编写一些代码。我必须验证一段软件在为16位平台构建时是否正确执行,我发现我们的XP工作站实际上可以运行16位DOS应用程序,这使得使用现有的批处理测试系统成为可能。

无论如何,该软件由一个库和一个数据库组成。较小的数据库(最多150 is )既可以定义为静态全局数组,也可以从文件中读取到用halloc()分配的缓冲区,因此我确信库和测试工具的构建是正确的。

然而,我们也有一些更大的数据库需要测试,高达1.8MB。这太大了,无法正常分配,所以我编写了一个小的支持库来分配XMS内存。这样,我就可以在一个小玩具程序中成功地分配、使用(即写入和读取数据),并释放高达16 to的数据。但是,在“真实”应用程序中使用XMS工具时,我会得到以下错误:

代码语言:javascript
复制
The NTVDM CPU has encountered an illegal instruction.
CS:0000 IP:00ba OP:0f 04 10 0e 51

在谷歌上搜索这个错误并没有给出什么相关的结果,这类错误似乎通常被归咎于杂项恶意软件。

代码库是严格的C90,目前用于DOS构建的编译器是使用“大”内存模型的OpenWatcom 1.9。生成时没有警告或错误。

XMS支持库如下所示,在调用xmsmalloc()之后似乎会发生错误:

代码语言:javascript
复制
/* This file implements rudimentary XMS memory handling.
 * Documentation on the XMS API was found on http://www.qzx.com/pc-gpe/xms30.txt
 */

#include <stddef.h> /* Definition of NULL */
#include <limits.h> /* Definition of UINT_MAX */
#include <stdio.h>  /* fprintf and (FILE *) */

/* Allow external configuration of maximum concurrent XMS allocations */
#ifndef MAX_XMS_ALLOCATIONS
#define MAX_XMS_ALLOCATIONS 4
#endif

/* Address of the XMS driver */
static long XMSControl;

/* Mapping of XMS handle <-> normal pointer */
typedef struct {
    unsigned int XMSHandle;
    void huge * XMSPointer; 
} XMSHandleMap;

static XMSHandleMap allocMap[MAX_XMS_ALLOCATIONS];

/* Set up the XMS driver, returns 0 on success and non-zero on failure */
static int initxms(void)
{
    char XMSStatus = 0;

    if ( XMSControl == 0 )
    {
        __asm {
        ; Is an XMS driver installed?
            mov ax,4300h
            int 2Fh
            mov [XMSStatus], al
        }

        if ( XMSStatus == 0x80 )
        {
            __asm {
            ; Get the address of the driver control function
                mov ax,4310h
                int 2Fh
                mov word ptr [XMSControl]  ,bx
                mov word ptr [XMSControl+2],es
            }
        }
    }

    return ( XMSControl == 0 );
}

/* Allocate a slab of memory from XMS */
void huge * xmsmalloc(long unsigned int size)
{
    unsigned int kB;
    unsigned int XMSStatus = 0;
    unsigned int XMSHandle = 0;
    void huge * XMSPointer = NULL;
    int n;

    /* If we can not initialize XMS, the allocation fails */
    if ( initxms() )
        return NULL;

    /* It is not possible to allocate more kilobytes than a 16-bit register can hold :-) */
    if ( size / 1024 > UINT_MAX )
        return NULL;

    /* Get the next free entry in the handle <-> pointer mapping */
    for ( n = 0; n < MAX_XMS_ALLOCATIONS; n++ )
    {
        if ( allocMap[n].XMSPointer == NULL )
            break;
    }

    if ( n == MAX_XMS_ALLOCATIONS )
        return NULL;

    kB = size / 1024 + (size % 1024 > 0);

    __asm {
    ; Allocate [kB] kilobytes of XMS memory
        mov ah, 09h
        mov dx, [kB]
        call [XMSControl]
        mov [XMSStatus], ax
        mov [XMSHandle], dx
    }

    /* Check if XMS allocation failed */
    if ( XMSStatus == 0)
        return NULL;

    __asm {
    ; Convert XMS handle to normal pointer
        mov ah, 0Ch
        mov dx, [XMSHandle]
        call [XMSControl]
        mov [XMSStatus], ax

        mov word ptr [XMSPointer],  bx 
        mov word ptr [XMSPointer+2],dx
    }

    if ( XMSStatus == 0 )
    {
        /* Lock failed, deallocate the handle */
        __asm {
        ; Free XMS handle
            mov ah, 0Ah
            mov dx, [XMSHandle]
            call [XMSControl]

        ; Return value is not really interesting 
        ;   mov [XMSStatus], ax
        }
        return NULL;
    }

    /* Create an entry in the handle <-> pointer mapping */
    allocMap[n].XMSHandle = XMSHandle;
    allocMap[n].XMSPointer = XMSPointer;

    return XMSPointer;
}

/* Free a pointer allocated with xmsalloc */
void xmsfree(void huge * XMSPointer)
{
    int n;

    if ( XMSPointer == NULL )
        return;

    if ( initxms() ) 
        return;

    for ( n = 0; n < MAX_XMS_ALLOCATIONS; n++ )
    {
        if ( allocMap[n].XMSPointer == XMSPointer )
        {
            int XMSHandle = allocMap[n].XMSHandle;

            __asm {
            ; Unlock handle so we can free the memory block
                mov ah, 0Dh
                mov dx, [XMSHandle]
                call [XMSControl]

            ; Free XMS memory
                mov ah, 0Ah
                mov dx, [XMSHandle]
                call [XMSControl]

            ; Return value ignored
            }

            /* Clear handle <-> pointer map entry so it can be reused */
            allocMap[n].XMSHandle = 0;
            allocMap[n].XMSPointer = NULL;

            return;
        }
    }
}

/* Write a memory report for debugging purposes */
void xmsreport(FILE * stream)
{
    int XMSVersionNumber = 0;
    int XMSLargestBlock = 0;
    int XMSTotal = 0;

    if ( initxms() ) 
    {
        puts("Could not initialize XMS Driver!");
        return;
    }

    __asm {
    ; Get the driver version number
        mov ah,00h
        call [XMSControl] ; Get XMS Version Number
        mov [XMSVersionNumber], ax

    ; Get the amount of free XMS memory
        mov ah, 08h
        call [XMSControl]
        mov [XMSLargestBlock], ax
        mov [XMSTotal], dx
    }

    fprintf(stream, "XMS Version number: %d\n", XMSVersionNumber);
    fprintf(stream, "Largest available block: %d kB (%d kB total)\n", XMSLargestBlock, XMSTotal);
}

一些具体问题:

在哪里可以找到有关错误消息的更多信息(我猜OP意味着操作码,但其他字段呢?)在运行Windows时,我找到的XMS引用是否仍然适用?或者是否有一些更新的版本-- reference?

  • Could我应该使用内联汇编程序来破坏系统状态?我该如何解决这个问题呢?

  • 您对如何解决这个问题有什么更好的想法吗?::) DOS扩展程序似乎需要32位模式,整个练习的重点是使用16位模式。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-02-08 08:49:24

我必须回答我自己的问题。

虽然可以使用XMS例程分配相对较大的内存,但不可能用16位指令来解决它。返回值是一个32位的线性地址,如果我正确地理解了这一点,那么就不可能在16位环境中使用它。

应该可以通过将内存块的一部分映射到1Mb可寻址限制以下的本地内存缓冲区来使用该内存块,并移动窗口,就像EMS的工作方式一样,但这根本不值得麻烦。

票数 2
EN

Stack Overflow用户

发布于 2010-12-16 09:27:24

我看到了两个问题。

第一:确保您的编译器使用的是远调用而不是近调用,否则您将跳到错误的段并执行未知代码,并生成无效的操作码.这似乎就是正在发生的事。如果编译器默认为近调用,请在代码中尝试"call far XMSControl“。

第二: NTVDM.EXE在虚拟86模式下运行代码,而不是真实模式。虽然windows支持16位应用程序是事实,但它的支持有限。因此,您可能会在数据库中遇到其他问题。例如,您将无法访问USB,因此您不能在USB打印机上打印,并且需要使用旧的并行口式打印机。祝你好运找到一个在车库拍卖时用的..。

票数 2
EN

Stack Overflow用户

发布于 2010-12-01 11:09:01

‘0f04100E51’是无效指令本身,您正在尝试执行。

我已经检查过了,我没有看到这个代码0F04的x86指令

http://ref.x86asm.net/geek.html#x0F04

因此,您需要找到生成此类无效指令的代码并对其进行修复。

至于内存,我一直认为EMS比XMS更容易使用,但我可能错了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4323622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档