How to make Most Significant Bit of a binary number= 1

4.2k views Asked by At

How can I make MSB of a binary number equal to 1 in MIPS? I have tried to do it with masking but I am getting error, so is there another way to do it?

The code for making MSB=1 by masking which is giving me error.

srl $s3,$s3,1

// error out of range

ori $s3,$s3,2147483648       # making MSB = 1

Regards

1

There are 1 answers

1
Matthew Slattery On BEST ANSWER

The MIPS opcode format for instructions with immediate operands only has 16 bits available for the immediate constant.

These 16 bits are usually taken to mean either an unsigned 16-bit value (i.e 0 to 65535, or 0x0000 - 0xFFFF in hex) for logical operations (such as ori here), or a signed 16-bit value (-32768 to 32767, or 0xFFFF8000 to 0x00007FFF) for arithmetic operations.

So you can't directly use 2147483648 (0x80000000) as an immediate value here - hence the "out of range" error.

But there is an instruction for loading a 16-bit immediate value into the top 16 bits of a register (leaving the bottom bits set to 0): lui (load upper immediate).

So, you can load 0x80000000 into a register like that, and then or it with your value:

lui  $t0, 0x8000
or   $t0, $t0, $s3