这条 ARM 线是如何工作的? [英] How does this line of ARM work?
本文介绍了这条 ARM 线是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在弄清楚此汇编代码的工作原理时遇到了一些麻烦.我知道它循环 16 次并更新每次迭代的总数.特别是在 .L2 标签的那一行,自上一行以来不会出现分段错误:ldr ip, [ip, #0] reassigns ip?有人可以解释这是如何工作的吗?提前感谢您抽出宝贵时间.
I am having some trouble figuring how this assembly code works. I know it loops 16 times and updates the total every iteration. Specifically on the line at the .L2 label, wouldn't that give a segmentation fault since the previous line: ldr ip, [ip, #0] reassigns ip? Can someone explains how this works? Thank you in advance for your time.
.L5:
.word data
.word total
_start:
ldr ip, .L5
mov r1, #0
ldr r0, .L5+4
mov r3, r1
mov r2, r1
ldr ip, [ip, #0]
str r1, [r0, #0]
.L2:
ldr r1, [ip, r3]
add r3, r3, #4
cmp r3, #64
add r2, r2, r1
str r2, [r0, #0]
bne .L2
推荐答案
您的代码:
- 加载
ip
中data
的地址和r0
中total
的地址. - 将寄存器
r1
、r2
和r3
归零 - 将
data
的内容加载到ip
中,替换之前的内容(data
的地址).这不是段错误.您可以从内存加载到任何寄存器,包括您刚刚用来指定加载的有效地址的寄存器.所有发生的事情都是平常的:无论旧值(在本例中为指针)都替换为新值(指针对象). - 将
0
存储到total
中. - 重复 16 次:
- 使用
ip
作为整数数组的基地址,将距基地址偏移r3
处的整数加载到r1
中. - 将
r1
累加到寄存器r2
中. - 将
r2
存储到total
.
- Loads the addresses of
data
inip
and the address oftotal
inr0
. - Zeroes registers
r1
,r2
andr3
- Loads into
ip
the contents ofdata
, replacing the previous contents (the address ofdata
). This does not segfault. You are allowed to load from memory into any register, including the one you just used to specify the effective address for the load. All that happens is the usual: Whatever old value was there (in this case, the pointer) is replaced with the new value (the pointee). - Stores
0
intototal
. - Repeats 16 times:
- Using
ip
as the base address of an array of integers, load intor1
the integer at offsetr3
from the base address. - Accumulate
r1
into the registerr2
. - Store
r2
tototal
.
基于此,我认为
data
的类型为int*
而total
的类型为int
>.因此,我对您的汇编程序的解释是:Based on this, I believe that
data
is of typeint*
and thattotal
is of typeint
. Therefore, my interpretation of your assembler is:伪代码-y C 类汇编器
int* data; int total; void start(void){ int** ip = &data; int* r0 = &total; int r1=0, r2=r1, r3=r1; int* ipnew = *ip; *r0 = r1;/* total = 0; */ do{ r1 = *(int*)((char*)ipnew + r3);/* r1 = ipnew[r3/4]; */ r3 += 4; r2 += r1; *r0 = r2;/* total = r2; */ }while(r3 != 64); }
纯 C
int* data; int total; void start(void){ int* arr = data; /* ip */ register int sum = 0;/* r2 */ register int i = 0;/* r3, divided by 4 */ total = 0; do{ sum += arr[i++];/* r1 contains the value loaded from the array. */ total = sum; /* r0 contains &total */ }while(i != 16); }
这篇关于这条 ARM 线是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Using
- 使用
查看全文