CS/COE0447 Computer Organization & Assembly LanguageArithmeticBinary Number RepresentationCase 1: Sign MagnitudeCase 2: One’s ComplementCase 3: Two’s ComplementSign ExtensionRange of numbersSummary2’s Complement RepresentationAdditionSubtraction1CS/COE0447Computer Organization & Assembly LanguageChapter 3 Part 12Arithmetic•So far we studied–Instruction set basics–Assembly & machine language•We will review binary arithmetic algorithms and their implementations•Binary arithmetic will provide basis for CPU’s “datapath” implementation3Binary Number Representation•We looked at unsigned numbers before–B31B30…B2B1B0–B31231+B30230+…+B222+B121+B020•Now we want to deal with more complicated cases–Negative numbers–Real numbers (a.k.a. floating-point numbers)•How do we represent negative numbers?–Bit patterns and what they represent…4Case 1: Sign Magnitude•{sign bit, absolute value (magnitude)}–Sign bit•“0” – positive number•“1” – negative number–EX. (assume 4-bit representation)•0000: 0•0011: 3•1001: -1•1111: -7•1000: ?•Properties–There are two zeros–There are equal number of positive and negative numbers5Case 2: One’s Complement•1C representation of -1*number: ((2N-1) – number)–1CRepOf(A) + 1CRepOf(-A) = 2N-1–Simple bit-inversion will give the same-magnitude number with a different sign–EX. (assume 4-bit representation)•0000: 0•0011: 3•0110: 6•1001: -6•1111: -0•1000: ?•Properties–There are two zeros–There are equal number of positive and negative numbers6Case 3: Two’s Complement•(2N – number)–2CRepOf(A) + 2CRepOf(-A) = 2N –1CRepOf(A) + 1CRepOf(-A) = 2N-1 recall –simple bit-inversion and adding 1 will give the same-magnitude number with a different sign–EX. (assume 4-bit representation)•0000: 0•0011: 3•0110: 6•1001: -7•1111: -1•1000: ?•Properties–There is a single zero–There are un-equal number of positive and negative numbers7Sign Extension•#s are often cast into vars with more capacity•Sign extension (in all 3 representations): extend the sign bit to the left, and everything works out•la $t0,0x00400033•addi $t1,$t0, 7•addi $t2,$t0, -7•R[rt] = R[rs] + SignExtImm•SignExtImm = {16{immediate[15]},immediate}8Range of numbers•Range (min to max) in N bits:–SM and 1C: -2^(N-1) -1 to +2^(N-1) -1–2C: -2^(N-1) to +2^(N-1) -19Summary•Issues–# of zeros–Balance (and thus range)–Operations’ implementationCode Sign-Magnitude 1’s Complement 2’s Complement000 +0 +0 +0001 +1 +1 +1010 +2 +2 +2011 +3 +3 +3100 -0 -3 -4101 -1 -2 -3110 -2 -1 -2111 -3 -0 -1102’s Complement Representation•32-bit signed numbers–0000 0000 0000 0000 0000 0000 0000 0000 = 0–0000 0000 0000 0000 0000 0000 0000 0001 = +1–0000 0000 0000 0000 0000 0000 0000 0010 = +2–…–0111 1111 1111 1111 1111 1111 1111 1110 = +2,147,483,646–0111 1111 1111 1111 1111 1111 1111 1111 = +2,147,483,647–1000 0000 0000 0000 0000 0000 0000 0000 = - 2,147,483,648 -2^31–1000 0000 0000 0000 0000 0000 0000 0001 = - 2,147,483,647–1000 0000 0000 0000 0000 0000 0000 0010 = - 2,147,483,646–…–1111 1111 1111 1111 1111 1111 1111 1101 = -3–1111 1111 1111 1111 1111 1111 1111 1110 = -2–1111 1111 1111 1111 1111 1111 1111 1111 = -111Addition•We can do binary addition just as we do decimal arithmetic–EX. •Case for two’s complement–We don’t care about signs of input data!–EX.12Subtraction•Notice that subtraction can be done using addition–A – B = A + (-B)–We know how to negate a number–The hardware used for addition can be used for subtraction with a negating unit at one
View Full Document