assemblymipsimmediate-operand

Comparing word against immediate value?


I'm struggling to grasp how to manipulate/compare data stored in registers (words/half-words/intermediate values) in MIPS. Does it matter if they each represent a different number of bytes? For example, is it valid to load an immediate value such as 5 via li, then load a word such as 5, and finally compare them using bne?


Solution

  • Does it matter if they each represent a different number of bytes?

    Short answer: No, it doesn't matter, as 5 is 5.

    Longer answer: MIPS registers are all 32 bits wide, and, in a sense, there's no way to manipulate less than 32 bits at a time — except for the byte and half word load/store instructions (though even those loads populate a full 32-bit register).

    For example, if your data is in memory and 8 bits wide (e.g. characters or bytes), and is a signed(unsigned) data type, then you can use lb(lbu) to load it into a 32 bit register while also sign(zero) extending the 8 bit value to 32 bits.  Numerically, sign(zero) extension keeps the value the same, so you can now work with the 32 bit value in the register.

    (Of course, you have to know in advance if the 8 bit data is numeric and should be sign vs. zero extended.  We know this in high level languages by a logical variable's declared (and permanent) data type, but in assembly, we're working with physical storage that doesn't have a permanent data type, so when it matters, we tell the processor by way of machine code instruction choices to access memory as either signed or unsigned.)

    If your data is not 8 or 16 bits wide (or 32), you can either work with individual bytes, or load more from memory than you're interested in and clear or sign extend bits that are irrelevant to you.