Does anyone know why memory addresses in microcontrollers increase by 4? Like starts at 0x00000000 and the next is 0x00000004, the registers are 4 bytes, does this have a correlation or is it just how it is? I thought that address are different from what they contain so if this is the case why would the register size affect the address.
#memory addresses increasing
104 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
if your register stores 4 bytes of data, you're gonna need 4 bytes in memory to store the same data. if I put 0xDEADBEEF at address 0x00000000, then put 0xCAFEBABE at address 0x00000001, then memory may look like
0 1 2 3 4
DECAFEBABE
but if we put the second word at address 0x00000004, it may look like
0 1 2 3 4 5 6 7
DEADBEEFCAFEBABE
yes. memory in modern computers is what's called "byte-addressable"
I understand now, so if I have data ie, 0x87654321 at address 0x00000000
How do I know which byte is at 0x00000000
And which is at 0x00000003
depends on if it's a big-endian or little-endian system
big-endian stores with the most significant byte first, so if storing 0xCAFEBABE we'd get
0 1 2 3
CAFEBABE
but little-endian stores with the least-significany byte first, so we'd get
0 1 2 3
BEBAFECA
you can bitwise and 0x87654321 for the first byte in bigendian if you do
0x87654321 & 0xff000000
but probably there is a better way ?
endianness only exists for storing in memory, it's always the "as expected" value when you're interacting with it
i suppose one must bitshift it as well
0x12345678 & 0xff will be 0x78 on both little- and big-endian
on a big-endian system specifically, sure
could also just e.g. ((unsigned char*)&value)[3]
is that faster, or is casting+accessing it doing something similar to what i did with bitoperations?
depends™️
compilers can do lots of neato optimizations, and not all hardware is equal. try it out
So like for this example how would I know?
which do you think it is?
Well
Using process of elimination it would be E3
But this example doesn’t do well.
How am I supposed to know the order?
B is after A
because A + 2 is C, not B
Yea but I’m confused on the ordering
So this means at FA the value is A2
How do I know it’s A2 and not 74
because the 74 is three bytes after FA
So the most significant is always on the left?
no
And most significant is the lowest address?
The byte with the smallest address is on the left
which has nothing to do with the value of the larger word
the 4-byte word at 0.04FA could be 0xA2E39574 if it's a big-endian system, or it could be the storage of 0x7495E3A2 if it's little-endian
Oh yes I understand
But why does endian not affect this
endianness just effects the order of the bytes for each value, not the order of bytes as a whole
effect what
Yes I understand this
But
Regardless of byte order we still have the same address order
the top-left cell here will always be 0x0.04FA. What endianness effects is if that's the first or the last byte of the data in those four bytes
But how do we know the byte order
Oh
Okay
I see now
So the left cell is always the lowest address
same as a number line
because it's little-endian, so the least-significant byte is stored first
well if you look at the right side of the image, where the values are being shown instead of the memory representation, we can see that 4B is the least significant byte
do you know what LSB means
wait
im looking and the order is different
in this
the firstsquare on the left (4B) is 03FC?
?
yes
yes
you can also just look at the number and see that it's the LSB
yea I got that from the start
i was just trying to figure out the otherimage
i just didnt know how the address and the squares in the other image correlated
@tawny hare Has your question been resolved? If so, type !solved :)