# SUB

(Difference between revisions)
Jump to: navigation, search

Instruction SUB[S] Subtraction Data processing All -

## SUB[S]

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 (unsigned underflow); V if the result generated a signed overflow.

SUB is useful for basic addition. Use SBC to perform addition 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)
```

### Syntax

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

### Function

```  dest = op_1 - op_2
```

### Example

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

### 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