问题描述
限时送ChatGPT账号..for (int i = 0; i < 10000; i++)
a[i] = b[i] + c[i]
这种高级语言的 ARM 程序集是什么样的?
What does the ARM assembly for this high level language look like?
我还假设 A 的基址在 R8 中,基址B的地址在R9中,C的基地址在R10中和 A,B,C 都是 int 数组
I'm also assuming the base address of A is in R8, the base address of B is in R9, and the base address of C is in R10 and A,B,C are all int arrays
非常感谢
我试过了:
MOV R0, #0 ; Init r0 (i = 0)
Loop:
a[i] = b[i] + c[i] //How to fix this?
ADD R0, R0, #1 ;Increment it
CMP R0, #1000 ;Check the limit
BLE Loop ;Loop if not finished
推荐答案
假设这种高级语言与 C 没有任何冲突,您可以使用 arm C 编译器从您的代码片段创建汇编代码.例如,如果您在 test.c 中有以下内容,
Assuming this high level language doesn't have anything conflicting with C, you can use an arm C compiler to create assembly code from your snippet. For example if you have the following in test.c,
void test() {
register int i asm("r0");
register int *a asm("r8");
register int *b asm("r9");
register int *c asm("r10");
for (i = 0; i < 10000; i++) {
a[i] = b[i] + c[i];
}
}
你可以跑
arm-linux-androideabi-gcc -O0 -S test.c
创建一个 test.s 文件,该文件将包含您的测试函数的汇编代码以及一些额外的东西.您可以在下面看到您的循环是如何编译成程序集的.
to create a test.s file, which will contain assembly code for your test function as well as some extra stuff. You can see how your loop got compiled into to assembly below.
<snipped>
.L3:
mov r2, r8
mov r3, r0
mov r3, r3, asl #2
add r3, r2, r3
mov r1, r9
mov r2, r0
mov r2, r2, asl #2
add r2, r1, r2
ldr r1, [r2, #0]
mov ip, sl
mov r2, r0
mov r2, r2, asl #2
add r2, ip, r2
ldr r2, [r2, #0]
add r2, r1, r2
str r2, [r3, #0]
mov r3, r0
add r3, r3, #1
mov r0, r3
.L2:
mov r2, r0
ldr r3, .L5
cmp r2, r3
ble .L3
sub sp, fp, #12
ldmfd sp!, {r8, r9, sl, fp}
bx lr
<snipped>
现在这种方法的问题是相信编译器会为您的研究生成最佳代码,这可能并非总是如此,但您将得到像上述问题的快速答案,而不是等待人们:)
Now the problem with this approach is trusting the compiler generates the optimal code for your study, which might not be always the case but what you'll get is fast answers to your questions like above instead of waiting for people :)
-- 额外--
GCC 允许您将变量放入某些寄存器,请参阅 相关文档.
GCC allows you to put variables into certain registers, see related documentation.
您可以在此处获取手臂说明备忘单.
较新版本的 GCC 创建了更好的 arm 代码,正如人们所期望的那样.以上截图是由 4.4.3 版本生成的,我可以确认 Linaro 的 4.7.1 证明了我的主张.因此,如果您采用我的方法,请使用您可以获得的最新工具链.
Newer versions of GCC creates better arm code as one would expected. Above snipped is generated by version 4.4.3, and I can confirm Linaro's 4.7.1 proves my claim. So if you take my approach use the most recent tool chain you can get.
这篇关于ARM汇编循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论