Download cs281_lec11

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Large numbers wikipedia , lookup

Location arithmetic wikipedia , lookup

Mathematics and architecture wikipedia , lookup

Positional notation wikipedia , lookup

Elementary mathematics wikipedia , lookup

Arithmetic wikipedia , lookup

Addition wikipedia , lookup

Transcript
Systems Architecture
Lecture 11: Arithmetic for Computers
Jeremy R. Johnson
Anatole D. Ruslanov
William M. Mongan
Some or all figures from Computer Organization and Design: The
Hardware/Software Approach, Third Edition, by David Patterson and
John Hennessy, are copyrighted material (COPYRIGHT 2004 MORGAN
KAUFMANN PUBLISHERS, INC. ALL RIGHTS RESERVED).
Lec 11
Systems Architecture
1
Introduction
•
Objective: To learn about computer arithmetic and how
number are represented in computers
– Review binary and hexadecimal numbers
– Negative numbers
•
•
signed-magnitude
two's complement
– Two's complement numbers
– Addition and subtraction
n 1
– Representation: a number A =
di  i

i
0
– Example for base 2 (binary): 1011 = 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20
Lec 11
Systems Architecture
2
Numbers
• Bits have no inherent meaning
— conventions define relationship between bits and numbers
• Binary numbers (base 2)
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001...
decimal: 0...2n-1
• Complications:
numbers are finite (overflow)
fractions and real numbers
negative numbers
–
e.g., no MIPS subi instruction – addi can add a negative number
• How do we represent negative numbers?
– Which bit patterns will represent which numbers?
Lec 11
Systems Architecture
3
Possible Representations
•
Sign Magnitude:
000 = +0
001 = +1
010 = +2
011 = +3
100 = -0
101 = -1
110 = -2
111 = -3
One's Complement:
000 = +0
001 = +1
010 = +2
011 = +3
100 = -3
101 = -2
110 = -1
111 = -0
Two's Complement:
000 = +0
001 = +1
010 = +2
011 = +3
100 = -4
101 = -3
110 = -2
111 = -1
• Issues: balance, number of zeros, ease of operations
• Which one is best? Why?
Lec 11
Systems Architecture
4
The Complement – Theory
•
Radix-minus-one complement
•
Radix-complement (the complement)
•
Radix-complement is the inverse with respect to addition
•
A 4-digit decimal example:
•
The following equation holds when subtracting
one number from another in FIXED decimal width:
Y = B – A + (9999 + 1 - 10000)
= B + (9999 - A + 1) – 10000
= B + ([9999 – A] + 1) – 10000
•
Observe: 9999 – A is 9-complement (radix-minus-one complement)
•
Observe: [9999 – A] + 1 is 10-complement (THE radix complement)
Lec 11
Systems Architecture
5
Why the Complement? – Example
• No borrowing is necessary when subtracting
6142
- 4816
1326
6142
+ 5183
11325
- 10000
1325
+
1
1326
• Due to fixed width of the registers, the leading 1 is lost
automatically due to carry overflow.
Lec 11
Systems Architecture
6
Two's Complement – Why does it work?
• Modular arithmetic (mod n): a ≡ b (mod n), i.e., a = b + qn
• [a] = { a + qn | for all q in Z },
i.e., all number equivalent to a mod n.
• This forms a residue class ring Zn.
• What is a ring? It is a group where addition is as expected
and addition has well-defined inverse. (This is not precise.)
• That is [a] + [b] = [a + b] and [a] x [b] = [a x b]
Lec 11
Systems Architecture
7
Two's Complement – Why it works?
• Example: Z8  [0], [1], [2], …, [7] is a residue class ring of
8 elements.
• For this example, we can use other representations:
0, 1, 2, 3, -4, -3, -2, -1 because these numbers are
equivalent, e.g. 4 ≡ -4 (mod 8), etc…
• Binary addition in k bits is equivalent to addition in Z2k ring.
• In k-bits, the largest number will be = 011…11 = 2k-1-1
• In k-bits, the smallest number will be = 100…00 = -2k-1
• Representation: bk-1…b0 = -bk-12k-1+bk-22k-2+…b121+b020
Lec 11
Systems Architecture
8
Two's Complement – Inversion
• Let a k-bit number X have a two’s complement inverse 2X
with respect to Z2k ring, i.e., X + 2X ≡ 0 (mod 2k).
• For instance, 3 + 5 mod 8 ≡ 0!
• Let 1X be a one’s complement of X (i.e., radix-minus-one
complement, which is equivalent to flipping all the bits in
binary).
• By definition, 2X = 1X + 1 and X + 1X = 2k – 1
• Therefore, X + 2X = 2k ≡ 0 mod 2k
Lec 11
Systems Architecture
9
MIPS signed numbers
•
32 bit signed numbers:
0000
0000
0000
...
0111
0111
1000
1000
1000
...
1111
1111
1111
Lec 11
0000 0000 0000 0000 0000 0000 0000two = 0ten
0000 0000 0000 0000 0000 0000 0001two = + 1ten
0000 0000 0000 0000 0000 0000 0010two = + 2ten
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
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
1111 1111 1111 1111 1111 1111 1101two = – 3ten
1111 1111 1111 1111 1111 1111 1110two = – 2ten
1111 1111 1111 1111 1111 1111 1111two = – 1ten
Systems Architecture
10
Two's Complement Operations
• Negating a two's complement number: invert all bits and
add 1
– remember: “negate” and “invert” are quite different!
• Converting n bit numbers into numbers with more than n
bits:
– MIPS 16 bit immediate gets converted to 32 bits for arithmetic
– Copy the most significant bit (the sign bit) into the other bits
0010
-> 0000 0010
1010
-> 1111 1010
– "sign extension" (lbu vs. lb)
Lec 11
Systems Architecture
11
Two's Complement – Sign Extension
• Sign extension involves propagating the sign bit to
increase the bit-size of the number.
• Why this works?
• Number before sign extension:
bk-1…b0 = -bk-12k-1+bk-22k-2+…b121+b020
• Number after sign extension by one bit:
bk…b0 = -bk2k+bk-12k-1+bk-22k-2+…b121+b020
• This is equivalent to adding 2k-1 and subtracting 2k, i.e.,
subtracting 2k - 2k-1 = 2k-1*(2-1)
• It is obvious why this works for positive numbers…
Lec 11
Systems Architecture
12
Addition & Subtraction
•
Just like in grade school (carry/borrow 1s)
0111
0111
0110
+ 0110
- 0110
- 0101
•
Two's complement operations easy
–
•
subtraction using addition of negative numbers
0111
+ 1010
Overflow (result too large for finite computer word):
–
Lec 11
e.g., adding two n-bit numbers does not yield an n-bit number
0111
+ 0001
note that overflow term is somewhat misleading,
1000
it does not mean a carry “overflowed”
Systems Architecture
13
Detecting Overflow
• No overflow when adding a positive and a negative number
• No overflow when signs are the same for subtraction
• Overflow occurs when the value affects the sign:
–
–
–
–
overflow when adding two positives yields a negative
or, adding two negatives gives a positive
or, subtract a negative from a positive and get a negative
or, subtract a positive from a negative and get a positive
• Consider the operations A + B, and A – B
– Can overflow occur if B is 0 ?
– Can overflow occur if A is 0 ?
Lec 11
Systems Architecture
14
Effects of Overflow
• An exception (interrupt) occurs
– Control jumps to predefined address for exception
– Interrupted address is saved for possible resumption
• Details based on software system / language
– example: flight control vs. homework assignment
• Don't always want to detect overflow
— new MIPS instructions: addu, addiu, subu
note: addiu still sign-extends!
note: sltu, sltiu for unsigned comparisons
Lec 11
Systems Architecture
15