繁体   English   中英

如何使用nasm为程序集8086中的数组动态分配内存

[英]how to dynamically allocate memory for an array in assembly 8086 using nasm

所以我需要在组装中做这样的事情

int *arr = malloc(sizeof (int) * size);

用户在其中输入大小并基于该大小,将调用while循环来填充数组。

所以我需要一个指向malloc已创建的空间的指针,该如何在ass 86中做到这一点? 另外,我在哪里存储该指针,以便以后可以使用该数组。 要说对输入进行二进制搜索吗?

问候arr

我会以某种方式猜测这不是您想要的。

简单地说,在汇编中分配内存并不是特别容易。

如果您要对系统函数进行调出,这很容易,但是在这种情况下,您需要了解操作系统例程,库和链接器的调用约定。 您可能通过中断来调用某种OS功能,而这又取决于操作系统。

因为通常,汇编程序倾向于具有非常静态的内存视图,并定义自己的内存映射。 您可以分配一个大数据块,然后将您自己的“ malloc”对应于该数据块。 例程加载后,原始块将成为您的块。 这可能更接近您想要做的事情,但是显然它可能需要做更多的工作。

如果一次不分配多个数组,只需在汇编源中定义一个“足够大”的块(例如10,000个整数)。 然后,您可以使用它。

假设您确实调用了一些内存分配例程,那么结果将在堆栈或寄存器中返回。 然后,您可以将该值保留在专用于在其余处理过程中保存该值的任务的寄存器中,也可以将其简单地存储到其他地方的内存中,然后在以后需要时加载它。

假如你赢下,使一个控制台应用程序(提供更多的细节在下一次),在艺术的汇编语言中发现(以大写字母,因为这是一个伟大的书)的解决方案是:

13.3.6.1分配内存

Function (ah):     48h  
Entry parameters:  bx- Requested block size (in paragraphs)  
Exit parameters:   If no error (carry clear):  
                       ax:0 points at allocated memory block  
                   If an error (carry set):  
                       bx- maximum possible allocation size  
                       ax- error code (7 or 8)  

This call is used to allocate a block of memory. On entry into DOS,
bx contains the size of the requested block in paragraphs (groups of
16 bytes). On exit, assuming no error, the ax register contains the
segment address of the start of the allocated block. If an error
occurs, the block is not allocated and the ax register is returned
containing the error code.  
If the allocation request failed due to insufficient memory, the bx 
register is returned containing the maximum number of paragraphs actually
available.

13.3.6.2释放内存

Function (ah):    49h  
Entry parameters: es:0- Segment address of block to be deallocated  
Exit parameters:  If the carry is set, ax contains the error code (7,9)  

This call is used to deallocate memory allocated via function 48h above. The
es register cannot contain an arbitrary memory address. It must contain a
value returned by the allocate memory function. You cannot use this call to
deallocate a portion of an allocated block. The modify allocation function
is used for that operation.

13.3.6.3修改内存分配

Function (ah):     4Ah
Entry parameters:  es:0- address of block to modify allocation size
                   bx- size of new block
Exit parameters:   If the carry is set, then ax contains the error code 7, 8,
                   or 9
                   bx contains the maximum size possible (if error 8)

This call is used to change the size of an allocated block. On entry, es must
contain the segment address of the allocated block returned by the memory
allocation function. Bx must contain the new size of this block in paragraphs.
While you can almost always reduce the size of a block, you cannot normally
increase the size of a block if other blocks have been allocated after the
block being modified. Keep this in mind when using this function.

第719页(如果需要);)

您可以直接从您的asm代码中调用malloc() 真!

本教程说明了如何进行。 不用担心,此技术不仅限于Mac OSX。

C编译为Assembly,并且生成的Assembler代码也将调用malloc。 在x86中,sizeof(int)应该为4字节/ 32位/ dword。

如果不需要分配超出当前函数的范围,则只需在堆栈上分配:

; calculate allocation size in eax, for example
sub   esp, eax
; esp points to the memory

如果这在函数中而不是在顶层,则该函数应使用堆栈帧( push ebp / mov ebp, esp ,function body, leave / ret )来还原esp以便可以pop其他寄存器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM