Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MIPS (curiosity) faster way of clearing a register?

What is the fastest way of clearing a register (=0) in MIPS assembly?

Some examples:

xor    $t0, $t0, $t0
and    $t0, $t0, $0
move   $t0, $0
li     $t0, 0
add    $t0, $0, $0

Which is the most efficient?

like image 935
lois Avatar asked Oct 26 '10 11:10

lois


People also ask

How do I clear MIPS registers?

You can simply use the $zero register as a reference and write its value, which is 0 or 0b00000000, into the register you want to clear up. If you're working with floats or doubles you can simply declare a float and or double variable in .

Can you use a register as an offset 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.

What does Addi do in MIPS?

The ADDI instruction performs an addition on both the source register's contents and the immediate data, and stores the result in the destination register. It's syntax is: ADDI $destination register's address, $source register's address, immediate data.

What is v0 in MIPS?

$a0 = number of bytes to allocate. $v0 contains address of allocated memory.


1 Answers

In many MIPS implementations, these ops will both compile to the same instruction, because typically 'mov $a, $b' is an idiom for or $a, $b, $0 and li $r, x is shorthand for ori $r, $0, x:

move $t0, $0
li $t0, 0

and these will both take place on the same pipeline, being architecturally equivalent:

xor $t0, $t0, $t0
and $t0, $t0, $0

and in every RISC implementation I've ever worked with, add is on the same pipe as xor/and/nor/etc.

Basically, this is all particular to the implementation of a particular chip, but they all ought to be single clock. If the chip is out of order, li or and x, $0, $0 might be fastest because they minimize false dependencies on other registers.

like image 191
Crashworks Avatar answered Nov 01 '22 03:11

Crashworks