如何将一个4字节的数组组合成一个32位.第一项应该进入对结果的最重要的一步。将结果存储在32位变量结果中。
输入: LIST = 0xC,0x2,0x6,0x9 (每个项目都是一个字节,使用DCB定义一个类型为字节的变量)
输出:结果= 0x0C020609
编辑答案:
ADD R1, R0
MOV R1, R1, LSL #8
ADD R0, R0, #8
ADD R1, R0
MOV R1, R1, LSL #8
ADD R0, R0, #8
ADD R1, R0
MOV R1, R1, LSL #8
ADD R0, R0, #8
ADD R1, R0发布于 2016-09-12 20:21:37
您所描述的情况与将4个连续字节视为存储在大端字节顺序中的32位整数是相同的。
gcc认为(在戈德波特编译器浏览器上,从大端到ARM的字节交换的最好方法是使用ARM为这个目的明确提供的指令:
rev r0, r0 #include <stdint.h>
#include <endian.h>
#include <string.h>
// type-punning with unions is an alternative to memcpy from a char array to an int
union be_bytes {
uint32_t be_word;
char bytes[4];
};
uint32_t be_bytes_to_native( char *array ) {
union be_bytes tmp;
memcpy(tmp.bytes, array, 4); // memcpy since we take a char* arg instead of a union be_bytes * arg.
// I *think* (union be_bytes*)array would be safe, but I'm not 100% sure.
// GNU C and many other compilers guarantee that writing one union member and reading another is safe. ISO C doesn't, so this technically isn't portable.
return be32toh(tmp.be_word); // from endian.h, uses compiler builtins, inline asm, or some C shift and mask instructions.
}编译成
be_bytes_to_native:
ldr r0, [r0] @ unaligned
rev r0, r0
bx lr在没有REV指令的情况下,@dwelch对ARM中的Endianness转换的回答提出了用于字节的4条指令序列--在ARM中交换32位值:
eor r3,r1,r1, ror #16
bic r3,r3,#0x00FF0000
mov r0,r1,ror #8
eor r0,r0,r3, lsr #8请注意,这是如何结合使用桶移位器和指示以外的MOV。我仍然不确定代码中的ADD R0, R0, #8 (r0 += 8)是用来做什么的。
https://stackoverflow.com/questions/39456854
复制相似问题