如何在armv8汇编中创建数组?
我觉得这是一个简单的问题,但到目前为止我无法在互联网上或我的任何教科书或课堂幻灯片中找到它。我有一个项目,如果不先创建一个数组就无法启动,所以我只是问如何将
int A[10];
(A[10] 当然是一个大小为 10 的数组)
转换为Armv8汇编代码
编辑:老实说,我不明白回答我的问题的问题,如果有帮助的话,它正在DS-5中编译
I feel like this is a simple question, but I am not able to find it anywhere on the internet or in any of my textbooks or powerpoints of my class so far. I have a project that I cannot start without first creating an array, so I am just asking how to translate
int A[10];
(A[10] of course being an array of size 10)
into Armv8 assembly code
edit: I honestly don't understand the questions in response to my question, it is being compiled in DS-5 if that helps
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于堆栈上的本地数组,它与其他架构几乎相同。只需减去堆栈指针并将您的值存储在那里即可。
对于
int a[10]
,sub sp, sp, #48
将在堆栈上分配数组。正如 Nate Eldredge 在评论中提到的,ARMv8 需要硬件进行 16 字节堆栈对齐,因此您不能编写sub sp, sp, #40
。您可以存储一些值,例如*a = r
的str r, [sp]
和str r, [sp, #4]
a[1] = r
,依此类推。对于全局数组,只需在
.bss
中定义一个符号并为其分配足够的空间即可。这会分配一个具有 10 个 32 位 int 的全局数组。
此类全局数组属于可执行文件中的某个部分。您可以使用自定义的读写可执行质量创建您喜欢的任何部分,但通常,非零初始化的可修改数据位于
.data
部分,而可修改的全零数据位于 < code>.bss 部分。有关基本详细信息,请参阅此处。您可以随时查看 Godbolt 来查看 C 中的每个语句如何转换为汇编语言。检查是否有优化,两者都会给你不同的有趣信息。
例如,以下是带有
-O3
的 Clang 如何在 C 语言中翻译这个简单的代码。Godbolt 指令未被过滤,因此部分切换可见。 (上面的块已过滤掉指令,但 .section 指令除外。)
了解本地数组和全局数组的分配和访问方式有何不同。我相信现在您有更具体的问题,以防您仍然遇到问题。
For local arrays on the stack, it's pretty much same as other architectures. Just subtract the stack pointer and store your values there.
For
int a[10]
,sub sp, sp, #48
will allocate your array on the stack. As Nate Eldredge mentioned in a comment, ARMv8 requires 16-byte stack alignment from hardware, so you cannot writesub sp, sp, #40
. You can store some value likestr r, [sp]
for*a = r
, andstr r, [sp, #4]
fora[1] = r
, and so on.For global arrays, just define a symbol in
.bss
and allocate enough space for it.This allocates a global array with 10 32-bit
int
s.Such global arrays belong to a certain section in an executable file. You can make whatever section you like with custom read-write-executable qualities, but usually, non-zero-initialized modifiable data goes in the
.data
section, while modifiable all-zero data goes in the.bss
section. See here for basic details.You can always check Godbolt to see how each statement in C is translated to assembly. Check with and without optimizations, both will give you different interesting information.
For example, here's how Clang with
-O3
translates this simple code in C.Godbolt with directives not filtered so the section switching is visible. (The block above has directives filtered out, except for .section directives.)
See how a local array and a global array are allocated and accessed differently. I believe now you have more specific questions in case you still have a problem.