Binary Arithmetic 2 Introduction to Assembly Language
Transcript of Binary Arithmetic 2 Introduction to Assembly Language
![Page 1: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/1.jpg)
BinaryArithmetic2IntroductiontoAssemblyLanguage
CS64:ComputerOrganizationandDesignLogicLecture#3Fall2018
ZiadMatni,Ph.D.
Dept.ofComputerScience,UCSB
![Page 2: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/2.jpg)
AdministrativeStuff
• Theclassisfull–IwillnotbeaddingmorepplL
• Didyoucheckoutthesyllabus?• Didyoucheckouttheclasswebsite?• DidyoucheckoutPiazza(andgetaccesstoit)?• Didyougotolabyesterday?
10/9/18 Matni,CS64,Fa18 2
![Page 3: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/3.jpg)
AnyQuestionsFromLastLecture?
10/9/18 Matni,CS64,Fa18 3
![Page 4: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/4.jpg)
5-MinutePopQuiz!!!
YOUMUSTSHOWYOURWORK!!!1. Calculateandgiveyouranswerinhexadecimal:
~(0x3E|0xFC)
2. ConvertfrombinarytodecimalANDtohexadecimal.Useanytechnique(s)youlike:
a) 1001001b) 10010010
10/9/18 Matni,CS64,Fa18 4
![Page 5: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/5.jpg)
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
![Page 6: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/6.jpg)
LectureOutline
• Two’scomplement• Additionandsubtractioninbinary• CarryOutandOverflowbits
• IntrotoAssembly
10/9/18 Matni,CS64,Fa18 6
![Page 7: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/7.jpg)
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:
![Page 8: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/8.jpg)
Let’sdoitBackwards…BydoingitTHESAMEEXACTWAY!
• 2s-ComplementtoDecimalmethodisthesame!
• Take1010fromourpreviousexample• Negateitanditbecomes0101• Nowadd1toit&itbecomes0110,whichis6(10)
10/9/18 Matni,CS64,Fa18 8
![Page 9: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/9.jpg)
AnotherViewof2sComplement
10/9/18 Matni,CS64,Fa18 9
NOTE:InTwo’sComplement,ifthenumber’sMSBis“1”,thenthatmeansit’sanegativenumberandifit’s“0”thenthenumberispositive.
![Page 10: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/10.jpg)
AnotherViewof2sComplement
10/9/18 Matni,CS64,Fa18 10
NOTE:Oppositenumbersshowupassymmetricallyoppositeeachotherinthecircle.
VERYIMPORTANT:Whenwetalkof2scomplement,wemustalsomentionthenumberofbitsinvolved
![Page 11: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/11.jpg)
Ranges
• Therangerepresentedbynumberofbitsdiffersbetweenpositiveandnegativebinarynumbers
• GivenNbits,therangerepresentedis:0to
and
10/9/18 Matni,CS64,Fa18 11
+2N–1forpositivenumbers–2N-1to+2N-1–1for2’sComplementnegativenumbers
![Page 12: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/12.jpg)
Addition• Wehaveanelementarynotionofaddingsingledigits,along
withanideaofcarryingdigits– Example:whenadding3to9,weputforward2andcarrythe1
(i.e.tomean12)
• Wecanbuildonthisnotiontoaddnumberstogetherthataremorethanonedigitlong
• Example: 123 + 389
10/9/18 Matni,CS64,Fa18 12
11
215
carrieddigits
![Page 13: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/13.jpg)
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???
![Page 14: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/14.jpg)
Exercises
Implementingan8-bitadder:• Whatis(0x52)+(0x4B)?– Ans:0x9D,outputcarrybit=0
• Whatis(0xCA)+(0x67)?– Ans:0x31,outputcarrybit=1
10/9/18 Matni,CS64,Fa18 14
![Page 15: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/15.jpg)
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!
![Page 16: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/16.jpg)
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
![Page 17: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/17.jpg)
Carryvs.Overflow
• Thecarrybit/flagworksfor–andislookedat–onlyforunsigned(positive)numbers
• Asimilarbit/flagworksislookedatforifsigned(two’scomplement)numbersareusedintheaddition: theoverflowbit
10/9/18 Matni,CS64,Fa18 17
![Page 18: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/18.jpg)
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
![Page 19: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/19.jpg)
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
![Page 20: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/20.jpg)
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
![Page 21: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/21.jpg)
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
![Page 22: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/22.jpg)
YOURTO-DOs
• Assignment#2– DueonFriday!!!
• Nextweek:AssemblyLanguage!– Doyourreadings!!
10/9/18 Matni,CS64,Fa18 22
![Page 23: Binary Arithmetic 2 Introduction to Assembly Language](https://reader033.fdocuments.us/reader033/viewer/2022060317/629490563310e04d68676949/html5/thumbnails/23.jpg)
10/9/18 Matni,CS64,Fa18 23