I'm going over some notes from Berkeley's CS61C here and there is a part that is talking about memory addresses.
On most current architectures, including the MIPS architecture, an
int
must have an address that's divisible by 4. So if you think of memory as being full of integers, it looks like this
but if you think of memory as full of characters, it looks like this:
Saying this another way, the rightmost two bits in any int address are zero.
The reason for this last statement is that the two rightmost bits represent 2^0 and 2^1 which are not divisible by four; the entire integer will be divisible by four if and only if those rightmost two bits are zero.
Question: why must the address be divisible by four?
This is my interpretation
- addresses are 32 bits, so they go from 0 to 2^32-1
- the smallest addressible unit of memory is a byte
- so each of those addresses refers to one of these bytes
- but an integer is 32 bits, so four bytes, ie it fits into four addresses
- lets assume address number 4 points to a integer; this integer will be represented in memory positions 4, 5, 6, 7; therefore, the next available address for an integer is address 8
- therefore, as long as the smallest address is divisible by four, all the others will be divisible by four necessarily.
- if the first address of an int had been address 3, then this property that the rightmost two bits are zero wouldn't hold.
- is this correct?