There is an instruction in MIPS which allows us to load a number from Memory. like this:
.data
array: .word 1,2,3,4
.text
la $t0, A
lw $t1, 8($t0) #will load 3 to $t1
Is there any way to use (the number inside a register) instead of (immediate number)? like:
li $t2, 8
lw $t1, $t2($t1) # again will load 3 to $t1
If not how can I do indexed addressing with two registers?
MIPS lacks the ability to perform variable offsetting like you described. As Jester explained, you will just have to add
$t2to$t1like so:Remember, pointer arithmetic should always be done with unsigned addition/subtraction, as signed arithmetic instructions may cause an overflow exception (something you don't want in this case.)