SPRUI30H November 2015 – May 2024 DRA745 , DRA746 , DRA750 , DRA756
Signed Subtraction of Two Register Values
SUB src1, src2, dst
Functional unit = D
16 bit
15 | 13 | 12 | 10 | 9 | 7 | 6 | 0 |
dst | src2 | src1 | 0 | 1 | 1 | 0 | 0 | 1 | 1 |
3 | 3 | 3 | opcode |
Subtract src1 from src2 and store result to dst.
CSR[2]EQ = (dst == 0)
CSR[5]C = {not borrow} from (src2 - src1)
CSR[7]V = {overflow} from (src2 - src1)
Note that the carry (CSR[5]C) and overflow (CSR[7]V) bits are relevant to unsigned and signed operations, respectively. Appropriate bits are checked depending on if the operands are represented (or, interpreted) as unsigned or signed numbers.
The status of the CSR[5]C bit can be used to synthesize multiword (wider) subtraction. With the SUB instruction, the CSR[5]C bit is set if no borrow occurs and the CSR[5]C bit is cleared if a borrow occurs. In other words, for SUB instructions, the CSR[5]C bit represents a not borrow. To synthesize multiword subtractions, subsequent instructions can use the CSR[5]C bit as a NOT(borrow) operand, performing a normal subtraction if CSR[5]C == 1 and subtracting one more than usual if CSR[5]C == 0.
For example, if register pairs R0/R1 and R2/R3 hold 64-bit values (where R0 and R2 hold the least-significant words), the following instructions leave the 64-bit difference in the register pair R4/R5:
SUB R2, R0, R4 ; R4=R0-R2, sets CSR[C]
MVC CSR, R6 ; leaves CSR[C] untouched
EXTU 5, 5, R6, R6 ; if (CSR[C]) R6=0x1 else R6=0x0
BNEQ L1
SUB R3, R1, R5 ; R5=R1-R3
SUB 1, R5, R5 ; R5=R5-1 if CSR[C] was NOT set
L1: NOP ; else R5=R5
Note that the above example needs appropriate modification in order to generate correct carry/overflow applicable for the total 64-bit result.
dst = src2 - src1