Endianness
Integers are usually 4 bytes. There are two ways that an integer could be stored. Either they could be big endian or little endian.
However, since the order matters only if you access the identical data both as a word and as four individual bytes, few need to be aware of the “endianness”. You only really need to be aware of Endianness when you do Byte Addressing, where you are trying to access individual bytes.
Big Endian (human-readable)
- Used in MIPS, where the most significant byte (left most) is at the lowest memory address (usual left-right reading; store from least to largest).
- When you read an int as a binary, it’s essentially a big endian (if you assume you’re reading from lower address to higher address)
Address → Value
0x1000 → 0x12 (MSB)
0x1001 → 0x34
0x1002 → 0x56
0x1003 → 0x78 (LSB)
Little Endian (Most systems use this)
- Used in RISC-V, where address of 64-bit word refers to address of ‘little’ or rightmost byte, (containing bit 0 of word)
Address → Value
0x1000 → 0x78 (LSB)
0x1001 → 0x56
0x1002 → 0x34
0x1003 → 0x12 (MSB)
do not confuse with stack
generally, do we read from low address to high address, or high address to low address? Because stack grows from high to low address??
In general:
- We read memory from low address to high address (increasing addresses).
- The stack grows from high address to low address (decreasing addresses).
See Stack Pointer for updated notes.
Is this for convenience because the stack grows downwards (from high address to low address)?
No, it’s more because many arithmetic operations work more naturally when the least significant byte comes first, allowing addition to start from the lowest address without extra shifting.