0

Can the instruction la $4,-16($9) be replaced with a single MIPS machine instruction? I've been searching and every time I find a combination of lui and ori.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user1286550
  • 101
  • 1
  • 6
  • 3
    `la $4,-16($9)` is just `addiu $4, $9, -16`. Why are you using `la` in the first place for something that isn't a symbol? And what assembler is allowing but failing to assemble it to a single instruction? – Peter Cordes Jul 14 '21 at 02:19
  • For a normal `la` like `la $4, foobar`, a single machine instruction is only possible if the address is in the low 64kiB (`ori`), or is 64k-aligned (`lui`). – Peter Cordes Jul 14 '21 at 02:20
  • Thank you for your reply. I'm not using it, I was asked the question and I've been researching. – user1286550 Jul 14 '21 at 02:23
  • 1
    What assembler are you using that uses lui + ori? `clang -target mips foo.s` assembles it to just that `addiu`, according to `llvm-objdump -d` (on my x86-64 Linux desktop). I assume either MARS or SPIM is dumber for this non-standard use of `la` as a kind of generalized x86-style `lea`, and foolishly waste 2 instructions to materialize `-16` in a register as an input for `addu`? – Peter Cordes Jul 14 '21 at 02:32

1 Answers1

2

la is a pseudo instruction, and, there is one form — la $<target>, label — that is effectively essential for MARS and QtSPIM, since these assemblers don't support the %hi and %lo functions that other assemblers support.  You have discovered by searching online that this form of la translates into lui and ori (or something similar).  Because this is probably the most common use of la, and an essential form, that's what we find in online search.

However, these assemblers (MARS & QtSPIM) are very helpful in allowing various "nice-to-have" forms that are not actual machine instructions.  So, yes, the form you're showing can be substituted with a single "add" immediate to register operation, which you can (and should) write in the first place.  There's no good reason to use that instead of the more direct addiu.  (However, not all forms of la can be substituted with one instruction.)

Many pseudo instructions generate more machine code instructions than you would expect.  For example, on MARS, la $t1, 4($t2) generates two machine code instructions, one to load constant 4 into $at and the other do add reg, reg, $at — wholly unnecessary since a single addiu would have worked the same.  What's more is that the assembler chooses add instead of addu, risking a spurious signed integer overflow trap on addressing, which should always be treated as unsigned instead.


You can see this for yourself using MARS — just look at the disassembly for a pseudo instruction.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53