如何使用 nasm 为程序集 8086 中的数组动态分配内存
所以我需要在汇编中执行类似的操作
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不知怎的,我会猜测这不是你想做的。
简而言之,在汇编中分配内存并不是特别容易。
如果您要对系统函数进行调用,并且需要了解操作系统例程、库和链接器的调用约定,那么这很容易。您可以通过中断来调用某种操作系统函数,这又取决于操作系统。
因为通常情况下,汇编程序往往具有相当静态的内存视图,并定义自己的内存映射。您可以分配一个大的数据块,然后针对该块调整您自己的“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.
假设您在 Win 下制作一个控制台应用程序(下次提供更多详细信息),在《汇编语言的艺术》(大写字母,因为这是一本伟大书)中找到的解决方案是:
13.3.6.1 分配内存
13.3.6.2 取消分配内存
13.3.6.3 修改内存分配
第 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
13.3.6.2 Deallocate Memory
13.3.6.3 Modify Memory Allocation
page 719 if you need it ;)
您可以直接从 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.
如果不需要最后的分配超出当前函数的范围,只需在堆栈上分配:
如果这是在函数中,而不是在顶层,则该函数应该使用堆栈帧(
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:
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 restoreesp
so you canpop
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.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.