Download Binary - Cornell Computer Science

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

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

Document related concepts
no text concepts found
Transcript
AnneBracy
CS3410
ComputerScience
CornellUniversity
The slides are the product of many rounds of teaching CS 3410 by
Professors Weatherspoon, Bala, Bracy, and Sirer.
inst
memory
32
pc
32
2
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
102101 100
6·102 +3·101 +7·100 =637
• Canjustaseasilyuseotherbases
– Base2— Binary
– Base8— Octal
1001111101
29 28 27 26 25 24 232221 20
0o1175
838281
– Base16— Hexadecimal
80
1·83 +1·82 +7·81 +5·80 =637
0x27d
162161160
4
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
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=255
0b100000000=256
0o77=63
0o100=64
0xff =255
0x100=256
5
Baseconversion viarepetitivedivision
• Dividebybase,writeremainder,moveleftwithquotient
637 ÷ 8 =79
79÷ 8 =9
9÷ 8 =1
1÷ 8=0
remainder5
remainder7
remainder1
remainder1
lsb (leastsignificantbit)
msb (mostsignificantbit)
637 = 0o1175
msb
lsb
6
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)
7
Convertthenumber63710tobase16
Whatistheleastsignificantdigitofthisnumber?
a)
b)
c)
d)
e)
D
E
6
7
13
8
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
9
Thereare10typesofpeopleintheworld:
Thosewhounderstandbinary
Andthosewhodonot
And thosewhoknowthisjokewaswritteninbase3
10
BinaryOperations
•
•
•
•
•
Numberrepresentations
One-bitandfour-bitadders
Negativenumbersandtwo’scompliment
Addition(two’scompliment)
Subtraction(two’scompliment)
11
Howdowedoarithmeticinbinary?
Additionworksthesameway
1
183
regardlessofbase
• Addthedigitsineachposition
+254
• Propagatethecarry
437
Carry-out
Carry-in
111
001110 Unsignedbinaryadditionisprettyeasy
•
Combinetwobitsatatime
+011100
• Alongwithacarry
1
1010 0
12
A
B
Cout
S
A
B Cout S
0
0
0
1
1
0
1
1
• Addstwo1-bitnumbers
• Computes1-bitresultand
1-bitcarry
• Nocarry-in
ClickerQuestion
WhatistheequationforCout?
a) A+B
b) AB
c) AÅ B
d) A+!B
e) !A!B
13
A
B
Cout
S
A
B Cout S
0
0
0
1
1
0
1
1
• Addstwo1-bitnumbers
• Computes1-bitresultand
1-bitcarry
• Nocarry-in
"B+AB
"
• S=A
• Cout =AB
AB
AB
Cout
S
14
A
B
• 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
15
A
B
• 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
ClickerQuestion
WhatistheequationforCout?
a) A+B+Cin
b) !A+!B+!C
c) AÅ BÅ Cin
d) AB+ACin +BCin
e) ABCin
16
A[4] B[4]
Cin
Cout
S[4]
• Addstwo4-bitnumbersandcarryin
• Computes4-bitresultandcarryout
• Canbecascaded
17
0011
0010
A 3 B3
A 2 B2
A 1 B1
A 0 B0
Cout 00100 Cin
S3
S2
01
S1
S0
01
• Addstwo4-bitnumbers,alongwithcarry-in
• Computes4-bitresultandcarryout
• Carry-out=resultdoesnotfitin4bits
18
BinaryOperations
•
•
•
•
•
Numberrepresentations
One-bitandfour-bitadders
Negativenumbersandtwo’scompliment
Addition(two’scompliment)
Subtraction(two’scompliment)
19
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
20
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
-8: 8Þ 1000Þ 0111Þ 1000
-0: 0Þ 0000Þ 1111Þ 0000(thisisgood,-0=+0)21
Non-negatives
unchanged:
+0=0000
+1=0001
+2=0010
+3=0011
+4=0100
+5=0101
+6=0110
+7=0111
+8=1000
flip
Negatives
0% =1111
1% =1110
2% =1101
3% =1100
4% =1011
5% =1010
6% =1001
7% =1000
8% = 0111
thenadd1
-0=0000
-1=1111
-2=1110
-3=1101
-4=1100
-5=1011
-6=1010
-7=1001
-8=1000
22
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
23
Whatisthevalueofthe2scomplementnumber
11010
a)
b)
c)
d)
e)
26
-26
6
-6
-10
24
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
25
Extending tolargersize(1st caseonslide23-24)
•
•
•
•
1111=-1
11111111=-1
0111=7
00000111=7
Truncate tosmallersize
• 00001111=15
• BUT, 0000 1111=1111=-1
26
Additionasusual.Ignorethesign.Itjustworks!
-1=
1111
Examples
1+-1=
-3+-1=
-7+3=
7+(-3)=
Whatiswrongwiththefollowing?
7+1
-7+-3
-7+-1
-2=
-3=
-4=
-5=
-6=
-7=
-8=
+7=
+6=
+5=
+4=
+3=
+2=
+1=
0=
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
27
Whencanoverflow occur?
• addinganegativeandapositive?
– Overflowcannot occur (Why?)
• addingtwopositives?
– Overflowcan occur (Why?)
• addingtwonegatives?
– Overflowcan occur (Why?)
-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
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
BinaryOperations
•
•
•
•
•
•
Numberrepresentations
One-bitandfour-bitadders
Negativenumbersandtwo’scompliment
Addition(two’scompliment)
Detectingandhandlingoverflow
Subtraction(two’scompliment)
– Whycreateanewcircuit?
– Justuseadditionusingtwo’scomplementmathHow?
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
31
Two’sComplementSubtraction
• Subtractionisadditionwithanegatedoperand
– Negationisdonebyinvertingallbitsandaddingone
" +1)
A– B=A+(-B)=A+(B
B3
A3
B2
A2
B1
A1
B0
A0
Cout
1
S3
S2
S1
S0
32
Two’sComplementAdderwithoverflowdetection
B3
mux
A3
B2
A2
mux
B1
mux
A1
B0
A0
mux
over
flow
S3
S2
S1
S0
Note:4-bitadderforillustrativepurposesandmaynotrepresenttheoptimaldesign.
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.
34
Wecannowimplementcombinationallogiccircuits
• Designeachblock
– Binaryencodednumbersforcompactness
• Decomposelargecircuitintomanageableblocks
– 1-bitHalfAdders,1-bitFullAdders,
n-bitAddersviacascaded1-bitFullAdders,...
• CanimplementcircuitsusingNANDorNORgates
• CanimplementgatesusingusePMOSandNMOStransistors
• Andcanaddandsubtractnumbers(intwo’s
compliment)!
• Nexttime,stateandfinitestatemachines…
35
Related documents