Search code examples
mipsmips32

confused over mips I-Type Instruction syntax


Sorry about this really stupid question. But I have been reading my textbook the entire day and its getting really confusing.

I would like to clarify some syntax related questions with MIPS I-type instructions.

I understand that instructions like

addi $21, $22, -50

are possible and the immediate value is interpreted as a decimal (2s complement).

But can I write addi $21, $22, 0xF1DF? such that the immediate value is in hexadecimal?

That brings me to my next question with andi $10, $11, 0xFFFF and ori $8, $0, 0x0FA5. I understand that these 2 codes are correct.

But can I write andi $10, $11, 120 and ori $8, $0, -16such that the immediate values are in decimal? If it is possible, does it interpret 120 as hexadecimal or does it convert decimal 120 to hexadecimal which equates to 78 in hexadecimal.

I am very confused.


Solution

  • You can express immediates in both base 10 or hexadecimal. To express an hedacimal value you need to use the 0x prefix before the numeral (e.g. 0x10, which is 16 in base 10). If you don't use the prefix 0x before the numeral, the assembler will assume the numeral is in decimal notation.

    Using your example, if you want to add 120 (base 10) you should encode the instruction like so addi $10, $11, 120 or like so addi $10, $11, 0x78. The assembler will produce the same binary instruction 0x20000000. You can try it for yourself here with this online assembler.