Specifications
Usage
Carry Detection (unsigned operands):
The carry bit indicates an unsigned overflow. Before or after a
sub operation, a carry out of the MSB can be detected by
checking whether the first operand is less than the second
operand. The carry bit can be written to a register, or a
conditional branch can be taken based on the carry condition.
Both cases are shown in the following code:
sub rC, rA, rB
cmpltu rD, rA, rB
sub rC, rA, rB
bltu rA, rB, label
# The original sub operation (optional)
# rD is written with the carry bit
# The original sub operation (optional)
# Branch if carry generated
Overflow Detection (signed operands):
Detect overflow of signed subtraction by comparing the sign of
the difference that is written to rC with the signs of the
operands. If rA and rB have different signs, and the sign of rC is
different than the sign of rA, an overflow occurred. The
overflow condition can control a conditional branch, as shown
in the following code:
sub rC, rA, rB
xor rD, rA, rB
xor rE, rA, rC
and rD, rD, rE
blt rD, r0, label
# The original sub operation
# Compare signs of rA and rB
# Compare signs of rA and rC
# Combine comparisons
# Branch if overflow occurred
Exceptions
None
Instruction Type
R
NII51017
2015.04.02
sub
8-79
Instruction Set Reference
Altera Corporation
Send Feedback