University*of*Washington*
|
|
- Nathan Robinson
- 5 years ago
- Views:
Transcription
1 Roadmap C: car c = malloc(sizeof(car)); c->miles = 1; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp %rbp Java: Car c = new Car(); c.setmiles(1); c.setgals(17); float mpg = c.getmpg(); OS: UniversityofWashington Memory&data Integers&floats Machinecode&C x86assembly Procedures&stacks Arrays&structs Memory&caches Processes Virtualmemory MemoryallocaIon Javavs.C Integers! RepresentaIonofintegers:unsignedandsigned! CasIng! ArithmeIcandshiPing! Signextension Autumn213 Integers&Floats 1 Autumn213 Integers&Floats 2 Butbeforewegettointegers.! Encodeastandarddeckofplayingcards.! 52cardsin4suits! Howdoweencodesuits,facecards?! WhatoperaIonsdowewanttomakeeasytoimplement?! Whichisthehighervaluecard?! Aretheythesamesuit? TwopossiblerepresentaIons! 52cards 52bitswithbitcorrespondingtocardsetto1! One>hot encoding! Drawbacks:! Hardtocomparevaluesandsuits! Largenumberofbitsrequired low-order 52 bits of 64-bit word Autumn213 Integers&Floats 3 Autumn213 Integers&Floats 4
2 TwopossiblerepresentaIons! 52cards 52bitswithbitcorrespondingtocardsetto1! One>hot encoding! Drawbacks:! Hardtocomparevaluesandsuits! Largenumberofbitsrequired low-order 52 bits of 64-bit word Twobe\errepresentaIons! Binaryencodingofall52cards only6bitsneeded low-order 6 bits of a byte! Fitsinonebyte! Smallerthanone>hotencodings.! Howcanwemakevalueandsuitcomparisonseasier?! 4bitsforsuit,13bitsforcardvalue 17bitswithtwosetto1! Pairofone>hotencodedvalues! Easiertocomparesuitsandvalues! SJllanexcessivenumberofbits! Canwedobe\er? Autumn213 Integers&Floats 5 Autumn213 Integers&Floats 6 Twobe\errepresentaIons CompareCardSuits mask:abitvectorthat,whenbitwise ANDedwithanotherbitvectorv,turns allbutthebitsofinterestinvto! Binaryencodingofall52cards only6bitsneeded! Fitsinonebyte! Smallerthanone>hotencodings.! Howcanwemakevalueandsuitcomparisonseasier? low-order 6 bits of a byte! Binaryencodingofsuit(2bits)andvalue(4bits)separately suit value! Alsofitsinonebyte,andeasytodocomparisons Autumn213 Integers&Floats 7 #define SUIT_MASK x3 int samesuitp(char card1, char card2) { return (! (card1 & SUIT_MASK) ^ (card2 & SUIT_MASK)); //return (card1 & SUIT_MASK) == (card2 & SUIT_MASK); } returnsint SUIT_MASK = x3 = 1 1 suit value char hand[5]; // represents a 5-card hand char card1, card2; // two cards to compare card1 = hand[]; card2 = hand[1];... if ( samesuitp(card1, card2) ) {... } equivalent Autumn213 Integers&Floats 8
3 CompareCardValues VALUE_MASK = xf = suit value char hand[5]; // represents a 5-card hand char card1, card2; // two cards to compare card1 = hand[]; card2 = hand[1];... if ( greatervalue(card1, card2) ) {... } mask:abitvectorthat,whenbitwise ANDedwithanotherbitvectorv,turns allbutthebitsofinterestinvto worksevenifvalue #define VALUE_MASK xf isstoredinhighbits int greatervalue(char card1, char card2) { return ((unsigned int)(card1 & VALUE_MASK) > (unsigned int)(card2 & VALUE_MASK)); } Autumn213 Integers&Floats 9 EncodingIntegers! Thehardware(andC)supportstwoflavorsofintegers:! unsigned onlythenon>negajves! signed bothnegajvesandnon>negajves! Thereareonly2 W disinctbitpa\ernsofwbits,so...! Cannotrepresentalltheintegers! Unsignedvalues:...2 W a1! Signedvalues:a2 Wa1...2 Wa1 a1! Reminder:terminologyforbinaryrepresentaIons Most>significant or high>order bit(s) Least>significant or low>order bit(s) Autumn213 Integers&Floats 1 UnsignedIntegers SignedIntegers:SignaandaMagnitude! Unsignedvaluesarejustwhatyouexpect! b 7 b 6 b 5 b 4 b 3 b 2 b 1 b =b b b b b 2! Usefulformula: N>1 =2 N >1! Addandsubtractusingthenormal carry and borrow rules,justinbinary.! Howwouldyoumakesigned)integers? ! Let'sdothenaturalthingfortheposiIves! Theycorrespondtotheunsignedintegersofthesamevalue! Example(8bits):x=,x1=1,,x7F=127! But,weneedtoletabouthalfofthembenegaIve! Usethehighaorderbittoindicatenega,ve:callitthe signbit! Callthisa sign>and>magnitude representajon! Examples(8bits):! x= 2 isnon>negajve,becausethesignbitis! x7f= isnon>negajve! x85=1 2 isnegajve! x8=1 2 isnegajve... Autumn213 Integers&Floats 11 Autumn213 Integers&Floats 12
4 SignedIntegers:SignaandaMagnitude! Howshouldwerepresenta1inbinary?! 11 2 UsetheMSBfor+or>,andtheotherbitstogivemagnitude. MostSignificantBit SignaandaMagnitudeNegaIves! Howshouldwerepresenta1inbinary?! 11 2 UsetheMSBfor+or>,andtheotherbitstogivemagnitude. (Unfortunatesideeffect:therearetworepresentaIonsof!) Autumn213 Integers&Floats Autumn213 Integers&Floats 14 SignaandaMagnitudeNegaIves! Howshouldwerepresenta1inbinary?! 11 2 UsetheMSBfor+or>,andtheotherbitstogivemagnitude. (Unfortunatesideeffect:therearetworepresentaJonsof!)! Anotherproblem:arithmeIciscumbersome.! Example: 4>3!=4+(>3) How do we solve these problems? Autumn213 Integers&Floats Two scomplementnegaives! Howshouldwerepresenta1inbinary? Autumn213 Integers&Floats 16
5 Two scomplementnegaives! Howshouldwerepresenta1inbinary? Ratherthanasignbit,letMSBhavesamevalue,butnega+veweight.! b fori)<)w31:b i =1adds+2 i w31 =1addsa2 w31 tothevalue. tothevalue. b w31 ) b w32 ).).).) b ) Autumn213 Integers&Floats Two scomplementnegaives! Howshouldwerepresenta1inbinary? Ratherthanasignbit,letMSBhavesamevalue,butnega+veweight.! b w31 =1addsa2 w31 tothevalue. fori)<)w31:b i =1adds+2 i tothevalue. e.g.unsigned 2 : b w31 ) b w32 ).).).) b ) =1 1 2 scompl. 2 : > => Autumn213 Integers&Floats Two scomplementnegaives! Howshouldwerepresenta1inbinary? Ratherthanasignbit,letMSBhavesamevalue,butnega+veweight.! b w31 =1addsa2 w31 tothevalue. fori)<)w31:b i =1adds+2 i tothevalue. e.g.unsigned 2 : b w31 ) b w32 ).).).) b ) =1 1 2 scompl. 2 : > => ! >1isrepresentedas =>2 3 +(2 3 1) AllnegaJveintegerssJllhaveMSB=1.! Advantages:singlezero,simplearithmeJc! TogetnegaIverepresentaIonof anyinteger,takebitwisecomplement andthenaddone! ~x + 1 == -x Autumn213 Integers&Floats Autumn213 4abitUnsignedvs.Two scomplement x1+2 2 x+2 1 x1+2 x1 >2 3 x1+2 2 x+2 1 x1+2 x1 Integers&Floats
6 4abitUnsignedvs.Two scomplement 2 3 x1+2 2 x+2 1 x1+2 x1 >2 3 x1+2 2 x+2 1 x1+2 x1 11 >5 (math)difference=16=2 4 4abitUnsignedvs.Two scomplement 2 3 x1+2 2 x+2 1 x1+2 x1 >2 3 x1+2 2 x+2 1 x1+2 x1 11 >5 (math)difference=16= Autumn Integers&Floats Autumn Integers&Floats Two scomplementarithmeic! ThesameaddiIonprocedureworksforbothunsignedand two scomplementintegers! Simplifieshardware:onlyonealgorithmforaddiJon! Algorithm:simpleaddiJon,discardthehighestcarrybit! Examples:! Called modular addijon:resultissummodulo2 W1 Two scomplement! Whydoesitwork?! Putanotherway,forallposiJveintegersx,wewant:! bits(1x1)1+1bits(1 x1)1=(ignoringthecarry>outbit)! Thisturnsouttobethebitwise1complement1plus1one! Whatshouldthe8>bitrepresentaJonof>1be? 1 +???????? (wewantwhicheverbitstringgivestherightresult) +???????? +???????? Autumn213 Integers&Floats 23 Autumn213 Integers&Floats 24
7 Two scomplement! Whydoesitwork?! Putanotherway,forallposiJveintegersx,wewant:! bits(1x1)1+1bits(1 x1)1=(ignoringthecarry>outbit)! Thisturnsouttobethebitwise1complement1plus1one! Whatshouldthe8>bitrepresentaJonof>1be? (wewantwhicheverbitstringgivestherightresult) 1 +???????? +???????? Two scomplement! Whydoesitwork?! Putanotherway,forallposiJveintegersx,wewant:! bits(1x1)1+1bits(1 x1)1=(ignoringthecarry>outbit)! Thisturnsouttobethebitwise1complement1plus1one! Whatshouldthe8>bitrepresentaJonof>1be? (wewantwhicheverbitstringgivestherightresult) Autumn213 Integers&Floats 25 Autumn213 Integers&Floats 26 Unsigned&SignedNumericValues ConversionVisualized bits1 Unsigned Signed ! Signedandunsignedintegershavelimits.! Ifyoucomputeanumberthatistoobig (posijve),itwraps: 6+4=?15U+2U=?! Ifyoucomputeanumberthatistoo small(negajve),itwraps: >7>3=?U>2U=?! Answersareonlycorrectmod2 b! TheCPUmaybecapableof throwingan excepion foroverflowonsignedvalues.! Itwon'tforunsigned.! ButCandJavajustcruisealongsilently whenoverflowoccurs...oops.! Two scomplement Unsigned! OrderingInversion! NegaJve BigPosiJve 2 scomplement Range TMax1 1 2 TMin1 UMax1 UMax 11 TMax11+11 TMax1 Unsigned Range Autumn213 Integers&Floats 27 Autumn213 Integers&Floats 28
8 Overflow/Wrapping:Unsigned addijon:dropthecarrybit Overflow/Wrapping:Two scomplement addijon:dropthecarrybit Autumn Integers&Floats ModularArithmeJc Autumn213 > > Integers&Floats ModularArithmeJc 3 ValuesToRemember! UnsignedValues! UMin =!! UMax = 2 w 1! ValuesforW=32! Two scomplementvalues! TMin = 2 w 1! 1! TMax = 2 w 1 1! 11 1! NegaIveone! 111 1xF...F Decimal Hex Binary UMax 4,294,967,296 FF FF FF FF TMax 2,147,483,647 7F FF FF FF TMin a2,147,483, a1 a1 FF FF FF FF Signedvs.UnsignedinC! Constants! Bydefaultareconsideredtobesignedintegers! Use U suffixtoforceunsigned:! U, U Autumn213 Integers&Floats 31 Autumn213 Integers&Floats 32
9 Signedvs.UnsignedinC! CasIng! int tx, ty;! unsigned ux, uy;! ExplicitcasIngbetweensigned&unsigned:! tx = (int) ux;! uy = (unsigned) ty;! ImplicitcasIngalsooccursviaassignmentsandfuncIoncalls:! tx = ux;! uy = ty;! Thegccflag?Wsign?conversionproduceswarningsforimplicitcasts, but?walldoesnot!! HowdoescasIngbetweensignedandunsignedwork?! Whatvaluesaregoingtobeproduced?!!! Autumn213 Integers&Floats 33 Signedvs.UnsignedinC! CasIng! int tx, ty;! unsigned ux, uy;! ExplicitcasIngbetweensigned&unsigned:! tx = (int) ux;! uy = (unsigned) ty;! ImplicitcasIngalsooccursviaassignmentsandfuncIoncalls:! tx = ux;! uy = ty;! Thegccflag?Wsign?conversionproduceswarningsforimplicitcasts, but?walldoesnot!! HowdoescasIngbetweensignedandunsignedwork?! Whatvaluesaregoingtobeproduced?!!!! Bits)are)unchanged,justinterpreteddifferently! Autumn213 Integers&Floats 34 CasIngSurprises! ExpressionEvaluaIon! Ifyoumixunsignedandsignedinasingleexpression,then signed)values)are)implicitly)cast)to)unsigned.)! IncludingcomparisonoperaJons<,>,==,<=,>=! ExamplesforW=32:TMIN=a2,147,483,648TMAX=2,147,483,647! Constant 1 Constant 2 RelaIon EvaluaIon U U == unsigned >1-1 < signed >1-1 U U > unsigned > > signed U > < unsigned >1-1 >2-2 > signed (unsigned)>1-1 >2-2 > unsigned U < unsigned (int) u > signed!!! Autumn213 Integers&Floats 35 SignExtension! Whathappensifyouconverta32abitsignedintegertoa64a bitsignedinteger? Autumn213 Integers&Floats 36
10 SignExtension! Task:! Givenw>bitsignedintegerx! Convertittow+k>bitintegerwith1same1value1! Rule:! Makekcopiesofsignbit:! X=x w 1,,x w 1,x w 1,x w 2,,x 8abitrepresentaIons 1 11 kcopiesofmsb X # w X # k1 Autumn213 Integers&Floats 37 w1 C:casJngbetweenunsignedandsignedjustreinterpretsthesamebits. Autumn213 Integers&Floats 38 SignExtension SignExtension 4>bit2 4>bit2 8>bit2 8>bit2 4>bit>4 4>bit>4???? 8>bit>4 8>bit12 Autumn213 Integers&Floats 39 Autumn213 Integers&Floats 4
11 SignExtension SignExtension 4>bit2 4>bit2 8>bit2 8>bit2 4>bit>4 4>bit>4 1 8>bit> >bit>4 Autumn213 Integers&Floats 41 Autumn213 Integers&Floats 42 SignExtensionExample ShiPOperaIons! ConverIngfromsmallertolargerintegerdatatype! CautomaIcallyperformssignextension(Javatoo)! LePshiP: x<<y! ShipbitvectorxlepbyyposiJons! Throwawayextrabitsonlep! Fillwithsonright Argumentx <<3 Logical>>2 short int x = 12345; int ix = (int) x; short int y = ; int iy = (int) y; Decimal Hex Binary x ix y CF C iy FF FF CF C ! RightshiP: x>>y! Shipbit>vectorxrightbyyposiJons! Throwawayextrabitsonright! Logicalship(forunsignedvalues)! Fillwithsonlep! ArithmeJcship(forsignedvalues)! Replicatemostsignificantbitonlep! Maintainssignofx ArithmeJc>>2 Argumentx <<3 Logical>>2 ArithmeJc>>2 111 Autumn213 Integers&Floats 43 Thebehaviorof>>inCdependsonthecompiler!Itisarithme,cshiprightinGCC. Java:>>>islogicalshipright;>>isarithmeJcshipright. Autumn213 Integers&Floats 44
12 ShiPOperaIons! LePshiP: x<<y! ShipbitvectorxlepbyyposiJons! Throwawayextrabitsonlep! Fillwithsonright! RightshiP: x>>y! Shipbit>vectorxrightbyyposiJons! Throwawayextrabitsonright! Logicalship(forunsignedvalues)! Fillwithsonlep! ArithmeIcshiP(forsignedvalues)! Replicatemostsignificantbitonlep! Maintainssignofx! Why)is)this)useful?) Argumentx <<3 Logical>>2 ArithmeJc>>2 Argumentx <<3 Logical>>2 ArithmeJc>>2 x >> 9? 111 Thebehaviorof>>inCdependsonthecompiler!Itisarithme,cshiprightinGCC. Java:>>>islogicalshipright;>>isarithmeJcshipright. Autumn213 Integers&Floats 45 Whathappenswhen! x>>n?! x<<m? Autumn213 Integers&Floats 46 Whathappenswhen ShiPingandArithmeIc! x>>n:divideby2 n x=27; 1 x2 n y=x<<2; logicalshiplep: y==18 shipinzerosfromtheright! x<<m:muliplyby2 m fasterthangeneralmuljpleordivideoperajons overflow x/2 n logicalshipright: shipinzerosfromthelep rounding(down) 1 unsigned x=237; y=x>>2; y==59 Autumn213 Integers&Floats 47 Autumn213 Integers&Floats 48
13 ShiPingandArithmeIc UsingShiPsandMasks signed x=>11; y=x<<2; y==18 11 x2 n logicalshiplep: shipinzerosfromtheright 1! Extractthe2ndmostsignificantbyteofaninteger? x 1 overflow x/2 n arithmejcshipright: shipincopiesofmostsignificantbit fromthelep rounding(down) 11 signed x=>19; y=x>>2; y==>5 clarificajonfrommon.:shipsbyn<orn>=wordsizeareundefined Autumn213 Integers&Floats 49 Autumn213 Integers&Floats 5 UsingShiPsandMasks! Extractthe2ndmostsignificantbyteofaninteger:! Firstship,thenmask:(x>>16)&xFF UsingShiPsandMasks! Extractthe2ndmostsignificantbyteofaninteger:! Firstship,thenmask:(x>>16)&xFF x 1 x 1 x>>16 1 x>>16 1 (x>>16)&xff (x>>16)&xff ! Extractthesignbitofasignedinteger?! Extractthesignbitofasignedinteger:! (x>>31)&1>needthe &1 toclearoutallotherbitsexceptlsb! CondiIonalsasBooleanexpressions(assumingxisor1)! if(x)a=yelsea=z;whichisthesameasa=x?y:z;! Canbere>wriwen(assumingarithmeJcrightship)as: a=(((x<<31)>>31)&y) (((!x)<<31)>>31)&z); Autumn213 Integers&Floats 51 Autumn213 Integers&Floats 52
14 MulIplicaIon! WhatdoyougetwhenyoumulIply9x9?! Whatabout2 3 x3?! 2 3 x5?! a2 31 xa2 31? UnsignedMulIplicaIoninC Operands:wbits TrueProduct:2wbits Discardwbits:wbits u v!! StandardMulIplicaIonFuncIon! Ignoreshighorderwbits! ImplementsModularArithmeIc UMult w (u,v)=uyvmod2 w1 u! v! UMult w (u, v)! Autumn213 Integers&Floats 53 Autumn213 Integers&Floats 54 Poweraofa2MulIplywithShiP! OperaIon! u << kgivesu 2 k)! Bothsignedandunsigned Operands:wbits TrueProduct:w+kbits u 2 k! Discardk1bits:wbits UMult w (u, 2 k )! TMult w (u, 2 k )!! Examples! u << 3 == u 8! u << 5 - u << 3 == u 24! MostmachinesshipandaddfasterthanmulJply! CompilergeneratesthiscodeautomaJcally u! 2 k! k! 1 CodeSecurityExample / Kernel memory region holding user-accessible data / #define KSIZE 124 char kbuf[ksize]; / Copy at most maxlen bytes from kernel region to user buffer / int copy_from_kernel(void user_dest, int maxlen) { / Byte count len is minimum of buffer size and maxlen / int len = KSIZE < maxlen? KSIZE : maxlen; memcpy(user_dest, kbuf, len); return len; } #define MSIZE 528 void getstuff() { char mybuf[msize]; copy_from_kernel(mybuf, MSIZE); printf( %s\n, mybuf); } Autumn213 Integers&Floats 55 Autumn213 Integers&Floats 56
15 MaliciousUsage / Declaration of library function memcpy / void memcpy(void dest, void src, size_t n); / Kernel memory region holding user-accessible data / #define KSIZE 124 char kbuf[ksize]; / Copy at most maxlen bytes from kernel region to user buffer / int copy_from_kernel(void user_dest, int maxlen) { / Byte count len is minimum of buffer size and maxlen / int len = KSIZE < maxlen? KSIZE : maxlen; memcpy(user_dest, kbuf, len); return len; } FloaIngpointtopics! Background:fracIonalbinarynumbers! IEEEfloaIngapointstandard! FloaIngapointoperaIonsandrounding! FloaIngapointinC! Therearemanymoredetailsthatwewon tcover! It sa58>pagestandard #define MSIZE 528 void getstuff() { char mybuf[msize]; copy_from_kernel(mybuf, -MSIZE);... } Autumn213 Integers&Floats 57 Autumn213 Integers&Floats 58 FracIonalBinaryNumbers FracIonalBinaryNumbers! 2 i! 2 i 1! 4! 2! 1!.! b i! b i 1!! b 2! b 1! b! b 1! b 2! b 3!! b j! 1/2! 1/4! 1/8! 2 j!!! RepresentaIon! Bitstorightof binarypoint representfracjonalpowersof2! RepresentsraJonalnumber: i b k 2 k k = j! Value RepresentaIon! 5and3/4! 2and7/ ! 47/ ! ObservaIons! Shiplep=mulJplybypowerof2! Shipright=dividebypowerof2! Numbersoftheform arejustbelow1.! LimitaIons:! ExactrepresentaJonpossibleonlyfornumbersoftheformx2 y! OtherraJonalnumbershaverepeaJngbitrepresentaJons! 1/3= =.[1] 2 Autumn213 Integers&Floats 59 Autumn213 Integers&Floats 6
16 FixedPointRepresentaIon! Impliedbinarypoint.Examples: #1:thebinarypointisbetweenbits2and3 b 7 b 6 b 5 b 4 b 3 [.]b 2 b 1 b #2:thebinarypointisbetweenbits4and5 b 7 b 6 b 5 [.]b 4 b 3 b 2 b 1 b! SamehardwareasforintegerarithmeIc. #3:integers!thebinarypointisaperbit b 7 b 6 b 5 b 4 b 3 b 2 b 1 b [.]! Fixedpoint=fixedrangeandfixedprecision)! range:differencebetweenlargestandsmallestnumberspossible! precision:smallestpossibledifferencebetweenanytwonumbers IEEEFloaIngPoint! AnalogoustoscienIficnotaIon! x1 7 C:1.2e7! x1 >6 C:1.2e>6! IEEEStandard754usedbyallmajorCPUstoday! Drivenbynumericalconcerns! Rounding,overflow,underflow! Numericallywell>behaved,buthardtomakefastinhardware Autumn213 Integers&Floats 61 Autumn213 Integers&Floats 62 FloaIngPointRepresentaIon FloaIngPointRepresentaIon! Numericalform: V 1 =( 1) s M2 E! Numericalform: V 1 =( 1) s M2 E! SignbitsdetermineswhethernumberisnegaJveorposiJve! Significand(manJssa)MnormallyafracJonalvalueinrange[1.,2.)! ExponentEweightsvaluebya(possiblynegaJve)poweroftwo! SignbitsdetermineswhethernumberisnegaJveorposiJve! Significand(manJssa)MnormallyafracJonalvalueinrange[1.,2.)! ExponentEweightsvaluebya(possiblynegaJve)poweroftwo! RepresentaIoninmemory:! MSBsissignbits)! expfieldencodese)(butisnot1equal1toe)! fracfieldencodesm)(butisnot1equal1tom) s exp frac Autumn213 Integers&Floats 63 Autumn213 Integers&Floats 64
17 Precisions! Singleprecision:32bits s exp frac 1bit 8bits 23bits! Doubleprecision:64bits s exp frac 1bit 11bits 52bits NormalizaIonandSpecialValues V=( 1) s M2 E s exp frac! Normalized =Mhastheform1.xxxxx! AsinscienJficnotaJon,butinbinary!.11x2 5 and1.1x2 3 representthesamenumber,butthelawermakes beweruseoftheavailablebits! SinceweknowthemanJssastartswitha1,wedon'tbothertostoreit! Howdowerepresent.?Orspecial/undefinedvalueslike 1./.?! FiniterepresentaIonmeansnotallvaluescanberepresented exactly.somewillbeapproximated. Autumn213 Integers&Floats 65 Autumn213 Integers&Floats 66 NormalizaIonandSpecialValues V=( 1) s M2 E s exp frac! Normalized =Mhastheform1.xxxxx! AsinscienJficnotaJon,butinbinary!.11x2 5 and1.1x2 3 representthesamenumber,butthelawermakes beweruseoftheavailablebits! SinceweknowthemanJssastartswitha1,wedon'tbothertostoreit.! Specialvalues:! zero: s== exp==... frac==...! +,a : exp== frac==... 1./.= 1./.=+,1./.= 1./.=! NaN( NotaNumber ): exp==11...1frac!=... ResultsfromoperaJonswithundefinedresult:sqrt(>1),,,etc.! note:exp=11 1andexp= arereserved,limijngexprange Autumn213 Integers&Floats 67 FloaIngPointOperaIons:BasicIdea V=( 1) s M2 E s exp frac! x + f y = Round(x + y)! x f y = Round(x y)! BasicideaforfloaIngpointoperaIons:! First,computetheexactresult! Then,roundtheresulttomakeitfitintodesiredprecision:! Possiblyoverflowifexponenttoolarge! Possiblydropleast>significantbitsofsignificandtofitintofrac Autumn213 Integers&Floats 68
18 FloaIngPointMulIplicaIon FloaIngPointAddiIon!( 1) s1 )M1))2 E1 )))( 1) s2 )M2))2 E2)! ExactResult:( 1) s )M))2 E! Signs: s1^s2! SignificandM: M1M2! ExponentE: E1+E21! Fixing! IfM 2,shipMright,incrementE! IfEoutofrange,overflow! RoundMtofitfracprecision ( 1) s1 M12 E1 +(a1) s2 M22 E2 AssumeE1>E2! ExactResult:( 1) s )M))2 E! Signs,significandM:! Resultofsignedalign&add! ExponentE: E11! Fixing! IfM1 2,shipMright,incrementE! ifm<1,shipmlepkposijons,decrementebyk! OverflowifEoutofrange! RoundMtofitfracprecision + ( 1) s1 1M111 ( 1) s M E1 E21 ( 1) s2 M2 Autumn213 Integers&Floats 69 Autumn213 Integers&Floats 7 Roundingmodes! Possibleroundingmodes(illustratewithdollarrounding): $1.4 $1.6 $1.5 $2.5 $1.5! Round>toward>zero $1 $1 $1 $2 $1! Round>down(> ) $1 $1 $1 $2 $2! Round>up(+ ) $2 $2 $2 $3 $1! Round>to>nearest $1 $2??????! Round>to>even $1 $2 $2 $2 $2! RoundatoaevenavoidsstaIsIcalbiasinrepeatedrounding.! RoundsupabouthalftheJme,downabouthalftheJme.! DefaultroundingmodeforIEEEfloaJng>point MathemaIcalProperIesofFPOperaIons! Exponentoverflowyields+ ora #! Floatswithvalue+,a,andNaNcanbeusedinoperaIons! ResultusuallysJll+,>,orNaN;someJmesintuiJve,someJmesnot! FloaIngpointoperaIonsarenotalwaysassociaIveor distribuive,duetorounding!! (3.14+1e1)>1e1!=3.14+(1e1>1e1)! 1e2(1e2>1e2)!=(1e21e2)>(1e21e2) Autumn213 Integers&Floats 71 Autumn213 Integers&Floats 72
19 FloaIngPointinC! Cofferstwolevelsofprecision float singleprecision(32>bit) double doubleprecision(64>bit)!!!! #include <math.h>togetinfinityandnanconstants! Equality(==)comparisonsbetweenfloaIngpointnumbersare tricky,andopenreturnunexpectedresults! Justavoidthem! FloaIngPointinC! Conversionsbetweendatatypes:! CasJngbetweenint,float,anddoublechangesthebit representajon.! int float! Mayberounded;overflownotpossible! int doubleorfloat double! Exactconversion(32>bitints;52>bitfrac+1>bitsign)! long int double! Roundedorexact,dependingonwordsize! doubleorfloat int! TruncatesfracJonalpart(roundedtowardzero)! NotdefinedwhenoutofrangeorNaN:generallysetstoTmin!!! Autumn213 Integers&Floats 73 Autumn213 Integers&Floats 74 NumberRepresentaIonReallyMa\ers! 1991:PatriotmissiletargeIngerror! clockskewduetoconversionfromintegertofloajngpoint!!!! 1996:Ariane5rocketexploded($1billion)! overflowconverjng64>bitfloajngpointto16>bitinteger! 2:Y2Kproblem! limited(decimal)representajon:overflow,wrap>around! 238:Unixepochrollover! Unixepoch=secondssince12am,January1,197! signed32>bitintegerrepresentajonrollsovertotminin238! otherrelatedbugs! 1994:IntelPenJumFDIV(floaJngpointdivision)HWbug($475million)! 1997:USSYorktown smart warshipstranded:dividebyzero! 1998:MarsClimateOrbitercrashed:unitmismatch($193million) Autumn213 Integers&Floats 75 FloaIngPointandtheProgrammer #include <stdio.h> int main(int argc, char argv[]) { float f1 = 1.; float f2 =.; int i; for ( i=; i<1; i++ ) { f2 += 1./1.; } printf(x%8x x%8x\n, (int)&f1, (int)&f2); printf(f1 = %1.8f\n, f1); printf(f2 = %1.8f\n\n, f2); f1 = 1E3; f2 = 1E-3; float f3 = f1 + f2; printf (f1 == f3? %s\n, f1 == f3? yes : no ); return ; } $./a.out x3f8 x3f81 f1 = 1. f2 = 1.9 f1 == f3? yes Autumn213 Integers&Floats 76
20 MemoryReferencingBug double fun(int i) { volatile double d[1] = {3.14}; volatile long int a[2]; a[i] = ; / Possibly out of bounds / return d[]; } fun() > 3.14 fun(1) > 3.14 fun(2) > fun(3) > fun(4) > 3.14, then segmentation fault RepresenIng3.14asaDoubleFPNumber! =! 3.14=11.111! ( 1) s )M))2 E)! S=encodedas! M=1.111.(leading1lepout)! E=1encodedas124(withbias) s exp (11) frac (first 2 bits) 11 Autumn213 ExplanaJon: Saved State 4 d7 d4 3 d3 d 2 a[1] 1 a[] Integers&Floats LocaJonaccessedby fun(i) 77 Autumn213 frac (the other 32 bits) 1 Integers&Floats 78 MemoryReferencingBug(Revisited) double fun(int i) { volatile double d[1] = {3.14}; volatile long int a[2]; a[i] = ; / Possibly out of bounds / return d[]; } fun() > 3.14 fun(1) > 3.14 fun(2) > fun(3) > fun(4) > 3.14, then segmentation fault MemoryReferencingBug(Revisited) double fun(int i) { volatile double d[1] = {3.14}; volatile long int a[2]; a[i] = ; / Possibly out of bounds / return d[]; } fun() > 3.14 fun(1) > 3.14 fun(2) > fun(3) > fun(4) > 3.14, then segmentation fault Autumn213 Saved State d7 d4 d3 d a[1] a[] Integers&Floats LocaJon accessed byfun(i) 79 Autumn213 Saved State d7 d4 d3 d a[1] a[] Integers&Floats LocaJon accessed byfun(i) 8
21 MemoryReferencingBug(Revisited) Summary double fun(int i) { volatile double d[1] = {3.14}; volatile long int a[2]; a[i] = ; / Possibly out of bounds / return d[]; } fun() > 3.14 fun(1) > 3.14 fun(2) > fun(3) > fun(4) > 3.14, then segmentation fault Saved State d7 d4 d3 d a[1] a[] LocaJon accessed byfun(i)! Aswithintegers,floatssufferfromthefixednumberofbits availabletorepresentthem! Cangetoverflow/underflow,justlikeints! Some simplefracjons havenoexactrepresentajon(e.g.,.2)! Canalsoloseprecision,unlikeints! EveryoperaJongetsaslightlywrongresult! MathemaIcallyequivalentwaysofwriInganexpressionmay computedifferentresults! ViolatesassociaJvity/distribuJvity! NevertestfloaIngpointvaluesforequality!! CarefulwhenconverIngbetweenintsandfloats! Autumn213 Integers&Floats 81 Autumn213 Integers&Floats 82 Manymoredetailsforthecurious...! Exponentbias! Denormalizedvalues togetfinerprecisionnearzero! DistribuIonofrepresentablevalues! FloaIngpointmulIplicaIon&addiIonalgorithms! Roundingstrategies! Wewon tbeusingortesingyouonanyoftheseextrasin 351. Autumn213 Integers&Floats 83 Autumn213 Integers&Floats 84
22 NormalizedValues V=( 1) s M2 E s exp frac k n! CondiIon:exp andexp 111 1! Exponentcodedasbiasedvalue:E)=))exp)3)Bias)! expisanunsignedvaluerangingfrom1to2 k >2(k==#bitsinexp)! Bias=2 k>1 >1! Singleprecision:127(soexp:1 254,E:> )! Doubleprecision:123(soexp:1 246,E:> )! TheseenablenegaJvevaluesforE,forrepresenJngverysmallvalues! Significandcodedwithimpliedleading1:M))= 1.xxx x 2! xxx x:thenbitsoffrac! Minimumwhen (M=1.)! Maximumwhen111 1 (M=2. ε)! Getextraleadingbitfor free Autumn213 Integers&Floats 85 NormalizedEncodingExample V=( 1) s M2 E s exp frac k n! Value:float f = ;! = 2 =1.1 2 x2 13 (normalizedform)! Significand: M = frac= 1 2! Exponent:E=exp>Bias,soexp=E+Bias E 1 = 13 Bias = 127 exp = 14 = 1 2! Result: 1 1 s exp frac Autumn213 Integers&Floats 86 DenormalizedValues! CondiIon:exp=! Exponentvalue:E)=exp Bias+1(insteadofE=exp Bias)! Significandcodedwithimpliedleading:M)=).xxx x 2! xxx x:bitsoffrac! Cases! exp=,frac=! Representsvalue! NotedisJnctvalues:+and (why?)! exp=,frac! Numbersverycloseto.! Loseprecisionasgetsmaller! Equispaced Autumn213 Integers&Floats 87 SpecialValues! CondiIon:exp=111 1! Case:exp=111 1,frac=! Representsvalue (infinity)! OperaJonthatoverflows! BothposiJveandnegaJve! E.g.,1./.= 1./.=+,1./.= 1./.=! Case: exp=111 1,frac! Not>a>Number(NaN)! Representscasewhennonumericvaluecanbedetermined! E.g.,sqrt( 1),, # Autumn213 Integers&Floats 88
23 VisualizaIon:FloaIngPointEncodings TinyFloaIngPointExample s exp frac NaN > # >Normalized >Denorm > + +Denorm +Normalized + # NaN! 8abitFloaIngPointRepresentaIon! thesignbitisinthemostsignificantbit.! thenextfourbitsaretheexponent,withabiasof7.! thelastthreebitsarethefrac! SamegeneralformasIEEEFormat! normalized,denormalized! representajonof,nan,infinity Autumn213 Integers&Floats 89 Autumn213 Integers&Floats 9 DynamicRange(PosiIveOnly) Denormalized numbers Normalized numbers s exp frac E Value /81/64 = 1/ /81/64 = 2/ /81/64 = 6/ /81/64 = 7/ /81/64 = 8/ /81/64 = 9/ /81/2 = 14/ /81/2 = 15/ /81 = /81 = 9/ /81 = 1/ /8128 = /8128 = n/a inf closesttozero largestdenorm smallestnorm closestto1below closestto1above largestnorm DistribuIonofValues! 6abitIEEEalikeformat! e=3exponentbits! f=2fracjonbits! Biasis2 3>1 >1=3 s exp frac 1 3 2! NoIcehowthedistribuIongetsdensertowardzero Denormalized Normalized Infinity Autumn213 Integers&Floats 91 Autumn213 Integers&Floats 92
24 DistribuIonofValues(closeaupview)! 6abitIEEEalikeformat! e=3exponentbits! f=2fracjonbits! Biasis3 s exp Denormalized Normalized Infinity frac InteresIngNumbers {single,double} DescripIon exp frac NumericValue! Zero.! SmallestPos.Denorm. 1 2 {23,52} 2 {126,122}! Single ! Double ! LargestDenormalized (1. ε)2 {126,122}! Single ! Double ! SmallestPos.Norm {126,122}! Justlargerthanlargestdenormalized! One ! LargestNormalized (2. ε)2 {127,123}! Single ! Double Autumn213 Integers&Floats 93 Autumn213 Integers&Floats 94 SpecialProperIesofEncoding! FloaIngpointzero( + )exactlythesamebitsasintegerzero! Allbits=! Can(Almost)UseUnsignedIntegerComparison! Mustfirstcomparesignbits! Mustconsider a = + =! NaNsproblemaJc! Willbegreaterthananyothervalues! Whatshouldcomparisonyield?! OtherwiseOK! Denormvs.normalized! Normalizedvs.infinity FloaIngPointMulIplicaIon ( 1) s1 M12 E1 ( 1) s2 M22 E2! ExactResult:( 1) s M2 E! Signs: s1^s2//xorofs1ands2! SignificandM: M1M2! ExponentE: E1+E2! Fixing! IfM 2,shipMright,incrementE! IfEoutofrange,overflow! RoundMtofitfracprecision Autumn213 Integers&Floats 95 Autumn213 Integers&Floats 96
25 FloaIngPointAddiIon ( 1) s1 M12 E1 +( 1) s2 M22 E2 Assume1E11>1E21! ExactResult:( 1) s M2 E! Signs,significandM:! Resultofsignedalign&add! ExponentE:E1 ( 1) s1 1M111! Fixing! IfM 2,shipMright,incrementE! ifm<1,shipmlepkposijons,decrementebyk! OverflowifEoutofrange! RoundMtofitfracprecision + ( 1) s M E1 E21 ( 1) s2 M2 Autumn213 Integers&Floats 97 CloserLookatRoundaToaEven! DefaultRoundingMode! Hardtogetanyotherkindwithoutdroppingintoassembly! AllothersarestaJsJcallybiased! SumofsetofposiJvenumberswillconsistentlybeover>orunder> esjmated! ApplyingtoOtherDecimalPlaces/BitPosiIons! Whenexactlyhalfwaybetweentwopossiblevalues! Roundsothatleastsignificantdigitiseven! E.g.,roundtonearesthundredth (Lessthanhalfway) (Greaterthanhalfway) (Halfway roundup) (Halfway rounddown) Autumn213 Integers&Floats 98 RoundingBinaryNumbers! BinaryFracIonalNumbers! Halfway whenbitstorightofroundingposijon=1 2! Examples! Roundtonearest1/4(2bitsrightofbinarypoint) Value Binary Rounded AcJon RoundedValue 23/ (<1/2 down) 2 23/ (>1/2 up) 21/4 27/ (1/2 up) 3 25/ (1/2 down) 21/2 Autumn213 Integers&Floats 99
Roadmap. The Interface CSE351 Winter Frac3onal Binary Numbers. Today s Topics. Floa3ng- Point Numbers. What is ?
The Hardware/So@ware Interface CSE351 Winter 013 Floa3ng- Point Numbers Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine
More informationSystems Programming and Computer Architecture ( )
Systems Group Department of Computer Science ETH Zürich Systems Programming and Computer Architecture (252-0061-00) Timothy Roscoe Herbstsemester 2016 1 3: Integers in C Computer Architecture and Systems
More informationComputer Systems CEN591(502) Fall 2011
Computer Systems CEN591(502) Fall 2011 Sandeep K. S. Gupta Arizona State University 4 th lecture Data representation in computer systems (Slides adapted from CSAPP book) Announcements Programming assignment
More informationRepresenta7on of integers: unsigned and signed Conversion, cas7ng Expanding, trunca7ng Addi7on, nega7on, mul7plica7on, shiaing
Today s Topics Representa7on of integers: unsigned and signed Conversion, cas7ng Expanding, trunca7ng Addi7on, nega7on, mul7plica7on, shiaing CSE351 Inaugural Edi7on Spring 2010 1 Encoding Integers C short
More informationCS140 Lecture 08: Data Representation: Bits and Ints. John Magee 13 February 2017
CS140 Lecture 08: Data Representation: Bits and Ints John Magee 13 February 2017 Material From Computer Systems: A Programmer's Perspective, 3/E (CS:APP3e) Randal E. Bryant and David R. O'Hallaron, Carnegie
More informationCourse Overview. Jo, Heeseung
Course Overview Jo, Heeseung Course Theme: Abstraction Is Good But Don't Forget Reality Most CS and CE courses emphasize abstraction Abstract data types Asymptotic analysis These abstractions have limits
More informationFloating Point II, x86 64 Intro
Floating Point II, x86 64 Intro CSE 351 Autumn 2018 Instructor: Teaching Assistants: Justin Hsia Akshat Aggarwal An Wang Andrew Hu Brian Dai Britt Henderson James Shin Kevin Bi Kory Watson Riley Germundson
More informationData III & Integers I
Data III & Integers I CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis Administrivia Everyone has VM
More informationData III & Integers I
Data III & Integers I CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun
More informationThe Hardware/Software Interface CSE351 Spring 2015
The Hardware/Software Interface CSE351 Spring 2015 Lecture 6 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt, Dylan Johnson, Luke Nelson, Alfian Rizqi, Kritin Vij, David Wong, and Shan Yang
More informationComputer Organization: A Programmer's Perspective
A Programmer's Perspective Bits, Bytes, Nibbles, Words and Strings Gal A. Kaminka galk@cs.biu.ac.il Topics Why bits? Why 0/1? Basic terms: Bits, Bytes, Nibbles, Words Representing information as bits Characters
More informationData III & Integers I
Data III & Integers I CSE 351 Autumn 2018 Instructor: Justin Hsia Teaching Assistants: Akshat Aggarwal An Wang Andrew Hu Brian Dai Britt Henderson James Shin Kevin Bi Kory Watson Riley Germundson Sophie
More informationUniversity*of*Washington*
Roadmap* C:* car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly* language:* Machine* code:* Computer* system:* get_mpg: pushq movq... popq ret %rbp %rsp,
More informationAnnouncements* Hardware:*Logical*View* Hardware:*SemiVLogical*View* Hardware:*Physical*View*
Announcements* Hardware:*Logical*View* On*the*website:*cs.uw.edu/351* Speedometer!** Anonymous*feedback*form* Make*sure*you*are*subscribed*to*the*mailing*list* Lecture*slides*on*the*web*schedule*(these*will*be*linked*1>2*days*prior)*
More informationHardware: Logical View
Hardware: Logical View CPU Memory Bus Disks Net USB Etc. 1 Hardware: Physical View USB I/O controller Storage connections CPU Memory 2 Hardware: 351 View (version 0) instructions? Memory CPU data CPU executes
More informationBut first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15
Integer Representation Representation of integers: unsigned and signed Sign extension Arithmetic and shifting Casting But first, encode deck of cards. cards in suits How do we encode suits, face cards?
More informationRoadmap. Java: Assembly language: OS: Machine code: Computer system:
Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp
More informationFloating Point II, x86 64 Intro
Floating Point II, x86 64 Intro CSE 351 Autumn 2017 Instructor: Justin Hsia Teaching Assistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan
More informationRepresenting and Manipulating Integers. Jo, Heeseung
Representing and Manipulating Integers Jo, Heeseung Unsigned Integers Encoding unsigned integers B [ bw 1, bw 2,..., b0 ] x = 0000 0111 1101 0011 2 D( B) w 1 b i i 0 2 i D(x) = 2 10 + 2 9 + 2 8 + 2 7 +
More informationRepresenting Integers. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University
Representing Integers Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Unsigned Integers Encoding unsigned integers B [ bw 1, bw2,..., b0 ] x = 0000
More information1/%*2.&34.&%56+74.&%*8"(%&,.9%*!"#$%&'()*)+,'-.&.' & /* '012*)314'-5'"16*1+713'
1/%*2.&34.&%56+74.&%*8"(%&,.9%*!"#$%&'()*)+,'-.&.' & /* '012*)314'-5'"16*1+713' 8"'(&:9(+&;'' 89:';13
More informationCS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.
CS 33 Data Representation, Part 2 CS33 Intro to Computer Systems VII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved. Signed Integers Two s complement b w-1 = 0 Þ non-negative number value = b
More informationMemory, Data, & Addressing I
Memory, Data, & Addressing I CSE 351 Autumn 2017 Instructor: Justin Hsia Teaching Assistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan http://xkcd.com/953/
More informationIntegers II. CSE 351 Autumn Instructor: Justin Hsia
Integers II CSE 351 Autumn 2017 Instructor: Justin Hsia Teaching Assistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan http://xkcd.com/557/
More informationIntegers II. CSE 351 Autumn 2018
Integers II CSE 351 Autumn 2018 Instructor: Teaching Assistants: Justin Hsia Akshat Aggarwal An Wang Andrew Hu Brian Dai Britt Henderson James Shin Kevin Bi Kory Watson Riley Germundson Sophie Tian Teagan
More informationInts and Floating Point
Ints and Floating Point CSE 351 Winter 2017 http://xkcd.com/899/ Administriia Lab 1 due Friday How is it going? HW 1 out today Numerical representation and executable inspection 2 Using Shifts and Masks
More informationMachine Programming. CSE 351 Autumn Instructor: Justin Hsia
Machine Programming CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun http://www.smbc
More informationCSE351: Memory, Data, & Addressing I
CSE351: Memory, Data, & Addressing I CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis http://xkcd.com/138/
More informationIntegers II. CSE 351 Autumn Instructor: Justin Hsia
Integers II CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun http://xkcd.com/571/
More informationCS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VIII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.
CS 33 Data Representation, Part 2 CS33 Intro to Computer Systems VIII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved. Numeric Ranges Unsigned Values UMin = 0 000 0 UMax = 2 w 1 111 1 Two s Complement
More informationAssembly Programming I
Assembly Programming I CSE 410 Winter 2017 Instructor: Justin Hsia Teaching Assistants: Kathryn Chan, Kevin Bi, Ryan Wong, Waylon Huang, Xinyu Sui Heartbeat could be used as a password to access electronic
More informationData Representation Floating Point
Data Representation Floating Point CSCI 2400 / ECE 3217: Computer Architecture Instructor: David Ferry Slides adapted from Bryant & O Hallaron s slides via Jason Fritts Today: Floating Point Background:
More informationUniversity*of*Washington*
Roadmap* C:* car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly* language:* Machine* code:* Computer* system:* get_mpg: pushq movq... popq %rbp %rsp,
More informationCS241 Computer Organization Spring
CS241 Computer Organization Spring 2015 Prof. Searleman jets@clarkson.edu http://www.clarkson.edu/~jets/cs241 ! Name (as you like to be called)! Major! Graduating Year! Hometown! Programming & Computer
More informationData Representation Floating Point
Data Representation Floating Point CSCI 2400 / ECE 3217: Computer Architecture Instructor: David Ferry Slides adapted from Bryant & O Hallaron s slides via Jason Fritts Today: Floating Point Background:
More informationRepresenting and Manipulating Integers Part I
Representing and Manipulating Integers Part I Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Introduction The advent of the digital age Analog
More informationIntegers. Today. Next time. ! Numeric Encodings! Programming Implications! Basic operations. ! Floats
Integers Today! Numeric Encodings! Programming Implications! Basic operations! Programming Implications Next time! Floats Fabián E. Bustamante, 2007 Integers in C! C supports several integral data types
More informationCS 33. Data Representation (Part 3) CS33 Intro to Computer Systems VIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.
CS 33 Data Representation (Part 3) CS33 Intro to Computer Systems VIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved. Byte-Oriented Memory Organization 00 0 FF F Programs refer to data by address
More informationStructs and Alignment CSE 351 Spring
Structs and Alignment CSE 351 Spring 2018 http://xkcd.com/1168/ Administrivia Homework 3 due Wednesday Lab 3 released, due next week Lab 2 and midterm will be graded this week [in that order] 2 Roadmap
More informationChapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation
Chapter 2 Float Point Arithmetic Topics IEEE Floating Point Standard Fractional Binary Numbers Rounding Floating Point Operations Mathematical properties Real Numbers in Decimal Notation Representation
More informationJin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019
Jin-Soo Kim (jinsoo.kim@snu.ac.kr) Systems Software & Architecture Lab. Seoul National University Integers Spring 2019 4190.308: Computer Architecture Spring 2019 Jin-Soo Kim (jinsoo.kim@snu.ac.kr) 2 A
More informationFloating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754
Floating Point Puzzles Topics Lecture 3B Floating Point IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties For each of the following C expressions, either: Argue that
More informationL14: Structs and Alignment. Structs and Alignment. CSE 351 Spring Instructor: Ruth Anderson
Structs and Alignment CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis Administrivia Lab 2 due TONIGHT
More informationCourse Overview. CSCI 224 / ECE 317: Computer Architecture. Instructors: Prof. Jason Fritts. Slides adapted from Bryant & O Hallaron s slides
Course Overview CSCI 224 / ECE 317: Computer Architecture Instructors: Prof. Jason Fritts Slides adapted from Bryant & O Hallaron s slides 1 Overview Course theme Five realities Logistics 2 Course Theme:
More informationICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2
ICS 2008 Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2 Data Representations Sizes of C Objects (in Bytes) C Data Type Compaq Alpha Typical 32-bit Intel IA32 int 4 4 4 long int 8 4 4
More informationBryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon
Carnegie Mellon Floating Point 15-213/18-213/14-513/15-513: Introduction to Computer Systems 4 th Lecture, Sept. 6, 2018 Today: Floating Point Background: Fractional binary numbers IEEE floating point
More informationRepresenting Integers
Representing Integers Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu)
More informationUniversity*of*Washington*
Roadmap C: car c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp
More informationFloating Point. CSE 238/2038/2138: Systems Programming. Instructor: Fatma CORUT ERGİN. Slides adapted from Bryant & O Hallaron s slides
Floating Point CSE 238/2038/2138: Systems Programming Instructor: Fatma CORUT ERGİN Slides adapted from Bryant & O Hallaron s slides Today: Floating Point Background: Fractional binary numbers IEEE floating
More informationJava and C CSE 351 Spring
Java and C CSE 351 Spring 2018 https://xkcd.com/801/ Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: get_mpg: pushq
More informationBits, Bytes, and Integers
Bits, Bytes, and Integers CENG331 - Computer Organization Instructors: Murat Manguoglu (Section 1) Adapted from slides of the textbook: http://csapp.cs.cmu.edu/ Hello World! What happens under the hood?
More informationData Representation Floating Point
Data Representation Floating Point CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides adapted from Bryant & O Hallaron s slides Today: Floating Point Background: Fractional
More informationFloating Point : Introduction to Computer Systems 4 th Lecture, May 25, Instructor: Brian Railing. Carnegie Mellon
Floating Point 15-213: Introduction to Computer Systems 4 th Lecture, May 25, 2018 Instructor: Brian Railing Today: Floating Point Background: Fractional binary numbers IEEE floating point standard: Definition
More informationSystems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties
Systems I Floating Point Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for
More informationJava and C. CSE 351 Autumn 2018
Java and C CSE 351 Autumn 2018 Instructor: Teaching Assistants: Justin Hsia Akshat Aggarwal An Wang Andrew Hu Brian Dai Britt Henderson James Shin Kevin Bi Kory Watson Riley Germundson Sophie Tian Teagan
More informationToday: Floating Point. Floating Point. Fractional Binary Numbers. Fractional binary numbers. bi bi 1 b2 b1 b0 b 1 b 2 b 3 b j
Floating Point 15 213: Introduction to Computer Systems 4 th Lecture, Jan 24, 2013 Instructors: Seth Copen Goldstein, Anthony Rowe, Greg Kesden 2 Fractional binary numbers What is 1011.101 2? Fractional
More informationFloating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754
Floating Point Puzzles Topics Lecture 3B Floating Point IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties For each of the following C expressions, either: Argue that
More informationStructs & Alignment. CSE 351 Autumn Instructor: Justin Hsia
Structs & Alignment CSE 351 Autumn 2018 Instructor: Justin Hsia Teaching Assistants: Akshat Aggarwal An Wang Andrew Hu Brian Dai Britt Henderson James Shin Kevin Bi Kory Watson Riley Germundson Sophie
More informationRoadmap. Java: Assembly language: OS: Machine code: Computer system:
Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp
More informationFloating Point Numbers
Floating Point Numbers Computer Systems Organization (Spring 2016) CSCI-UA 201, Section 2 Instructor: Joanna Klukowska Slides adapted from Randal E. Bryant and David R. O Hallaron (CMU) Mohamed Zahran
More informationFloating Point Numbers
Floating Point Numbers Computer Systems Organization (Spring 2016) CSCI-UA 201, Section 2 Fractions in Binary Instructor: Joanna Klukowska Slides adapted from Randal E. Bryant and David R. O Hallaron (CMU)
More informationSystem Programming CISC 360. Floating Point September 16, 2008
System Programming CISC 360 Floating Point September 16, 2008 Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Powerpoint Lecture Notes for Computer Systems:
More informationFloating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)
Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science) Floating Point Background: Fractional binary numbers IEEE floating point standard: Definition Example and properties
More informationLecture 5-6: Bits, Bytes, and Integers
CSCI-UA.0201-003 Computer Systems Organization Lecture 5-6: Bits, Bytes, and Integers Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Slides adapted from: Jinyang Li Bryant and O Hallaron
More informationComputer Organization: A Programmer's Perspective
A Programmer's Perspective Representing Numbers Gal A. Kaminka galk@cs.biu.ac.il Fractional Binary Numbers 2 i 2 i 1 4 2 1 b i b i 1 b 2 b 1 b 0. b 1 b 2 b 3 b j 1/2 1/4 1/8 Representation Bits to right
More informationCS367 Test 1 Review Guide
CS367 Test 1 Review Guide This guide tries to revisit what topics we've covered, and also to briefly suggest/hint at types of questions that might show up on the test. Anything on slides, assigned reading,
More informationFloating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.
class04.ppt 15-213 The course that gives CMU its Zip! Topics Floating Point Jan 22, 2004 IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Floating Point Puzzles For
More informationJava and C I. CSE 351 Spring Instructor: Ruth Anderson
Java and C I CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis Administrivia Homework 5 Due TONIGHT Wed
More informationGiving credit where credit is due
CSCE 230J Computer Organization Floating Point Dr. Steve Goddard goddard@cse.unl.edu http://cse.unl.edu/~goddard/courses/csce230j Giving credit where credit is due Most of slides for this lecture are based
More informationGiving credit where credit is due
JDEP 284H Foundations of Computer Systems Floating Point Dr. Steve Goddard goddard@cse.unl.edu Giving credit where credit is due Most of slides for this lecture are based on slides created by Drs. Bryant
More informationFloating Point January 24, 2008
15-213 The course that gives CMU its Zip! Floating Point January 24, 2008 Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties class04.ppt 15-213, S 08 Floating
More informationStructs and Alignment
Structs and Alignment CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun
More informationOverview. Course Overview and Introduction
Here early? Try going to http://chimein.cla.umn.edu/ and see if you can answer an ice cream question Course Overview and Introduction CSci 2021: Machine Architecture and Organization Lecture #1, September
More informationFloating point. Today. IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Next time.
Floating point Today IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Next time The machine model Fabián E. Bustamante, Spring 2010 IEEE Floating point Floating point
More informationBits, Bytes, and Integers
Bits, Bytes, and Integers with contributions from Dr. Bin Ren, College of William & Mary 1 Bits, Bytes, and Integers Representing information as bits Bit-level manipulations Integers Representation: unsigned
More informationCS , Fall 2001 Exam 1
Andrew login ID: Full Name: CS 15-213, Fall 2001 Exam 1 October 9, 2001 Instructions: Make sure that your exam is not missing any sheets, then write your full name and Andrew login ID on the front. Write
More informationStructs and Alignment
Structs and Alignment CSE 410 Winter 2017 Instructor: Justin Hsia Teaching Assistants: Kathryn Chan, Kevin Bi, Ryan Wong, Waylon Huang, Xinyu Sui Self Driving Cars Will Make Organ Shortages Even Worse
More informationThe Hardware/Software Interface CSE351 Spring 2015
The Hardware/Software Interface CSE351 Spring 2015 Instructor: Katelin Bailey Teaching Assistants: Kaleo Brandt, Dylan Johnson, Luke Nelson, Alfian Rizqi, Kritin Vij, David Wong, and Shan Yang Who are
More informationx86 Programming I CSE 351 Winter
x86 Programming I CSE 351 Winter 2017 http://xkcd.com/409/ Administrivia Lab 2 released! Da bomb! Go to section! No Luis OH Later this week 2 Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals
More information15213 Recitation 2: Floating Point
15213 Recitation 2: Floating Point 1 Introduction This handout will introduce and test your knowledge of the floating point representation of real numbers, as defined by the IEEE standard. This information
More informationBuilding an Executable
Building an Executable CSE 351 Summer 2018 Instructor: Justin Hsia Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan http://xkcd.com/1790/ Administrivia Lab 2 due Monday (7/16) Homework 3 due
More informationJava and C II. CSE 351 Spring Instructor: Ruth Anderson
Java and C II CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis Administrivia Lab 5 Due TONIGHT! Fri 6/2
More informationFloating Point. CSE 351 Autumn Instructor: Justin Hsia
Floating Point CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun http://xkcd.com/899/
More informationRepresenting and Manipulating Floating Points. Jo, Heeseung
Representing and Manipulating Floating Points Jo, Heeseung The Problem How to represent fractional values with finite number of bits? 0.1 0.612 3.14159265358979323846264338327950288... 2 Fractional Binary
More informationRepresenting and Manipulating Floating Points
Representing and Manipulating Floating Points Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu The Problem How to represent fractional values with
More informationIntegers Sep 3, 2002
15-213 The course that gives CMU its Zip! Topics Numeric Encodings Unsigned & Two s complement Programming Implications C promotion rules Basic operations Integers Sep 3, 2002 Addition, negation, multiplication
More informationSystems 1. Integers. Unsigned & Twoʼs complement. Addition, negation, multiplication
Systems 1 Integers Topics Numeric Encodings Unsigned & Twoʼs complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Programming Implications Consequences
More informationRepresenting and Manipulating Floating Points. Computer Systems Laboratory Sungkyunkwan University
Representing and Manipulating Floating Points Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu The Problem How to represent fractional values with
More informationInteger Encoding and Manipulation
CSE 2421: Systems I Low-Level Programming and Computer Organization Integer Encoding and Manipulation Presentation D Study: Bryant Chapter 2.1 2.3 Gojko Babić 01-24-2018 Unsigned & Signed Integer Encoding
More informationx86-64 Programming III & The Stack
x86-64 Programming III & The Stack CSE 351 Winter 2018 Instructor: Mark Wyse Teaching Assistants: Kevin Bi Parker DeWilde Emily Furst Sarah House Waylon Huang Vinny Palaniappan http://xkcd.com/1652/ Administrative
More informationIntegers. Dr. Steve Goddard Giving credit where credit is due
CSCE 23J Computer Organization Integers Dr. Steve Goddard goddard@cse.unl.edu http://cse.unl.edu/~goddard/courses/csce23j Giving credit where credit is due Most of slides for this lecture are based on
More informationFoundations of Computer Systems
18-600 Foundations of Computer Systems Lecture 4: Floating Point Required Reading Assignment: Chapter 2 of CS:APP (3 rd edition) by Randy Bryant & Dave O Hallaron Assignments for This Week: Lab 1 18-600
More informationCOMP2611: Computer Organization. Data Representation
COMP2611: Computer Organization Comp2611 Fall 2015 2 1. Binary numbers and 2 s Complement Numbers 3 Bits: are the basis for binary number representation in digital computers What you will learn here: How
More informationCS356: Discussion #3 Floating-Point Operations. Marco Paolieri
CS356: Discussion #3 Floating-Point Operations Marco Paolieri (paolieri@usc.edu) Today s Agenda More Integer operations exercise Floating-Point operations exercise for Lab 2 Data Lab 2: What to implement
More informationArrays. CSE 351 Autumn Instructor: Justin Hsia
rrays CSE 351 utumn 2017 Instructor: Justin Hsia Teaching ssistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan http://xkcd.com/1270/ dministrivia
More informationMemory Allocation I. CSE 351 Autumn Instructor: Justin Hsia
Memory Allocation I CSE 351 Autumn 2017 Instructor: Justin Hsia Teaching Assistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan Administrivia
More informationVirtual Memory I. CSE 351 Spring Instructor: Ruth Anderson
Virtual Memory I CSE 35 Spring 27 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis Administrivia Midterms Graded If you did
More informationFloating Point Numbers
Floating Point Floating Point Numbers Mathematical background: tional binary numbers Representation on computers: IEEE floating point standard Rounding, addition, multiplication Kai Shen 1 2 Fractional
More informationThe Hardware/Software Interface CSE351 Spring 2013 (spring has sprung!)
The Hardware/Software Interface CSE351 Spring 2013 (spring has sprung!) Instructor: Luis Ceze Teaching Assistants: Katelin Bailey, Jeremy Lee, Jake Sanders, Rachel Sobel 1 Who is Luis? PhD in architecture,
More informationFloating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !
Floating point Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties Next time! The machine model Chris Riesbeck, Fall 2011 Checkpoint IEEE Floating point Floating
More information