什么是非对齐访问?(ARM/基尔) [英] What is non-aligned access? (ARM/Keil)
问题描述
我正在使用 Keil 为 ARM 7 编写程序集.
I'm using Keil to write Assembly for ARM 7.
我有以下运行时错误:
Non-aligned Access: ARM Instruction at 000000F8H, Memory Access at 7F7F7F7FH
Data Abort: ARM Instruction at 000000F8H, Memory Access at 7F7F7F7FH
这并没有真正帮助我,因为我不知道非对齐访问"是什么,(除了显而易见的,但我并不真正理解它的含义)我我试图访问(存储)0x7F7F7F7F
,有什么问题?
This doesn't really help me, because I don't know what 'non-aligned access' is, (other than the obvious, but I don't really understand what it means) and I am trying to access (store) to 0x7F7F7F7F
, what's the issue?
搜索我只发现了几个类似的问题,都使用 C,并且通过某种方式解决了他们的代码非常具体的问题,并且与这个问题无关.
Searching I only found a couple of similar issues, both using C, and resolved by some means very specific to their code and which didn't relate to this issue.
我在做:
LDR R0, =0x7F7F7F7F
LDR R1, LABEL
STR R1, [R0]
然后我用不同的标签和 R0
的偏移量做类似的事情,但它首先在这里失败.
I then do a similar thing with a different label, and offsets of R0
, but it fails here first.
推荐答案
问题是您用于 32 位(4 字节)内存操作的地址必须与 4 字节边界对齐.这意味着地址必须是 4 的倍数,或者如果您愿意,地址的低两位必须为零.
The issue is that the address you use for a 32-bit (4-byte) memory operation must be aligned to a 4-byte boundary. This means the address must be a multiple of 4, or if you prefer, the bottom two bits of the address must be zero.
在这种情况下,最接近的 4 字节对齐地址将是 0x7F7F7F7C
或 0x7F7F7F80
.
In this case, the closest 4-byte aligned addresses would be 0x7F7F7F7C
or 0x7F7F7F80
.
同样,LDRH
/STRH
需要 2 字节对齐,而 LDRB
/STRB
可以在任何地方操作(1 字节对齐 == 未对齐).
Similarly, LDRH
/STRH
require 2-byte alignment, whereas LDRB
/STRB
can operate anywhere (1-byte alignment == unaligned).
一般来说,编译器/汇编器负责确保您的变量根据它们的大小正确对齐 - 如果您自己生成地址(根据问题),您应该只遇到这种情况.
In general the compiler/assembler takes care of making sure your variables are aligned correctly for the size they are - you should only run into this if you're generating addresses yourself (as per the question).
这篇关于什么是非对齐访问?(ARM/基尔)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!