CSAPP深入理解计算机系统实验一
2018-01-30 16:32
786 查看
/* * CS:APP Data Lab * * <Please put your name and userid here> * * bits.c - Source file with your solutions to the Lab. * This is the file you will hand in to your instructor. * * WARNING: Do not include the <stdio.h> header; it confuses the dlc * compiler. You can still use printf for debugging without including * <stdio.h>, although you might get a compiler warning. In general, * it's not good practice to ignore compiler warnings, but in this * case it's OK. */ #if 0 /* * Instructions to Students: * * STEP 1: Read the following instructions carefully. */ You will provide your solution to the Data Lab by editing the collection of functions in this source file. INTEGER CODING RULES: Replace the "return" statement in each function with one or more lines of C code that implements the function. Your code must conform to the following style: int Funct(arg1, arg2, ...) { /* brief description of how your implementation works */ int var1 = Expr1; ... int varM = ExprM; varJ = ExprJ; ... varN = ExprN; return ExprR; } Each "Expr" is an expression using ONLY the following: 1. Integer constants 0 through 255 (0xFF), inclusive. You are not allowed to use big constants such as 0xffffffff. 2. Function arguments and local variables (no global variables). 3. Unary integer operations ! ~ 4. Binary integer operations & ^ | + << >> Some of the problems restrict the set of allowed operators even further. Each "Expr" may consist of multiple operators. You are not restricted to one operator per line. You are expressly forbidden to: 1. Use any control constructs such as if, do, while, for, switch, etc. 2. Define or use any macros. 3. Define any additional functions in this file. 4. Call any functions. 5. Use any other operations, such as &&, ||, -, or ?: 6. Use any form of casting. 7. Use any data type other than int. This implies that you cannot use arrays, structs, or unions. You may assume that your machine: 1. Uses 2s complement, 32-bit representations of integers. 2. Performs right shifts arithmetically. 3. Has unpredictable behavior when shifting an integer by more than the word size. EXAMPLES OF ACCEPTABLE CODING STYLE: /* * pow2plus1 - returns 2^x + 1, where 0 <= x <= 31 4000 */ int pow2plus1(int x) { /* exploit ability of shifts to compute powers of 2 */ return (1 << x) + 1; } /* * pow2plus4 - returns 2^x + 4, where 0 <= x <= 31 */ int pow2plus4(int x) { /* exploit ability of shifts to compute powers of 2 */ int result = (1 << x); result += 4; return result; } FLOATING POINT CODING RULES For the problems that require you to implent floating-point operations, the coding rules are less strict. You are allowed to use looping and conditional control. You are allowed to use both ints and unsigneds. You can use arbitrary integer and unsigned constants. You are expressly forbidden to: 1. Define or use any macros. 2. Define any additional functions in this file. 3. Call any functions. 4. Use any form of casting. 5. Use any data type other than int or unsigned. This means that you cannot use arrays, structs, or unions. 6. Use any floating point data types, operations, or constants. NOTES: 1. Use the dlc (data lab checker) compiler (described in the handout) to check the legality of your solutions. 2. Each function has a maximum number of operators (! ~ & ^ | + << >>) that you are allowed to use for your implementation of the function. The max operator count is checked by dlc. Note that '=' is not counted; you may use as many of these as you want without penalty. 3. Use the btest test harness to check your functions for correctness. 4. Use the BDD checker to formally verify your functions 5. The maximum number of ops for each function is given in the header comment for each function. If there are any inconsistencies between the maximum ops in the writeup and in this file, consider this file the authoritative source. /* * STEP 2: Modify the following functions according the coding rules. * * IMPORTANT. TO AVOID GRADING SURPRISES: * 1. Use the dlc compiler to check that your solutions conform * to the coding rules. * 2. Use the BDD checker to formally verify that your solutions produce * the correct answers. */ #endif /* Copyright (C) 1991-2014 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, see <http://www.gnu.org/licenses/>. */ /* This header is separate from features.h so that the compiler can include it implicitly at the start of every compilation. It must not itself include <features.h> or any other header that includes <features.h> because the implicit include comes before any feature test macros that may be defined in a source file before it first explicitly includes a system header. GCC knows the name of this header in order to preinclude it. */ /* glibc's intent is to support the IEC 559 math functionality, real and complex. If the GCC (4.9 and later) predefined macros specifying compiler intent are available, use them to determine whether the overall intent is to support these features; otherwise, presume an older compiler has intent to support these features and define these macros by default. */ /* wchar_t uses ISO/IEC 10646 (2nd ed., published 2011-03-15) / Unicode 6.0. */ /* We do not support C11 <threads.h>. */ /* * addOK - Determine if can compute x+y without overflow * Example: addOK(0x80000000,0x80000000) = 0, * addOK(0x80000000,0x70000000) = 1, * Legal ops: ! ~ & ^ | + << >> * Max ops: 20 * Rating: 3 */ //求和溢出问题 int addOK(int x, int y) { int sum = x + y; return !((x ^ sum) & (y ^ sum)); }/*会一个整形数字的符号位的判别*/ //ops 6 /* * allEvenBits - return 1 if all even-numbered bits in word set to 1 * Examples allEvenBits(0xFFFFFFFE) = 0, allEvenBits(0x55555555) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 12 * Rating: 2 */ //所有的偶数为都为1 //way 1 int allEvenBits(int x) { int k0 = x & 0xff; int k1 = (x >> 8) & 0xff; int k2 = (x >> 16) & 0xff; int k3 = (x >> 24) & 0xff; k0 = 0x55 + (~k0 + 1); k1 = 0x55 + (~k1 + 1); k2 = 0x55 + (~k2 + 1); k3 = 0x55 + (~k3 + 1); x = k0 | k1 | k2 | k3; return !x; } //ops 23 //way 2 int allEvenBits(int x) { int EvenBits = (((0x55 << 8) | 0x55) << 16) | (0x55 << 8) | 0x55;//结果0x55555555 return !((EvenBits & x) ^ EvenBits); //如果x是0x55555555或者含有0x55555555的数,EvenBits & x得到0x55555555,否则得到0 //0x55555555 ^ 0x55555555 = 0 //0 ^ 0x55555555 = 0x55555555 } /* * bitParity - returns 1 if x contains an odd number of 0's * Examples: bitParity(5) = 0, bitParity(7) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 20 * Rating: 4 * 00000111 -> 7 * 00000101 -> 5 */ //统计0的个数 int bitParity(int x) { x ^= x >> 16; x ^= x >> 8; x ^= x >> 4; x ^= x >> 2; x ^= x >> 1; return x&1; } /* * float_f2i - Return bit-level equivalent of expression (int) f * for floating point argument f. * Argument is passed as unsigned int, but * it is to be interpreted as the bit-level representation of a * single-precision floating point value. * Anything out of range (including NaN and infinity) should return * 0x80000000u. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while * Max ops: 30 * Rating: 4 */ //求浮点数变整形 int float_f2i(unsigned uf) { int sign = uf >> 31;//0xffffffff或者0 int temp = (uf >> 23) & 0xff;//阶码 int num = uf & ((1 << 23) - 1);//尾数 int ans; if(temp < 127)//e = E - 127 ans = 0; if(temp > 157) ans = 0x80000000u; ans = ((1 << 30) + (num << 7)) >> (157 - temp); if(sign) ans = ~ans + 1; return ans; } /* * byteSwap - swaps the nth byte and the mth byte * Examples: byteSwap(0x12345678, 1, 3) = 0x56341278 * byteSwap(0xDEADBEEF, 0, 2) = 0xDEEFBEAD * You may assume that 0 <= n <= 3, 0 <= m <= 3 * Legal ops: ! ~ & ^ | + << >> * Max ops: 25 * Rating: 2 */ //交换字节 //way 1 int byteSwap(int x, int n, int m) { int x1 = (0xff << (n << 3)) & x; int x2 = (0xff << (m << 3)) & x; x = x ^ x1 ^ x2; x1 = ((x1 >> (n << 3)) & 0xff) << (m << 3); x2 = ((x2 >> (m << 3)) & 0xff) << (n << 3); x = x | x1 | x2; return !x; } //way 2 int byteSwap(int x, int n, int m) { int xn = n << 3; int xm = m << 3; int x1 = x >> xn; int x2 = x >> xm; return (((x & (~(0xff << xn))) & (~(0xff << xm))) | (x1 << xm) | (x2 << xn); //和way 1差不多的思路 } /* * float_twice - Return bit-level equivalent of expression 2*f for * floating point argument f. * Both the argument and result are passed as unsigned int's, but * they are to be interpreted as the bit-level representation of * single-precision floating point values. * When argument is NaN, return argument * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while * Max ops: 30 * Rating: 4 */ //返回一个浮点数的两倍 unsigned float_twice(unsigned uf) { unsigned sign = uf & 0x80000000; unsigned temp = uf & 0x7f800000; if(temp) { if(temp != 0x7f800000){ uf = uf + 0x00800000; if(temp == 0x7f800000)//not a number uf = uf & 0xff800000; } } else uf = (uf << 1) | sign;//如果阶码为0,那么尾数部分向左移动移位就是乘2 return uf; } /* * isGreater - if x > y then return 1, else return 0 * Example: isGreater(4,5) = 0, isGreater(5,4) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 24 * Rating: 3 */ //比较两个数的大小 int isGreater(int x, int y) { int signx = x >> 31; int signy = y >> 31;//结果,0xffffffff或者0 int signxy = !(signx ^ signy);//结果,同号为1,异号为0 return !((signxy & ((x + (~y)) >> 31)) | (( !signxy ) & signx)); //x + (~y) = x - y - 1,如果x < y会使得结果为0 } /* * satAdd - adds two numbers but when positive overflow occurs, returns * maximum possible value, and when negative overflow occurs, * it returns minimum positive value. * Examples: satAdd(0x40000000,0x40000000) = 0x7fffffff * satAdd(0x80000000,0xffffffff) = 0x80000000 * Legal ops: ! ~ & ^ | + << >> * Max ops: 30 * Rating: 4 */ //求和问题 int satAdd(int x, int y) { int ans = x + y; int overflow = ((x ^ ans) & (y ^ ans)) >> 31;//结果有两种,溢出为0xffffffff,不溢出为0 return ((ans >> (overflow & 31)) + (overflow << 31); //正正溢出,0xffffffff + 1 = 0x7fffffff //负负溢出,0 + 0x80000000(0xffffffff << 31) //不溢出,(ans >> 0) + (0 << 31) } /* * copyLSB - set all bits of result to least significant bit of x * Example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000 * Legal ops: ! ~ & ^ | + << >> * Max ops: 5 * Rating: 2 */ //复制最低位 int copyLSB(int x) { return ((x << 31) >> 31); }/*注意逻辑移位和算术移位的区别*/
如果想更加的了解题目的解法,可以参考:
http://blog.csdn.net/y_universe/article/details/79075963
相关文章推荐
- CSAPP 深入理解计算机系统 Buflab实验,缓冲区溢出攻击实验(5)
- CSAPP 深入理解计算机系统 Buflab实验,缓冲区溢出攻击实验(3)
- CSAPP 深入理解计算机系统 Buflab实验,缓冲区溢出攻击实验(6)
- 深入理解计算机系统(CSAPP)课程实验bomb程序炸弹实验日志(phase_1)
- 深入理解计算机系统(CSAPP)课程实验bomb程序炸弹实验日志(phase_3)
- 深入理解计算机系统(CSAPP)课程实验bomb程序炸弹实验日志(phase_2)
- 深入理解计算机系统(CSAPP)实验二 datalab-handout
- CSAPP(深入理解计算机系统) 实验——实现shell
- CSAPP 深入理解计算机系统课程实验 bomb实验 反向编译 汇编(4)
- CSAPP 深入理解计算机系统 Buflab实验,缓冲区溢出攻击实验(4)
- 深入理解计算机系统(CSAPP)课程实验bomb程序炸弹实验日志(phase_5)
- CSAPP 深入理解计算机系统 Buflab实验,缓冲区溢出攻击实验(1)
- CSAPP深入理解计算机系统实验datalab解析
- 深入理解计算机系统(CSAPP)课程实验bomb程序炸弹实验日志(phase_6)
- CSAPP 深入理解计算机系统 Buflab实验,缓冲区溢出攻击实验(2)
- CSAPP深入理解计算机系统实验2-二进制炸弹(Windows版本)
- CSAPP(深入理解计算机系统)第二版家庭作业答案-第二章
- 读完了csapp(中文名:深入理解计算机系统)
- 深入理解计算机系统(CSAPP)读书总结
- 《深入理解计算机系统CSAPP(第二版)》读书心得与全书推荐