In the RISC MIPS instruction set, we have load byte (lbu
), load half word (lhu
) and load word (lw
) instructions. It appears to me that everything lbu
and lhu
can do can be achieved with lw
.
So why did the MIPS designers introduce lbu
and lhu
? In what circumstances (preferably non-obscure ones) might they be useful? Perhaps lw
takes longer than lbu
to execute, even though both are single instructions?
Loading and storing bytes The MIPS ―load byte‖ instruction lb transfers one byte of data from main memory to a register.
A word generally means the number of bits that can be transferred at one time on the data bus, and stored in a register. In the case of MIPS, a word is 32 bits, that is, 4 bytes. Words are always stored in consecutive bytes, starting with an address that is divisible by 4. Caution: other processors, other definitions.
lw
requires the address that you load from to be word-aligned (i.e. the address must be a multiple of 4).
So let's say that you have you have this array located at address 0x1000:
array: .byte 0,1,2,3
And you want to load the second byte (1
), which is located at address 0x1001, which isn't word-aligned. That clearly won't work, unless you did an lw
from address 0x1000 and then performed some shifting and ANDing to get the byte you wanted, which would be a real hassle as a programmer.
Or let's say you wanted to load the 0
, which is located at a word-aligned address, and compare it against some value. So you do lw
from address 0x1000, but now your target register will contain either 0x00010203
or 0x03020100
(depending on the endianness) rather than just 0
. So before performing the comparison you'll have to do a bitwise AND to extract the byte you wanted.
As I'm sure you can see it would be very inconvenient to have to do these extra steps whenever you want to process individual bytes of data - which in most programs is a pretty common operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With