How to determine the endian mode the processor is running in?

会有一股神秘感。 提交于 2019-12-05 17:15:41

There is no CPSR bit for endianness in ARMv4 (ARM7TDMI) or ARMv5 (ARM9), so you need to use other means.

If your core implements system coprocessor 15, then you can check the bit 7 of the register 1:

MRC p15, 0, r0, c1, c0 ; CP15 register 1
TST r0, #0x80          ; check bit 7 (B)
BNE big_endian
B   little_endian

However, the doc (ARM DDI 0100E) seems to hint that this bit is only valid for systems where the endianness is configurable at runtime. If it's set by the pin, the bit may be wrong. And, of course, on most(all?) ARM7 cores, the CP15 is not present.

There is a platform-independent way of checking the endianness which does not require any hardware bits. It goes something like this:

   LDR R0, checkbytes
   CMP R0, 0x12345678
   BE  big_endian
   BNE little_endian
checkbytes
   DB 0x12, 0x34, 0x56, 0x78

Depending on the current endianness, the load will produce either 0x12345678 or 0x78563412.

ARMv6 and later versions let you check CPSR bit E (9) for endianness.

Before ARMv6 co-processor 15 register c1 bit 7 should tell which endianness core is using.

In both cases 1 is big-endian while 0 is little-endian.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!