深入理解计算机系统第二版习题答案.pdf
《深入理解计算机系统第二版习题答案.pdf》由会员分享,可在线阅读,更多相关《深入理解计算机系统第二版习题答案.pdf(89页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、深入理解计算机系统第二版 习题答案Computer Systems:A Programmers PerspectiveInstructors Solution Manual1Randal E.BryantDavid R.OHallaronDecember 4,20031Copyright c?2003,R.E.Bryant,D.R.OHallaron.All rights reserved.Chapter 1Solutions to Homework ProblemsThe text uses two different kinds of exercises:Practice Problems
2、.These are problems that are incorporated directly into the text,with explanatorysolutions at the end of each chapter.Our intention is that students will work on these problems as theyread the book.Each one highlights some particular concept.Homework Problems.These are found at the end of each chapt
3、er.They vary in complexity fromsimple drills to multi-week labs and are designed for instructors to give as assignments or to use asrecitation examples.This document gives the solutions to the homework problems.1.1Chapter 1:A Tour of Computer Systems1.2Chapter 2:Representing and Manipulating Informa
4、tionProblem 2.40 Solution:This exercise should be a straightforward variation on the existing code.code/data/show-ans.c1void show_short(short int x)23show_bytes(byte_pointer)&x,sizeof(short int);456void show_long(long int x)78show_bytes(byte_pointer)&x,sizeof(long);912CHAPTER 1.SOLUTIONS TO HOMEWORK
5、 PROBLEMS1011void show_double(double x)1213show_bytes(byte_pointer)&x,sizeof(double);14code/data/show-ans.cProblem 2.41 Solution:There are many ways to solve this problem.The basic idea is to create some multibyte datum with differentvalues for the most and least-significant bytes.We then read byte
6、0 and determine which byte it is.In the following solution is to create an int with value 1.We then access its first byte and convert it to anint.This byte will equal 0 on a big-endian machine and 1 on a little-endian machine.code/data/show-ans.c1int is_little_endian(void)23/*MSB=0,LSB=1*/4int x=1;5
7、6/*Return MSB when big-endian,LSB when little-endian*/7return(int)(*(char*)&x);8code/data/show-ans.cProblem 2.42 Solution:This is a simple exercise in masking and bit manipulation.It is important to mention that 0 xFF is a wayto generate a mask that selects all but the least significant byte that wo
8、rks for any word size.(x&0 xFF)|(y&0 xFF)Problem 2.43 Solution:These exercises require thinking about the logical operation!in a nontraditional way.Normally we thinkof it as logical negation.More generally,it detects whether there is any nonzero bit in a word.A.!xB.!xC.!(x&0 xFF)D.!(x&0 xFF)Problem
9、2.44 Solution:1.2.CHAPTER 2:REPRESENTING AND MANIPULATING INFORMATION3There are many solutions to this problem,but it is a little bit tricky to write one that works for any wordsize.Here is our solution:code/data/shift-ans.c1int int_shifts_are_arithmetic()23int x=0;/*All 1s*/45return(x 1)=x;6code/da
10、ta/shift-ans.cThe above code peforms a right shift of a word in which all bits are set to 1.If the shift is arithmetic,theresulting word will still have all bits set to 1.Problem 2.45 Solution:This problem illustrates some of the challenges of writing portable code.The fact that 132 yields 0 onsome
11、32-bit machines and 1 on others is common source of bugs.A.The C standard does not define the effect of a shift by 32 of a 32-bit datum.On the SPARC(andmany other machines),the expression x k shifts by?,i.e.,it ignores all but the leastsignificant 5 bits of the shift amount.Thus,the expression 1 32
12、yields 1.B.Compute beyond_msb as 2 31.C.We cannot shift by more than 15 bits at a time,but we can compose multiple shifts to get thedesired effect.Thus,we can compute set_msb as 2 15 15,and beyond_msb asset_msb 1.Problem 2.46 Solution:This problem highlights the difference between zero extension and
13、 sign extension.It also provides an excuseto show an interesting trick that compilers often use to use shifting to perform masking and sign extension.A.The function does not perform any sign extension.For example,if we attempt to extract byte 0 fromword 0 xFF,we will get 255,rather than.B.The follow
14、ing code uses a well-known trick for using shifts to isolate a particular range of bits and toperform sign extension at the same time.First,we perform a left shift so that the most significant bitof the desired byte is at bit position 31.Then we right shift by 24,moving the byte into the properposit
15、ion and peforming sign extension at the same time.code/data/xbyte.c1int xbyte(packed_t word,int bytenum)24CHAPTER 1.SOLUTIONS TO HOMEWORK PROBLEMS3int left=word (3-bytenum)24;5code/data/xbyte.cProblem 2.47 Solution:?Problem 2.48 Solution:This problem lets students rework the proof that complement pl
16、us increment performs negation.We make use of the property that twos complement addition is associative,commutative,and has additiveinverses.Using C notation,if we define y to be x-1,then we have y+1 equal to-y,and hence y equals-y+1.Substituting gives the expression-(x-1)+1,which equals-x.Problem 2
17、.49 Solution:This problem requires a fairly deep understanding of twos complement arithmetic.Some machines onlyprovide one form ofmultiplication,and hence the trick shown in thecode here isactually required to performthat actual form.As seen in Equation 2.16 we have?!?$#!?&%(*)+,#-%.*)+?%#-?&%(*)/%.
18、*)?10%.The final termhas no effect on the?32-bit representation of?3,but the middle term represents a correction factor thatmust be added to the high order2bits.This is implemented as follows:code/data/uhp-ans.c1unsigned unsigned_high_prod(unsigned x,unsigned y)23unsigned p=(unsigned)signed_high_pro
19、d(int)x,(int)y);45if(int)x 0)/*x_w-1=1*/6p+=y;7if(int)y 0)/*y_w-1=1*/8p+=x;9return p;10code/data/uhp-ans.cProblem 2.50 Solution:Patterns of the kind shown here frequently appear in compiled code.1.2.CHAPTER 2:REPRESENTING AND MANIPULATING INFORMATION5A.?:x+(x 2)B.?:x+(x 3)C.?:(x 4)-(x1)D.?:(x 3)-(x
20、6)Problem 2.51 Solution:Bit patterns similar to these arise in many applications.Many programmers provide them directly in hex-adecimal,but it would be better if they could express them in more abstract ways.A.%(?.(1 k)-1)B.%(?.(1 k)-1)jProblem 2.52 Solution:Byte extraction and insertion code is use
21、ful in many contexts.Being able to write this sort of code is animportant skill to foster.code/data/rbyte-ans.c1unsigned replace_byte(unsigned x,int i,unsigned char b)23int itimes8=i 3;4unsigned mask=0 xFF itimes8;56return(x&mask)|(b k;5/*Make mask of low order 32-k bits*/6unsigned mask=k?(1 k;5/*Ma
22、ke mask of high order k bits*/6unsigned mask=k?(1 (32-k)-1):0;78return(x 0)?mask|xsrl:xsrl;9code/data/rshift-ans.cProblem 2.54 Solution:These“C puzzle”problems are a great way to motivate students to think about the properties of computerarithmetic from a programmers perspective.Our standard lecture
23、 on computer arithmetic starts by showinga set of C puzzles.We then go over the answers at the end.A.(x-y).No,Let x=?0?.B.(x+y)1)1)31;6unsigned sy=uy 31;78return9(ux1=0&uy=0,y=uy)|/*x=0,y=0*/12(sx&sy&ux=uy);/*x 0,y 0*/13code/data/floatge-ans.cProblem 2.57 Solution:Exercises such as this help student
24、s understand floating point representations,their precision,and theirranges.A.The numberwill have?,?0?,0)?,and.The exponent bitswill be?and the fraction bits will be?.B.The largest odd integer that can be represented exactly will have a binary representation consistingof#1s.It will have?,?0?,?0?,and
25、a value?().The bit representation of the exponent will be the binary representation of#?*).The bit representation of the fraction will be?.C.The reciprocal of the smallest positive normalized value will have value?00.It will have?*)?,?,and.The bit representation of the exponent will be?.The bitrepre
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 深入 理解 计算机系统 第二 习题 答案
限制150内