Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Offset by value in a register in MIPS

Tags:

mips

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?

like image 414
user2103851 Avatar asked Feb 24 '13 05:02

user2103851


People also ask

Can you offset by a register in MIPS?

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.

What does offset do in MIPS?

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.

What is LW and SW in MIPS?

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.

What is .word in MIPS?

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.


1 Answers

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.

like image 95
gusbro Avatar answered Sep 20 '22 06:09

gusbro