简体   繁体   English

在汇编中声明可变大小的数组

[英]Declaring variable-sized arrays in assembly

I'm writing an assembly program which I want to be able to do the (basic) following: 我正在编写一个汇编程序,希望能够执行以下(基本)操作:

x = 100;
y = int[x]

Eg the size of y depends on the value of x. 例如,y的大小取决于x的值。

NOTE: I am using NASM instruction set on a 64 bit Ubuntu system. 注意:我在64位Ubuntu系统上使用NASM指令集。

In assembly I know that the size of an array needs to be declared in the data section of the file eg 在汇编中,我知道需要在文件的数据部分声明数组的大小,例如

myvariable resq 1000

The problem is I won't know how big to make it till I have done a previous calculation. 问题是在完成之前的计算之前,我不知道要制作多大的尺寸。 What I really want is something like: 我真正想要的是这样的:

mov rax, 100
myvariable resq rax

But that's not allowed right? 但这是不允许的吗? Just having some confusion over array access/declarations in assembly. 只是对汇编中的数组访问/声明有些困惑。

Any pointers appreciated! 任何指针表示赞赏!

Your C example is only possible if you declare the array on the stack or if you pull the memory from the heap with malloc or similar. 仅当您在堆栈上声明数组或使用malloc或类似方法从堆中拉出内存时,C示例才可行。 For small values its perfectly fine (and faster) to use the stack: 对于较小的值,使用堆栈非常好(并且更快):

mov rax, 100   # 100 elemtents
shl rax, 3     # muliply with 8, the size of an element
sub rsp, rax   # rsp points now to your array

# do something with the array
mov rbx, [rsp]    # load array[0] to rbx
mov [rsp+8], rbx  # store to array[1]

add rsp, rax   # rsp point to the return address again

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

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