汇编:没有malloc和syscall的动态内存分配? [FreeDOS应用程序] [英] Assembly: dynamic memory allocation without malloc and syscalls? [FreeDOS application]

查看:133
本文介绍了汇编:没有malloc和syscall的动态内存分配? [FreeDOS应用程序]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于程序集(尤其是MASM)中动态内存分配的逻辑.关于此主题的文章很多,而且所有文章都依赖于malloc或brk的使用.但是,根据我的理解,必须(或可以)肯定地将malloc作为C语言的一部分用汇编语言编写.等同于brk,因为它是操作系统的一部分,因此也写在C上,可以用汇编1对1替换.很久很久以前,我在PCMag中看到了一篇有关使用纯asm在MS-DOS中进行动态内存分配的文章.不幸的是,我已经失去了这段精彩作品的所有痕迹.现在,我正在使用FreeDOS(可精确引导的FreeDOS闪存卡),并且想知道如果有人决定编写自己的内存分配器该如何进行?不依赖操作系统机制的内存分配的起点和逻辑是什么?

My question is about the logic of dynamic memory allocation in assembly (particularly, MASM). There are lot of articles on this topic and all of them rely on the use of malloc or brk. However, according to my understanding, malloc as a part of C language must (or could) be certainly written on assembly. Idem for brk, because it's a part of the operating system, thus also written on C which can be replaced 1 to 1 by assembly. Very very long time ago I have seen an article in PCMag about dynamic memory allocation in MS-DOS using pure asm. Unfortunately, I have lost all the traces of this wonderful piece of writing. Now I'm working with FreeDOS (precisely bootable FreeDOS flash card) and wondering how to proceed if someone decides to write his own memory allocator? What is the starting point and the logic of memory allocation without relying on OS mechanisms?

推荐答案

当DOS加载.COM程序时,它将640KB区域(0a000h:00000h以下)中的所有可用内存分配给该程序,并且该程序可以管理自己的记忆.如果需要使用MSDOS内存管理,则程序首先必须使用INT 21H, AH=49H, ES=segment, BX=# paragraphs释放内存.然后,它可以使用INT 21H, AH=48H, BX=# paragraphs分配内存.

When DOS loads a .COM program, it allocates all of the memory available in the 640KB area (below 0a000h:00000h) to the program, and the program can manage its own memory. If it is desired to use MSDOS memory management, the program first has to release the memory using INT 21H, AH=49H, ES=segment, BX=# paragraphs. It can then use INT 21H, AH=48H, BX=# paragraphs, to allocate memory.

如评论中所述,.EXE程序可能会或可能不会分配640KB区域中的所有内存.

As noted in the comments, an .EXE program may or may not allocate all of the memory in the 640KB area.

.COM汇编代码示例,以释放然后分配所有可用内存. MSDOS通常会消耗16个字节的开销.在此示例中,将BX设置为代码的末尾,然后设置为下一个段落边界,该边界位于代码末尾之后256字节,以用作堆栈空间.该堆栈的末尾是INT 21H, AH=4AH调用释放的内存的基础.

Example .COM assembly code, to release, and then allocate all available memory. MSDOS will generally consume 16 bytes for its overhead. In this example, BX is set to the end of the code, then set to the next paragraph boundary that is 256 bytes past the end of the code to use as stack space. The end of this stack is the base of the memory released by the INT 21H, AH=4AH call.

        .286
        .model  tiny,c
        .code
        org     0100h
;       cs,ds,es,ss = program segment prefix, sp = 0fffeh
start:  mov     bx,offset cdend         ;set bx=end stack
        add     bx,0010fh
        and     bx,0fff0h
        mov     sp,bx                   ;sp = new end of stack
        mov     cl,4                    ;release memory
        shr     bx,cl
        mov     ax,04a00h
        int     21h
        mov     ax,04800h               ;set bx = available memory
        mov     bx,0ffffh
        int     21h
        mov     ax,04800h               ;allocate all of it
        int     21h                     ; returns segment in ax
exit:   mov     ax,04c00h               ;exit
        int     21h
cdend:
        end     start

这篇关于汇编:没有malloc和syscall的动态内存分配? [FreeDOS应用程序]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆