Search code examples
assemblymipsspimimmediate-operand

MIPS - SPIM parser immediate value out of range for bitwise ANDI


Was trying to test my program with SPIM and have this message

spim: (parser) immediate value (-16) out of range (0 .. 65535) on line 56 of file code1.a

  andi $t1, $t0, 0xfffffff0

what could be the problem?


Solution

  • The MIPS processor cannot do that operation in one instruction.  The andi instruction is an I-Type instruction, which holds a 16-bit immediate — further the andi instruction zero extends the 16-bit immediate to 32-bit, so it cannot hold a negative number (ori & xori also zero extend, whereas addi and all the others sign extend the immediate).

    Whenever we cannot do something in one instruction, use a sequence of instructions.  In this case, load the immediate into a register, then use the and R-Type instruction.

    FYI, the MARS simulator's assembler will take the andi $t1, $t0, 0xfffffff0, though it treats that as a pseudo instruction and will expand that one line of assembly into a 3-instruction machine code sequence that loads the constant into a register (using two instructions: one more than needed to do the job), then uses that and.

    Apparently, Spim doesn't offer that particular pseudo instruction.