声明装配可变大小的数组 [英] Declaring variable-sized arrays in assembly

查看:172
本文介绍了声明装配可变大小的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写,我希望能够做的(基本)以下的汇编程序:

I'm writing an assembly program which I want to be able to do the (basic) following:

x = 100;
y = int[x]

例如。 y的大小取决于x的值

E.g. the size of y depends on the value of x.

请注意:我使用的是64位的Ubuntu系统上NASM指令集

NOTE: I am using NASM instruction set on a 64 bit Ubuntu system.

在组件我知道需要在文件的数据部分中声明的阵列的尺寸例如

In assembly I know that the size of an array needs to be declared in the data section of the file e.g.

myvariable resq 1000

问题是我不知道有多大使它,直到我做了previous计算。
我真正想要的是一样的东西:

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.

任何指针AP preciated!

Any pointers appreciated!

推荐答案

如果你声明在堆栈上的数组,或者如果你拉从使用malloc或类似的堆内存您的C例子仅仅是可能的。对于小值其完美的罚款(和更快)使用堆栈:

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. 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

这篇关于声明装配可变大小的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆