Lecture 7 COMP2611 Arithmetic Part1
Lecture 7 COMP2611 Arithmetic Part1
We’ve learnt:
How to represent negative numbers
How to represent fractions and real numbers
What is the representable range
Signed numbers
negative or non-negative integers, e.g. int in C/C++
Unsigned numbers
non-negative integers, e.g. unsigned int in C/C++
What are the values in registers $t0 and $t1 in the examples below?
slt $t0, $s0, $s1 # signed comparison
$s0 = -110, $s1 = 110, $t0 = 1
sltu $t1, $s0, $s1 # unsigned comparison
$s0 = 429496729510, $s1 = 110, $t1 = 0
Sign extension: Fill the leftmost bits (n-th ~ (m-1)-th) with the sign
bit
2 (16 bits -> 32 bits):
0000 0000 0000 0010 -> 0000 0000 0000 0000 0000 0000 0000 0010
-2 (16 bits -> 32 bits):
1111 1111 1111 1110 -> 1111 1111 1111 1111 1111 1111 1111 1110
addi, lb
Zero extension: Pad the leftmost bits (n-th ~ (m-1)-th) with 0
and, or
The MIPS32 Instruction Set states that the word ‘unsigned’ as part of
add and subtract instructions, is a misnomer. The difference between
signed and unsigned versions of commands is not a sign extension of
the operands, but controls whether a trap is executed on overflow (i.e.
add) or an overflow is ignored (i.e. addu). An immediate operand
CONST to these instructions is always sign-extended.
Addition
Bits are added bit by bit from right to left, with carries passed
to the next bit position to the left
Subtraction
Subtraction uses addition
The appropriate operand is negated before being added to the
other operand
Overflow
The result is too large to fit into a word (32 bits in MIPS)
Addition (7 + 6 = 13):
Subtraction (7 - 6 = 1):
Addition (X + Y) Subtraction (X - Y)
Overflow condition
Processor
Control Unit
ALU
Registers
& Cache
a
0
Result
1
b
CarryIn
a
(1) (1) (0) (carries)
0 1 1 1
Sum
0 1 1 0
1 (1)1 (1)0 (0)1 b
CarryOut
Truth table:
Inputs Outputs
Comments
a b CarryIn CarryOut SumOut
0 0 0 0 0 0 + 0 + 0 = 002
0 0 1 0 1 0 + 0 + 1 = 012
0 1 0 0 1 0 + 1 + 0 = 012
0 1 1 1 0 0 + 1 + 1 = 102
1 0 0 0 1 1 + 0 + 0 = 012
1 0 1 1 0 1 + 0 + 1 = 102
1 1 0 1 0 1 + 1 + 0 = 102
1 1 1 1 1 1 + 1 + 1 = 112
Logic equations:
CarryIn
CarryOut
3 in 1 building block
Use the Operation bits to decide what result to push out
Operation = 0, do AND
Operation
Operation = 1, do OR
CarryIn
Operation = 2, do addition
a
0
1
Result
2
b
CarryOut
implementations (based on
the carry lookahead idea
to be explained later)
a31 CarryIn
Result31
ALU31
b31
a
0
1
Result
b 0 2
CarryOut
The 32-bit ALU being designed so far can perform add, sub, and, or
operations which constitute a large portion of MIPS’ instruction set
Two instructions not yet supported are: slt and beq
If Rs – Rt ≥ 0 If Rs – Rt < 0
31 30 29 28 ... 3 2 1 0 31 30 29 28 ... 3 2 1 0
$s1 ... ...
31 30 29 28 ... 3 2 1 0 31 30 29 28 ... 3 2 1 0
$s2 ... ...
31 30 29 28 ... 3 2 1 0 31 30 29 28 ... 3 2 1 0
Res 0 ...
1 ...
31 30 29 28 ... 3 2 1 0 31 30 29 28 ... 3 2 1 0
$t0 ... ...
0 1
All 0
COMP2611 CSE HKUST Arithmetic for Computers
Tailoring the ALU for MIPS (cont’d) 26
a
a 0
0
1
1
Result
b 0 2
Result
1
b 0 2
Less 3
1
Set
Less 3
Overflow Overflow
detection
CarryOut
1-bit ALU for bits 0 to 30 1-bit ALU for the MSb (bit 31)
B in v e r t C a rry In O p e r a t io n
subtraction, A – B b0 ALU0
Le ss
R e s u lt0
It is passed to LSB
C a rryO u t
instruction is being
executed) a2
b2
C a rry In
ALU2 R e s u lt2
0 Le ss
C a rryO u t
C a rry In
To support beq
We need to compare Rs to Rt
detector
For addition and
a0 CarryIn Result0
b0 ALU0
AND/OR operations Less
CarryOut
both Bnegate and
CarryIn are 0 and for a1 CarryIn Result1
subtract, they are both b1
0
ALU1
Less
1 so we combine them CarryOut Zero
Result31
a31 CarryIn
b31 ALU31 Set
0 Less Overflow
Knowing what is exactly inside a 32-bits ALU, from now on we will use
the universal symbol for a complete ALU as follows:
ALU operation
ALU Control lines Operation
a 000 AND
Zero 001 OR
ALU Result
Overflow 010 ADD
b 110 SUB
111 SLT
CarryOu t
Using the ripple carry adder, the carry has to propagate from the LSb
to the MSb in a sequential manner, passing through all the 32 1-bit
adders one at a time. SLOW for time-critical hardware!
Key idea behind fast carry schemes without the ripple effect:
WE can build a circuit to predict all Carries at the same time and do
the additions in parallel
Possible because electronic chips becoming cheaper and denser