简体   繁体   中英

When I'm accessing memory values in my microcontroller why is it pointing to the end of my data value?

I'm trying to get the value of at an address in memory in my microcontroller. The address is at 0x1fff7000 and that's the start of the memory block (so anything before is undefined). In my code I have char *ptr = (char *)BASE_ADDR; where #define BASE_ADDR ((uint32_t)0x1FFF7000) . My value at 0x1FFF7000 is 0x12345678 and I know 100% that it's there.

In GDB, I'm doing (gdb) p/x *ptr and my return value is $6 = 0x78 . Why is it at 0x78 and not returning 0x12345678 or even 0x12 ?

Because you have a little-endian system/mcu, which means least significant bytes are stored first. In your example, the 0x12345678 , stored as a 32 bit integer, is going to look as 78 56 34 12 as raw data in memory.

define it as uint32_t *ptr = (volatile char *)BASE_ADDR; then read.

(char *) can reference the value from -128 to 127.

Also read about the endianess. https://en.wikipedia.org/wiki/Endianness

As I wrote in your other questions - before you program uCs you need to learn some basics. Bitwise operations, endianess etc etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM