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

时间:2011-04-06 03:08:34

标签: assembly memory-management malloc nasm

所以我需要在汇编中做这样的事情

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

用户输入大小并根据大小调用while循环来填充数组。

所以我需要一个指向空间malloc的指针已经创建了如何在屁股86中执行此操作? 此外,我在哪里存储此指针,以便稍后我可以使用该数组。要说在输入上做二进制搜索吗?

问候 ARR

5 个答案:

答案 0 :(得分:2)

不知怎的,我猜这不是你想做的事。

简单地说,在装配中使用mallocing内存并不是特别容易。

如果你要对系统函数进行调用,那么你很容易理解你的操作系统例程,库和链接器的调用约定。你可能会通过一个中断来调用某种操作系统功能,而这又取决于操作系统。

因为通常情况下,汇编程序往往具有相当静态的内存视图,并定义自己的内存映射。您可以分配一大块数据,然后根据该块确定您自己的“malloc”。例程加载时,原始块将成为您的块。这可能更接近你想要做的事情,但显然它可以做更多的工作。

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

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

答案 1 :(得分:2)

假设您处于Win并制作控制台应用程序(下次提供更多详细信息),在汇编语言艺术中找到的解决方案(大写字母,因为这是伟大的书籍)是:

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页如果你需要它;)

答案 2 :(得分:1)

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

This tutorial显示了如何做到这一点。不用担心,此技术不仅限于Mac OS X.

答案 3 :(得分:0)

C编译成Assembly,生成的Assembler代码也会调用malloc。 sizeof(int)应为x86中的4字节/ 32位/双字。

答案 4 :(得分:0)

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

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

如果这是在一个函数中,而不是在顶层,那么该函数应该使用堆栈框架(push ebp / mov ebp, esp,函数体,leave / {{1} })恢复ret,以便esp您的其他注册。