Arithmetic/Logical Unit
(ALU)
Digital Computer Design
Arithmetic Circuits
• Arithmetic circuits are the central building blocks of
computers.
• Computers and digital logic perform many arithmetic
functions:
– addition, subtraction, comparisons, shifts, multiplication
and division
2
1-Bit Adders
Half
Adder
Full
Adder
A
A
Cout
B
Cout
+
B
0
1
0
1
Cout
0
0
0
1
S
=A⊕B
Cout = AB
Cin
+
S
A
0
0
1
1
B
S
S
0
1
1
0
Cin
0
0
0
0
1
1
1
1
A
0
0
1
1
0
0
1
1
B
0
1
0
1
0
1
0
1
Cout
0
0
0
1
0
1
1
1
S
0
1
1
0
1
0
0
1
S = A ⊕ B ⊕ Cin
Cout = AB + ACin + BCin
3
Multibit Adders - Carry Propagate Adders
(CPAs)
• An N-bit adder sums two N-bit inputs A and B, and a carry in
Cin to produce an N-bit result S and a carry out Cout.
Cout
A
B
N
N
+
Cin
N
S
• Multibit adder is commonly called a carry propagate adder
(CPA)
– because the carry out of one bit propagates into the next bit.
4
CPAs: Ripple-Carry Adder
• Chain together N full adders.
– The Cout of one stage acts as the Cin of the next stage
A31
Cout
B31
+
S31
A30
C30
B30
+
S30
C29
A1
C1
B1
+
S1
A0
C0
B0
+
Cin
S0
• The ripple-carry adder is slow when N is large.
– For an 32-bit adder, S31 depends on C30, which depends
on C29, which depends on C28, and so forth all the way
back to Cin.
5
CPAs: Ripple-Carry Adder
• The fundamental reason that large ripple-carry adders are
slow is that the carry signals must propagate through every
bit in the adder.
A31
Cout
B31
+
A30
C30
S31
B30
+
C29
A1
C1
S30
B1
+
S1
A0
C0
B0
+
Cin
S0
tripple = NtFA
where tFA is the delay of a 1-bit full adder
6
Subtracter
• Subtraction is almost as easy:
– flip the sign of the second number, then add.
Symbol
Implementation
A
A
B
N
B
N
N
N
N
Y
N
+
N
Y
• Flipping the sign of a two’ s complement number is done by
inverting the bits and adding 1.
7
Comparator: Equality
• A comparator determines whether two binary numbers
are equal or if one is greater or less than the other.
Symbol
Implementation
A3
B3
A
B
4
4
=
Equal
A2
B2
A1
B1
Equal
A0
B0
8
Comparator: Less Than
• Compute A − B and looking at the sign (most significant bit)
of the result.
– If the result is negative (i.e., the sign bit is 1), then A is less than
B. Otherwise A is greater than or equal to B.
A
B
N
N
N
[N-1]
A<B
Sign bit
• This comparator, however, functions incorrectly upon
overflow.
9
Arithmetic/Logical Unit
• ALU is one of the main components in the
microprocessor.
31:28
27:26
25:20
15:12
PCSrc
Control
MemtoReg
Unit
MemWrite
Cond
ALUControl
Op
ALUSrc
Funct
ImmSrc
Rd
RegWrite
Flags
ALUFlags
0
A
RD
Instruction
Memory
15
0
CLK
RA1
1
3:0
0
1
RA2
15:12
+
4
PCPlus4
PCPlus8
A1
CLK
WE3
A2
RD2
A3 Register
WD3 File
R15
WE
SrcA
RD1
0
1
SrcB
ALU
PC
+
PC'
Instr
1
19:16
RegSrc
0 1
CLK
ALUResult
WriteData
A
RD
Data
Memory
WD
ReadData
1
0
4
23:0
Extend
ExtImm
Result 10
ALU: Arithmetic/Logical Unit
• An Arithmetic/Logical Unit (ALU ) combines a variety of
mathematical and logical operations into a single unit.
• The ALU forms the heart of most computer systems.
ALUControl1:0
Function
00
Add
01
Subtract
10
AND
11
OR
The ALU receives a 2-bit control signal ALUControl that specifies
which function to perform.
11
ALU Implementation
ALUControl1:0
Function
00
Add
01
Subtract
10
AND
11
OR
12
ALU Implementation
ALUControl1:0
Function
00
Add
01
Subtract
10
AND
11
OR
Example: Perform A OR B
ALUControl1:0 = 11
Mux selects output of OR gate as Result
Result = A OR B
13
ALU Implementation
ALUControl1:0
Function
00
Add
01
Subtract
10
AND
11
OR
Example: Perform A + B
ALUControl1:0 = 00
Cin to adder = 0
2nd input to adder is B
Mux selects Sum as Result
Result = A + B
14
ALU with Status Flags
Flag
Description
N
Result is Negative
Z
Result is Zero
C
Adder produces Carry out
V
Adder oVerflowed
Some ALUs produce extra outputs, called flags, that indicate
information about the ALU output.
15
ALU with Status Flags
16
ALU with Status Flags: Negative
N = 1 if:
Result is negative
So, N is connected to
most significant bit of
Result
17
ALU with Status Flags: Zero
Z = 1 if:
all of the bits of Result
are 0
18
ALU with Status Flags: Carry
C = 1 if:
Cout of Adder is 1
AND
ALU is adding or
subtracting (ALUControl
is 00 or 01)
19
ALU with Status Flags: oVerflow
V = 1 if:
The addition of 2 samesigned numbers
produces a result with
the opposite sign. (the
result is too big to fit
in the available digits.)
20
ALU with Status Flags: oVerflow
V = 1 if:
ALU is performing addition or subtraction
(ALUControl1 = 0)
21
ALU with Status Flags: oVerflow
V = 1 if:
ALU is performing addition or subtraction
(ALUControl1 = 0)
AND
A and Sum have opposite signs
22
ALU with Status Flags: oVerflow
V = 1 if:
ALU is performing addition or subtraction
(ALUControl1 = 0)
AND
A and Sum have opposite signs
AND
A and B have same signs upon addition
(ALUControl0 = 0)
OR
A and B have different signs upon subtraction
(ALUControl0 = 1)
23
Shifters/Rotators
• Shifters and rotators move bits and multiply or divide by
powers of 2.
• As the name implies, a shifter shifts a binary number left or
right by a specified number of positions.
Logical shifter: shifts value to left or right and fills empty
spaces with 0’s
– Ex: 11001 >> 2 = 00110
– Ex: 11001 << 2 = 00100
24
Shifters/Rotators
Arithmetic shifter: right shift, fills empty spaces with the old
most significant bit (msb)
– Ex: 11001 >>> 2 = 11110
– Ex: 11001 <<< 2 = 00100
Rotator: rotates bits in a circle, such that bits shifted off one
end are shifted into the other end
– Ex: 11001 ROR 2 = 01110
– Ex: 11001 ROL 2 = 00111
25
Example: Logical Shift Right Implementation
• An N-bit shifter can be built from N N:1
multiplexers.
• The input is shifted by 0 to N−1 bits,
depending on the value of the log2N-bit
select lines.
A3 A 2 A 1 A 0
shamt1:0
2
00
S1:0
01
Y3
10
11
00
01
S1:0
Y2
10
11
shamt1:0
00
2
01
S1:0
Y1
10
A3:0
4
>>
4
Y3:0
11
00
01
Depending on the value of the 2-bit shift
amount shamt1:0, the output Y receives the
input A shifted by 0 to 3 bits.
10
S1:0
Y0
11
0 (Ground)
26
Further Reading
• You can read Chapter 5 of your book
– From Section 5.1 to 5.2.5
27