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

发布于 2024-10-30 19:11:55 字数 239 浏览 4 评论 0原文

所以我需要在汇编中执行类似的操作

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

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

所以我需要一个指向 malloc 创建的空间的指针,我该如何在 ass 86 中执行此操作? 另外,我在哪里存储这个指针,以便稍后可以使用该数组。是说对输入进行二分搜索?

问候 到达

So i need to do something like this in assembly

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

where the user inputs the size and based on the size, a while loop will be called to populate the array.

So i need a pointer to the space malloc has created how do i do this in ass 86?
Also, where do i store this pointer so i can use the array later on. To say do a binary search on the input?

regards
arr

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

幸福不弃 2024-11-06 19:11:55

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

简而言之,在汇编中分配内存并不是特别容易。

如果您要对系统函数进行调用,并且需要了解操作系统例程、库和链接器的调用约定,那么这很容易。您可以通过中断来调用某种操作系统函数,这又取决于操作系统。

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

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

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

Somehow I'm going to guess that this isn't what you want to do.

Simply, mallocing memory in assembly isn't particularly easy.

It's easy if you're going to do a callout to a system function, in the case you need to understand the calling conventions for your operating system routines and libraries and linkers. You could be calling some kind of OS function, through, perhaps, an interrupt, and this, again, depends on the operating system.

Because normally, assembly programs tend to have pretty static views of memory, and define their own memory map. You could allocate a large block of data, and then right your own "malloc" against that block. The original block will become yours when the routine loads. This is probably closer to what you want to do, but obviously it can be a lot more work.

If you're not allocating more than one array at a time, simply define a block in the assembly source that is "big enough" (10,000 integers, say). Then you can just use that.

Assuming you do call some memory allocation routine, the result will be returned either on the stack, or in a register. You would then either leave that value in a register dedicated to the task of holding it for the remainder of your processing, or you can simply store the value in to memory someplace else, and then load it when you need it later.

最美的太阳 2024-11-06 19:11:55

假设您在 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 页(如果需要);)

Supposing you're under Win and making a console app (give more details next time), the solution found in The Art Of Assembly Language (in capital letters because that's a great book) is:

13.3.6.1 Allocate Memory

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 Deallocate Memory

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 Modify Memory Allocation

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.

page 719 if you need it ;)

兰花执着 2024-11-06 19:11:55

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

本教程展示了如何操作。不用担心,这种技术并不局限于 Mac OS X。

You could call malloc() directly from your asm code. Really!

This tutorial shows how to do it. Don't worry, this technique is not restricted to Mac OS X.

苍景流年 2024-11-06 19:11:55

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

; 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 所以您可以弹出您的其他寄存器。

在执行此操作之前,通常应将 EAX 舍入为 16 的倍数,以保持堆栈对齐。例如添加eax,15 / 和eax,-16。或者,如果您的系统或函数没有保持比这更多的堆栈对齐,则只有 4。

If you don't need the allocation to last beyond the scope of the current function, just allocate on the stack:

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

If this is in a function, not at the top level, then the function should use a stack frame (push ebp / mov ebp, esp, function body , leave / ret) to restore esp so you can pop your other registers.

You should normally round EAX up to a multiple of 16 before doing this, to preserve stack alignment. e.g. add eax, 15 / and eax, -16. Or only 4 if your system or function doesn't maintain more stack alignment than that.

眸中客 2024-11-06 19:11:55

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

C compiles into Assembly, and the generated Assembler code will also call malloc.
sizeof(int) should be 4 byte / 32 bits / dword in x86.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文