Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading and storing bytes in MIPS

Tags:

mips

I'm studying for an exam tomorrow and I'm quit confused on the loading/storing bytes topic. I have this example:

I don't understand how he got the answers in red at all. Could someone help explain this to me?

like image 626
jubblybean Avatar asked Feb 24 '15 22:02

jubblybean


1 Answers

add    $s3, $zero, $zero

This performs the addition $s3 = 0 + 0, effectively setting the register $s3 to a value of zero.

lb     $t0, 1($s3)

This loads a byte from a location in memory into the register $t0. The memory address is given by 1($s3), which means the address $s3+1. This would be the 0+1=1st byte in memory. Since we have a big-endian architecture, we read bytes the 4-byte chunks "big end first".

byte:  0   1   2   3
      00  90  12  A0

The 0th byte is 00, and the 1st byte is 90. So we load the byte 90 into $t0.

sb     $t0, 6($s3)

This stores a byte from the register $t0 into a memory address given by 6($s3). Again this means the address $s3+6.

byte:  4   5   6   7
      FF  FF  FF  FF

becomes

byte:  4   5   6   7
      FF  FF  90  FF

Now, what if the architecture was little-endian? This would mean bytes are arranged "little end first" in memory, so the effect of the 2nd and 3rd instructions change.

lb     $t0, 1($s3)

This loads the byte in memory address 1 into register $t0. But now the addresses are "little end first", so we read 12 into the register instead.

byte:  3   2   1   0
      00  90  12  A0

Next...

sb     $t0, 6($s3)

This stores the byte in register $t0, which is 12 into a memory address 6. Again with little-endian architecture:

byte:  7   6   5   4
      FF  FF  FF  FF

becomes

byte:  7   6   5   4
      FF  12  FF  FF
like image 76
eigenchris Avatar answered Oct 11 '22 23:10

eigenchris