lab1操作系统和原理详解

/* 
 * 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
   */
  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>.  */
 * absVal - absolute value of x
 *   Example: absVal(-1) = 1.
 *   You may assume -TMax <= x <= TMax
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 10
 *   Rating: 4
 */
int absVal(int x) {
int k;
k=x>>31;
  return (x^(k))+(~k+1);
}
此题要求是求int型数的绝对值,k的意思把这个数字右移31位如果是正数则返回0否则返回-1,在输出的时候~k+1的意思1是-k,异或是相同为零不同为1,而把k和x异或 当x是正数的时候相当于x和0异或再减0 没有影响若是x是负数的话 x与-1异或 二-1的二进制是111111 得到(x-1)-(-1)得到x的绝对值
/* 
 * bitXor - x^y using only ~ and & 
 *   Example: bitXor(4, 5) = 1
 *   Legal ops: ~ &
 *   Max ops: 14
 *   Rating: 1
 */
int bitXor(int x, int y) {
    ~((~(x&~y))&(~(~x&y)));
  return 2;
}
此题是用~和&符号来实现异或,根据真值表来得出最后的结果
           &  ^
0 0  1  0  0   0
0 1  0  0  0   1
1 0  1  1  0   1
1 1  0  0  1   0
/*
 * bitCount - returns count of number of 1's in word
 *   Examples: bitCount(5) = 2, bitCount(7) = 3
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 40
 *   Rating: 4
 */
int bitCount(int x) {
  //数据结构习题解析第三版 邓俊辉 第一章 12题(b) Page 9
  /*
   * Warning: 42 operators exceeds max of 40
   *
  int mask0 = (0x55) | (0x55 << 8) | (0x55 << 16) | (0x55 << 24);  //0x55555555
  int mask1 = (0x33) | (0x33 << 8) | (0x33 << 16) | (0x33 << 24);  //0x33333333
  int mask2 = (0x0F) | (0x0F << 8) | (0x0F << 16) | (0x0F << 24);  //0x0F0F0F0F
  int mask3 = (0xFF) |               (0xFF << 16);                 //0x00FF00FF
  int mask4 = (0xFF) | (0xFF << 8);                                //0x0000FFFF
  */

  int a = (0x55) | (0x55 << 8);
  int mask0 = a | (a << 16);
  int b = (0x33) | (0x33 << 8);
  int mask1 = b | (b << 16);
  int c = (0x0F) | (0x0F << 8);
  int mask2 = c | (c << 16);
  int mask3 = (0xFF) | (0xFF << 16);
  int mask4 = (0xFF) | (0xFF << 8);

  //unsigned int n = (unsigned int)x;
  int n = x;
  n = (n & mask0) + (n >> 0x01  & mask0);
  n = (n & mask1) + (n >> 0x02  & mask1);
  n = (n & mask2) + (n >> 0x04  & mask2);
  n = (n & mask3) + (n >> 0x08  & mask3);
  n = (n & mask4) + (n >> 0x10  & mask4);

  return n;
}
/* 
 * byteSwap - swaps the nth byte and the mth byte
 *  Examples: byteSwap(0x 12 34 56 78, 1, 3) = 0x56341278
 *            byteSwap(0xDE AD BE EF, 0, 2) = 0xDE EF BE AD
 *  You may assume that 0 <= n <= 3, 0 <= m <= 3
 *  Legal ops: ! ~ & ^ | + << >>
 *  Max ops: 25
 *  Rating: 2
 */
int byteSwap(int x, int n, int m) {
Int n1=n<<3;
Int m1=m<<3;
Int chaneg1,change2,locate;
Change1=(((x>>n1)&0xFF)<<m1);
Change2=(((x>>m1)&0xFF<<n1);
Locate=(x&(~(0xFF<<n1))&(0xFF<<m1));
    return change1|change2|locate;
}
因为16进制下8比特才等于一字节也就是每两个字母等于一个字节,所以每次要改变的要乘以八倍,change1是为了把x在n位置的字节移动到m字节上并且除了m字节其他全为0,change2是为了把x在m位置的字节移动到m字节上并且除了n字节其他全为0,locate是为了求出x除了n,m字节的其他字节,最后相或从而实现换位
/* 
 * oddBits - return word with all odd-numbered bits set to 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 8
 *   Rating: 2
 */


int oddBits(void) {
Int x=1;
Int y=-1;
1010101010  return 2;
}
因为10的二进制码为1010所以不断左移并相加得到32位的该数
/* 
 * isLess - if x < y  then return 1, else return 0 
 *   Example: isLess(4,5) = 1.
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 24
 *   Rating: 3
 */

int isLess(int x,int y)
{
Int x1=x>>31;
Int y1=y>>31;
Int judge1=(!(x1^y1));
Int judge2=(x1&(!y1));
Int first=(!((y+~x)>>31));
Int middle=judge1&first;
Int ans=middle|judge2;
Return ans;
}
//(!(sx^sy))
//+ - 0
//- + 0
//+ + 1
//- - 1
//=== 0
//                  (!(sx^sy))    !((y+~x+1)>>31)           (sx&(!sy))
//+   -  0               0           0             0              0       0
//-   +  1               0           1             0              1       1
//6   5  0               1           0             0              0       0
//5   6  1               1           1             1              0       1
//-4 -5  0               1           0             0              0       0
//-5 -4  1               1           1             1              0       1/*
此题分两种情况,一种是x,y异号,当x为正y为负的时候肯定返回0,当x为负y为正的时候肯定返回1,另外一种是当x,y同号的时候就是直接相减判断,这样做的目的是当防止他俩相加或相减正溢出或负溢出,
判断x,y的大小如果x<y返回1否则返回0,x1,y1右移31位,当他们是正数的时候返回0,否则返回-1,而judge1是为了限制他的判断条件当x,y同号的时候返回1否则返回0,
Judge2是为了判断x,y异号的时候的返回值,first是判断x-y返回的正负,但是我用y-x是因为y-x取非这样的话-1变成0,0变成1,更方便计算,middle是把只取first中同号判断部分二和judge2相或是判断整体的


 * 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
 */
正溢出=0111111111111;
负溢出=1000000000000;
int satAdd(int x, int y) {
Int ans=x+y;
Int middle=(((x^ans)&(y^ans))>>31;
Int ans1=((ans>>(middle&31)));
Int ans2=middle<<31;
  return ans1+ans2;
}
这是个判断溢出的题,如果x+y没有溢出就正常输出x+y,否则就输出0x7fffff
ans是算出x+y的值,如果x+y没有溢出的话middle得出的值就是0,ans1得出的结果是ans右移0位,而ans2也是0对答案没有影响,
但当x+y溢出的时候ans变成0x800000得到middle为-1而ans1会根据middle改变而右移31位,ans2会左移31位变成0x800000 再和ans1的-1相加得到0x7fffff
/* 
 * 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) {
if((uf&0x7f800000)==0))
uf=(uf&0x80000000)|(uf&0x007ffff)<<1);
else
    if((uf&0x7f800000)!=0x7f800000)
uf+=0x00800000;
//else
if((uf&0x7800000)==0x7f800000)
    uf=uf;
  return uf;
}
//0x7800000      0 11111111 00000000
三种情况判断
第一种就是阶码全为0,只有尾数的第一个是保证负数不变号 第二个是该数的尾数部分乘以2倍
第二种就是阶码不全为1,直接加上指数的1 就是乘以二倍了
第三种就是阶码全为1输出原值
第一个if判断是
/* 
 * 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) {
/**/
	unsigned ALL = 0x7FFFFFFF;	
	unsigned minNaN = 0x7F800001;
	unsigned temp = uf & ALL;
	if (temp >= minNaN) return uf ; 
						else return temp;
}
求浮点型数的绝对值,由题意知 当该无符号正数属于NaN的时候返回原值 其他的情况下返回其绝对值,ALL是01111111,来保证uf的值变成正数,

题意是当uf大于0x7F8000001返回uf否则返回绝对值 而float二进制码中正负的判断只有第一位决定第一位为0是正数 第一位为1位1负数,temp是返回的正数 无论uf是正是负

/* 
 * isEqual - return 1 if x == y, and 0 otherwise 
 *   Examples: isEqual(5,5) = 1, isEqual(4,5) = 0
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 5
 *   Rating: 2
 */
int isEqual(int x, int y) {
    !(a^b)
  return 2;
}
非常简单,如果x,y相同就返回1否则返回0,一看就知道是异或的相反 相同为0不同为1,再取个非 直接得到结果

#include<stdio.h>
void printBin(int n){
if(n==0) return;
printBin(n/2);	//打印前k-1位
printf("%d",n%2);//打印最后一位

}

猜你喜欢

转载自blog.csdn.net/qq_40970841/article/details/84851747