在ARM字节序转换 [英] Endianness conversion in ARM

查看:1964
本文介绍了在ARM字节序转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何大端转换为小端ARM中的?

How do I convert big endian to little endian in ARM?

推荐答案

你说的是ARM的端模式,或者读一些其他一些大端处理器编写等?

Are you talking about ARM's endian modes, or reading something written by some other big endian processor, etc?

通常从大/小尾数转换成/你身边交换字节。所以0xABCD是0xCDAB作为一个16位的数为0x12345678看时0x78563412作为32位的数字观察时

Normally converting to/from big/little endian you swap the bytes around. So 0xABCD is 0xCDAB when viewed as a 16 bit number 0x12345678 is 0x78563412 when viewed as a 32 bit number.

ARM内核的ARMv5及以上(ARM7,ARM9等)有一个称为BE-32的端模式,这意味着大端字不变。 ARMv6及更高版本(MPCore的,皮质出头)有BE​​-8,或大端字节不变。

ARM cores armv5 and older (ARM7, ARM9, etc) have an endian mode known as BE-32, meaning big endian word invariant. armv6 and newer (mpcore, cortex-somethings) have BE-8, or big endian byte invariant.

因此​​,如果您使用的是ARM版本,例如在大端模式和纯(小)尾数模式价值为0x12345678读一个字(LDR)是为0x12345678为大端字在同一地址读取。字固定含义字读给相同的答案。在同一个地址的小端模式的读取地址为零的字节是0x78和大端读取的字节(LDRB)是0×12。

So if you are using an armv4 for example in big endian mode and native (little) endian mode a word read (ldr) of the value 0x12345678 would be 0x12345678 for a big endian word read at the same address. Word invariant meaning word reads give the same answer. A byte read of address zero in little endian mode of the same address would be 0x78 and big endian byte read (ldrb) would be 0x12.

所以,你必须超越只是说是或大或小尾数,但正在使用哪些指令。

So you have to go beyond just saying is it big or little endian but what instruction is being used.

为ARMv6或更新的版本,如果在为0x12345678地址的一些结果的LDR随后在大端模式下,从同一地址的LDR会导致0x78563412。需要注意的是或大或小尾数模式下该地址取指令上的ARMv6或更新将取为0x12345678。一个LDRB小端模式的ARMv6相同的数据相同的地址会导致0x78,L​​DRB大端ARMv6的或更新的版本也导致0x78。这是因为用于ARMv6和新的字节固定含义字节访问到同一地址结果相同的值,半字,字和双字访问都在这些架构时大端模式交换。由于指令取不转变,而且由于尾数位是PSR同时运行一个小尾数编译的程序,你可以切换到大端,做了一些说明,然后返回到本机模式,它不会影响读取指令,也没有中断了发生。

For an armv6 or newer, if an ldr at some address results in 0x12345678 then in big endian mode the ldr from the same address would result in 0x78563412. Note that big or little endian mode an instruction fetch for that address on an armv6 or newer would fetch 0x12345678. An ldrb little endian mode armv6 same data same address results in 0x78, ldrb big endian armv6 or newer also results in 0x78. this is because the armv6 and newer are byte invariant meaning byte accesses to the same address result in the same value, halfword, word and double word accesses are swapped on these architectures when in big endian mode. Because instruction fetches are not swapped, and because the endian bit is in the psr while running a little endian compiled program you can switch to big endian, do a number of instructions then return to native mode and it wont affect the instruction fetches nor interrupts that occur.


setend be
ldr r0,[r1]
add r0,r0,#7
str r0,[r1]
setend le

如果你想运行本地小端(一个很不错的主意)和(你在做什么并不总是一个好主意,取决于)执行使用交换汇编某些网页会提到这四款指令字节交换。

Some web pages will mention this four instruction byte swap, in case you want to run native little endian (a very good idea) and perform the swap using assembler (not always a good idea, depends on what you are doing).


  eor r3,r1,r1, ror #16
  bic r3,r3,#0x00FF0000
  mov r0,r1,ror #8
  eor r0,r0,r3, lsr #8

R1是它出现在输入和r0的作为输出

with r1 being the input it appears and r0 being the output

有关的ARMv6或更新上面可以执行

For armv6 or newer the above can be performed with


  rev r0,r1

这篇关于在ARM字节序转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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