2

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

Naruto
  • 1,710
  • 7
  • 28
  • 39

1 Answers1

1

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
Matthew Slattery
  • 45,290
  • 8
  • 103
  • 119
  • I am initializing an array as .byte 'a','b','c' when I try to access it, I get memory out of bound error. Here is the link to my question please check it.http://stackoverflow.com/questions/12881850/compile-time-initialized-array-error – Naruto Oct 14 '12 at 17:31