1

Does li (load immediate) Pseudo-Instruction in MIPS loads the constant into the register differently based on the sign and size of the constant? Is li translated into different instructions when for example the constant is a 16-bit number in two's complement (-2^15 <= k < 2^15) in contrast to a positive number(0 <= k < 2^16)? what about 32-bit numbers?

The links that I found did not address it specifically. That would kind, if you explain it with examples (I use MARS simulator).

Infinity
  • 307
  • 6
  • 16
  • Load immediate is always sign-extended if that's what you are asking – Konrad Lindenbach Jun 12 '14 at 16:09
  • @KonradLindenbach: I'm asking how li is translated into actual instructions (as li is a Pseudo-Instruction) when the constant is a 16-bit positive or negative number. Is it translated into same sequence / set of instructions or based on the sign of number, different instructions are used? And what instructions are used for 32-bit numbers? – Infinity Jun 12 '14 at 16:32

1 Answers1

4

LI gets translated into a single instruction by the assembler when the immediate constant can be represented as a 16 bit two's complement number. (Typically addiu $dst, 0, imm).

LI is translated by the assembler into LUI (load upper immediate) followed by ORI when the immediate constant is too large to be represented as a 16 bit two's complement number.

markgz
  • 6,054
  • 1
  • 19
  • 41
  • There is no `li` instruction. `li` will be normally turned into `addiu $dst, $0, imm`, although other possibilities exist. When the constant doesn't fit into 16 bits, `lui` will be the used to load the top part first because that clears the low half, followed by `ori`. – Jester Jun 12 '14 at 20:10
  • To handle values that fit in 0..65535 but not -32768..32767, MARS will use `ori $dst, $0, imm` instead of `addiu`. ([What does "extend immediate to 32 bits" mean in MIPS?](https://stackoverflow.com/a/55290104) has details about how MIPS sign-extends most immediates, but zero extends for logical instructions) – Peter Cordes Apr 14 '21 at 03:21