在嵌入式设备上转换指针

标签 c x86 embedded arm contiki

我在 32 位嵌入式系统上转换和修改指针时遇到了一个奇怪的问题(redbee econotag 运行 contiki OS 具体而言)。

uint32_t array[2];
array[0] = 0x76543210;
array[1] = 0xfedcba98;

uint8_t* point = ((uint8_t*)array)+1;

printf("%08x \n", *(uint32_t*)point );

在我的电脑上输出:

98765432

嵌入式设备上的输出:

10765432

我的计算机的行为与我预期的一样,但是嵌入式设备似乎在到达单词末尾时环绕。为什么会这样?

最佳答案

您的目标“redbee econotag”被声明为具有 ARMv4 架构的 ARM7。 ARMv4 不像 ARMv7 或英特尔机器那样提供未对齐的内存访问。

引自ARM's documentation :

On ARMv4 and ARMv5 architectures, and on the ARMv6 architecture depending on how it is configured, care needs to be taken when accessing unaligned data in memory, lest unexpected results are returned. For example, when a conventional pointer is used to read a word in C or C++ source code, the ARM compiler generates assembly language code that reads the word using an LDR instruction. This works as expected when the address is a multiple of four, for example if it lies on a word boundary. However, if the address is not a multiple of four, the LDR returns a rotated result rather than performing a true unaligned word load. Generally, this rotation is not what the programmer expects.

关于在嵌入式设备上转换指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14032434/

相关文章:

c++ - 如何编写带有 'asm volatile'的btr指令

linux - 调用函数不起作用

assembly - pcasm书籍示例

c - 为什么要投指针?

c++ - 嵌入式 C++ 项目 - 需要支持智能指针。可能的可移植性问题?

c++ - 在链表中遍历后如何获得列表的头部?

c - 热衷于从 GRUB2 获取 ELF 符号

在c中将摩尔斯电码转换为英语

c - 如何在 RTOS 中查找任务中的内存泄漏?

C:无法解决为什么该结构的两个成员没有正确递增的问题?