version 2009-08-08

SUB : SUBstract two operands

Substraction is identical to addition, but the first operand is negated (like A-B=A+(-B)).

Short forms : The second operand (snd) is substracted from the first operand (si4, either a register or a sign-extended 4-bit value) and the result is written into the second operand (snd).

Long immediate version : The source register (snd) is substracted from the sign-extended immediate field (Imm16) and the result is written to the destination register (si4).

; D4 = 123h
; R1 = 1h
SUB R1 D4   ; D4= D4+(-R1) = 123h+FFFFFFFEh = 122h
SUB 4 R1 D4 ; D4= 4h+(-R1) =   4h+FFFFFFFEh =   3h

Note that substraction by an immediate value is possible by ADD-ing a negative immediate value.

; R1 = 5678h
ADD -4 R1 D4 ; D4= 5678h+(-4) = 5674h

NEGation of a register is possible with an alias that substracts this register from 0.

The result is truncated to the 32 lower bits. The borrow bit is put in the Least Significant Bit of the NPC register.
Due to architectural optimisations, the borrow bit is negated : it is set when the substraction did not overflow.

; R1 = 4h
SUB 3 R1 ; R1 = 3 - 4 = -1 ==> borrow=0
SUB 4 R1 ; R1 = 4 - 4 = 0  ==> borrow=1
SUB 5 R1 ; R1 = 5 - 4 = 1  ==> borrow=1