#memory addresses increasing

104 messages · Page 1 of 1 (latest)

tawny hare
#

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.

bleak vigilBOT
#

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.

vale idol
#

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
tawny hare
#

So

#

Does this mean that every byte of memory takes up 1 address ?

vale idol
#

yes. memory in modern computers is what's called "byte-addressable"

tawny hare
#

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

vale idol
#

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
graceful mulch
#

you can bitwise and 0x87654321 for the first byte in bigendian if you do
0x87654321 & 0xff000000

#

but probably there is a better way ?

vale idol
graceful mulch
#

i suppose one must bitshift it as well

vale idol
#

0x12345678 & 0xff will be 0x78 on both little- and big-endian

graceful mulch
#

oh ok

#

(0x87654321 & 0xff000000) >> 6
so this would yield what jesus asked then?

vale idol
#

on a big-endian system specifically, sure

#

could also just e.g. ((unsigned char*)&value)[3]

graceful mulch
vale idol
#

depends™️

#

compilers can do lots of neato optimizations, and not all hardware is equal. try it out

tawny hare
vale idol
tawny hare
#

Well

#

Using process of elimination it would be E3

#

But this example doesn’t do well.

#

How am I supposed to know the order?

vale idol
#

B is after A

tawny hare
#

Yes

#

But how do I know it’s E3 and not 95

vale idol
#

because A + 2 is C, not B

tawny hare
#

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

vale idol
#

because the 74 is three bytes after FA

tawny hare
#

So the most significant is always on the left?

vale idol
#

no

tawny hare
#

And most significant is the lowest address?

vale idol
#

the byte with the smallest address is

#

endian-ness doesn't effect anything here

tawny hare
#

The byte with the smallest address is on the left

vale idol
#

which has nothing to do with the value of the larger word

tawny hare
#

How come in this example it becomes flipped?

vale idol
#

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

tawny hare
#

4B 57 turned to 574B

#

Due to little endian

tawny hare
#

But why does endian not affect this

vale idol
#

endianness just effects the order of the bytes for each value, not the order of bytes as a whole

#

effect what

tawny hare
#

Hmm

#

I don’t understand

#

So what determines the order of the bytes?

vale idol
#

regardless of the byte order, it's still 0x05, then 0x06, then 0x07, etc

tawny hare
#

Yes I understand this

#

But

#

Regardless of byte order we still have the same address order

vale idol
tawny hare
#

But how do we know the byte order

#

Oh

#

Okay

#

I see now

#

So the left cell is always the lowest address

vale idol
#

same as a number line

tawny hare
#

But how come in this the left cell isn’t the lowest addresss

#

Any of the data

vale idol
#

because it's little-endian, so the least-significant byte is stored first

tawny hare
#

How do we know which is the LSB

#

57?

vale idol
#

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

tawny hare
#

yes

#

but how do I know

vale idol
#

because it's the least significant byte

tawny hare
#

my misunderstanding is how do we know if 4B or 57 is the LSB

#

from this

vale idol
#

do you know what LSB means

tawny hare
#

the byte on the right

#

yes

#

but given 4B and 57 howdo weknow which is LSB

vale idol
tawny hare
#

wait

#

im looking and the order is different

#

in this

#

the firstsquare on the left (4B) is 03FC?

vale idol
vale idol
#

yes

tawny hare
#

so this means that in little endian 4B is LSB

#

thats all I wanted to figure out

vale idol
#

yes

tawny hare
#

okay thanks

#

i understand now

vale idol
#

you can also just look at the number and see that it's the LSB

tawny hare
#

i was just trying to figure out the otherimage

#

i just didnt know how the address and the squares in the other image correlated

bleak vigilBOT
#

@tawny hare Has your question been resolved? If so, type !solved :)