理解 c #define returns 内存地址处的值
understanding a c #define that returns a value at a memory address
此 c 代码 returns 内存地址处的值。
value = MemoryRead((ptr))
MemoryRead
是 #define
定义为
#define MemoryRead(A) (*(volatile unsigned char*)(A))
这是如何工作的?有人可以解释一下这个 returns 是地址中的值吗?
*(addr) returns 存储在 addr
.
的值
上面的代码看起来不言自明。如果干运行.
考虑value = MemoryRead((ptr))
变成value = (*(volatile unsigned char*)((ptr)))
#define
一开始看起来真的很神秘。理解这一点的方法是将其分解成多个部分,as done here;
First of all,
unsigned char
means we are using a byte-sized memory location. Byte being 8-bits
wide.
unsigned char *
means we are declaring a pointer that points to a byte-sized location.
(unsigned char *) (ptr)
means the byte-sized pointer points to address ptr
. The C compiler
will refer to address ptr
. The assembly code will end up using ptr
in Load(LD) and Store (STR) instructions.
(*(unsigned char *)(ptr))
The first asterisk from the left signifies that we want to manipulate
the value in address ptr
. * means “the value pointed to by the
pointer”.
volatile
volatile forces the compiler to issue a Load or Store anytime
MemoryRead
is accessed as the value may change without the compiler
knowing it.
So any address entered at ptr
will be directly accessed by your
code. (If the address is present in memory.)
此 c 代码 returns 内存地址处的值。
value = MemoryRead((ptr))
MemoryRead
是 #define
定义为
#define MemoryRead(A) (*(volatile unsigned char*)(A))
这是如何工作的?有人可以解释一下这个 returns 是地址中的值吗?
*(addr) returns 存储在 addr
.
上面的代码看起来不言自明。如果干运行.
考虑value = MemoryRead((ptr))
变成value = (*(volatile unsigned char*)((ptr)))
#define
一开始看起来真的很神秘。理解这一点的方法是将其分解成多个部分,as done here;
First of all,
unsigned char
means we are using a byte-sized memory location. Byte being 8-bits wide.
unsigned char *
means we are declaring a pointer that points to a byte-sized location.
(unsigned char *) (ptr)
means the byte-sized pointer points to address
ptr
. The C compiler will refer to addressptr
. The assembly code will end up using ptr in Load(LD) and Store (STR) instructions.(*(unsigned char *)(ptr))
The first asterisk from the left signifies that we want to manipulate the value in address
ptr
. * means “the value pointed to by the pointer”.volatile
volatile forces the compiler to issue a Load or Store anytime
MemoryRead
is accessed as the value may change without the compiler knowing it.So any address entered at
ptr
will be directly accessed by your code. (If the address is present in memory.)