基于C语言实现的DataLab数据表示实验

目 录

第1章 实验基本信息 - 4 -
1.1 实验目的 - 4 -
1.2 实验环境与工具 - 4 -
1.2.1 硬件环境 - 4 -
1.2.2 软件环境 - 4 -
1.2.3 开发工具 - 4 -
1.3 实验预习 - 4 -
第2章 实验环境建立 - 5 -
2.1 Ubuntu下CodeBlocks安装(5分) - 5 -
2.2 64位Ubuntu下32位运行环境建立(5分) - 5 -
第3章 C语言的位操作指令 - 6 -
3.1 逻辑操作(1分) - 6 -
3.2 无符号数位操作(2分) - 6 -
3.3 有符号数位操作(2分) - 6 -
第4章 汇编语言的位操作指令 - 7 -
4.1 逻辑运算(1分) - 7 -
4.2无符号数左右移(2分) - 7 -
4.3有符号左右移(2分) - 7 -
4.4循环移位(2分) - 7 -
4.5带进位位的循环移位(2分) - 7 -
4.6测试、位测试BTx(2分) - 7 -
4.7条件传送CMOVxx(2分) - 7 -
4.8条件设置SETCxx(1分) - 7 -
4.9进位位操作(1分) - 7 -
第5章 BITS函数实验与分析 - 8 -
5.1 函数lsbZero的实现及说明 - 8 -
5.2 函数byteNot的实现及说明函数 - 8 -
5.3 函数byteXor的实现及说明函数 - 8 -
5.4 函数logicalAnd的实现及说明函数 - 8 -
5.5 函数logicalOr的实现及说明函数 - 8 -
5.6 函数rotateLeft的实现及说明函数 - 8 -
5.7 函数parityCheck的实现及说明函数 - 8 -
5.8 函数mul2OK的实现及说明函数 - 9 -
5.9 函数mult3div2的实现及说明函数 - 9 -
5.10 函数subOK的实现及说明函数 - 9 -
5.11 函数absVal的实现及说明函数 - 9 -
5.12 函数float_abs的实现及说明函数 - 9 -
5.13 函数float_f2i的实现及说明函数 - 9 -
5.14函数XXXX的实现及说明函数(CMU多出来的函数-不加分) - 9 -
第6章 总结 - 10 -
10.1 请总结本次实验的收获 - 10 -
10.2 请给出对本次实验内容的建议 - 10 -
参考文献 - 11 -
1.2 实验环境与工具
1.2.1 硬件环境
X64 CPU;2.5GHz;8G RAM;500GHD Disk 以上
1.2.2 软件环境
Windows10 64位;VirtualBox;Ubuntu 16.04 LTS 64位;
1.2.3 开发工具
Visual Studio 2017 64位;CodeBlocks; /vim/gpedit+gcc
1.3 实验预习
1 逻辑操作
逻辑与x=x&x;
逻辑或x=x|x;
逻辑非x=~x;
逻辑异或x=x^x;
2 无符号数位操作
左移:<< 右移:>>(逻辑右移)
3 有符号数位操作
左移:<< 右移:>>(算术右移)

逻辑运算:
逻辑与AND %rax, %rbx
逻辑或OR %rax, %rbx
逻辑异或XOR %rax, %rbx
逻辑非NOT %rax, %rbx

无符号数左右移
逻辑左移SHL %rax, %rbx
逻辑右移SHR %rax, %rbx

有符号左右移
算数左移 SAL: %rax, %rbx
算数右移SAR:%rax, %rbx

循环移位
循环左移:ROL %rax, %rbx
循环右移:ROR %rax, %rbx

带进位位的循环移位
带进位位的循环左移:RCL %rax, %rbx
带进位位的循环右移:RCR %rax, %rbx

测试、位测试BTx
将指定位n复制到进位标志中,目的操作数包含要操作的位号n。BT将源操作数的位n复制到进位标志中;BTC将源操作数的位n复制到进位标志中并将位n变反;BTR将源操作数的位n复制到进位标志中并将位n清除;BTS将源操作数的位n复制到进位标志中并将位n置1。按位与(不保存运算结果,值改变标志位的值)
TEST %rax, %rbx
BT 2, %rax
BTC 2, %rax
BTR 2, %rax
BTS 2, %rax

条件传送CMOVxx
CMOV+a、b、e、n、l分别表示:大于、小于、等于、否定、进位
有符号数:用g、l、e、n、o分别表示:大于、小于、等、否、溢出
CMOVO 溢出 OF = 1 时传送送
CMOVNO 末溢出 OF = 0 时传送
CMOVS 带符号(负) SF = 1 时传送
CMOVNS 无符号(非负) SF = 0 时传送

/* Testing Code */

#include <limits.h>
#include <math.h>

/* Routines used by floation point test code */

/* Convert from bit level representation to floating point number */
float u2f(unsigned u) {
    
    
  union {
    
    
    unsigned u;
    float f;
  } a;
  a.u = u;
  return a.f;
}

/* Convert from floating point number to bit-level representation */
unsigned f2u(float f) {
    
    
  union {
    
    
    unsigned u;
    float f;
  } a;
  a.f = f;
  return a.u;
}

/* Copyright (C) 1991-2016 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 Unicode 8.0.0.  Version 8.0 of the Unicode Standard is
   synchronized with ISO/IEC 10646:2014, plus Amendment 1 (published
   2015-05-15).  */
/* We do not support C11 <threads.h>.  */
int test_lsbZero(int x) {
    
    
 if((x>0 && x%2==1) ||(x<0 && x%2==-1))
   x--;
 return x;
}
int test_byteNot(int x, int n)
{
    
    
    union {
    
    
  int xs;
  char b[4];
 }exp;
 exp.xs=x;
 exp.b[n]=~exp.b[n];
 return x=exp.xs;
}
int test_byteXor(int x, int y, int n) {
    
    
  union {
    
    
  int xs;
  char b[4];
 }exp1,exp2;
 exp1.xs=x;
 exp2.xs=y;
 return (exp1.b[n] - exp2.b[n]) != 0;
}
int test_logicalAnd(int x, int y) {
    
    
  return x && y;
}
int test_logicalOr(int x, int y) {
    
    
  return x || y;
}
int test_rotateLeft(int x, int n) {
    
    
  unsigned u = (unsigned) x;
  int i;
  for (i = 0; i < n; i++) {
    
    
      unsigned msb = u >> 31;
      unsigned rest = u << 1;
      u = rest | msb;
  }
  return (int) u;
}
int test_parityCheck(int x) {
    
    
  int result = 0;
  int i;
  for (i = 0; i < 32; i++)
    result ^= (x >> i) & 0x1;
  return result;
}
int test_mul2OK(int x)
{
    
    
  if ((x+x)/2 != x)
    return 0;
  else
    return 1;
}
int test_mult3div2(int x)
{
    
    
  return (x*3)/2;
}
int test_subOK(int x, int y)
{
    
    
  long long ldiff = (long long) x - y;
  return ldiff == (int) ldiff;
}
int test_absVal(int x) {
    
    
  return (x < 0) ? -x : x;
}
unsigned test_float_abs(unsigned uf) {
    
    
  float f = u2f(uf);
  unsigned unf = f2u(-f);
  if (isnan(f))
    return uf;
  /* An unfortunate hack to get around a limitation of the BDD Checker */
  if ((int) uf < 0)
      return unf;
  else
      return uf;
}
int test_float_f2i(unsigned uf) {
    
    
  float f = u2f(uf);
  int x = (int) f;
  return x;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/newlw/article/details/132799238