binary arithmetic 2 introduction to assembly language
TRANSCRIPT
BinaryArithmetic2IntroductiontoAssemblyLanguage
CS64:ComputerOrganizationandDesignLogicLecture#3Fall2018
ZiadMatni,Ph.D.
Dept.ofComputerScience,UCSB
AdministrativeStuff
• Theclassisfull–IwillnotbeaddingmorepplL
• Didyoucheckoutthesyllabus?• Didyoucheckouttheclasswebsite?• DidyoucheckoutPiazza(andgetaccesstoit)?• Didyougotolabyesterday?
10/9/18 Matni,CS64,Fa18 2
AnyQuestionsFromLastLecture?
10/9/18 Matni,CS64,Fa18 3
5-MinutePopQuiz!!!
YOUMUSTSHOWYOURWORK!!!1. Calculateandgiveyouranswerinhexadecimal:
~(0x3E|0xFC)
2. ConvertfrombinarytodecimalANDtohexadecimal.Useanytechnique(s)youlike:
a) 1001001b) 10010010
10/9/18 Matni,CS64,Fa18 4
Answers…
1. Calculateandgiveyouranswerinhexadecimal:~(0x3E|0xFC)
2. ConvertfrombinarytodecimalANDhexadecimal.Useanytechniqueyoulike:a) 1001001
b) 10010010
10/9/18 Matni,CS64,Fa18 5
=~(0xFE)=0x01
=01001001=0x49=1+8+64=73=10010010=0x92Iseethatit’s(1001001)x2=146
LectureOutline
• Two’scomplement• Additionandsubtractioninbinary• CarryOutandOverflowbits
• IntrotoAssembly
10/9/18 Matni,CS64,Fa18 6
TwosComplementMethod
• ThisishowTwosComplementfixesthis.• Let’swriteout-6(10)in2s-Complementbinaryin4bits:
So,–6(10)=1010(2)accordingtothisrule
10/9/18 Matni,CS64,Fa18 7
011010011010
Firsttaketheunsigned(abs)value(i.e.6)andconverttobinary:
Thennegateit(i.e.doa“NOT”functiononit):Nowadd1:
Let’sdoitBackwards…BydoingitTHESAMEEXACTWAY!
• 2s-ComplementtoDecimalmethodisthesame!
• Take1010fromourpreviousexample• Negateitanditbecomes0101• Nowadd1toit&itbecomes0110,whichis6(10)
10/9/18 Matni,CS64,Fa18 8
AnotherViewof2sComplement
10/9/18 Matni,CS64,Fa18 9
NOTE:InTwo’sComplement,ifthenumber’sMSBis“1”,thenthatmeansit’sanegativenumberandifit’s“0”thenthenumberispositive.
AnotherViewof2sComplement
10/9/18 Matni,CS64,Fa18 10
NOTE:Oppositenumbersshowupassymmetricallyoppositeeachotherinthecircle.
VERYIMPORTANT:Whenwetalkof2scomplement,wemustalsomentionthenumberofbitsinvolved
Ranges
• Therangerepresentedbynumberofbitsdiffersbetweenpositiveandnegativebinarynumbers
• GivenNbits,therangerepresentedis:0to
and
10/9/18 Matni,CS64,Fa18 11
+2N–1forpositivenumbers–2N-1to+2N-1–1for2’sComplementnegativenumbers
Addition• Wehaveanelementarynotionofaddingsingledigits,along
withanideaofcarryingdigits– Example:whenadding3to9,weputforward2andcarrythe1
(i.e.tomean12)
• Wecanbuildonthisnotiontoaddnumberstogetherthataremorethanonedigitlong
• Example: 123 + 389
10/9/18 Matni,CS64,Fa18 12
11
215
carrieddigits
AdditioninBinary
• Samemathematicalprincipalapplies
0011+1101
10/9/18 Matni,CS64,Fa18 13
1
00
11
00
3+13
161
1
Q:What’sbeingassumedhere???
A:Thatthesearepurelypositivenumbers
Theoretically,Icanaddanybinaryno.withN1digitstoanyotherbinaryno.withN2digits.Practically,aCPUmusthaveadefinedno.ofdigitsthatit’sworkingwith.WHY???
Exercises
Implementingan8-bitadder:• Whatis(0x52)+(0x4B)?– Ans:0x9D,outputcarrybit=0
• Whatis(0xCA)+(0x67)?– Ans:0x31,outputcarrybit=1
10/9/18 Matni,CS64,Fa18 14
BlackBoxPerspectiveofANYN-BitBinaryAdder
10/9/18 Matni,CS64,Fa18 15
N-bitBINARYADDER
XY
OutputResultsBitsOutputCarryBitCIN
COUT
X+Y+CIN
InputBitsCarry-inbit
N
N
N
ThisisausefulperspectiveforeitherwritinganN-bitadderfunctionincode,
orfordesigningtheactualdigitalcircuitthatdoesthis!
OutputCarryBitSignificance
• Forunsigned(i.e.positive)numbers,COUT=1meansthattheresultdidnotfitintothenumberofbitsallotted
• Couldbeusedasanerrorconditionforsoftware– Forexample,you’vedesigneda16-bitadderandduringsomecalculationofpositivenumbers,yourcarrybit/flaggoesto“1”.Conclusion?
– Yourresultisoutsidethemaximumrangeallowedby16bits.
10/9/18 Matni,CS64,Fa18 16
Carryvs.Overflow
• Thecarrybit/flagworksfor–andislookedat–onlyforunsigned(positive)numbers
• Asimilarbit/flagworksislookedatforifsigned(two’scomplement)numbersareusedintheaddition: theoverflowbit
10/9/18 Matni,CS64,Fa18 17
Overflow:forNegativeNumberAddition
• WhataboutifI’maddingtwonegativenumbers?Like:1001+1011?– Then,Iget:0100withtheextrabitsetat1– SanityCheck:
That’sadding(-7)+(-5),soIexpected-12,sowhat’swronghere?– Theanswerisbeyondthecapabilityof4bitsin2’scomplement!!!
• Theextrabitinthiscaseiscalledoverflowanditindicatesthattheadditionofnegativenumbershasresultedinanumberthat’s beyondtherangeofthegivenbits.
10/9/18 Matni,CS64,Fa18 18
HowDoWeDetermineifOverflowHasOccurred?
• Whenadding2signednumbers: x+y=s
if x,y>0 AND s<0OR if x,y<0 AND s>0---------------------------------------------Then,overflowhasoccurred
10/9/18 Matni,CS64,Fa18 19
Example1Add:-39and92insigned8-bitbinary-39 92 --- 53
There’sacarry-out(wedon’tcare)Butthereisnooverflow(V)NotethatV=0,whileCout=1andCin_signed_bit=110/9/18 Matni,CS64,Fa18 20
Side-note:Whatistherangeofsignednumbersw/8bits?-27to(27–1),or-128to127
1101100101011100---------100110101
That’s53insigned8-bits!Looksok!
1Cin_signed_bit
Cout
Example2
Add:104and45insigned8-bitbinary
104 45 --- 149
There’snocarry-out(again,wedon’tcare)Butthereisoverflow!
Giventhatthisbinaryresultisnot149,butactually–107!NotethatV=1,whileCout=0andCin_signed_bit=110/9/18 Matni,CS64,Fa18 21
0110100000101101---------10010101
That’sNOT149insigned8-bits!
1Cin_signed_bit
Cout=0
V=Cout+Cin_signed_bit
Side-note(again):Whatistherangeofsignednumbersw/8bits?-27to(27–1),or-128to127
YOURTO-DOs
• Assignment#2– DueonFriday!!!
• Nextweek:AssemblyLanguage!– Doyourreadings!!
10/9/18 Matni,CS64,Fa18 22
10/9/18 Matni,CS64,Fa18 23