Professional Documents
Culture Documents
jalr r2
Or
jalr r31, r2
jr r2
31
26 25
21 20
16 15
11 10
31
26 25
21 20
16 15
11 10
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 1 0 0 1
rd
opcode
rs
0
0
funct
(default=31)
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0
opcode
rs
0
0
funct
Arithmetic
performance
abstractions
instruction set architecture
assembly language and machine language
What's up ahead:
a
32
ALU
result
32
b
32
Numbers
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001...
decimal: 0, , 2n-1
n bits
for some fractions and real numbers, finitely many bits is not enough, so
overflow & approximation errors: e.g., represent 1/3 as binary!
negative integers
Possible Representations
One's Complement
000 = 0
001 = +1
010 = +2
011 = +3
100 = -3
101 = -2
110 = -1
111 = 0
ambiguous zero
Sign Magnitude:
000 = 0
001 = +1
010 = +2
011 = +3
100 = 0
101 = -1
110 = -2
111 = -3
Issues:
ambiguous zero
Two's Complement
000 = 0
001 = +1
010 = +2
011 = +3
100 = -4
101 = -3
110 = -2
111 = -1
Representation Formulae
Twos complement:
xn xn-1x0 = xn * -2n + xn-1 * 2n-1 + + x0 * 20
or
xnX = xn * -2n + X (writing rightmost n bits xn-1x0 as X)
=
X,
if xn = 0
-2n + X, if xn = 1
Ones complement:
xnX =
X,
if xn = 0
-2n + 1 + X, if xn = 1
MIPS 2s complement
1111
1111
0000
0000
0000
1111
1111
0000
0000
0000
1111
1111
0000
0000
0000
1111
1111
0000
0000
0000
1111
1111
0000
0000
0000
1110two
1111two
0000two
0001two
0010two
=
=
=
=
=
+
+
2,147,483,646ten
2,147,483,647ten
2,147,483,648ten
2,147,483,647ten
2,147,483,646ten
maxint
minint
Example: 4-bit
8-bit
0010
0000 0010
1010
1111 1010
MIPS Notes
sb and sh
slt & slti
Examples (4-bits):
0101
0110
1011
1001
1111
0001
0101
0111
1010
1110
terminology alert: overflow does not mean there was a carry-out from
the msb that we lost (though it sounds like that!) it means simply
that the result in the fixed-sized register is incorrect
as can be seen from the above examples there are cases when the result
is correct even after losing the carry-out from the msb
Detecting Overflow
A
A
A
A
+
+
B
B
B
B
Operand A
0
0
0
0
Operand B
0
0
0
0
Result
Indicating Overflow
0
0
0
0
Effects of Overflow
Multiply
1000
x 1001
1000
0000
0000
1000
01001000
Multiplicand
Multiplier
Product
Shift-add Multiplier
Start
Product0 = 1
1. Test
Product0
Product0 = 0
Multiplicand
32 bits
32-bit ALU
Product
Shift right
Write
Control
test
64 bits
32nd repetition?
Yes: 32 repetitions
Done
Algorithm
Observations on Multiply
Faster Multiplier
Cost/performance tradeoff
Can be pipelined
MIPS Notes
Divide
1001
Divisor 1000
1001010
1000
10
101
1010
1000
10
Quotient
Dividend
Remainder
Junior school method: see how big a multiple of the divisor can be
subtracted, creating quotient digit at each step
Binary makes it easy first, try 1 * divisor; if too big, 0 * divisor
Start
Divide
Divisor
Remainder >
0
32 bits
Test Remainder
Remainder < 0
32-bit ALU
3a. Shift the Remainder register to the
left, setting the new rightmost bit to 1
Remainder
Shift right
Shift left
Write
Control
test
64 bits
32nd repetition?
Yes: 32 repetitions
Algorithm
Observations on Divide
Signed divide:
MIPS Notes
Floating Point
Representation:
binary point
31
sign
bits 30 to 23
8-bit exponent
31
sign
23-bit significand
bits 30 to 20
11-bit exponent
bits 19 to 0
upper 20 bits of 52-bit significand
bits 31 to 0
Special treatment of 0:
exponent
significand
Floating-Point Example
S=1
Fraction = 01000002
Exponent = 100000012 = 129
Double Precision
Object
Represented
Exponent
Fraction
Exponent
Fraction
0 (zero)
Non-zero
Non-zero
Denormaliz
ed number
1-254
Anything
1-2046
Anything
Floatingpoint number
255
2047
Infinity
255
Non-zero
2047
Non-zero
NaN (Not a
Number)
Single-Precision Range
Exponent: 00000001
actual exponent = 1 127 = 126
Fraction: 00000 significand = 1.0
1.0 2126 1.2 1038
Largest value
exponent: 11111110
actual exponent = 254 127 = +127
Fraction: 11111 significand 2.0
2.0 2+127 3.4 10+38
Double-Precision Range
Exponent: 00000000001
actual exponent = 1 1023 = 1022
Fraction: 00000 significand = 1.0
1.0 21022 2.2 10308
Largest value
Exponent: 11111111110
actual exponent = 2046 1023 = +1023
Fraction: 11111 significand 2.0
2.0 2+1023 1.8 10+308
Round
Algorithm
Initial values
1 00000001
S
000001100
0 00000011
010000111
000001100
M
difference = 2
0 00000011
S
010000111
M
010000011
0 00000011
010000111
Add mantissas
1 00000011
S
010000011
0 00000011
010000111
-0.010000011 + 1.010000111 =
1.000000100
0
S
000000100
E
000000100
M
Final answer
1 00000001
S
000001100
0 00000011
010000111
0 00000011
S
000000100
M
Hardware design
determine
smaller
exponent
Hardware design
shift mantissa
of smaller
number right
by exponent
difference
Hardware design
add mantissas
Hardware design
normalize result by
shifting mantissa of
result
Hardware design
round result
Hardware design
renormalize if
necessary
FP Adder Hardware
Can be pipelined
Round
Algorithm
Initial values
1 00000111
S
100000000
0 11100000
100000000
-1.5 x 27-127
1.5 x 2224-127
Add exponents
1 00000111
S
100000000
0 11100000
100000000
-1.5 x 27-127
1.5 x 2224-127
Subtract bias
1 00000111
S
0 11100000
S
100000000
-1.5 x 27-127
100000000
1.5 x 2224-127
100000000
0 11100000
100000000
01101000
S
-1.5 x 27-127
1.5 x 2224-127
0 11100000
S
100000000
M
100000000
M
01101001
S
-1.5 x 27-127
001000
M
1.5 x 2224-127
0 11100000
S
1 01101001
S
100000000
-1.5 x 27-127
100000000
1.5 x 2224-127
001000
M
-1.125 x 2105-127
FP Arithmetic Hardware
Can be pipelined
instruction
memory
P
C
integer
register
file
HI
LO
flt pt
register
file
data
memory
integer
multiplier
flt pt
adder
flt pt
multiplier
.
.
.
31
31
control/status register
FCR31
implementation/revision
register
FCR0
f30
f31
32 32-bit FPRs
16
Control/status register
Status
Implementation/revision register
Identifies
FP Instructions in MIPS
FP hardware is coprocessor 1
Separate FP registers
Computation
Add,
Comparison instructions
Branch instructions
MIPS has a floating point coprocessor (numbered 1) with thirtytwo 32-bit registers $f0 - $f31. Two of these are required to
hold doubles.
Floating point arithmetic: add.s (single addition), add.d
(double addition), sub.s, sub.d, mul.s, mul.d, div.s,
div.d
Floating point comparison: c.x.s (single), c.x.d (double),
where x may be eq, neq, lt, le, gt, ge
other complexities
Rounding
Rounding
Rounding
Truncate
1.00100
-> 1.01
-> 1.00
-> 1.00
Round-to-nearest-even
Rounds