gpt4 book ai didi

c++ - 了解 ARM 汇编指令和 C/C++ 指针

转载 作者:太空宇宙 更新时间:2023-11-04 06:05:18 24 4
gpt4 key购买 nike

我正在尝试解码对地址、16 位 ARM 拇指指令进行操作的汇编指令。所以,我认为我不应该关心数据类型。因为我只对那里的 16 位存储感兴趣。我有单独的解释器来理解这些位,无论如何我不想将其用作数据。

如果我有一个指针 p 并且我想读取 4 个字节(即从 pp+3 地址的数据)。将 p 转换为 int * 并取消引用会给我数据吗?

最佳答案

你有一个指向某种类型的指针。指针算术和取消引用遵循数据类型。

请注意,您只能通过具有兼容类型或字符指针的左值表达式访问任何变量(对象)的存储值。盲目地强制指针转换为不同的不兼容类型并尝试取消引用,这将违反严格的别名规则,您将面临 undefined behavior .

引用 C11,第 §6.5 章

An object shall have its stored value accessed only by an lvalue expression that has one of the following types:88)

— a type compatible with the effective type of the object,

— a qualified version of a type compatible with the effective type of the object,

— a type that is the signed or unsigned type corresponding to the effective type of the object,

— a type that is the signed or unsigned type corresponding to a qualified version of the effective type of the object,

— an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union), or

— a character type.

但是,您始终可以使用 char * 指向任何类型,并取消引用和递增(并重复)以获取字节的各个值,但您需要自己处理字节顺序。

相关,引用 C11,第 §6.3.2.3 章

[....] When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object.

关于c++ - 了解 ARM 汇编指令和 C/C++ 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44515378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com