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?
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 .
In MIPS you can use a register, an offset, or the addition of both; but not two registers to form an effective address.
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.
$a0 = number of bytes to allocate. $v0 contains address of allocated memory.
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.
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