5

I would like to ask about memory accessing. When I execute a load word command, what memory restrictions exist? Meaning what's the biggest number I can use as offset or base register?

Registers are 32 bits and as far as I know the "immediate" is 16 bits. Thus, I'm pretty sure I can't do something like

array:  .word 0:20000
~

la     $s0, array
lw     $s1, 15000($s0)
...

So if I want to access the 15000, I might need to la something smaller and go on from there right? But what's the smaller value I need to access in order to be ok and why?

Elias
  • 53
  • 1
  • 5

1 Answers1

2

The immediate field in lw is 16 bits, yes; and it is signed two's complement, so the possible range of immediate offsets is -32768..32767 - so lw $s1, 15000($s0) should be fine.

Realise that la is not a real MIPS instruction. Rather, it directs the assembler to generate the most optimal instruction sequence such that the immediate value you have specified is placed in the specified register. Thus, the full 32 bit range of values may be set using la, but one can generally produce more optimal code by using la once to place a suitable value into some register such that several subsequent instructions are able to use immediate offsets from that value than by using la each time an immediate value is needed.

So, supposing you needed to load values from offsets 40000 and 50000 of your array, you might do:

array:  .word 0:20000
~
la     $s0, array          # get address of array
la     $s1, 40000          # get offset into a register
add    $s0, $s0, $s1       # add offset to address, i.e. calculate (array+40000)
lw     $s1, 0($s0)         # fetch data from (array+40000)
lw     $s2, 10000($s0)     # fetch data from (array+40000+10000)
moonshadow
  • 86,889
  • 7
  • 82
  • 122
  • So if i would like to access a value with an offset bigger than 32767 then i would need to `la` at 32767 and use another offset?Because in my example i do use a suitable value (the first word of the array) but in the case i say i have to use another 'la' or is there another way? – Elias Mar 09 '11 at 09:20
  • If you want to access a value with an offset that won't fit in the immediate offset field, yes, you'll need to get that offset into a register and add it to the address yourself. `la` will give you the most optimal way of getting an arbitrary immediate value into a register. If you're doing this once in isolation, just use `la` for the offset you want, and `0` in the `lw`'s offset field. If you're accessing multiple things that are nearby, perform the `la` and addition once, then use the appropriate immediate offsets for the accesses. – moonshadow Mar 09 '11 at 09:28