MIPS load word syntax

aherlambang picture aherlambang · Feb 22, 2010 · Viewed 19.9k times · Source

If I want to load a value from a memory which base address is at $a0 and off set $t2, why can't I do the following:

lw  $s2, $a1($t2)

so what is the equivalent of the expression above?

Answer

Carl Norum picture Carl Norum · Feb 23, 2010

You can't do that because there's no MIPS instruction encoding that supports such a thing. You need to do the addition yourself:

add $a2, $a1, $t2
lw  $s2, 0($a2) 

The lw instruction encoding looks like this:

1000 11ss ssst tttt iiii iiii iiii iiii

Where sssss is the source register number, ttttt is the destination register number, and iiiiiiiiiiiiiiii is the immediate. There's no room in that encoding (and no alternate instruction encodings) that use two registers to generate the memory address. The specific machine instruction that would get encoded from the example above is:

1000 1100 1101 0010 0000 0000 0000 0000

Since the immediate is 0, $s2 is register 18 and $a2 is register 6.