What is non-aligned access? (ARM/Keil)

前端 未结 5 1876
抹茶落季
抹茶落季 2020-12-16 05:49

I\'m using Keil to write Assembly for ARM 7.

I have the following runtime error:

Non-aligned Access: ARM Instruction at 000000F8H, Memory Access at 7         


        
5条回答
  •  醉话见心
    2020-12-16 06:42

    Some platforms will not allow unaligned access to memory, as you have noticed. Reads and writes are expected to be aligned on N byte boundaries. I don't know your platform, but let's assume we require 4-byte alignment.

    You have an address 0x7F7F7F7F. 0x7F7F7F7F % 4 == 3, i.e., you have a remainder and this address is not aligned on a four byte boundary. Unaligned access on many platforms will be slower than aligned access (you may want to look at how C pads structures), but some architectures simply do not allow it at all.

    So, if you're going to be plopping data down at fixed addresses, make sure said address begins at an N byte boundary (where N is 4 for LDR on an ARM 7). Your compiler will synthesize aligned access for you, but not if you're hardcoding your addresses (obviously).

    From some quick reading, it seems that unaligned accesses are in fact allowed on ARM7, but there is a caveat. From the link below:

    Further, unaligned accesses are only allowed to regions marked as Normal memory type, and unaligned access support must be enabled by setting the SCTLR.A bit in the system control coprocessor. Attempts to perform unaligned accesses when not allowed will cause an alignment fault (data abort).

    Further reading: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka15414.html

提交回复
热议问题