I have a register($t2) that has a randomly generated number which I then multiply by 4. My question is, is it possible to use the value in $t2 as an offset when using the lw instruction?
In MIPS you can use a register, an offset, or the addition of both; but not two registers to form an effective address. Take into account that by performing the addition you are loosing $t1 previous value, so you should use some free register as the target of the addition.
The offset is a 16-bit signed integer contained in the instruction. The sum of the address in the base register with the (sign-extended) offset forms the memory address. Here is the load word instruction in assembly language: lw d,off(b) # $d <-- Word from memory address b+off # b is a register.
Used by lw (load word), sw (store word) etc. There is one more format: the J-type format. Each MIPS instruction must belong to one of these formats. opcode.
A word generally means the number of bits that can be transferred at one time on the data bus, and stored in a register. In the case of MIPS, a word is 32 bits, that is, 4 bytes. Words are always stored in consecutive bytes, starting with an address that is divisible by 4.
In MIPS you can use a register, an offset, or the addition of both; but not two registers to form an effective address.
So, if you want to load a word pointed by a single register into, say $t0
, you would do:
lw $t0, ($t2)
However if you want to load a word pointed by the effective address $t1 + $t2
into $t0
you would first need to perform the addition and then load the word from memory, e.g.:
addu $t1, $t1, $t2
lw $t0, ($t1)
Take into account that by performing the addition you are loosing $t1
previous value, so you should use some free register as the target of the addition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With