sub
Subtract with overflow trap
Subtracts $t3, or an immediate, from $t2 into $t1 and traps if the signed result overflows.
Used in
- The 32 registers and their namesMIPS assembly
- The MIPS instruction setMIPS assembly
- The bitmap display and keyboard registersMIPS assembly
- Moving values aroundMIPS assembly
- Variables in memory and constantsMIPS assembly
- A jump tableMIPS assembly
- Read two numbers and print their sumMIPS assembly
- Drawing shapes on the screenMIPS assembly
- A bouncing ballMIPS assembly
- Flappy birdMIPS assembly
Forms
sub , ,
Pseudo-instructions
The assembler turns these into one or more real instructions.
Operands
$t1- Any general-purpose register,
$0to$31, using its numeric or named spelling, such as$8or$t0($zerois$0;$rais$31). $t2- A general-purpose register. It may be the same register as
$t1. $t3- A general-purpose register. It may be the same register as
$t1. imm- An immediate value: a signed 16-bit value (
-32768to32767) or a 32-bit value (-2147483648to4294967295). The assembler chooses a supported encoding for the value and other operands; a pseudoinstruction may expand into several instructions.
Raises an overflow exception if the true result does not fit in 32 bits. Use 'subu' when overflow should be silently ignored.
# Initialize both inputs, then execute sub with initialized inputs. Inspect $t2: 4.
li $t0, 7
li $t1, 3
sub $t2, $t0, $t1