Chap 2 Representing and Manipulating Information (CS:APP)

--------------------------------------------------------------------------------------------------

Author: YuManZI

2014/06/23  1.1-3.5

2014/06/24  3.6-3.8

2014/06/27  4.1

2014/06/28  4.2-4.5

--------------------------------------------------------------------------------------------------

1. Information Storage

1.1 Virtual Memory: a machine-level program views memory as a very large array of bytes.

1.2 Data Sizes (bytes)

32 bit: char 1; short [int] 2; int 4; long [int] 4; long long [int] 8; char *(any pointer) 4; float 4; double 8;

64 bit: char 1; short [int] 2; int 4; long [int]8; long long [int] 8; char *(any pointer)8; float 4; double 8;

Contents within square brackets [] are optional. The main difference between 32 bit and 64 bit machines are following two points: a) different sizes of data type long; b) different sizes of pointers.

1.3 Byte Ordering: big endian & little endian. (Takinh 0x01234567 as example.)

Big endian, the most significant byte comes first(lower address), bytes from low address to high address are 01 23 45 67, respectively;

Little endian, the least significant byte comes first(lower address), bytes from low address to high address are 67 45 23 01, respectively.

1.4 Shift Operations in C

Left shift << k: dropping off the k most significant bits and filling the right end with k zero;

Logical right shift >>k: dropping off the k least significant bits and filling the lest end with k zero;

Arithmetic right shift >>k: dropping off the k least significant bits and filling the lest end with k themost significant bit.

Arithmetic right shift uses the most significant bit as filling unit, because of the two‘s complement representation of negative integers. In some languages(e.g. java), the number of shifting bits can never be more than bit sizes of data types.

2. Integral Data Types

2.1 Unsigned Encodings (w bits, x=[x_(w-1), x_(w-2),...,x_0])

B2U(x)=sigma{i=[0,w-1]}(x_i*2^i)

B2U means Bits to Unsigned

It can represent integers between [0..2^w-1]

2.2 Two‘s-Complement Encodings (same setting as 2.1)

B2T(x)=-x_(w-1)*2^(w-1) + sigma{i=[0,w-2]}(x_i*2^i)

B2T means Bits to Two‘s

It‘s a signed encoding, can represent integers between [-2^(w-1), 2^(w-1)-1], the difference between it and Unsigned encoddings are the weight of the significant bit, i.e. positive for unsigned and negative for two‘s-complement.

2.3 Conversions

Signed<-->Unsigned with identical size: the key is to keep bit representation stable;

Large size-> Small size with same type of signed or unsigned: truncate directly;

<

时间: 2024-11-07 05:58:44

Chap 2 Representing and Manipulating Information (CS:APP)的相关文章

《CS:APP》 chapter 2 Representing and Manipulating Information 笔记

Representing and Manipulating Information 首先,普及一下历史知识,原来十进制数都使用1000多年了...其实我真不知道...斐波拉契(Fibonacci)很屌的说(废话....) The familiar decimal, or base-10, representation has been in use for over 1000 years, having been developed in India, improved by Arab math

《CSAPP》读书杂记 - Chapter 2. Representing and Manipulating Information

1. 代码: #include <stdio.h> typedef unsigned char *byte_pointer; void show_bytes(byte_pointer start, int len) { int i; for(i = 0; i < len; i++) { printf(" %.2x", start[i]); } printf("\n"); } void show_int(int x) { show_bytes((by

Computer Science - CS:APP - 2.1 信息存储

CS:APP - 2.1 信息存储 未知: 新知: 字长指明指针数据的标称大小.字长决定的最重要的系统参数就是虚拟地址空间的最大大小 char类型也能被用来存储整数值 使用确定大小的整数类型是程序员准确控制数据表示的最佳途径 面向普通用户的机器中排列表示一个对象字节的模式是小端模式 字节顺序在以下情景中会成为问题: 在不同类型的机器之间通过网络传送二进制数据 阅读表示整数的数据的字节序列 编写规避正常的类型系统的程序 原文地址:https://www.cnblogs.com/samaritan-

深入理解计算机系统 (CS:APP) Lab2 - Bomb Lab 解析

原文地址:https://billc.io/2019/04/csapp-bomblab/ 写在前面 CS:APP是这学期的一门硬核课程,应该是目前接触到最底层的课程了.学校的教学也是尝试着尽量和CMU同步,课件和习题都直接照搬原版.包括现在着手的第二个实验室Bomb Lab.这个lab很有意思,没有提供全部c语言代码,需要手动根据反汇编语言推测在每一个阶段需要输入的内容,输入正确就可以进入下一个阶段. 理论上每个人获取到的lab都是不一样的,但对于自学学生而言在官网http://csapp.cs

ubuntu12.04 安装CS:APP Y86模拟器

下的第一UBUNTU12.04下Y86模拟器的安装:(參考http://archive.cnblogs.com/a/1865627/ 作适当改动) 1.安装bison和flex词法分析工具 sudo apt-get install bison flex 2.下载sim解压.地址http://csapp.cs.cmu.edu/public/students.html Chapter 4: Processor Architecture Y86 tools and documentation Sour

cs:app 第二章homework

主要完成mod 4==0的题目,有错误望指正 2.55 这里show_bytes.c文件可以在官网csapp.cs.cmu.edu/3e/code.html下载. 这里需要学习的是Linux环境下c程序的编写:写好xxx.c文件,然后运行gcc xxx.c,最后会发现多出一个a.out文件,然后在命令行输入./a.out即可看到编译运行的结果了.

CS:APP CH01.A Tour of Computer Systems

程序语言到机器指令的过程 1.hello.c 源程序一个文本文件 经过预处理(pre-processer)成为 hello.i 2.hello.i 修改的源程序 经过编译处理(compiler) 成为 hello.s 3.hello.s 汇编程序 经过汇编器(assembler) 成为 hello.o 4.hello.o 一个二进制文件+printf.o 经过链接器(linker) 成为可执行文件(executable) hello 原文地址:https://www.cnblogs.com/Ge

深入理解计算机系统 (CS:APP) - 高速缓存实验 Cache Lab 解析

原文地址:https://billc.io/2019/05/csapp-cachelab/ 这个实验是这学期的第四个实验.作为缓存这一章的配套实验,设计得非常精妙.难度上来讲,相比之前的修改现成文件,直接写一个程序也更高了一些.需要注意的是检查程序在编译时开启了 -Werror,需要保证没有警告才能成功编译. 从官方文档得知需要完善 csim.c 和 trans.c 文件,第一个是模拟一个高速缓存的程序并从由 valgrind 程序生成的 trace 文件中统计 hit, miss 和 evic

六星经典CSAPP笔记(2)信息的操作和表示

2.Representing and Manipulating Information 本章从二进制.字长.字节序,一直讲到布尔代数.位运算,最后无符号.有符号整数.浮点数的表示和运算.诚然有些地方的数学证明有些枯燥,但总体上看,本章还是干货十足的! 2.1 Decimal vs. Binary Notation 我们习惯十进制只是因为我们有十根手指头(?),所以会对二进制感到不习惯.但是二值信号(two-value signal)在表示.存储.传输方面有巨大优势,从打孔带上的有没有孔洞(代码的