# SUB

(Corrected dumb-ass mistake for which I kneel on the floor in utter, utter shame...) |
(Added more descriptive title.) |
||

Line 8: | Line 8: | ||

}} | }} | ||

− | ==SUB[S]== | + | ==SUB[S] : Subtraction== |

SUB will subtract one value from another. | SUB will subtract one value from another. | ||

## Latest revision as of 07:07, 22 December 2011

Instruction | SUB[S] |
---|---|

Function | Subtraction |

Category | Data processing |

ARM family | All |

Notes | - |

## Contents |

## [edit] SUB[S] : Subtraction

SUB will subtract one value from another.

Operand 1 is a register, operand 2 can be a register, shifted register, or an immediate value (which may be shifted).

If the S bit is set (* SUBS*), the N and Z flags are set according to the result, and the C and V flags are set as follows:

**C**if the result generated a borrow (a not carry; unsigned underflow);

**V**if the result generated a signed overflow.

* SUB* is useful for basic subtraction. Use SBC to perform subtraction with the Carry flag considered.

If you should need to perform the subtraction in reverse (i.e. the shifted register/immediate minus register), look at RSB (Reverse SuBtract) and RSC (Reverse Subtract with Carry).

* SUBS* can be a useful loop counter without the need for a comparison operation. Code such as the following may be used:

MOV R10, #123 ; our loop counter start value .loop ...some code here... SUBS R10, R10, #1 BNE loop ; comes to here when EQ (ie when R10 = 0)

### [edit] Syntax

SUB<suffix> <dest>, <op 1>, <op 2>

### [edit] Function

dest = op_1 - op_2

### [edit] Example

SUB R0, R0, R1 ; Subtract R1 from R0 SUB R0, R0, #1 ; Decrement R0 by one

### [edit] Technical

The instruction bit pattern is as follows:

31 - 28 | 27 | 26 | 25 | 24 - 21 | 20 | 19 - 16 | 15 - 12 | 11 - 0 |
---|---|---|---|---|---|---|---|---|

condition | 0 | 0 | I | 0 0 1 0 | S | op_1 | dest | op_2/shift |