Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
AnneBracy CS3410 ComputerScience CornellUniversity The slides are the product of many rounds of teaching CS 3410 by Professors Weatherspoon, Bala, Bracy, and Sirer. See:P&HChapter2.4,3.2,B.2,B.5,B.6 inst memory 32 2 32 pc register file 5 5 5 00 new pc calculation control SimplifiedSingle-cycleprocessor alu focus for today BinaryOperations • • • • • Numberrepresentations One-bitandfour-bitadders Negativenumbersandtwo’scompliment Addition(two’scompliment) Subtraction(two’scompliment) 3 Recall:Binary • Twosymbols(base2):true andfalse;1 and0 • BasisofLogicCircuitsandalldigitalcomputers So,howdowerepresentnumbersin Binary (base2)? • WeknowrepresentnumbersinDecimal (base10). – E.g.637 6·102 +3·101 +7·100 =637 102101 100 • Canjustaseasilyuseotherbases – Base2— Binary 1001111101 9 8 7 6 5 4 3 2 1 0 2 2 – Base8— Octal 2 2 2 2 0o1175 2 2 2 2 838281 80 – Base16— Hexadecimal 0x27d 162161160 1·83 +1·82 +7·81 +5·80 =637 4 Howdowecountindifferentbases? • Dec (base10) Bin(base2) Oct (base8) Hex (base16) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 . . 99 100 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 10010 . . 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 21 22 . . 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 0b11111111= 0b100000000= 0o77= 0o100= 0xff = 0x100= . . 5 Baseconversion viarepetitivedivision • Dividebybase,writeremainder,moveleftwithquotient 637 ÷ 8 =79 79÷ 8 =9 9÷ 8 =1 1÷ 8=0 remainder5 remainder7 remainder1 remainder1 lsb (least significant bit) msb (mostsignificant bit) 637 = 0o1175 msb lsb 7 Baseconversionviarepetitivedivision Dividebybase,writeremainder,moveleftwithquotient 637 ÷ 2=318 remainder 1 lsb (leastsignificantbit) 318÷ 2=159 remainder 0 159÷ 2=79 remainder 1 79÷ 2=39remainder 1 39÷ 2=19remainder 1 19÷ 2=9remainder 1 9÷ 2=4 remainder 1 4÷ 2=2 remainder 0 2÷ 2=1remainder 0 1÷ 2=0remainder 1 msb (mostsignificantbit) 637 msb = 1001111101lsb (or0b1001111101) 8 Baseconversion viarepetitivedivision Dividebybase,writeremainder,moveleftwithquotient 637 ÷ 16=39 remainder13 lsb 39÷ 16=2 remainder7 2÷ 16=0remainder 2 msb dec =hex =bin 637 =0x2713=0x27d ? Thus, 637 = 0x27d 10=0xa 11=0xb 12=0xc 13=0xd 14=0xe 15=0xf =1010 =1011 =1100 =1101 =1110 =1111 9 Binary to Octal • Convertgroupsofthreebits frombinarytooct • 3bits(000—111)havevalues0…7=1octaldigit • E.g.0b1001111101 11 75 à 0o1175 Binary to Hexadecimal • Convertnibble (groupoffourbits)frombinaryto hex • Nibble(0000—1111)hasvalues0…15=1hexdigit • E.g.0b1001111101 2 7d à 0x27d 10 Thereare10typesofpeopleintheworld: Thosewhounderstandbinary Andthosewhodonot And thosewhoknowthisjokewaswritteninbase3 11 BinaryOperations • • • • • Numberrepresentations One-bitandfour-bitadders Negativenumbersandtwo’scompliment Addition(two’scompliment) Subtraction(two’scompliment) 12 Howdowedoarithmeticinbinary? 1 183 +254 437 Carry-in Additionworksthesameway regardlessofbase • Addthedigitsineachposition • Propagatethecarry Carry-out 111 001110 Unsignedbinaryadditionisprettyeasy +011100 • Combinetwobitsatatime • Alongwithacarry 101010 13 A B Cout S A B Cout S 0 0 0 1 1 0 1 1 HalfAdder • Addstwo1-bitnumbers • Computes1-bitresultand 1-bitcarry • Nocarry-in 14 A B FullAdder • Addsthree1-bitnumbers Cin • Computes1-bitresult,1-bitcarry • Canbecascaded Cout S A B Cin 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 Cout S NowYouTry: 1. FillinTruthTable 2. CreateSum-of-ProductForm 3. Minimizetheequation • K-Maps • AlgebraicMinimization 4. DrawtheCircuits 17 A[4] B[4] 4-BitFullAdder • Addstwo4-bitnumbersandcarryin Cin • Computes4-bitresultandcarryout • Canbecascaded Cout S[4] 18 0011 0010 A3 B3 A2 B2 A1 B1 A0 B0 Cout 00100 Cin S3 S2 01 S1 S0 01 • Addstwo4-bitnumbers,alongwithcarry-in • Computes4-bitresultandcarryout • Carry-out=resultdoesnotfitin4bits 19 SubfrequencyCode1à A SubfrequencyCode2à B Easytoremember:AnneBracy Areyou: A. CSMinor B. CSMajorinArts&Sciences C. CSMajorinEngineering D. MEngstudent E. Other 20 BinaryOperations • • • • • Numberrepresentations One-bitandfour-bitadders Negativenumbersandtwo’scompliment Addition(two’scompliment) Subtraction(two’scompliment) 21 FirstAttempt:Sign/MagnitudeRepresentation • 1bitforsign(0=positive,1=negative) • N-1bitsformagnitude Problem? 0111=7 0111= 1111=-7 1111= • Twozero’s:+0differentthan-0 • Complicatedcircuits • -2+1=??? 0000=+0 1000=-0 IBM7090 22 Positivenumbersarerepresentedasusual • 0=0000,1=0001,3=0011,7=0111 Leading1’sfornegativenumbers Tonegateany number: • • • • • • • complementall thebits(i.e.flipallthebits) thenadd1 -1:1⇒ 0001⇒ 1110⇒ 1111 -3:3⇒ 0011⇒ 1100 ⇒ 1101 -7:7⇒ 0111⇒ 1000⇒ 1001 -8:8⇒ 1000⇒ 0111⇒ 1000 -0:0⇒ 0000⇒ 1111⇒ 0000(thisisgood,-0=+0)23 Non-negatives Negatives (asusual): +0=0000 +1=0001 +2=0010 +3=0011 +4=0100 +5=0101 +6=0110 +7=0111 +8=1000 (two’scomplement:flipthenadd1): 0" =1111 1" =1110 2" =1101 3" =1100 4" =1011 5" =1010 6" =1001 7" =1000 8" = 0111 -0=0000 -1=1111 -2=1110 -3=1101 -4=1100 -5=1011 -6=1010 -7=1001 -8=1000 24 4bit Two’s Complement -8…7 -1= -2= -3= -4= -5= -6= -7= -8= +7= +6= +5= +4= +3= +2= +1= 0= 1111 1110 1101 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001 0000 =15 =14 =13 =12 =11 =10 =9 =8 =7 =6 =5 =4 =3 =2 =1 =0 4bit Unsigned Binary 0 …15 26 Signedtwo’scomplement • Negativenumbershaveleading1’s • zeroisunique:+0=- 0 • wrapsfromlargestpositivetolargestnegative Nbitscanbeusedtorepresent • unsigned:range0…2N-1 – eg:8bits⇒ 0…255 • signed(two’scomplement):-(2N-1)…(2N-1 - 1) – E.g.:8bits⇒ (10000000)…(01111111) – -128…127 27 Extending tolargersize • • • • 1111=-1 11111111=-1 0111=7 00000111=7 Truncate tosmallersize • 00001111=15 • BUT, 0000 1111=1111=-1 28 =Additionasusual,ignorethesign(itjustworks) Examples 1+-1= -3+-1= -7+3= 7+(-3)= Whatiswrongwiththefollowingadditions? 7+1-7+-3-7+-1 29 Whycreateanewcircuit? Justuseadditionusingtwo’scomplementmath • How? 32 Two’sComplementSubtraction • Subtractionisaddition withanegatedoperand – Negationisdonebyinvertingallbitsandaddingone , +1) A– B=A+(-B)=A+(B B3 A3 B2 A2 B1 A1 B0 A0 Cout S3 S2 S1 S0 33 Two’sComplementSubtraction • Subtractionisadditionwithanegatedoperand – Negationisdonebyinvertingallbitsandaddingone , +1) A– B=A+(-B)=A+(B B3 A3 B2 A2 B1 A1 B0 A0 1 Cout S3 S2 S1 S0 34 BinaryOperations • • • • • • Numberrepresentations One-bitandfour-bitadders Negativenumbersandtwo’scompliment Addition(two’scompliment) Subtraction(two’scompliment) Detectingandhandlingoverflow Whencanoverflow occur? • addinganegativeandapositive? • addingtwopositives? • addingtwonegatives? 36 Whencanoverflow occur? AMSB over flow BMSB Cout_MSB SMSB Cin_MSB MSB A B Cin Cout S 0 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 1 1 1 1 Wrong Sign Wrong Sign Ruleofthumb: • Overflowhappenediff msb’s carryin!=carryout Two’sComplementAdderwithoverflowdetection B3 mux A3 B2 A2 mux B1 mux A1 B0 A0 mux over flow S3 S2 S1 S0 Note:4-bitadderforillustrative purposesandmaynotrepresenttheoptimaldesign. 0=add 1=sub Digitalcomputersareimplementedvialogiccircuitsandthus representall numbersinbinary(base2). Wewritenumbersasdecimalorhexforconvenienceandneedtobe abletoconverttobinaryandback(tounderstandwhatthecomputer isdoing!). Addingtwo1-bitnumbersgeneralizestoaddingtwonumbersofany sizesince1-bitfulladderscanbecascaded. UsingTwo’scomplementnumberrepresentationsimplifiesadder Logiccircuitdesign(0isunique,easytonegate).Subtractionisadding, whereoneoperandisnegated(two’scomplement;tonegate:flipthe bitsandadd1). OverflowifsignofoperandsAandB!=signofresultS. CandetectoverflowbytestingCin !=Cout ofthemostsignificantbit (msb),whichonlyoccurswhenpreviousstatementistrue. 40 Wecannowimplementcombinationallogiccircuits • Designeachblock – Binaryencodednumbersforcompactness • Decomposelargecircuitintomanageableblocks – 1-bitHalfAdders,1-bitFullAdders, n-bitAddersviacascaded1-bitFullAdders,... • CanimplementcircuitsusingNANDorNORgates • CanimplementgatesusingusePMOSandNMOStransistors • Andcanaddandsubtractnumbers(intwo’s compliment)! • Nexttime,stateandfinitestatemachines… 41