简体   繁体   English

ARM Cortex M0 / M3 / M4:为什么PC在Thumb状态下总是偶数

[英]ARM Cortex M0/M3/M4:Why PC is always Even number in Thumb State

As far as I understand it, ARM Cortex-M CPUs are always in Thumb state, which means: 据我所知,ARM Cortex-M CPU始终处于Thumb状态,这意味着:

Thumb state indicated by program counter being odd (LSB = 1). 程序计数器指示的拇指状态为奇数(LSB = 1)。 Branching to an even address will cause an exception, since switching back to ARM state is not allowed. 分支到偶数地址将导致异常,因为不允许切换回ARM状态。

However, while I am using CortexM0 and M4 CPU, the PC is always even . 但是,当我使用CortexM0和M4 CPU时, PC始终是均匀的 Each time I branch, the LR records PC+1 and each time I return, the PC gives LR-1. 每次分支时,LR记录PC + 1,每次返回时,PC都给出LR-1。

For example, if lr = 0x0000_01D5, 例如,如果lr = 0x0000_01D5,

Execute 执行

BX lr BX lr

Then PC should be 0x0000_01D5, whereas it gives 0x0000_01D4. 然后PC应该是0x0000_01D5,而它给出0x0000_01D4。

Isn't this impossible? 这不可能吗?

Any comment will be appreciated. 任何评论将不胜感激。

From Cortex-M4 Technical Reference Manual : Cortex-M4技术参考手册

2.3.1 Program counter 2.3.1程序计数器

Register R15 is the Program Counter (PC). 寄存器R15是程序计数器(PC)。

Bit [0] is always 0, so instructions are always aligned to word or halfword boundaries. 位[0]始终为0,因此指令始终与字或半字边界对齐。

Reading from PC shouldn't return an odd address. PC读取不应返回奇数地址。 However when you write to PC , LSB of value is loaded into the EPSR T-bit. 但是,当您写入PC ,值的LSB将加载到EPSR T位中。 From Cortex-M3 Devices Generic User Guide - 2.1.3. Cortex-M3设备通用用户指南 - 2.1.3。 Core registers 核心登记册

Thumb state 拇指状态

The Cortex-M3 processor only supports execution of instructions in Thumb state. Cortex-M3处理器仅支持Thumb状态下的指令执行。 The following can clear the T bit to 0: 以下可以将T位清除为0:

 instructions BLX, BX and POP{PC} restoration from the stacked xPSR value on an exception return bit[0] of the vector value on an exception entry or reset. 

Attempting to execute instructions when the T bit is 0 results in a fault or lockup. 当T位为0时尝试执行指令会导致故障或锁定。 See Lockup for more information. 有关更多信息,请参阅锁定。

In other words, you can read even values from PC but can't write such values under normal circumstances. 换句话说,您可以从PC读取偶数值,但在正常情况下无法写入这些值。

I had that confusion as well. 我也有这种困惑。 The lsbit is set for situations where the address is going to be used by a BX. lsbit设置用于BX将使用地址的情况。 The lsbit is stripped when it goes into the pc itself. 当它进入电脑本身时,它会被剥离。 If you disassemble some simple pc relative addressing that will demonstrate what is going on. 如果你反汇编一些简单的pc相对寻址,它将演示正在发生的事情。

While there are 4,294,967,296 [2^32] memory locations, the actual data bus is only 8 bits wide. 虽然有4,294,967,296 [2 ^ 32]个存储单元,但实际数据总线只有8位宽。

For 16-bit instructions, you need 2 memory accesses per instruction load: that's where arguments about "Endian" start. 对于16位指令,每个指令加载需要2次内存访问:这是关于“Endian”的参数开始的地方。 Whichever way you order it, 2 bytes get read in with the 1st byte at an address ending in 0. 无论您采用哪种方式,都会以第一个字节读取2个字节,地址以0结尾。

[sigh] If the ARM data bus was 16 [or 32] bits wide, you could forget about bits in the PC - and have double [or quadruple] the instruction space. [叹气]如果ARM数据总线宽度为16 [或32]位,您可能会忘记PC中的位 - 并且指令空间有两倍[或四倍]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM