Arithmetic Operations
MIPS provides a rich set of arithmetic instructions for performing mathematical operations. These instructions work with registers and follow the three-operand format: operation destination, source1, source2. Understanding these basic operations is essential for any MIPS program.
Key Points:
- add performs register addition
- sub performs register subtraction
- addi adds an immediate value
- Results overflow if they exceed 32-bit range
Example Code
# Addition
li $t0, 15
li $t1, 25
add $t2, $t0, $t1 # $t2 = 40
# Subtraction
sub $t3, $t1, $t0 # $t3 = 10
# Add immediate (constant)
addi $t4, $t0, 100 # $t4 = 115
# Subtract immediate
subi $t5, $t1, 5 # $t5 = 20Try It Yourself
1
2
3
4
5
6
7
8
9
10
11
12
13