1. Little endian vs Big Endian
CPU can be little-endian, or big-endian.
Little Endian
Little endian means MSB (most significant byte) is in higher memory address.
Consequence of little endian 1: Saving 0x0000 8015 to 0x0000 3200
The instruction populates 0x8015 to bottom 16 bits of $12. So we are saving $12 = 0x0000 8015. We are saving it to an address (512($10) = 512+ 0x3000 = 0x200 + 0x3000 = 0x3200 = 0x0000 3200).
1. Here, we are populating 0x0000 3200, 0x0000 3201, 0x0000 3202, 0x0000 3203 (higher address)
2. Little endian: MSB (0x0000 8015) is written from higher address (0x0000 3203)
3:
- 0x0000 3200 has 15
- 0x0000 3201 has 80
- 0x0000 3202 has 00
- 0x0000 3203 has 00
Consequence of little endian 2: Loading double
Here, it's easier if you remember it like this:
In little endian, lower address (0($t0)) contains LSB, and that has to be loaded to lower floating register ($f4).
Noting that a double is stored in this format, where 63 is the MSB:
If we want to load the sign bit, we think of it like this:
63th bit (sign bit) is MSB, MSB is stored in higher address. So we retrieve higher part (not 0($t0), but 4($t0)):
`lw $t1 4($t0)`
$t1 (32 bit register)'s MSB contains 63th bit (S).
If we want to check if the number is negative:
We have to check the last bit is 1, and we can do that by just flipping all the bits of $t1 to 0 other than the $t1's MSB (which should be sign bit).
`andi $t2, $t1, 0x80 00 00 00`.
2. Stack frame / pointer
Each process has a virtual view of a memory like this:
By convention, it's high memory address (top) to low memory address (bottom)
To save some data before calling another function, you increase stack by pulling pointer sp downwards (so decrement sp), then populate the gained space like above. This is called creating a 'stack frame'. Since $sp is available to both the caller and callee function, they can use it for their purpose (caller needs it for $ra - return address - and callee needs it if caller has sent more arguments than 4).