2 1 / 4
ComputerSystems:AProgrammer'sPerspective
Instructor'sSolutionManual 1 RandalE.Bryant DavidR.O'Hallaron December4,2003 1 Copyrightc
2003,R.E.Bryant,D.R.O'Hallaron.Allrightsreserved. 2 / 4
Chapter1 SolutionstoHomeworkProblems
Thetextusestwodifferentkindsofexercises:
PracticeProblems.Theseareproblemsthatareincorporateddirectlyintothetext,withexplanatory solutionsattheendofeachchapter.Ourintentionisthatstudentswillworkontheseproblemsasthey readthebook.Eachonehighlightssomeparticularconcept.
HomeworkProblems.Thesearefoundattheendofeachchapter.Theyvaryincomplexityfrom simpledrillstomulti-weeklabsandaredesignedforinstructorstogiveasassignmentsortouseas recitationexamples.Thisdocumentgivesthesolutionstothehomeworkproblems.
1.1Chapter1:ATourofComputerSystems
1.2Chapter2:RepresentingandManipulatingInformation
Problem2.40Solution:
Thisexerciseshouldbeastraightforwardvariationontheexistingcode.code/data/show-ans.c 1voidshow_short(short intx) 2{
- show_bytes((byte_pointer) &x,sizeof(shortint));
- show_bytes((byte_pointer) &x,sizeof(long));
4} 5 6voidshow_long(longintx) 7{
9}
1 3 / 4
2CHAPTER1.SOLUTIONSTOHOMEWORKPROBLEMS
10 11voidshow_double(double x) 12{ 13 show_bytes((byte_pointer) &x,sizeof(double)); 14} code/data/show-ans.c
Problem2.41Solution:
Therearemanywaystosolvethisproblem.Thebasicideaistocreatesomemultibytedatumwithdifferent valuesforthemostandleast-signicantbytes.Wethenreadbyte0anddeterminewhichbyteitis.Inthefollowingsolutionistocreateanintwithvalue1.Wethenaccessitsrstbyteandconvertittoan int.Thisbytewillequal0onabig-endianmachineand1onalittle-endianmachine.code/data/show-ans.c 1intis_little_endian(void) 2{
3 /*MSB=0,LSB=1*/
- intx=1;
- /*ReturnMSBwhenbig-endian,LSBwhenlittle-endian*/
- return(int)(*(char*)&x);
5
8} code/data/show-ans.c
Problem2.42Solution:
Thisisasimpleexerciseinmaskingandbitmanipulation.Itisimportanttomentionthat0xFFisaway togenerateamaskthatselectsallbuttheleastsignicantbytethatworksforanywordsize.(x&0xFF)|(y&0xFF)
Problem2.43Solution:
Theseexercisesrequirethinkingaboutthelogicaloperation!inanontraditionalway.Normallywethink ofitaslogicalnegation.Moregenerally,itdetectswhetherthereisanynonzerobitinaword.A.!!x B.!!x C.!!(x&0xFF) D.!!(x&0xFF)
Problem2.44Solution:
- / 4