ComputerSystems:AProgrammer’sPerspective
Instructor’sSolutionManual1
RandalE.BryantDavidR.O’HallaronDecember4,2003
1
Copyrightc2003,R.E.Bryant,D.R.O’Hallaron.Allrightsreserved.
2
Chapter1
SolutionstoHomeworkProblems
Thetextusestwodifferentkindsofexercises:
PracticeProblems.Theseareproblemsthatareincorporateddirectlyintothetext,withexplanatorysolutionsattheendofeachchapter.Ourintentionisthatstudentswillworkontheseproblemsastheyreadthebook.Eachonehighlightssomeparticularconcept.
HomeworkProblems.Thesearefoundattheendofeachchapter.Theyvaryincomplexityfromsimpledrillstomulti-weeklabsandaredesignedforinstructorstogiveasassignmentsortouseasrecitationexamples.
Thisdocumentgivesthesolutionstothehomeworkproblems.
1.1Chapter1:ATourofComputerSystems
1.2Chapter2:RepresentingandManipulatingInformation
Problem2.40Solution:
Thisexerciseshouldbeastraightforwardvariationontheexistingcode.
2
1011121314
CHAPTER1.SOLUTIONSTOHOMEWORKPROBLEMSvoidshow_double(doublex){
show_bytes((byte_pointer)&x,sizeof(double));}
code/data/show-ans.c
12345678
intis_little_endian(void){
/*MSB=0,LSB=1*/intx=1;
/*ReturnMSBwhenbig-endian,LSBwhenlittle-endian*/return(int)(*(char*)&x);}
1.2.CHAPTER2:REPRESENTINGANDMANIPULATINGINFORMATION3
Therearemanysolutionstothisproblem,butitisalittlebittrickytowriteonethatworksforanywordsize.Hereisoursolution:
code/data/shift-ans.c
Theabovecodepeformsarightshiftofawordinwhichallbitsaresetto1.Iftheshiftisarithmetic,theresultingwordwillstillhaveallbitssetto1.Problem2.45Solution:
Thisproblemillustratessomeofthechallengesofwritingportablecode.Thefactthat1<<32yields0onsome32-bitmachinesand1onothersiscommonsourceofbugs.
A.TheCstandarddoesnotde?netheeffectofashiftby32ofa32-bitdatum.OntheSPARC(andmanyothermachines),theexpressionx< B.Computebeyond_msbas2<<31. C.Wecannotshiftbymorethan15bitsatatime,butwecancomposemultipleshiftstogetthedesiredeffect.Thus,wecancomputeset_msbas2<<15<<15,andbeyond_msbasset_msb<<1.Problem2.46Solution: Thisproblemhighlightsthedifferencebetweenzeroextensionandsignextension.Italsoprovidesanexcusetoshowaninterestingtrickthatcompilersoftenusetouseshiftingtoperformmaskingandsignextension.A.Thefunctiondoesnotperformanysignextension.Forexample,ifweattempttoextractbyte0fromword0xFF,wewillget255,ratherthan. B.Thefollowingcodeusesawell-knowntrickforusingshiftstoisolateaparticularrangeofbitsandtoperformsignextensionatthesametime.First,weperformaleftshiftsothatthemostsigni?cantbitofthedesiredbyteisatbitposition31.Thenwerightshiftby24,movingthebyteintotheproperpositionandpeformingsignextensionatthesametime.