logo资料库

华中科技大学计算机系统基础实验报告.docx

第1页 / 共34页
第2页 / 共34页
第3页 / 共34页
第4页 / 共34页
第5页 / 共34页
第6页 / 共34页
第7页 / 共34页
第8页 / 共34页
资料共34页,剩余部分请下载后查看
实验1: 数据表示
实验2: Binary Bombs
实验3: 缓冲区溢出攻击
实验总结
课 程 实 验 报 告 课程名称: 计算机系统基础 专业班级: 学 号: 姓 名: 指导教师: 报告日期: 2016 年 5 月 24 日 计算机科学与技术学院 1
目录 实验 1:......................................................................................................2 实验 2:......................................................................................................9 实验 3:....................................................................................................23 实验总结...................................................................................................32 2
实验 1: 数据表示 1.1 实验概述 本实验的目的是更好地熟悉和掌握计算机中整数和浮点数的二进制编码表 示。 实验中,你需要解开一系列编程“难题”——使用有限类型和数量的运算操 作实现一组给定功能的函数,在此过程中你将加深对数据二进制编码表示的了解。 实验语言:c; 实验环境: linux 1.2 实验内容 需要完成 bits.c 中下列函数功能,具体分为三大类:位操作、补码运算和浮 点数操作。 1.3 实验设计 源码如下: /* * * * * * */ lsbZero - set 0 to the least significant bit of x Example: lsbZero(0x87654321) = 0x87654320 Legal ops: ! ~ & ^ | + << >> Max ops: 5 Rating: 1 int lsbZero(int x) { //x 右移一位再左移一位实现把最低有效位置 0 x = x>>1; x = x<<1; return x; } /* * byteNot - bit-inversion to byte n from word x * * * * * Bytes numbered from 0 (LSB) to 3 (MSB) Examples: getByteNot(0x12345678,1) = 0x1234A978 Legal ops: ! ~ & ^ | + << >> Max ops: 6 Rating: 2 1
*/ int byteNot(int x, int n) { //x 第 n 个字节每位都和 1 异或实现取反 int y = 0xff; n = n<<3; y = y<> Max ops: 20 Rating: 2 * * * * * */ int byteXor(int x, int y, int n) { //把 x 和 y 的第 n 个字节取出来异或,再转换为逻辑的 0 和 1 n = n<<3; x = x>>n; y = y>>n; x = x&(0xff); y = y&(0xff); return !!(x^y); } /* * * * * */ logicalAnd - x && y Legal ops: ! ~ & ^ | + << >> Max ops: 20 Rating: 3 2
int logicalAnd(int x, int y) { //把 x 和 y 分别转化为逻辑的 0 和 1,再相与 x = (!(!x))&(!(!y)); return x; } /* * * * * */ logicalOr - x || y Legal ops: ! ~ & ^ | + << >> Max ops: 20 Rating: 3 int logicalOr(int x, int y) { //把 x 和 y 分别转化为逻辑的 0 和 1,再相或 x = (!(!x))|(!(!y)); return x; } /* * rotateLeft - Rotate x to the left by n Can assume that 0 <= n <= 31 Examples: rotateLeft(0x87654321,4) = 0x76543218 Legal ops: ~ & ^ | + << >> ! Max ops: 25 Rating: 3 * * * * * */ int rotateLeft(int x, int n) { //先构造低 n 位为 1,高(32-n)位为 0 的数 z,x 左移 n 位后的数加上 x 右 移(32-n)位的数&z 即可 int z; z = ~(((1<<31)>>31)<>(32+(~n+1)))&z)+(x<
Legal ops: ! ~ & ^ | + << >> Max ops: 20 Rating: 4 * * * */ int parityCheck(int x) { //每次将数的低半数位与高半数位比较,再把 y 右移 31 位,最后把 y 转化为 逻辑的 0 和 1 int y; y = x<<16; y = y^x; y = y^(y<<8); y = y^(y<<4); y = y^(y<<2); y = y^(y<<1); y = y>>31; return !(!y); } /* * mul2OK - Determine if can compute 2*x without overflow Examples: mul2OK(0x30000000) = 1 mul2OK(0x40000000) = 0 Legal ops: ~ & ^ | + << >> Max ops: 20 Rating: 2 * * * * * * */ int mul2OK(int x) { //把 x 第 31 位和 30 位分别和 1 做按位与,再异或,再和 1 异或 int m; m = ((x>>31)&0x1)^((x>>30)&0x1); return m^0x1; } /* * mult3div2 - multiplies by 3/2 rounding toward 0, * Should exactly duplicate effect of C expression (x*3/2), 4
including overflow behavior. Examples: mult3div2(11) = 16 mult3div2(-9) = -13 mult3div2(1073741824) = -536870912(overflow) Legal ops: ! ~ & ^ | + << >> Max ops: 12 Rating: 2 * * * * * * * */ int mult3div2(int x) { //左移一位再+x 即 x*3,右移一位的时候,当 y 的最高位和最低位都为 0 时还 要+1 int y = (x<<1)+x; y = (y>>1)+(((y>>31)&1)&(((y<<31)>>31)&1)); return y; } /* * subOK - Determine if can compute x-y without overflow Example: subOK(0x80000000,0x80000000) = 1, subOK(0x80000000,0x70000000) = 0, Legal ops: ! ~ & ^ | + << >> Max ops: 20 Rating: 3 * * * * * */ int subOK(int x, int y) { //x 的最高有效位和 y 的最高有效位不同且 x 和(x-y)的最高位不同才能判 断溢出 int m = (x>>31)&1; int n = (y>>31)&1; x = (m^n)&(m^(((x+(~y+1))>>31)&1)); return (!x); } /* * absVal - absolute value of x * * Example: absVal(-1) = 1. You may assume -TMax <= x <= TMax 5
Legal ops: ! ~ & ^ | + << >> Max ops: 10 Rating: 4 * * * */ int absVal(int x) { //x 最高位为 0 时就是 x,最高位为 1 时是~x+1 int y = x>>31; x = (y&(~x+1))+((~y)&x); return x; } /* * float_abs - Return bit-level equivalent of absolute value of 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 representations of single-precision floating point values. When argument is NaN, return argument.. Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while * * */ Max ops: 10 Rating: 2 unsigned float_abs(unsigned uf) { int x=uf&(~(1<<31)); if(x>0x7f800000) { } return uf; else return x; } /* * 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 6
分享到:
收藏