Presentation on theme: "B261 Systems Architecture"— Presentation transcript:
1B261 Systems Architecture Representing NumbersB261 Systems Architecture
2Previously Computer Architecture Instruction Set (MIPS) Common misconceptionsPerformanceInstruction Set (MIPS)How data is moved inside a computerHow instructions are executed
3Outline Numbers How numbers are represented internally Addition Limitations of computer arithmeticHow numbers are represented internallyRepresenting positive integersRepresenting negative integersAdditionBit-wise operations
4Binary Representation Computers internally represent numbers in binary formThe sequence of binary digitsdn-1 dn d1 d0 twoAt its simplest, this represents the decimal number which is the sum of terms 2i for each di = 1.Eg, 10111two = 16ten+4ten+2ten+1ten = 23tend0 is called the ‘least significant bit’ LSBdn-1 is called the ‘most significant bit’ MSBBut we will need to use different meanings for the bits in order to represent negative or floating-point numbers.
5Range of Positive Numbers and Word Lengths For an n-bit word length2n different numbers can be representedincluding zero.0,1,2,..., ((2n) -1) (if only want +ve numbers)A 3-bit word length would support0,1,2,3,4,5,6,7MIPS has a 32-bit word length232 different numbers can be represented0,1,2,..., ((232)-1) (= 4,294,967,295).
6Negative NumbersThe range afforded by the word length must simultaneously support both positive and negative numbers, equally.Two requirements:There must be a way, within one word, to tell if it represents a positive or negative number, and its value.A positive number x, and its complement (-x) must clearly add to zerox + (-x) = 0.
7Two’s Complement Example of n=3 bit word: 000 = 0 001 = 1 010 = 2 011 = 3100 = -4101 = -3110 = -2111 = -1For positive numbers theMSB (‘sign bit’) is always 0For negative numbers theMSB (‘sign bit’) is always 1
8Two’s complement In general for a n-bit word There will be positive numbers (and zero)0,1,2, … , ((2n-1) - 1)Negative numbers-(2n-1), … , -1The highest positive number is ((2n-1) - 1)The lowest negative numbers is -(2n-1 )Note the slight imbalance of positive and negative.
9Conversion For an n-bit word, suppose that dn-1 is the sign bit. Then the decimal value is:(-dn-1)* (2n-1) + the usual conversion of the remainder of the word.Eg, n=3, then101 = (-1 * 4) = = -3011 = (0 * 4) = = 3
10Negating a NumberThere is a simple two-step process for negation (eg, turn 3 = 011 into -3 = 101):invert every bit (e.g. replace 011 by 100)add 1 (e.g = 101).Example, 4-bit word 11011101 = = -33 = 0011Invert bits: 1100add 1: 1101 = -3 as required.
11Sign Extension Turning an n-bit representation into one with more bits Eg, in 3 bits the number -3 is 101In 4 bits the number -3 is 1101Replicate the sign bit from the smaller word into all extra slots of larger wordFor 5-bit word from 3 bits: = -3For 5-bit word from 4 bits: = -3
12Integer TypesSome languages (e.g. C++) support two types of int: signed and unsigned.Signed integers have their MSB treated as a sign bitso negative numbers can be representedUnsigned integers have their MSB treated without such an interpretation (no negative numbers).
13Signed/Unsigned Int Suppose we had a 4-bit word, then int x; x could have range 0 to 7 positiveand -8 to -1 negativeunsigned int x;x has range 0 to 15 only.
14AdditionAddition is carried out in the same way as decimal arithmetic:01010001 +0110Subtractions involve negating the relevant number:== 0010
15OverflowSince there are only a fixed set of bits available for arithmetic things can go wrong:Consider n=4, and (6+5).01100101 +1011But 1011 = = -5 !
17Overflow Situations A + B A - B where A>0, B>0, A+B too big, result negativewhere A<0, B<0, A+B too small, result positiveA - Bwhere A>0, B<0, A-B too big, result negativewhere A<0, B>0, A-B too small, result positive.
18Bit Operations Shifts shift a word x bits to the right or left: 0110 shift left by 1 is 11000110 shift right by 1 is 0011In C++ shifts are expressed using <<,>>unsigned int y = x<<5, z = x>>3;means y is the value of x shifted 5 to left, and z is x shifted 3 to the right.One application is multiplication/division by powers of 2.
19AND/ORAND is a bitwise operation on two words, where for each corresponding bit a and b:a AND b = 1 only when a=1 and b=1, otherwise 0.OR is a bitwise operation, such thata OR b = 0 only if both a=0 and b=0, otherwise 1.For example:1011 AND 0010 = 00101011 OR 0010 = 1011
21Exceptions Where the signed versions (add, addi, etc) are used if there is overflowthen an ‘exception’ is raised by the hardwarecontrol passes to a special procedure to ‘handle’ the exception, and then returns to the next instruction after the one that raised the exception.
22More MIPS Load upper immediate lui $1,100$1 = 216 * 100(100, shifted left by 16 = upper half of the word).and, or, and immediate (andi), or immediate (ori), shift left logical (sll), shift right logical (srl)all of form $1,$2,$3 or $1,$2,10 (for immediate).
23Summary Basic number representation (integers) Representation of negativesBasic arithmetic (+ and -)Logical operationsNext time - building an ALU.