MIPS: Why do we load bytes when we already have a boot word?

In the RISC MIPS instruction set, we have the load byte ( lbu ), type the word half word ( lhu ) and the load word ( lw ) lw . It seems to me that all lbu and lhu can be achieved with lw .

So why did MIPS designers introduce lbu and lhu ? In what circumstances (preferably unclear) can they be useful? Perhaps lw takes longer to execute than lbu , although both are single instructions?

+6
source share
1 answer

lw requires the address you download to be word aligned (i.e. the address must be a multiple of 4).

So, let's say that you have this array located at 0x1000:

 array: .byte 0,1,2,3 

And you want to load the second byte ( 1 ), which is located at 0x1001, which does not align by word. This obviously won't work if you didn't lw from address 0x1000 and then made some changes and ANDing to get the byte you wanted, which would be a real problem as a programmer.

Or say that you want to load 0 , which is located at the address aligned by word, and compare it with some value. Thus, you execute lw from address 0x1000, but now your target register will contain 0x00010203 or 0x03020100 (depending on the entity), and not just 0 . Therefore, before performing the comparison, you will have to bitwise AND retrieve your byte, which you need.

As I am sure, you can see that it would be very inconvenient to take these additional steps whenever you want to process individual bytes of data, which is a fairly common operation in most programs.

+6
source

Source: https://habr.com/ru/post/1012674/


All Articles