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

試著忘記壹切 提交于 2020-01-02 05:26:07

问题


How do I determine the endian mode the ARM processor is running in using only assembly language.

I can easily see the Thumb/ARM state reading bit 5 of the CPSR, but I don't know if there a corresponding bit in the CPSR or elsewhere for endianness.

;silly example trying to execute ARM code when I may be in Thumb mode....
MRS R0,CPSR    
ANDS R0,#0x20
BNE ThumbModeIsActive
B   ARMModeIsActive

I've got access to the ARM7TDMI data sheet, but this document does not tell me how to read the current state.
What assembly code do I use to determine the endianness?

Let's assume I'm using an ARM9 processor.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/22354542/how-to-determine-the-endian-mode-the-processor-is-running-in

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