Pet*_*ton 5 arrays assembly x86-64 nasm variable-length-array
我正在编写一个汇编程序,我希望能够执行以下(基本)操作:
x = 100;
y = int[x]
Run Code Online (Sandbox Code Playgroud)
例如,y 的大小取决于 x 的值。
注意:我在 64 位 Ubuntu 系统上使用 NASM 指令集。
在汇编中,我知道数组的大小需要在文件的数据部分中声明,例如
myvariable resq 1000
Run Code Online (Sandbox Code Playgroud)
问题是在我完成之前的计算之前我不知道它有多大。我真正想要的是这样的:
mov rax, 100
myvariable resq rax
Run Code Online (Sandbox Code Playgroud)
但这是不允许的吧?只是对汇编中的数组访问/声明有些困惑。
任何指示表示赞赏!
仅当您在堆栈上声明数组或使用 malloc 或类似方法从堆中提取内存时,您的 C 示例才可能。对于小值,使用堆栈非常好(而且更快):
mov rax, 100 # 100 elements
shl rax, 3 # multiply with 8, the size of an element
sub rsp, rax # rsp now points 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 points to the return address again
Run Code Online (Sandbox Code Playgroud)