Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arm

endianness

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.

like image 804
Johan Avatar asked Jan 12 '23 00:01

Johan


2 Answers

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.

like image 187
Igor Skochinsky Avatar answered Jan 13 '23 12:01

Igor Skochinsky


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.

like image 36
auselen Avatar answered Jan 13 '23 13:01

auselen