Frage

I would like to do some byte manipulation using MIPS instruction set.

  • I have register $S0 which has 0x8C2E5F1E and register $S1 which has 0x10AC32BB.
  • I would like to store the second byte of $S0, 5F, into the third byte of $S1, AC.

My logic would be to store the byte of register $S0 into another register, shift it to the desired byte. Then I would and register $S1 with 0xFF00FFFF. Finally, I would just or the two registers. How does that sound? Is it correct? Any better way?

Any suggestions or solution would be appreciated.

War es hilfreich?

Lösung

For Release 2 and later, MIPS includes an Insert Bit Field instruction which takes bits starting at the least significant from one register and placing them into the specified range in a second register. Thus your byte insertion could be performed by the following:

// rotating right one byte rather than shift to preserve data
// without using an additional register
ROTR $S0, $S0, 8;
// insert LSbits from $S0 into $S1 starting at bit 16
// with size of 8 bits
INS $S1, $S0, 16, 8;

Andere Tipps

Consider the following:

ori $t0 $s0 0xFF00 #extract byte 2
sll $t0 $t0 8 #shift to third byte

#create mask to clear third byte
lui  $t1 0xFF 
not  $t1 $t1

and  $s1 $s1 $t1 #clear third byte
or   $s1 $s1 $t0 #set third byte
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top