binary arithmetic 2 introduction to assembly language

23
Binary Arithmetic 2 Introduction to Assembly Language CS 64: Computer Organization and Design Logic Lecture #3 Fall 2018 Ziad Matni, Ph.D. Dept. of Computer Science, UCSB

Upload: others

Post on 30-May-2022

30 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Arithmetic 2 Introduction to Assembly Language

BinaryArithmetic2IntroductiontoAssemblyLanguage

CS64:ComputerOrganizationandDesignLogicLecture#3Fall2018

ZiadMatni,Ph.D.

Dept.ofComputerScience,UCSB

Page 2: Binary Arithmetic 2 Introduction to Assembly Language

AdministrativeStuff

•  Theclassisfull–IwillnotbeaddingmorepplL

•  Didyoucheckoutthesyllabus?•  Didyoucheckouttheclasswebsite?•  DidyoucheckoutPiazza(andgetaccesstoit)?•  Didyougotolabyesterday?

10/9/18 Matni,CS64,Fa18 2

Page 3: Binary Arithmetic 2 Introduction to Assembly Language

AnyQuestionsFromLastLecture?

10/9/18 Matni,CS64,Fa18 3

Page 4: Binary Arithmetic 2 Introduction to Assembly Language

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

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

LectureOutline

•  Two’scomplement•  Additionandsubtractioninbinary•  CarryOutandOverflowbits

•  IntrotoAssembly

10/9/18 Matni,CS64,Fa18 6

Page 7: Binary Arithmetic 2 Introduction to Assembly Language

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

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

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

AnotherViewof2sComplement

10/9/18 Matni,CS64,Fa18 10

NOTE:Oppositenumbersshowupassymmetricallyoppositeeachotherinthecircle.

VERYIMPORTANT:Whenwetalkof2scomplement,wemustalsomentionthenumberofbitsinvolved

Page 11: Binary Arithmetic 2 Introduction to Assembly Language

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

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

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

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

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

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

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

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

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

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

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

YOURTO-DOs

•  Assignment#2– DueonFriday!!!

•  Nextweek:AssemblyLanguage!– Doyourreadings!!

10/9/18 Matni,CS64,Fa18 22

Page 23: Binary Arithmetic 2 Introduction to Assembly Language

10/9/18 Matni,CS64,Fa18 23