如何确定处理器运行的字节序模式? [英] How to determine the endian mode the processor is running in?

查看:29
本文介绍了如何确定处理器运行的字节序模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅使用汇编语言确定 ARM 处理器运行的字节序模式.

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

我可以很容易地看到 Thumb/ARM 状态读取 CPSR 的第 5 位,但我不知道 CPSR 或其他地方是否有相应的位用于字节序.

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

我可以访问 ARM7TDMI 数据表,但该文档没有告诉我如何读取当前状态.
我使用什么汇编代码来确定字节序?

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?

假设我使用的是 ARM9 处理器.

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

推荐答案

ARMv4 (ARM7TDMI) 或 ARMv5 (ARM9) 中没有 CPSR bit for endianness,所以需要使用其他方式.

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

如果您的内核实现了系统协处理器 15,那么您可以检查寄存器 1 的第 7 位:

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

然而,文档 (ARM DDI 0100E) 似乎暗示该位仅对在运行时可配置字节序的系统有效.如果它是由引脚设置的,则该位可能是错误的.而且,当然,在大多数(所有?)ARM7 内核上,CP15 不存在.

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

根据当前的字节顺序,加载将产生 0x12345678 或 0x78563412.

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

这篇关于如何确定处理器运行的字节序模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆