在嵌入式设备上投射指针 [英] Casting pointers on embedded devices

查看:309
本文介绍了在嵌入式设备上投射指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I encountered a strange problem when casting and modifying pointers on a 32bit embedded system (redbee econotag running contiki OS to be specific).

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

我的电脑行为正如我预期的那样,嵌入式设备似乎在到达单词的末尾时会包围。为什么会发生这种情况?

My computer behaves as I expect it to, the embedded device however seems to wrap around when it reaches the end of the word. Why does this happen?

推荐答案

您的目标redbee econotag被称为具有ARMv4架构的ARM7。 ARMv4不提供像ARMv7或intel机器这样的不一致的内存访问。

Your target "redbee econotag" is stated as an ARM7 which has ARMv4 architecture. ARMv4 doesn't provide unaligned memory access like an ARMv7 or an intel machine.

ARM的文档



在ARMv4和ARMv5架构以及ARMv6架构上,取决于配置方式,在访问内存中的未对齐数据时需要注意,以免返回意外的结果。例如,当使用常规指针读取C或C ++源代码中的单词时,ARM编译器将生成使用LDR指令读取单词的汇编语言代码。当地址是四的倍数时,如预期的那样工作,例如,如果它位于字边界上。但是,如果地址不是四位数,则LDR会返回一个旋转的结果,而不是执行一个真正的未对齐的字加载。一般来说,这个轮换不是程序员期望的。


这篇关于在嵌入式设备上投射指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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