2
votes

I learned that sign extension occurs in immediate value of MIPS addi instruction. (http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/addi.html)

However, I'm not sure when will it sign extend to a minus value.

I tried

main:
    addi    $t0, $0, 0x8000
    jr  $31

this code, and qtSpim gave the following error.

immediate value (32768) out of range (-32768 .. 32767) on line 3 of file addi $t0, $0, 0x8000

In order to sign extend to a minus value, MSB has to be '1', but it gets out of range to be an immediate value.

So, what I am guessing is that addi is not 'in actual' happen to be sign extended. Am I guessing it correct?

Or if not, can you please tell me where I have led myself wrong? Also an example of a sign extension will help a lot :)

1
I think the assembler treats 0x8000 as a positive constant. You need to use -0x8000 or -32768phuclv
Thanks! I tried -0x8000 and it worked fine.user3415167

1 Answers

0
votes

addi takes a signed value. The format of the addi instruction when assembled is:

bit    31-26  25-21  20-16  15-0  
value    8      rs     rd   (signed) const

*Source See MIPS Run

Where 8 is the opcode, rs is the destination register, rd is the source register, and the (signed) const is the signed integer being added to the rd.

The assembler is expecting an (optionally) signed 16-bit integer where you give it the 0x8000 value. The particular assembler you're using is seeing this as a positive integer in hex, 32768 as the error denotes, which won't fit.

I'd be careful about overflow with whatever you're doing btw.

Keep in mind whether 0x8000 is dealt with as an integer or a raw binary value depends on your assembler you're using. Lưu Vĩnh Phúc points out, -0x8000 or -32768 should work. I would recommend using -32768 instead of -0x8000, this is more likely to be accepted by other assemblers in the same way. Sorry don't have a mips toolchains handy to a test on.