Professional Documents
Culture Documents
Jason Mars
instruction
number
R-format
I-format ...
signed
...
integer
unsigned
...
text chars
..............
floating point
single precision
...
double precision
...
Binary
0000
Decimal
4
Binary
0100
1
2
3
0001
0010
0011
5
6
7
0101
0110
0111
3+3=6
Binary
0000
Decimal
4
Binary
0100
1
2
3
0001
0010
0011
5
6
7
0101
0110
0111
3+3=6
Binary
0000
Decimal
4
Binary
0100
1
2
3
0001
0010
0011
5
6
7
0101
0110
0111
3+3=6
Binary
0000
Decimal
4
Binary
0100
1
2
3
0001
0010
0011
5
6
7
0101
0110
0111
3+3=6
Binary
0000
Decimal
4
Binary
0100
1
2
3
0001
0010
0011
5
6
7
0101
0110
0111
3+3=6
Binary
0000
Decimal
4
Binary
0100
1
2
3
0001
0010
0011
5
6
7
0101
0110
0111
3+3=6
Binary
0000
Decimal
4
Binary
0100
1
2
3
0001
0010
0011
5
6
7
0101
0110
0111
3+3=6
Negative Numbers?
We would like a number system that provides
obvious representation of 0,1,2...
uses adder for addition
single value of 0
equal coverage of positive and negative numbers
easy detection of sign
easy negation
2 s Complement Binary
Decimal
2 s Complement Binary
0
1
2
0000
0001
0010
-1
-2
-3
1111
1110
1101
3
4
0011
0100
-4
-5
1100
1011
5
6
7
0101
0110
0111
-6
-7
-8
1010
1001
1000
Examples: 7 - 6 = 7 + (- 6) = 1
3 - 5 = 3 + (- 5) = -2
2 s Complement Binary
Decimal
2 s Complement Binary
0
1
2
0000
0001
0010
-1
-2
-3
1111
1110
1101
3
4
0011
0100
-4
-5
1100
1011
5
6
7
0101
0110
0111
-6
-7
-8
1010
1001
1000
Examples: 7 - 6 = 7 + (- 6) = 1
1
Thursday, January 24, 13
3 - 5 = 3 + (- 5) = -2
2 s Complement Binary
Decimal
2 s Complement Binary
0
1
2
0000
0001
0010
-1
-2
-3
1111
1110
1101
3
4
0011
0100
-4
-5
1100
1011
5
6
7
0101
0110
0111
-6
-7
-8
1010
1001
1000
Examples: 7 - 6 = 7 + (- 6) = 1
3 - 5 = 3 + (- 5) = -2
2 s Complement Binary
Decimal
2 s Complement Binary
0
1
2
0000
0001
0010
-1
-2
-3
1111
1110
1101
3
4
0011
0100
-4
-5
1100
1011
5
6
7
0101
0110
0111
-6
-7
-8
1010
1001
1000
Examples: 7 - 6 = 7 + (- 6) = 1
3 - 5 = 3 + (- 5) = -2
2 s Complement Binary
Decimal
2 s Complement Binary
0
1
2
0000
0001
0010
-1
-2
-3
1111
1110
1101
3
4
0011
0100
-4
-5
1100
1011
5
6
7
0101
0110
0111
-6
-7
-8
1010
1001
1000
Examples: 7 - 6 = 7 + (- 6) = 1
3 - 5 = 3 + (- 5) = -2
2 s Complement Binary
Decimal
2 s Complement Binary
0
1
2
0000
0001
0010
-1
-2
-3
1111
1110
1101
3
4
0011
0100
-4
-5
1100
1011
5
6
7
0101
0110
0111
-6
-7
-8
1010
1001
1000
Examples: 7 - 6 = 7 + (- 6) = 1
3 - 5 = 3 + (- 5) = -2
2 s Complement Binary
Decimal
2 s Complement Binary
0
1
2
0000
0001
0010
-1
-2
-3
1111
1110
1101
3
4
0011
0100
-4
-5
1100
1011
5
6
7
0101
0110
0111
-6
-7
-8
1010
1001
1000
Examples: 7 - 6 = 7 + (- 6) = 1
3 - 5 = 3 + (- 5) = -2
2 s Complement Binary
Decimal
2 s Complement Binary
0
1
2
0000
0001
0010
-1
-2
-3
1111
1110
1101
3
4
0011
0100
-4
-5
1100
1011
5
6
7
0101
0110
0111
-6
-7
-8
1010
1001
1000
Examples: 7 - 6 = 7 + (- 6) = 1
3 - 5 = 3 + (- 5) = -2
2 s Complement Binary
Decimal
2 s Complement Binary
0
1
2
0000
0001
0010
-1
-2
-3
1111
1110
1101
3
4
0011
0100
-4
-5
1100
1011
5
6
7
0101
0110
0111
-6
-7
-8
1010
1001
1000
Examples: 7 - 6 = 7 + (- 6) = 1
3 - 5 = 3 + (- 5) = -2
1
2 s Complement Binary
Decimal
2 s Complement Binary
0
1
2
0000
0001
0010
-1
-2
-3
1111
1110
1101
3
4
0011
0100
-4
-5
1100
1011
5
6
7
0101
0110
0111
-6
-7
-8
1010
1001
1000
Examples: 7 - 6 = 7 + (- 6) = 1
3 - 5 = 3 + (- 5) = -2
1
2 s Complement Binary
Decimal
2 s Complement Binary
0
1
2
0000
0001
0010
-1
-2
-3
1111
1110
1101
3
4
0011
0100
-4
-5
1100
1011
5
6
7
0101
0110
0111
-6
-7
-8
1010
1001
1000
Examples: 7 - 6 = 7 + (- 6) = 1
3 - 5 = 3 + (- 5) = -2
2 s Complement Binary
Decimal
2 s Complement Binary
0
1
2
0000
0001
0010
-1
-2
-3
1111
1110
1101
3
4
0011
0100
-4
-5
1100
1011
5
6
7
0101
0110
0111
-6
-7
-8
1010
1001
1000
Examples: 7 - 6 = 7 + (- 6) = 1
3 - 5 = 3 + (- 5) = -2
2 s Complement Binary
Decimal
2 s Complement Binary
0
1
2
0000
0001
0010
-1
-2
-3
1111
1110
1101
3
4
0011
0100
-4
-5
1100
1011
5
6
7
0101
0110
0111
-6
-7
-8
1010
1001
1000
Examples: 7 - 6 = 7 + (- 6) = 1
3 - 5 = 3 + (- 5) = -2
Overflow Detection
0
-6
-4
-2
-6
-4
-5
Overflow Detection
0
OK
-6
-4
-2
-6
-4
-5
Overflow Detection
0
OK
-6
Not OK
-4
-2
-6
-4
-5
Operand
Fetch
a
Execute
32
ALU
result
Result
Store
Next
Instruction
32
b
32
Zero
ALU
Result
Overflow
N
CarryOut
Function
And
Or
Add
Subtract
Set-on-less-than
-4
-2
-6
-4
-2
-6
0
1
-4
-2
-6
-4
-2
-6
0
1
-4
-2
-6
0
1
-4
-2
-6
-4
-2
-6
-4
-2
-6
-4
-2
-6
-4
-2
-6
0
Thursday, January 24, 13
32 Bit ALU
1-bit ALU
32-bit ALU
Operand
Fetch
a
Execute
32
ALU
result
Result
Store
Next
Instruction
32
b
32
Instruction
Fetch
Instruction
Decode
operation
Operand
Fetch
a
Execute
32
ALU
result
Result
Store
Next
Instruction
32
b
32
Instruction
Fetch
Instruction
Decode
Execute
32
ALU
result
Result
Store
Next
Instruction
Will all come together with single cycle cpu, I promise ;-)
operation
Operand
Fetch
32
b
32
Adding Subtraction
Keep in mind the following:
(A - B) is the same as: A + (-B)
2s Complement negate: Take the inverse of every bit and add 1
Bit-wise inverse of B is !B:
A - B = A + (-B) = A + (!B + 1) = A + !B + 1
Adding Subtraction
Keep in mind the following:
(A - B) is the same as: A + (-B)
2s Complement negate: Take the inverse of every bit and add 1
Bit-wise inverse of B is !B:
A - B = A + (-B) = A + (!B + 1) = A + !B + 1
Adding Subtraction
Keep in mind the following:
(A - B) is the same as: A + (-B)
2s Complement negate: Take the inverse of every bit and add 1
Bit-wise inverse of B is !B:
A - B = A + (-B) = A + (!B + 1) = A + !B + 1
Adding Subtraction
Keep in mind the following:
(A - B) is the same as: A + (-B)
2s Complement negate: Take the inverse of every bit and add 1
Bit-wise inverse of B is !B:
C-32
Appendix C The Basics of Logic Design
A - B = A + (-B) = A + (!B + 1) = A + !B + 1
Ainvert
Operation
Binvert
CarryIn
0
0
Result
CarryOut
__
__
multiplexor in Figure C.5.8 to add an input for the slt result. We call that new input
Adding Set-on-less-than
Adding Set-on-less-than
C.5
Operation
Ainvert
Binvert
a
CarryIn
1
1
Result
b
1
Less
CarryOut
We are mostly
there!
Operation
Ainvert
CarryIn
A < B = (A Binvert
- B) < 0
If true,
set LSB
to 1, all others 0
a
0
0
Else, set all bits to 0
1
1
Thursday, January 24, 13
C-33
!
1
Less
Adding Set-on-less-than
C.5
Binvert
CarryIn
a
Operation
Ainvert
Operation
Ainvert
Binvert
C-33
CarryOut
CarryIn
Result
b
Result
b
2
Less
Set
Less
MSB
CarryOut
We are mostly
there!
Operation
Ainvert
CarryIn
A < B = (A Binvert
- B) < 0
If true,
set LSB
to 1, all others 0
a
0
0
Else, set all bits to 0
1
1
Thursday, January 24, 13
Overflow
detection
Overflow
__
FIGURE C.5.10 (Top) A 1-bit ALU that performs AND, OR, and addition on a and b or b, and
(bottom) a 1-bit ALU for the most significant bit. The top drawing includes a direct input that is
connected to perform the set on less than operation (see Figure C.5.11); the bottom has a direct output from
the adder for the less than comparison called Set. (See Exercise C.24 at the end of this Appendix to see how
to calculate overflow with fewer inputs.)
AppendixC-9780123747501.indd 33
!
1
Less
Adding Set-on-less-than
C.5
Binvert
CarryIn
a
Operation
Ainvert
Operation
Ainvert
Binvert
C-33
CarryOut
CarryIn
Result
b
Result
1
2
Less
Set
Less
Overflow
__
MSB
CarryOut
We are mostly
there!
Operation
Ainvert
CarryIn
A < B = (A Binvert
- B) < 0
If true,
set LSB
to 1, all others 0
a
0
0
Else, set all bits to 0
1
1
Thursday, January 24, 13
Overflow
detection
FIGURE C.5.10 (Top) A 1-bit ALU that performs AND, OR, and addition on a and b or b, and
(bottom) a 1-bit ALU for the most significant bit. The top drawing includes a direct input that is
connected to perform the set on less than operation (see Figure C.5.11); the bottom has a direct output from
the adder for the less than comparison called Set. (See Exercise C.24 at the end of this Appendix to see how
to calculate overflow with fewer inputs.)
+
AppendixC-9780123747501.indd 33
0
1
1
0
1
1
0
1
0
0
1
1
0
1
1
Overflow Detection
Decimal
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
Overflow Detection
Decimal
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
Overflow Detection
Decimal
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
Overflow Detection
Decimal
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
Overflow Detection
Decimal
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
Overflow Detection
Decimal
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
-6
-4
-2
-6
-4
-5
Overflow Detection
Decimal
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
-6
OK
-4
-2
-6
-4
-5
Overflow Detection
Decimal
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
-6
OK
Not
OK
-4
-2
-6
-4
-5
1-bit
Result0
ALU
CarryIn1 CarryOut0
1-bit
Result1
ALU
CarryIn2 CarryOut1
1-bit
Result2
ALU
CarryOut2
CarryIn3
A3
B3
1-bit
ALU
CarryOut3
Result3
X XOR Y
0
0
0
1
0
1
1
1
0
1
1
0
1-bit
Result0
ALU
CarryIn1 CarryOut0
1-bit
Result1
ALU
CarryIn2 CarryOut1
X XOR Y
0
0
0
1
0
1
1
1
0
1
1
0
1-bit
Result2
ALU
CarryOut2
CarryIn3
A3
B3
1-bit
ALU
CarryOut3
Result3
Overflow
Adding Branch-if-equal
Adding Branch-if-equal
Subtract to the rescue
again!
Adding Branch-if-equal
Subtract to the rescue
again!
If (A - B = 0) --> A = B
Adding Branch-if-equal
Subtract to the rescue
again!
If (A - B = 0) --> A = B
Zero Detection Logic is just
one BIG NOR gate (for
equality test)
Any non-zero input to the
NOR gate will cause its
output to be zero
simplest way is to OR all the outputs together and then send that signal through
an inverter:
Zero = ( Result31 + Result30 + . . . + Result2 + Result1 + Result0 )
Figure B.5.12 shows the revised 32-bit ALU. We can think of the combination
of the 1-bit Ainvert line, the 1-bit Binvert line, and the 2-bit Operation lines as 4bit control lines for the ALU, telling it to perform add, subtract, AND, OR, or set
on less than. Figure B.5.13 shows the ALU control lines and the corresponding
ALU operation.
Adding Branch-if-equal
Subtract to the rescue
again!
Operation
Bnegate
Ainvert
If (A - B = 0) --> A = B
Zero Detection Logic is just
one BIG NOR gate (for
equality test)
Any non-zero input to the
NOR gate will cause its
output to be zero
a0
b0
CarryIn
ALU0
Less
CarryOut
a1
b1
0
CarryIn
ALU1
Less
CarryOut
CarryIn
ALU2
Less
CarryOut
a2
b2
0
..
.
.
..
. .. ..
.
a31
b31
0
Result0
Result1
..
.
Result2
..
. CarryIn
CarryIn
ALU31
Less
..
.
..
.
Result31
Set
FIGURE B.5.12 The final 32-bit ALU. This adds a Zero detector to Figure B.5.11.
Zero
Overflow
Full ALU
what signals accomplish:
Binvert CIn Oper
add?
sub?
and?
or?
beq?
slt?
ALU operation
a
Zero
ALU
Result
Overflow
CarryOut
Full ALU
what signals accomplish:
Binvert CIn Oper
0
0
2
add?
sub?
and?
or?
beq?
slt?
ALU operation
a
Zero
ALU
Result
Overflow
CarryOut
Full ALU
what signals accomplish:
Binvert CIn Oper
0
0
2
add?
1
1
2
sub?
and?
or?
beq?
slt?
ALU operation
a
Zero
ALU
Result
Overflow
CarryOut
Full ALU
what signals accomplish:
Binvert CIn Oper
0
0
2
add?
1
1
2
sub?
0
0
0
and?
or?
beq?
slt?
ALU operation
a
Zero
ALU
Result
Overflow
CarryOut
Full ALU
what signals accomplish:
Binvert CIn Oper
0
0
2
add?
1
1
2
sub?
0
0
0
and?
0
0
1
or?
beq?
slt?
ALU operation
a
Zero
ALU
Result
Overflow
CarryOut
Full ALU
what signals accomplish:
Binvert CIn Oper
0
0
2
add?
1
1
2
sub?
0
0
0
and?
0
0
1
or?
1
1
2
beq?
slt?
ALU operation
a
Zero
ALU
Result
Overflow
CarryOut
Full ALU
what signals accomplish:
Binvert CIn Oper
0
0
2
add?
1
1
2
sub?
0
0
0
and?
0
0
1
or?
1
1
2
beq?
1
1
3
slt?
ALU operation
a
Zero
ALU
Result
Overflow
CarryOut
f a b CarryIn is true, then all of the other three terms must also be true, so we
an leave out this last term corresponding to the fourth line of the table. We can
hus simplify the equation to
CarryOut = (b CarryIn) + (a CarryIn) + (a b)
igure C.5.5 shows that the hardware within the adder black box for CarryOut
onsists of three AND gates and one OR gate. The three AND gates correspond
xactly to the three parenthesized terms of the formula above for CarryOut, and
he OR gate sums the three terms.
a
0
1
1
1
CarryIn
The
carry1bit may have to
1
0
1
propagate
from LSB to MSB
1
0
Worst
delay
foris an
IGURE C.5.4 Values
of thecase
inputs when
CarryOut
a 1. N-bit
RC adder: 2N-gate delay
CarryIn0
A0
B0
A1
B1
CarryIn
A2
B2
A3
B3
CarryOut
IGURE C.5.5 Adder hardware for the CarryOut signal. The rest of the adder hardware is the logic
or the Sum output given in the equation on this page.
Thursday, January 24, 13
1-bit
Result0
ALU
CarryIn1 CarryOut0
1-bit
Result1
ALU
CarryIn2 CarryOut1
1-bit
Result2
ALU
CarryIn3 CarryOut2
1-bit
ALU
CarryOut3
Result3
f a b CarryIn is true, then all of the other three terms must also be true, so we
an leave out this last term corresponding to the fourth line of the table. We can
hus simplify the equation to
CarryOut = (b CarryIn) + (a CarryIn) + (a b)
igure C.5.5 shows that the hardware within the adder black box for CarryOut
onsists of three AND gates and one OR gate. The three AND gates correspond
xactly to the three parenthesized terms of the formula above for CarryOut, and
he OR gate sums the three terms.
a
0
1
1
1
CarryIn
The
carry1bit may have to
1
0
1
propagate
from LSB to MSB
1
0
Worst
delay
foris an
IGURE C.5.4 Values
of thecase
inputs when
CarryOut
a 1. N-bit
RC adder: 2N-gate delay
CarryIn0
A0
B0
A1
B1
CarryIn
A2
B2
1-bit
Result0
ALU
CarryIn1 CarryOut0
1-bit
Result1
ALU
CarryIn2 CarryOut1
1-bit
Result2
ALU
CarryIn3 CarryOut2
A3
B3
CarryOut
IGURE C.5.5 Adder hardware for the CarryOut signal. The rest of the adder hardware is the logic
or the Sum output given in the equation on this page.
1-bit
ALU
CarryOut3
Result3