Basic Arithmetic Operations
Basic Arithmetic Operations
Computer Architecture
Number representation Review, basic arithmetic ops
Reading assignment - PH 3.1-3.4, Appendix B
Review
A bit is a single value, either 1 or 0.
A byte is an ordered sequence of 8 bits.
memory is typically byte addressed each byte has a unique address.
.. 3 2 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1011
Most Significant
bit (MSB)
Byte
Review-Unsigned Numbers
Number can be represented in any Base: Base 10, Base 2, Base 16,
Number Base B => d31d30 ... d1d0 is a 32 digit number
Value in decimal = d31 B31 + d30 B30 + ... + d1 B1 + d0 B0
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
4
00001
...
01111
Example: 1101two
=
=
=
=
=
2 N x 2 N 1 x 1
and add 1
- 0 0 1 0 1 1 0 1 (45)
(unsigned)
=
=
=
=
=
=
20
10
5
2
1
0
remainder =
remainder =
remainder =
remainder =
remainder =
remainder =
(41)10 = (101001)2
1
0
0
1
0
1
Add them
Subtract them
Multiply them
Divide them
Compare them
11
Binary Addition
Rules of Binary Addition
0+0=0
0+1=1
1+0=1
1 + 1 = 0, and carry 1 to the next more
significant bit
12
Binary Subtraction
Rules of Binary Subtraction
0-0=0
0 - 1 = 1, and borrow 1 from the next more significant bit
1-0=1
1-1=0
13
Overflow
Overflow occurs when the result is too large to represent in the
number of bits allocated.
Example (4-bit unsigned numbers):
+15
1111
+3
0011
+18
10010
But we dont have room for 5-bit solution, so the solution would be 0010,
which is +2, and wrong.
15
easy and simple ALU does ADD, SUB, bitwise AND, bitwise OR
OP
A
32
ALU
32
32
Result
16
16
17
17
Multiplication (1/3)
More complicated than addition
Can be accomplished via shifting and adding
Rules [0 x 0 = 0];[1 x 0 = 0]; [0 x 1 = 0]; [1 x 1 = 1]
Multiplicand
Multiplier
1000
8
x 1001
9
1000
0000
0000
+1000
01001000
m bits x n bits = m + n bit product
Note: Length of product is the sum of operand lengths
the product of 2 n-bit numbers requires 2n bits
18
Multiplication Hardware
Start with long-multiplication approach
Initially 0
Multiplication Ex
1 0 0 1 MR
P 0 0 0 0 0 0 0 0
P 0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0
MD
Iteration 2, MR[0]=0
0 1 0 0 0 0 0 0
0 0 0 1 MR
P 0 0 0 0 1 0 0 0
MD
Iteration 4, MR[0]=1
0 0 1 0 MR
P 0 0 0 0 1 0 0 0
MD
Iteration 3, MR[0]=0
0 1 0 0 MR
1 0 0 0 0 0 0 0
0 0 0 0 MR
P 0 1 0 0 1 0 0 0
20
20
Multiplication
Optimization
Least significant bit of the multiplier (Multiplier0) determines whether the multiplicand is
added to the Product register (step 1 and 1.a)
Left shift in step 2 moves the intermediate operands to the left.
Shift right in step 3 gives us the next bit of the multiplier to examine in the following iteration.
These three steps are repeated 32 times to obtain the product. If one clock cycle per step took a
clock cycle, then almost 100 clock cycles required to multiply two 32-bit numbers.
Optimization: Perform steps in parallel: add/shift
Optimized Multiplier
Multiplication
Loop for n iterations, in each do
EX2
MR:Multiplier
P 0 0 0 0 1 0 0 1
MR
Iteration 1, P[0]=1 MD 1 0 0 0
P 1 0 0 0 1 0 0 1
P 0 1 0 0 0 1 0 0
Iteration 2, P[0]=0
P 0 0 1 0 0 0 1 0
Iteration 3, P[0]=0
P 0 0 0 1 0 0 0 1
Iteration 4, P[0]=1 MD 1 0 0 0
P 1 0 0 1 0 0 0 1
P 0 1 0 0 1 0 0 0
23
mult
rs, rt
multu
rs, rt
we multiply 32 bit registers, so: 32-bit value x 32-bit value = 64-bit value
puts 64-bit product in special result registers HI and LO
HI: most-significant 32 bits (i.e., upper half )
LO: least-significant 32-bits (i.e., lower half )
(HI, LO) = rs* rt
special registers beyond the 32 registers
mulrd,rs,rt
Least-significant 32 bits of product > rd
mult $s2,$s3
# b*c
mfhi $s0
# upper half of
# product into $s0
mflo $s1
# lower half of
# product into $s1
Note: Often, we only care about the lower half of
the product.
Multiplication Machine
Instructions
opcod
e
mult $rs,
000
$rt
000
multu $rs, 000
$rt
000
000
mfhi $rd
000
000
mflo $rd
000
5 bits
00000
00000
5 bits
5 bits
00000
00000
00000
00000
5 bits
00000
00000
00000
5 bits
00000
functi
on
011
000
011
001
010
000
010
010
Unsigned number:
Quotient
1001 Divisor
1000)1001010 Dividend
Remainder
-1000
(or Modulo result)
10
Dividend = Quotient x Divisor + Remainder
101
1010
-1000
10
Division
Division is a shift and subtract" procedure : based on repeated
subtraction. The quotient of two integers is not necessarily an integer
set 1 in quotient bit, subtract and check to see if the result is positive,
otherwise, 0 set as the quotient bit, and bring down the next bit from dividend.
Note that the divisor and remainder can be the same size.
28
Division Hardware
Initially divisor
in left half
Initially dividend
Division Alg
R: Remainder
D:Divisor
R 0 0 0 0 0 1 1 1
7/2
Q:Quotient
0 0 0 0 Q
D 0 0 1 0 0 0 0 0
Iteration 1, R<0
0 0 0 0 Q
D 0 0 1 0 0 0 0 0
R=R+D 0 0 0 0 0 1 1 1
Iteration 2, R<0
Srl D
0 0 0 1 0 0 0 0
R=R-D
1 1 1 1 0 1 1 1
R=R+D 0 0 0 0 0 1 1 1
Srl D
0 0 0 0 1 0 0 0
0 0 0 0 Q
Division Alg
Iteration 2, R<0
R=R-D
1 1 1 1 0 1 1 1
0 0 0 0 Q
R=R+D 0 0 0 0 0 1 1 1
Srl D
Iteration 3, R<0
R=R-D 1 1 1 1 1 1 1 1
0 0 0 0 0 1 0 0
R=R-D 0 0 0 0 0 0 1 1
Srl D
Iteration 5, R 0
0 0 0 0 Q
R=R+D 0 0 0 0 0 1 1 1
Srl D
Iteration 4, R 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0
R=R-D 0 0 0 0 0 0 0 1
Srl D
0 0 0 1 Q
0 0 0 0 0 0 0 1
0 0 1 1 Q
Optimized Divider
divu rs, rt
in MIPS: a$s0;b$s1;c$s2;d$s3
div $s2,$s3
mflo $s0
mfhi $s1
opcode
div $rs, $rt
divu $rs, $rt
000
000
000
000
# lo=c/d, hi=c%d
# get quotient
# get remainder
register register register shift functio
s
t
d
amount
n
011
5bits
5bits 00000 00000
010
011
5bits
5bits- 00000 00000
011
33
MIPS
Assembl
y
Languag
e
34