文档详情

计算机系实验报告(二)资料

w****i
实名认证
店铺
PDF
264.28KB
约13页
文档ID:107674779
计算机系实验报告(二)资料_第1页
1/13

1 课程实验报告 课程 名 称:计算机组成与结构 实验项目名称:datalab 专业 班 级:通信 1301 班 姓名: 学号: 指 导 教师:某神 完 成 时 间:2015年4月17日 计算机科学与工程系 2 实验题目:datalab 实验目的:完善 bit.c 里的各个函数,实现其功能,并通过 btest 的测试 实验环境:个人电脑、linux 发行版本 *实验内容及操作步骤: 第一步: 完善 bits.c 里的各个函数,代码如下: (1).bitAnd 函数,要求如下: /* * bitAnd - x } (2).getByte 函数,要求如下: /* * getByte - Extract byte n from word x *Bytes numbered from 0 (LSB) to 3 (MSB) *Examples: getByte(0x12345678,1) = 0x56 *Legal ops: ! ~ //将 x 的第 n 个 byte 移到最低 byte 位 return ret//返回最低位信息 BAB|A 3 } (3).logicalShift 函数,要求如下: /* * logicalShift - shift x to the right by n, using a logical shift *Can assume that 0 n t=(tn)16);//将 val 的高 16 位加到低 16 位上 val+=(val8);//再将 val 的高 8 位加到低 8 位上 return val//保留 val 的最低 byte 信息 为最终结果 } #贴一下某神代码(如果实验可以用 0x0-0xff 以外的数) : *****二分合并,log 级别复杂度***** 5 (5).bang 函数,要求如下: /* * bang - Compute !x without using ! *Examples: bang(3) = 0, bang(0) = 1 *Legal ops: ~ } (6).tmin 函数,要求如下: /* * tmin - return minimum two's complement integer *Legal ops: ! ~ } (7).fitsBits 函数,要求如下: /* * fitsBits - return 1 if x can be represented as an *n-bit, two's complement integer. *1 n)+((!!lowx) } (9).negate 函数,要求如下: /* * negate - return -x *Example: negate(1) = -1. *Legal ops: ! ~ } (10).isPositive 函数,要求如下: /* * isPositive - return 1 if x 0, return 0 otherwise *Example: isPositive(-1) = 0. *Legal ops: ! ~ //s 表示~x 的符号位 22)1 (2) 12() 1x~(2 U ) 12() 12( )x(2U) 12()x~(2 U 12)x(2 U )x(2 U 2)x(2 U 1 0 1 0 1 0 w 1 0 ww 1 0 1 0 得证。

写成将 由                     w i i i w i i i w i i iw w i i iww w i i iww w i i iw xxxT xTT xTT xT 8 return s//返回 x 为正数满足的条件 /* return !((x31)|(!x)); */ } (11).isLessOrEqual 函数,要求如下: /* * isLessOrEqual - if x 31);//sy 表示 y 的符号位 int z=y+(~x+1);//z=y-x int s=!(z31);//s 表示~z 的符号位 return (!(sx^sy)//返回上述思路中两种情况 } (12).ilog2 函数,要求如下: /* * ilog2 - return floor(log base 2 of x), where x 0 *Example: ilog2(16) = 4 *Legal ops: ! ~ s=!!(x16);//判断最高位是否在高 16 位上 s1=s8);//判断最高位是否在高 8 位上 s2=s4);//. s3=s2); s4=s1); s5=s;//最后一位 return s1+s2+s3+s4+s5;//累加得出结果 } (13).float_neg 函数,要求如下: /* * float_neg - Return bit-level equivalent of expression -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. ||, //符号位取反 return uf;//返回 NaN } (14).float_i2f 函数,要求如下: /* * float_i2f - Return bit-level equivalent of expression (float) x *Result is returned as unsigned int, but *it is to be interpreted as the bit-level representation of a *single-precision floating point values. *Legal ops: Any integer/unsigned operations incl. ||, //初始化 exp int frac=0;//用来保存尾数 11 int delta;//保存精度 int frac_mask=(1i) exp=i+127;//偏置指数 e=E+Bias,Bias=127 x=x128||((x==128)//处理精度,四舍五入 frac+=delta;//加上精度 if(frac23){//如果尾数溢出 frac//取尾数的后 23 位 exp+=1;//产生进位 } } return s|(exp23)|frac;//返回最后结果 } (15).float_twice 函数,要求如下: /* * 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. ||, //取阶码 int frac=uf if((exp^0xff)){//如果阶码部分不为 255 if(!exp){//阶码部分不为 0 frac=1;//将尾数左移一位即可 } else{//如果阶码为 0 exp++;//将阶码加 1 if(exp==255)//所得阶码为 255 frac=0;//将尾数设置为 0,表示无穷大 } }//省略 else 情况,即 uf 为 NaN return s|(exp23)|frac;//整合结果并返回 } 第二步: 在 Linux 下测试函数是否正确,指令如下: *编译:./dlc bits.c *测试 make btest ./btest *实验结果及分析: 13 如上图,测试后结果完全正确,说明思路和代码全部正确。

*收获与体会: 通过本实验,我对各种数据类型的在计算机中的表示方法有了更好的了解;用各种 运算符在一定的操作数限定内实现了函数的功能,更加熟悉了各种运算符,锻炼了思 维和代码能力 实 验 成 绩 。

下载提示
相似文档
正为您匹配相似的精品文档