Two’s Complement
Subtraction
Overflow
| Operation | Sign Bit of X | Sign Bit of Y | Sign Bit of Result | Sign Bit of Expected |
|---|---|---|---|---|
| Adding two +ve (0) | ||||
| X + Y | 0 | 0 | 1 | X + Y = 0 |
| X - Y | 0 | 1 | 1 | (X - (-Y)) = 0 |
| Subtracting two -ve (1) | ||||
| X + Y | 1 | 1 | 0 | ((-X) + (-Y)) = 1 |
| X - Y | 1 | 0 | 0 | ((-X) - Y) = 1 |
MIPS “Unsigned”
- Sign extension (arithmetic operations)
addiaddiusltilhlb
- Zero extension (logical operations)
andiorilhulbu
Important
Immediate operand in MIPS is always sign extended

Arithmetic Logic Unit (ALU)
1-Bit Full Adder
| A | B | Carry In | Carry Out | Sum Out | a + b + CarryIn |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 + 0 + 0 = 00 |
| 0 | 0 | 1 | 0 | 1 | 0 + 0 + 1 = 01 |
| 0 | 1 | 0 | 0 | 1 | 0 + 1 + 0 = 01 |
| 0 | 1 | 1 | 1 | 0 | 0 + 1 + 1 = 10 |
| 1 | 0 | 0 | 0 | 1 | 1 + 0 + 0 = 01 |
| 1 | 0 | 1 | 1 | 0 | 1 + 0 + 1 = 10 |
| 1 | 1 | 0 | 1 | 0 | 1 + 1 + 0 = 10 |
| 1 | 1 | 1 | 1 | 1 | 1 + 1 + 1 = 11 |
Note that
Note that

- Bin and Cin can be combined
| BInvert | CarryIn | |
|---|---|---|
| AND | 0 | x |
| OR | 0 | x |
| NOT | 0 | 0 |
| SUB | 1 | 1 |
- slt
- operation 3 for slt
- alu1 - alu31 set = 0
- alu0 set = alu31 set
Overflow
| Operation | Sign A | Sign B | Sign Result | BInvert | a3 | b3 | set |
|---|---|---|---|---|---|---|---|
| A + B | 0 | 0 | 1 | 0 | 0 | 0 | 1 |
| A + B | 1 | 1 | 0 | 0 | 1 | 1 | 0 |
| A - B | 0 | 1 | 1 | 1 | 0 | 1 | 1 |
| A - B | 1 | 0 | 0 | 1 | 1 | 0 | 0 |
beq (A - B == 0)
- BInvert = 1
- Operation = 2
- Check if all bits 0, and then NOT
Multiplication
- Multiplier, stored in product
- Multiplicand (M), 32 bits
- Product (P), 32 + 32 = 64 bits
- Initially zero extended
Left(P)is 0Right(P)is the multiplier
- If
Product0is 1Left(P) = Left(P) + M
P >> 1- Exit on the 32nd repetition
Signed Multiplication
- Do the above calculation based on the absolute value
- Negate result based on sign of multiplicand/multiplier
- Booth’s algorithm
Unsigned Overflow
No overflow if Hi is 0
multu $s0, $s1
mfhi $at
mflo $s2
beq $at, $0, no_overflow
# overflow
no_overflow:
# no overflow
Signed Overflow
No overflow if every bit in Hi is same as sign bit of Lo
sra $t0, $s2, 31 will turn 0...x to 0...0, making all bit to the sign of Lo, then compare it with Hi
mult $s0, $s1
mfhi $at
mflo $s2
sra $t0, $s2, 31 # shift right arithmetic
beq $at, $t0, no_overflow
# overflow
no_overflow:
# no overflow
Division
- Quotient, stored in remainder
- Dividend, stored in remainder
- Divisor (D), 32 bits
- Remainder (R), 32 + 32 = 64 bits
- Initially zero extended
Left(R)is 0Right(R)is dividend
- Quotient appear from the right by shifting
- Remainder will appear at the left at the end
- Initially zero extended
R << 1, now the lsb is the useless 0Left(R) = Left(R) - D- If
Left(R) < 0Left(R) = Left(R) + D, to undo subtractionR << 1R_0 = 0
- If
Left(R) >= 0R << 1R_0 = 1
- Exit on 32nd repetition
Left(R) >> 1
If divisor is 0, quotient will be all 1
Signed Division
- Sign of dividend and remainder is same
- Deduce sign of quotient by looking at divisor
| Dividend | Divisor | Quotient | Remainder |
|---|---|---|---|
| + | + | + | + |
| - | + | - | - |
| + | - | - | + |
| - | - | + | - |