FASM:动态数组

发布于 2024-12-29 10:46:47 字数 94 浏览 1 评论 0原文

如何将变量存储在数组中,其大小仅在运行时才知道?我怎样才能访问这个数组的元素?我认为这应该很容易,但我没有找到办法。

我的意思是 C 中的动态数组之类的东西。

How can I store variables in an array, which size is known only on run-time? How can I access elements of this array? I think it should be easy, but I don't see a way.

I mean something like dynamic arrays in C.

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

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

发布评论

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

评论(3

任性一次 2025-01-05 10:46:47

您没有说明哪个操作系统,但在 Windows 下, VirtualAlloc 是分配粗略内存块的简单方法。它返回一个指针,您可以将其加载到寄存器中并用作基地址。

invoke  VirtualAlloc,NULL,size,MEM_COMMIT+MEM_RESERVE,PAGE_READWRITE
mov     [eax],something

You don't state which operating system, but under Windows, VirtualAlloc is an easy way of allocating coarse blocks of memory. It returns a pointer which you can load into a register and use as a base address.

invoke  VirtualAlloc,NULL,size,MEM_COMMIT+MEM_RESERVE,PAGE_READWRITE
mov     [eax],something
终难愈 2025-01-05 10:46:47

您还可以在函数开始时在堆栈上分配静态大小的内存:

proc yourFunction stdcall param1:DWORD
local yourData[256]:BYTE
  ;...
endp

它的缺点是具有静态大小(上例中为 256 字节),但您不必调用特定于平台的 API,如 VirtualAlloc 及其当您离开函数时会被清理(无需跟踪分配的数据并调用 VirtualFree())。

You could also allocate memory with a static size on stack at the beginning of your function:

proc yourFunction stdcall param1:DWORD
local yourData[256]:BYTE
  ;...
endp

It has the disadvantage of having a static size (256 bytes in the example above) but you don't have to call plattform specific APIs like VirtualAlloc and it is cleaned up when you leave your function (no need to keep track of your allocated data and call VirtualFree()).

萝莉病 2025-01-05 10:46:47

对于 WinAPI,这将是这样的:

invoke HeapAlloc, hHeap, flags, size
mov    [pointer], eax

有关更多信息,请参阅此 (HeapAlloc)

https://learn.microsoft.com/ en-us/windows/win32/api/heapapi/nf-heapapi-heapalloc

和这个(Windows 中的堆)

https://learn.microsoft.com/en-us/windows /win32/api/heapapi/

For WinAPI this would be sth like:

invoke HeapAlloc, hHeap, flags, size
mov    [pointer], eax

For more information see this (HeapAlloc)

https://learn.microsoft.com/en-us/windows/win32/api/heapapi/nf-heapapi-heapalloc

and this (Heaps in Windows)

https://learn.microsoft.com/en-us/windows/win32/api/heapapi/

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