gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

ASM > Is there an advantage to using RSB over SUB sometimes?

#46294 - Digeraticus - Wed Jun 22, 2005 8:04 pm

I was reading an article on ARM assembly code (looking for something else) when I stumbled across a statement that said that RSB has an advantage over SUB when the operands you want to use aren?t in the right order. It then said that the reason for this would be explained later in the article (which it wasn?t).

Anyone have a clue what this is about?

#46295 - DekuTree64 - Wed Jun 22, 2005 8:13 pm

Yes, it can save you an instruction sometimes. Most common is negating a number. With rsb, you can just do
Code:
rsb reg, reg, #0

Which is reg = 0 - reg.
Without rsb, you need 2 instructions and a temp register:
Code:
mov rTemp, #0
sub reg, rTemp, reg


'course, you could also negate without the temp reg :)
Code:
mvn reg, reg
add reg, reg, #1

but it still takes 2 instructions.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#46330 - tum_ - Thu Jun 23, 2005 8:58 am

DekuTree64 wrote:
Yes, it can save you an instruction sometimes. Most common is negating a number. With rsb, you can just do
Code:
rsb reg, reg, #0

Which is reg = 0 - reg.
Without rsb
[snipped]


In other words, the advantage is due to the fact that
in ARM the two operands are not 'equal in rights':
ARM Data Sheet wrote:

The first operand is always a register (Rn). The second operand may be a
shifted register (Rm) or a rotated 8 bit immediate value (Imm)


The same applies to RSC vs SBC.

#46391 - Digeraticus - Fri Jun 24, 2005 2:53 am

Thank you for your quick & to-the-point responses.