Beruflich Dokumente
Kultur Dokumente
Major Goals
10012 = (1 x 23) + ( 0 x 22 ) + ( 0 x 21 ) + ( 1 x 20 )
10
= 910
...
...
3 2 1 0
Hexadecimal Representation
Why hexadecimal?
To avoid reading and writing long binary numbers
Hexadecimal (base 16) numbers are commonly used
Conversion to hexadecimal
Since base 16 is a power of 2, we can simply convert by replacing
each group of four bits by a single hexadecimal digit, and vice versa
Example of hexadecimal-to-binary conversion:
0hex - 9hex
for 00002 - 10012
ahex - fhex
for
10102 11112
1000 0000 0000 0000 0000 0000 0000 0000 2 = -23110 = -2,147,483,64810
Signed numbers
Unsigned numbers
Comparison:
Arithmetic:
Load:
lbu (load byte unsigned), lhu (load half unsigned)
Sign Extension
1111 1111 1111 1110 -> 1111 1111 1111 1111 1111 1111 1111 1110
Addition
Digits are added bit by bit from right to left, with carries
passed to the next digit to the left
Subtraction
Overflow
Examples
10
Addition (7 + 6 = 13):
=
=
710
610
1310
=
=
710
-610
110
Subtraction (7 - 6 = 1):
Examples
11
= 107374182410
= 107374182410
214748364810
Detecting Overflow
12
Addition (X + Y)
Subtraction (X - Y)
Overflow condition
Operation
Sign Bit of X
Sign Bit of Y
X+Y
X+Y
XY
XY
Effects of Overflow
13
14
15
Processor
Control Unit
ALU
Registers
& Cache
Universal Representation
16
Knowing what is exactly inside a 32-bits ALU, from now on we will use
ALU operation
a
Zero
Result
Overflow
ALU
b
Operation
AND
OR
ADD
SUB
SLT
CarryOu t
17
a
0
Result
b
18
CarryIn
(1)
(1)
(0)
(carries)
0
1
1
1
0
1
1
0_____
1 (1)1 (1)0 (0)1
a
Sum
b
CarryOut
Copyright Dr. Lai An Chow
19
Truth table:
a
0
0
0
0
1
1
1
1
Inputs
b
CarryIn
0
0
1
1
0
0
1
1
Outputs
CarryOut SumOut
0
1
0
1
0
1
0
1
0
0
0
1
0
1
1
1
Comments
0+0
0+0
0+1
0+1
1+0
1+0
1+1
1+1
0
1
1
0
1
0
0
1
+
+
+
+
+
+
+
+
0=
1=
0=
1=
0=
1=
0=
1=
002
012
012
102
012
102
102
112
Logic equations:
20
(b CarryIn) (a CarryIn) (a b)
C arryIn
C arryOut
21
3 in 1 building block
0
1
Result
2
b
CarryOut
32-Bit ALU
22
CarryIn
a0
CarryIn
Result0
ALU0
b0
Operation
CarryOut
a1
CarryIn
Result1
ALU1
b1
CarryOut
a2
CarryIn
Result2
ALU2
b2
CarryOut
a31
CarryIn
Result31
ALU31
b31
Subtraction
23
24
and subtraction
Binvert
Operation
CarryIn
0
1
Result
CarryOut
25
The 32-bit ALU being designed so far can perform add, sub, and, or
Binvert
Operation
CarryIn
26
Binvert
Operation
CarryIn
0
1
1
Result
b
Result
b
2
Less
3
Set
Less
3
Overflow
detection
Overflow
CarryOut
B in v e r t
C a rry In
a0
b0
C a rry In
ALU0
Le ss
C a rry O u t
a1
b1
0
C a rry In
ALU1
Le ss
C a rry O u t
a2
b2
0
C a rry In
ALU2
Le ss
C a rry O u t
27
O p e r a t io n
R e s u l t0
R e s u l t1
R e s u l t2
C a rry In
a31
b31
0
C a rry In
ALU 31
Le ss
R e s u l t3 1
S et
O v e r f lo w
Bnegate
detector
For addition and
AND/OR operations
both Bnegate and
CarryIn are 0 and for
subtract, they are both
1 so we combine them
into a single line
28
Operation
a0
b0
CarryIn
ALU0
Less
CarryOut
Result0
a1
b1
0
CarryIn
ALU1
Less
CarryOut
Result1
a2
b2
0
CarryIn
ALU2
Less
CarryOut
Result2
a31
b31
0
CarryIn
ALU31
Less
Zero
Result31
Set
Overflow
29
ALU operation
Bnegate
a0
b0
CarryIn
ALU0
Less
CarryOut
Result0
a1
b1
0
CarryIn
ALU1
Less
CarryOut
Result1
a2
b2
0
CarryIn
ALU2
Less
CarryOut
Result2
ALU operation
a
ALU
Zero
Result
Overflow
Zero
Result
b
CarryOu t
a31
b31
0
CarryIn
ALU31
Less
Result31
Set
Overflow
30
We can subtract as usual, but use the CarryOut31 to tell the result
If A < B
10
Carry Lookahead
31
Using the ripple carry adder, the carry has to propagate from the LSb
Key idea behind fast carry schemes without the ripple effect:
32
3. Multiplication
Multiplication
33
Product
1000
1001
1000
0000
0000
1000
1001000
Observation:
11
Signed Multiplication
34
positive number
Use any one of the above methods to compute the product of two
positive numbers
The product should be negated if the original signs of the operands
disagree
35
Multiplicand
Multiplier
Product
x
+
+
+
+
=
Convention
0010
0110
0000
0010
0010
0000
0001100
+
+
+
=
Booth
0010
0110
0000
0010
0000
0010
0001100
shift
subtract
shift
add
Keys
Shifts are much faster than adds
Algorithm looks at two bits of multiplier at a time from right to left
Take action only when the two-bit pair is either 01 or 10
Avoiding unnecessary additions to speed up the calculation
Copyright Dr. Lai An Chow
36
Multiplier = 00111100
i.e. b1 = 2, b2 = 5
M x 00111100 = 22 *M + 23 *M + 24 *M + 25 *M
= 22 * (20 + 21 + 22 + 23) *M
= 22 * (24 - 1) *M
= (26 - 22) *M
Running the Booths algorithm by scanning multiplier from right to left
Iteration 0, pattern = 00
Iteration 1, pattern = 00
Iteration 2, pattern = 10
Iteration 3, pattern = 11
Iteration 4, pattern = 11
Iteration 5, pattern = 11
Iteration 6, pattern = 01
Copyright Dr. Lai An Chow
12
Booths Algorithm
37
Detailed algorithm:
We look at 2 bits at a time (current bit and previous one):
00: middle of a string of 0s; no arithmetic operation
01: end of a string of 1s; add M to the left half of product
10: start of a string of 1s; subtract M from the left half of product
11: middle of a string of 1s- no arithmetic operations
Previous bit is set to 0 for the first iteration to form a two-bit pattern
Copyright Dr. Lai An Chow
Multiply in MIPS
38
39
4. Division
13
Division
40
Divisor
1000
1001
1001010
-1000000
0001010
0001010
0001010
-1000
10
Quotient
Dividend
Remainder
Example
41
0010
00000111
Quotient
Dividend
00001110
-0010
00011100
-0010
00111000
-0010
00011000
00110000
-0010
0001
Remainder
Copyright Dr. Lai An Chow
Division Hardware
32-bit ALU
42
Divisor
32 bits
Two registers:
32-bit ALU
Remainder
Shift right
Shift left
Write
Control
test
64 bits
Operations:
32-bit divisor is always subtracted from the left half of remainder register
The result is written back to the left half of the remainder register
The right half of the remainder register is initialized with the dividend
Left shift remainder register by one before starting
The new order of the operations in the loop is that the remainder register
will be shifted left one time to many
Thus, final correction step: must right shift back only the
remainder in the left half of the remainder register
14
43
Start
Remainder >
0
Test Remainder
Remainder < 0
32nd repetition?
Yes: 32 repetitions
Example
44
Divisor (D)
Remainder (R)
0011
0000 0111
0000 1110
1101 1110
0000 1110
0001 1100
1110 1100
0001 1100
0011 1000
0000 1000
0001 0001
1110 0001
0001 0001
0010 0010
0001 0010
0
1
2
3
4
extra
Remainder
correction
Remark
Initial state
R = R << 1
Left(R) = Left(R) D
Undo
R = R << 1, R0 = 0
Left(R) = Left(R) D
Undo
R = R << 1, R0 = 0
Left(R) = Left(R) D
R = R << 1, R0 = 1
Left(R) = Left(R) D
Undo
R = R << 1, R0 = 0
Left(R) = Left(R) >> 1
Quotient
Arithmetic for Computers
Signed Division
45
If the signs of the divisor and dividend are different, then the
quotient should be negated.
If the remainder is nonzero, then its sign should be the same as
that of the dividend.
Example:
Dividend
+7
-7
+7
-7
Copyright Dr. Lai An Chow
Divisor
+2
+2
-2
-2
Quotient Remainder
+3
+1
-3
-1
-3
+1
+3
-1
Arithmetic for Computers
15
Divide in MIPS
46
div ('divide')
divu ('divide unsigned')
Examples:
div
divu
$s1, $s2
$s1, $s2
47
Subtraction:
Multiplication:
Division:
Comparison:
48
16