9

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?

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
jubblybean
  • 159
  • 1
  • 3
  • 6
  • The answers is red are correct except for the $t0 value big endian, which should be 0xFFFFFF90, since 0x90 would be interpreted as a negative byte value, and sign extended by `lb` into the 32-bit register $t0. – Erik Eidt Jan 20 '22 at 00:22

1 Answers1

32
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
eigenchris
  • 5,791
  • 2
  • 21
  • 30
  • 2
    You are wrong on couple of points here: 1. Assuming the data at memory address 0x01 is 0x90, the value of $t0 would be 0xFFFFFF12 as `lb` sign extends. Use `lbu` if sign extension is not desired. 2. `lb` and `sb` doesn't care for endianness. Whatever byte is at memory address `x` is seen the same way by both big and little endian machines. – Cheshar Aug 06 '18 at 14:30
  • 1
    @Cheshar: No I think your 1. is wrong, even if `lb` do sign extend, `sb` still store the least significant byte into `0x06`. (and how did you get `0xFFFFFF12`? Isn't it `0xFFFFFF90`?) – Kindred Nov 27 '18 at 21:58