# MIPS: sign extend in I-Type commands

My lecturer told me that when I use an arithmetic I-Type command (ADDI,SUBI etc.) , the IMM field gets sign extended, and when I use a logic I-Type command (ORI,ANDI etc.) , the IMM field is just bits.

He also said that if the IMM field contains a negative number, it is represented in 2's complement.

Let's say I have a command, and two binary numbers X Y, so that X's 2's complement representation looks exactly like Y in regular representation.

Note: X is negative and not the same as Y.

Given a question in which I should say what is the value in the IMM field in an arithmetic I-Type command, how do I know if it's X or Y?

• No, that's not what I'm asking. I'm asking: How do if the IMM field contains X or Y? – HaloKiller Mar 17 '14 at 15:40

## 1 Answer

In an arithmetic I-type command, the IMM-field is treated as a signed number. So if the first bit is 1, the number will be treated as negative (X in your terms.) The number Y is simply too large to be used as argument of an arithmetic I-type command.

• So if the number is sign extended, the sign bit in only the lsb? – HaloKiller Mar 17 '14 at 16:04
• It's the MSB, not the LSB. But yes, sign extending means just setting all new bits to the value of the previous MSB. – FrankW Mar 17 '14 at 16:42
• MSB, okay. But I still don't get it: Say the IMM in a certain command is 1111111111111000, how do I know if it's a negative number represented in 2's complement or a regular positive binary number? – HaloKiller Mar 17 '14 at 16:46
• You know it from the type of instruction (i.e. the opcode). If it's an arithmetic instruction, the number is interpreted as negative; if it's a logic instruction, the number is interpreted as positive. – FrankW Mar 17 '14 at 17:14
• If it's an arithmetic instruction how do you know if it's negative? And if it is, how do you know where the sign extention starts? – HaloKiller Mar 19 '14 at 12:16