Download presentation
Presentation is loading. Please wait.
Published byBeatrix Richard Modified over 8 years ago
1
Lec 11Systems Architecture1 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).
2
Lec 11Systems Architecture2 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 –Representation: a number A = –Example for base 2 (binary): 1011 = 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0
3
Lec 11Systems Architecture3 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...2 n -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? Numbers
4
Lec 11Systems Architecture4 Sign Magnitude: One's Complement: Two's Complement : 000 = +0000 = +0000 = +0 001 = +1001 = +1001 = +1 010 = +2010 = +2010 = +2 011 = +3011 = +3011 = +3 100 = -0100 = -3100 = -4 101 = -1101 = -2101 = -3 110 = -2110 = -1110 = -2 111 = -3111 = -0111 = -1 Issues: balance, number of zeros, ease of operations Which one is best? Why? Possible Representations
5
Lec 11Systems Architecture5 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) The Complement – Theory
6
Lec 11Systems Architecture6 No borrowing is necessary when subtracting 6142 6142 - 4816+ 5183 1326 11325 - 10000 1325 + 1 1326 Due to fixed width of the registers, the leading 1 is lost automatically due to carry overflow. Why the Complement? – Example
7
Lec 11Systems Architecture7 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 Z n. 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] Two's Complement – Why does it work?
8
Lec 11Systems Architecture8 Example: Z 8 [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 Z 2 k ring. In k-bits, the largest number will be = 011…11 = 2 k-1 -1 In k-bits, the smallest number will be = 100…00 = -2 k-1 Representation: b k-1 …b 0 = -b k-1 2 k-1 +b k-2 2 k-2 +…b 1 2 1 +b 0 2 0 Two's Complement – Why it works?
9
Lec 11Systems Architecture9 Let a k-bit number X have a two’s complement inverse 2 X with respect to Z 2 k ring, i.e., X + 2 X ≡ 0 (mod 2 k ). For instance, 3 + 5 mod 8 ≡ 0! Let 1 X 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, 2 X = 1 X + 1 and X + 1 X = 2 k – 1 Therefore, X + 2 X = 2 k ≡ 0 mod 2 k Two's Complement – Inversion
10
Lec 11Systems Architecture10 32 bit signed numbers: 0000 0000 0000 0000 0000 0000 0000 0000 two = 0 ten 0000 0000 0000 0000 0000 0000 0000 0001 two = + 1 ten 0000 0000 0000 0000 0000 0000 0000 0010 two = + 2 ten... 0111 1111 1111 1111 1111 1111 1111 1110 two = + 2,147,483,646 ten 0111 1111 1111 1111 1111 1111 1111 1111 two = + 2,147,483,647 ten 1000 0000 0000 0000 0000 0000 0000 0000 two = – 2,147,483,648 ten 1000 0000 0000 0000 0000 0000 0000 0001 two = – 2,147,483,647 ten 1000 0000 0000 0000 0000 0000 0000 0010 two = – 2,147,483,646 ten... 1111 1111 1111 1111 1111 1111 1111 1101 two = – 3 ten 1111 1111 1111 1111 1111 1111 1111 1110 two = – 2 ten 1111 1111 1111 1111 1111 1111 1111 1111 two = – 1 ten maxint minint MIPS signed numbers
11
Lec 11Systems Architecture11 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) Two's Complement Operations
12
Lec 11Systems Architecture12 Sign extension involves propagating the sign bit to increase the bit-size of the number. Why this works? Number before sign extension: b k-1 …b 0 = -b k-1 2 k-1 +b k-2 2 k-2 +…b 1 2 1 +b 0 2 0 Number after sign extension by one bit: b k …b 0 = -b k 2 k +b k-1 2 k-1 +b k-2 2 k-2 +…b 1 2 1 +b 0 2 0 This is equivalent to adding 2 k-1 and subtracting 2 k, i.e., subtracting 2 k - 2 k-1 = 2 k-1 *(2-1) It is obvious why this works for positive numbers… Two's Complement – Sign Extension
13
Lec 11Systems Architecture13 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): –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” Addition & Subtraction
14
Lec 11Systems Architecture14 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 ? Detecting Overflow
15
Lec 11Systems Architecture15 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 Effects of Overflow
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.