深入理解计算机系统答案(超高清电子版)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

ComputerSystems:AProgrammer’sPerspectiveInstructor’sSolutionManual1RandalE.BryantDavidR.O’HallaronDecember4,20031Copyrightc2003,R.E.Bryant,D.R.O’Hallaron.Allrightsreserved.2Chapter1SolutionstoHomeworkProblemsThetextusestwodifferentkindsofexercises:PracticeProblems.Theseareproblemsthatareincorporateddirectlyintothetext,withexplanatorysolutionsattheendofeachchapter.Ourintentionisthatstudentswillworkontheseproblemsastheyreadthebook.Eachonehighlightssomeparticularconcept.HomeworkProblems.Thesearefoundattheendofeachchapter.Theyvaryincomplexityfromsimpledrillstomulti-weeklabsandaredesignedforinstructorstogiveasassignmentsortouseasrecitationexamples.Thisdocumentgivesthesolutionstothehomeworkproblems.1.1Chapter1:ATourofComputerSystems1.2Chapter2:RepresentingandManipulatingInformationProblem2.40Solution:Thisexerciseshouldbeastraightforwardvariationontheexistingcode.code/data/show-ans.c1voidshow_short(shortintx)2{3show_bytes((byte_pointer)&x,sizeof(shortint));4}56voidshow_long(longintx)7{8show_bytes((byte_pointer)&x,sizeof(long));9}12CHAPTER1.SOLUTIONSTOHOMEWORKPROBLEMS1011voidshow_double(doublex)12{13show_bytes((byte_pointer)&x,sizeof(double));14}code/data/show-ans.cProblem2.41Solution:Therearemanywaystosolvethisproblem.Thebasicideaistocreatesomemultibytedatumwithdifferentvaluesforthemostandleast-significantbytes.Wethenreadbyte0anddeterminewhichbyteitis.Inthefollowingsolutionistocreateanintwithvalue1.Wethenaccessitsfirstbyteandconvertittoanint.Thisbytewillequal0onabig-endianmachineand1onalittle-endianmachine.code/data/show-ans.c1intis_little_endian(void)2{3/*MSB=0,LSB=1*/4intx=1;56/*ReturnMSBwhenbig-endian,LSBwhenlittle-endian*/7return(int)(*(char*)&x);8}code/data/show-ans.cProblem2.42Solution:Thisisasimpleexerciseinmaskingandbitmanipulation.Itisimportanttomentionthat˜0xFFisawaytogenerateamaskthatselectsallbuttheleastsignificantbytethatworksforanywordsize.(x&0xFF)|(y&˜0xFF)Problem2.43Solution:Theseexercisesrequirethinkingaboutthelogicaloperation!inanontraditionalway.Normallywethinkofitaslogicalnegation.Moregenerally,itdetectswhetherthereisanynonzerobitinaword.A.!!xB.!!˜xC.!!(x&0xFF)D.!!(˜x&0xFF)Problem2.44Solution:1.2.CHAPTER2:REPRESENTINGANDMANIPULATINGINFORMATION3Therearemanysolutionstothisproblem,butitisalittlebittrickytowriteonethatworksforanywordsize.Hereisoursolution:code/data/shift-ans.c1intint_shifts_are_arithmetic()2{3intx=˜0;/*All1’s*/45return(x1)==x;6}code/data/shift-ans.cTheabovecodepeformsarightshiftofawordinwhichallbitsaresetto1.Iftheshiftisarithmetic,theresultingwordwillstillhaveallbitssetto1.Problem2.45Solution:Thisproblemillustratessomeofthechallengesofwritingportablecode.Thefactthat132yields0onsome32-bitmachinesand1onothersiscommonsourceofbugs.A.TheCstandarddoesnotdefinetheeffectofashiftby32ofa32-bitdatum.OntheSPARC(andmanyothermachines),theexpressionxkshiftsby,i.e.,itignoresallbuttheleastsignificant5bitsoftheshiftamount.Thus,theexpression132yields1.B.Computebeyond_msbas231.C.Wecannotshiftbymorethan15bitsatatime,butwecancomposemultipleshiftstogetthedesiredeffect.Thus,wecancomputeset_msbas21515,andbeyond_msbasset_msb1.Problem2.46Solution:Thisproblemhighlightsthedifferencebetweenzeroextensionandsignextension.Italsoprovidesanexcusetoshowaninterestingtrickthatcompilersoftenusetouseshiftingtoperformmaskingandsignextension.A.Thefunctiondoesnotperformanysignextension.Forexample,ifweattempttoextractbyte0fromword0xFF,wewillget255,ratherthan.B.Thefollowingcodeusesawell-knowntrickforusingshiftstoisolateaparticularrangeofbitsandtoperformsignextensionatthesametime.First,weperformaleftshiftsothatthemostsignificantbitofthedesiredbyteisatbitposition31.Thenwerightshiftby24,movingthebyteintotheproperpositionandpeformingsignextensionatthesametime.code/data/xbyte.c1intxbyte(packed_tword,intbytenum)2{4CHAPTER1.SOLUTIONSTOHOMEWORKPROBLEMS3intleft=word((3-bytenum)3);4returnleft24;5}code/data/xbyte.cProblem2.47Solution:˜˜Problem2.48Solution:Thisproblemletsstudentsreworktheproofthatcomplementplusincrementperformsnegation.Wemakeuseofthepropertythattwo’scomplementadditionisassociative,commutative,andhasadditiveinverses.UsingCnotation,ifwedefineytobex-1,thenwehave˜y+1equalto-y,andhence˜yequals-y+1.Substitutinggivestheexpression-(x-1)+1,whichequals-x.Problem2.49Solution:Thisproblemrequiresafairlydeepunderstandingoftwo’scomplementarithmetic.Somemachinesonlyprovideoneformofmultiplication,andhencethetrickshowninthecodehereisactuallyrequiredtoperformthatactualform.AsseeninEquation2.16wehave!$#!&%(’*)+,#-%.’*)+%#-&%(’*)/%.’*)10%.Thefinaltermhasnoeffectonthe32-bitrepresentationof3,butthemiddletermrepresentsacorrectionfactorthatmustbeaddedtothehighorder2bits.Thisisimplementedasfollows:code/data/uhp-ans.c1unsignedunsigned_high_prod(unsignedx,unsignedy)2{3unsi

1 / 89
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功