深入理解计算机系统第三版答案

所需积分/C币:46 2016-04-06 16:33:58 325KB PDF
收藏 收藏 4
举报

深入理解计算机系统第三版答案
Chapter 1 Solutions to homework problems The text uses two different kinds of exercises Practice Problems. These are problems that are incorporated directly into the text, with explanatory solutions at the end of each chapter Our intention is that students will work on these problems as they read the book. Each one highlights some particular concept Homework Problems. These are found at the end of each chapter. They vary in complexity from simple drills to multi-week labs and are designed for instructors to give as assignments or to use as recitation examples. This document gives the solutions to the homework problems 1.1 Chapter 1: A Tour of Computer Systems 1.2 Chapter 2: Representing and manipulating Information Problem 2.40 solution: This exercise should be a straightforward variation on the existing code. codeldata/show-ans c 1 void show short (short int x) 2{ 3 show bytes((byte pointer)&x, sizeof(short int)) w long(long int x) 8 show bytes((byte pointer)&x, sizeof(long))i CHAPTER L. SOLUTIONS TO HOMEWORK PROBLEMS 10 11 void show double(double x) show bytes((byte pointer)&x, sizeof(double)) codeidata/ show-ansc Problem 2.41 solution: There are many ways to solve this problem the basic idea is to create some multibyte datum with different values for the most and least-significant bytes. We then read byte o 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 an int. This byte will equal 0 on a big-endian machine and l on a little-endian machine codeldata/show-ansc 1 int is little endian(void) 234 /x MSB =0, ISB=1+/ int x =1: ,x Return MSB when big-endian, LSB when little-endian * 7 return int)(x (char *)&x) 8} code/data/show -ans. c Problem 2.42 Solution This is a simple exercise in masking and bit manipulation. It is important to mention that OxFF is a way to generate a mask that selects all but the least significant byte that works for any word size (X反0xFF)|(y&0xF三) Problem 2.43 Solution These exercises require thinking about the logical operation in a nontraditional way normally we think of it as logical negation. More generally, it detects whether there is any nonzero bit in a word A.! B.!!~x C.!!(x OxFF) D.!!(~x&0×FF) Problem 2.44 solution: 1. 2 CHAPTER 2: REPRESENTING AND MANIPULATING INFORMATION There are many solutions to this problem but it is a little bit tricky to write one that works for any word size. here is our solution coderdataishift-ans.c 1 int int shifts are arithmetic( 234 intx=0;/kA111′s*/ return (x >> 1)==x 6} code/data/shift-anSc The above code peforms a right shift of a word in which all bits are set to l. If the shift is arithmetic, the resulting word will still have all bits set to I Problem 2.45 Solution This problem illustrates some of the challenges of writing portable code. The fact that 1<<32 yields on some 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(and many other machines), the expression x < k shifts by k mod 32, i.e., it ignores all but the least significant 5 bits of the shift amount. Thus, the expression 1 < 32 yields 1 B. Compute be yond msb as 2 < 31 C. We cannot shift by more than 15 bits at a time, but we can compose multiple shifts to get the desired effect. Thus, we can compute set msb as 2 < 15 < 15, and beyond msh as set msb < 1 Problem 2.46 solution: This problem highlights the difference between zero extension and sign extension. It also provides an excuse to 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 from word OxFF. we will get 255. rather than -1 B. The following code uses a well-known trick for using shifts to isolate a particular range of bits and to perform sign extension at the same time. First, we perform a left shift so that the most significant bit of the desired byte is at bit position 31. Then we right shift by 24, moving the byte into the proper position and peforming sign extension at the same time code/data/xbyte. c 1 irt xbyte (packed t word, int bytenum CHAPTER L. SOLUTIONS TO HOMEWORK PROBLEMS 3 int left word <<((3-bytenum)<< 3)i 4 return left > 24 code/data/xbyte.c Problem 2.47 Solution tracT 0110 14|「10011 3 01Il15[100016[10001-15 [1100 11111 1|00000 0000001 100016[0111 15|「10000116 Problem 2. 48 Solution: This problem lets students rework the proof that complement plus increment performs negation We make use of the property that two's complement addition is associative, commutative, and has additive inverses. Using C notation, if we define y to be x-1, then we have y+l equal to -y, and hence y equals y+1. Substituting gives the expression-(x-1)+l, which equals -x Problem 2.49 Solution This problem requires a fairly deep understanding of two's complement arithmetic. Some machines only provide one form of multiplication, and hence the trick shown in the code here is actually required to perform that actual form As seen in Equation 2. 16 we have a'-y'=x-y+(aw-19+ yu-1 2+aa-13a-122. The final term has no effect on the 2w-bit representation of y, but the middle term represents a correction factor that must be added to the high order w bits. This is implemented as follows code/data/uhp-anS c 1 unsigned unsigned high prod(unsigned x, unsigned y) unsigned p =(unsigned) signed high prod((int)x,(int)y)i if ((int)x<0)/*x w-1) 7 if ((int)y<0)/ky fw-1 +=又 return pi codeldataluhp-ans.c Problem 2.50 Solution: Patterns of the kind shown here frequently appear in compiled code 1. 2 CHAPTER 2: REPRESENTING AND MANIPULATING INFORMATION A.K=5:x+(区<<2) B.K=9:Ⅹ+(x<<3) C.K=14:(x<<4)-(x<<1) D.K=56:(x<<3)-(x<<6) Problem 2.51 solution: Bit patterns similar to these arise in many applications. Many programmers provide them directly in hex- decimal, but it would be better if they could express them in more abstract ways A.1060 ((1<<k)-1 B.00-8-10 ((1<<k) 1)<< Problem 2.52 Solution Byte extraction and insertion code is useful in many contexts. Being able to write this sort of code is an important skill to foster code/data/rbyte-ansc 1 unsigned replace byte (unsigned x, int i, unsigned char b) int ities =i<< 3 unsigned mask OxFF < itimes8; return (x mask) times8)i code/data/rbyte-ansc Problem 2. 53 Solution These problems are fairly tricky. They require generating masks based on the shift amounts. Shift value k equal to O must be handled as a special case, since otherwise we would be generating the mask by performin a left shift by 32 code/ data/rshift-an CHAPTER L. SOLUTIONS TO HOMEWORK PROBLEMS 1 unsigned srl(unsigned x, int k) 23456789 /x Perform shift arithmetically */ unsigned sra =(int)x > ki /x Make mask of low order 32-k bits */ unsigned mask =k ?((1<<(32-k) 0 return sra maski ans. c code/data/rshift-ansc tsra(int×rint.<) /x Perform shift logically int srl =(unsigned)x >>ki /x Make mask of aigh order k bits */ unsigned mask =k ?((1 <<(32-k) 7 8 return(x<0)? mask srl srl; code/data/rshift ans.c Problem 2.54 Solution These"C puzzle" problems are a great way to motivate students to think about the properties of computer arithmetic from a programmer's perspective. Our standard lecture on computer arithmetic starts by showing a set of c puzzles. We then go over the answers at the end A. (x<y)==(-x>-y) NO, Let x=TMin32,y=0 B. ((X+y)<<4)+y-x==17*y+15*x. Yes, from the ring properties of two's complement arith- metic (x+y).NO,x+y-(--1)+(-y-1)--(x+y)-2-(x+y)-1=(x+y) D. (int)(ux-uy)==-(y-x). Yes. Due to the isomorphism between twos complement and unsigned arithmetic E.((x >>1)<< 1)<=x. Yes. Right shift rounds toward minus infinity Problem 2.55 Solution: This problem helps students think about fractional binary representation A. Letting v denote the value of the string, we can see that shifting the binary point k positions to the right gives a string 9-9 9y y yy. . which has numeric value y+V, and also value v x 2k. equating these gives 1. 2 CHAPTER 2: REPRESENTING AND MANIPULATING INFORMATION B.(a)Fory=001, we havc Y=1, k=3,V=7 (b)For y=1001, we have Y=9, k=4, v=s=3 (c)Fory=000111, we have Y=7, k=6, V=6 9 Problem 2.56 Solution: This problem helps students appreciate the property of ieee floating point that the relative magnitude of two numbers can be determined by viewing the combination of exponent and fraction as an unsigned integer Only the signs and the handling of o requires special consideration code/dataifioatge-aanSc 1 int float ge(float x, float y) 3 unsigned u- f2u(x) unsigned uy f2u(y)i unsigned sx=u×>>31 unsigned sy uy >> 3li 7 8 return 9 (ux<<1==0&uy<<1==0 /* Both are zero * 10 (!sx 5& sy 0 (!sxs!sy551×>=y)||/*×>=0,y>=C.x/ (sx & sy & ux < uy)i *x<0,y<C 13} code/dalu nloalge-unsc Problem 2.57 Solution Exercises such as this help students understand floating point representations, their precision, and their rang A. The number 5.0 will have E-2, M-1.012-,f=0.012-f, and V-5. The exponent bits will be 100... 01 and the fraction bits will be 0100...0 B. The largest odd integer that can be represented exactly will have a binary representation consisting of+11s. It will have E=1,M=1.11…12=2-2-,f=0.11…12=1-2-,and a value V=2n+ 1. The bit representation of the exponent will be the binary representation of 1. The bit representation of the fraction will be 11:11 C. The reciprocal of the smallest positive normalized value will have value V=22-2. It will have E=2-1-2,M=1, and f=0. The bit representation of the exponent will be 11.100.The bit representation of the fraction will be 00...00 Problem 2.58 Solution 8 CHAPTER L. SOLUTIONS TO HOMEWORK PROBLEMS This exercise is of practical value, since Intel-compatible processors perform all of their arithmetic in ex tended precision. It is interesting to see how adding a few more bits to the exponent greatly increases the range of values that can be represented Description Extended precision Value D ecma mallest deform 2-63×2-16323.64×10-491 Smallest norm 16382 3.36×10 4932 Largest norn.(2-6)×26331.19×10132 Problem 2.59 solution We have found that working through floating point representations for small word sizes is very instructive Problems such as this one help make the description of ieee floating point more concrete Description He ex M 0 8000 62 Smallest value> 1 3F01 257 2 256 4700 71 Largest denormalized OOFF 6225×2-70 彐F00 Number with hex representation 3AA0 Problem 2. 60 Solution This problem requires students to think of the relationship between int, =loat, and double A.(double)(float)x==dx. No. Try x=TMar 2. Note that it is true with Linux/GCC, since it uses a extended precision representation for both double and float B dx dy --(double)(y+x). No Let x=y=TMin 32 C. dx t dy t dz = dz t dy t dx. Yes. Since each value ranges between TMin32 and TMa32 their sum can be represented exactl D. dx dy x dz == dz x dy x dx. No Let dx=!Mac32, dy=TMac32-l, dz=TMa332 2.(Not detected with linux/gcc) E dx dx = dy /dy. No. Let x Problem 2.61 solution: This problem helps students understand the relation between the different categories of numbers. Gettin ll of the cutoff thresholds correct is fairly tricky. Our solution file contains testing code code/dataifpwr2-anSc

...展开详情
试读 89P 深入理解计算机系统第三版答案
立即下载 低至0.43元/次 身份认证VIP会员低至7折
    一个资源只可评论一次,评论内容不能少于5个字
    Liam_LC 这好像是第一版的答案吧。。。
    2018-04-01
    回复
    img
    Lansing

    关注 私信 TA的资源

    上传资源赚积分,得勋章
    最新推荐
    深入理解计算机系统第三版答案 46积分/C币 立即下载
    1/89
    深入理解计算机系统第三版答案第1页
    深入理解计算机系统第三版答案第2页
    深入理解计算机系统第三版答案第3页
    深入理解计算机系统第三版答案第4页
    深入理解计算机系统第三版答案第5页
    深入理解计算机系统第三版答案第6页
    深入理解计算机系统第三版答案第7页
    深入理解计算机系统第三版答案第8页
    深入理解计算机系统第三版答案第9页
    深入理解计算机系统第三版答案第10页
    深入理解计算机系统第三版答案第11页
    深入理解计算机系统第三版答案第12页
    深入理解计算机系统第三版答案第13页
    深入理解计算机系统第三版答案第14页
    深入理解计算机系统第三版答案第15页
    深入理解计算机系统第三版答案第16页
    深入理解计算机系统第三版答案第17页
    深入理解计算机系统第三版答案第18页
    深入理解计算机系统第三版答案第19页
    深入理解计算机系统第三版答案第20页

    试读已结束,剩余69页未读...

    46积分/C币 立即下载 >