深入理解计算机系统第二版家庭作业答案

合集下载

深入理解计算机系统习题答案

深入理解计算机系统习题答案

1
2
10 11 12 13 14
CHAPTER 1. SOLUTIONS TO HOMEWORK PROBLEMS
void show_double(double x) { show_bytes((byte_pointer) &x, sizeof(double)); } code/data/show-ans.c
Homework Problems. These are found at the end of each chapter. They vary in complexity from simple drills to multi-week labs and are designed for instructors to give as assignments or to use as recitation examples.
Problem 2.40 Solution: This exercise should be a straightforward variation on the existing code.
code/data/show-ans.c
1 2 3 4 5 6 7 8 9
void show_short(short int x) { show_bytes((byte_pointer) &x, sizeof(short int)); } void show_long(long int x) { show_bytes((byte_pointer) &x, sizeof(long)); }
code/data/show-ans.c
1 2 3 4 5 6 7 8
int is_little_endian(void) { /* MSB = 0, LSB = 1 */ int x = 1; /* Return MSB when big-endian, LSB when little-endian */ return (int) (* (char *) &x); } code/data/show-ans.c

计算机操作系统第二版答案

计算机操作系统第二版答案

习题一1.什么是操作系统?它的主要功能是什么?答:操作系统是用来管理计算机系统的软、硬件资源,合理地组织计算机的工作流程,以方便用户使用的程序集合;其主要功能有进程管理、存储器管理、设备管理和文件管理功能。

2.什么是多道程序设计技术?多道程序设计技术的主要特点是什么?答:多道程序设计技术是把多个程序同时放入内存,使它们共享系统中的资源;特点:(1)多道,即计算机内存中同时存放多道相互独立的程序;(2)宏观上并行,是指同时进入系统的多道程序都处于运行过程中;(3)微观上串行,是指在单处理机环境下,内存中的多道程序轮流占有CPU,交替执行。

3.批处理系统是怎样的一种操作系统?它的特点是什么?答:批处理操作系统是一种基本的操作系统类型。

在该系统中,用户的作业(包括程序、数据及程序的处理步骤)被成批的输入到计算机中,然后在操作系统的控制下,用户的作业自动地执行;特点是:资源利用率高、系统吞吐量大、平均周转时间长、无交互能力。

4.什么是分时系统?什么是实时系统?试从交互性、及时性、独立性、多路性和可靠性几个方面比较分时系统和实时系统。

答:分时系统:一个计算机和许多终端设备连接,每个用户可以通过终端向计算机发出指令,请求完成某项工作,在这样的系统中,用户感觉不到其他用户的存在,好像独占计算机一样。

实时系统:对外部输入的信息,实时系统能够在规定的时间内处理完毕并作出反应。

比较:(1)交互性:实时系统具有交互性,但人与系统的交互,仅限于访问系统中某些特定的专用服务程序。

它不像分时系统那样向终端用户提供数据处理、资源共享等服务。

实时系统的交互性要求系统具有连续人机对话的能力,也就是说,在交互的过程中要对用户得输入有一定的记忆和进一步的推断的能力。

(2)及时性:实时系统对及时性的要求与分时系统类似,都以人们能够接受的等待时间来确定。

而及时系统则对及时性要求更高。

(3)独立性:实时系统与分时系统一样具有独立性。

每个终端用户提出请求时,是彼此独立的工作、互不干扰。

深入理解计算机系统配套练习卷

深入理解计算机系统配套练习卷

第一章单选题1.2.0_60_1 hello.c在( b )阶段在编译过程中生成hello.o A. 预处理阶段B.编译阶段C.汇编阶段D.链接阶段1.2.0_60_2 hello.c在编译过程中通过编译器(ccl)翻译为文本文件( b ) A. hello.o B. hello.s C. hello.i D. hello.c 1.4.1_60_3 处理器执行程序时,用( a )来存放程序和程序处理的数据A. 主存B. 寄存器C. 磁盘D. 网盘1.4.1_60_4 处理器在指令的要求下将一个字节从主存复制到寄存器的操作是( b ) A. 存储B. 加载C. 操作D. 跳转1.4.1_60_5 利用( b )技术,数据可以不通过处理器直接从磁盘到达主存。

A. 随机存储器B. 高速缓存存储器C. 物理存储器D. 直接存储器1.4.1_60_6 代码和数据被加载到( d )处理器就开始执行main程序中的机器语言指令。

A. 寄存器B. 主存C. 磁盘D. 程序计数器1.6.0_60_7 位于存储器层次结构中的最顶部的是( a )。

A. 寄存器B. 主存C. 磁盘D. 高速缓存1.7.1_60_8 下面( a )是对处理器、主存和I/O设备的抽象表示。

A. 进程B. 虚拟存储器C. 文件D. 虚拟机1.7.3_60_9 当系统从主存将一串字节复制到网络适配器时,数据流经过网络到达( c )。

A. 本地寄存器B. 另一台主机C. 本地磁盘D. 网盘1.8.0_60_10 当调用malloc这样的C标准库函数时,( a )可以在运行时动态的扩展和收缩。

A. 栈B. 堆C. 共享库D. 内核虚拟存储器第二章单选题2.1.2_60_1(a)是计算机中表示信息的最小单位。

A)位 B)字 C)字节 D)字长2.1.4_60_2计算机在内存中存储数据时使用了大、小端模式,请分别写出A=0X123456在不模式。

同情况下的首字节是大端模式?小端模式? x86结构的计算机使用( a)模式。

计算机系统结构(第2版(课后习题答案

计算机系统结构(第2版(课后习题答案

word 文档下载后可自由复制编辑你计算机系统结构清华第 2 版习题解答word 文档下载后可自由复制编辑1 目录1.1 第一章(P33)1.7-1.9 (透明性概念),1.12-1.18 (Amdahl定律),1.19、1.21 、1.24 (CPI/MIPS)1.2 第二章(P124)2.3 、2.5 、2.6 (浮点数性能),2.13 、2.15 (指令编码)1.3 第三章(P202)3.3 (存储层次性能), 3.5 (并行主存系统),3.15-3.15 加 1 题(堆栈模拟),3.19 中(3)(4)(6)(8)问(地址映象/ 替换算法-- 实存状况图)word 文档下载后可自由复制编辑1.4 第四章(P250)4.5 (中断屏蔽字表/中断过程示意图),4.8 (通道流量计算/通道时间图)1.5 第五章(P343)5.9 (流水线性能/ 时空图),5.15 (2种调度算法)1.6 第六章(P391)6.6 (向量流水时间计算),6.10 (Amdahl定律/MFLOPS)1.7 第七章(P446)7.3 、7.29(互连函数计算),7.6-7.14 (互连网性质),7.4 、7.5 、7.26(多级网寻径算法),word 文档下载后可自由复制编辑7.27 (寻径/ 选播算法)1.8 第八章(P498)8.12 ( SISD/SIMD 算法)1.9 第九章(P562)9.18 ( SISD/多功能部件/SIMD/MIMD 算法)(注:每章可选1-2 个主要知识点,每个知识点可只选 1 题。

有下划线者为推荐的主要知识点。

)word 文档 下载后可自由复制编辑2 例 , 习题2.1 第一章 (P33)例 1.1,p10假设将某系统的某一部件的处理速度加快到 10倍 ,但该部件的原处理时间仅为整个运行时间的40%,则采用加快措施后能使整个系统的性能提高多少?解:由题意可知: Fe=0.4, Se=10,根据 Amdahl 定律S n To T n1 (1Fe )S n 1 10.6 0.4100.64 Fe Se 1.56word 文档 下载后可自由复制编辑例 1.2,p10采用哪种实现技术来求浮点数平方根 FPSQR 的操作对系统的性能影响较大。

深入理解计算机系统(第二版)家庭作业问题详解

深入理解计算机系统(第二版)家庭作业问题详解

int saturating_add(int x, int y){int w = sizeof(int)<<3;int t = x + y;int ans = x + y;x>>=(w-1);y>>=(w-1);t>>=(w-1);int pos_ovf = ~x&~y&t;int neg_ovf = x&y&~t;int novf = ~(pos_ovf|neg_ovf);return(pos_ovf & INT_MAX) | (novf & ans) | (neg_ovf & INT_MIN); }2.74对于有符号整数相减,溢出的规则可以总结为:t = a-b;如果a, b 同号,则肯定不会溢出。

如果a>=0 && b<0,则只有当t<=0时才算溢出。

如果a<0 && b>=0,则只有当t>=0时才算溢出。

不过,上述t肯定不会等于0,因为当a,b不同号时:1) a!=b,因此a-b不会等于0。

2) a-b <= abs(a) + abs(b) <= abs(TMax) + abs(TMin)=(2^w - 1)所以,a,b异号,t,b同号即可判定为溢出。

int tsub_ovf(int x, int y){int w = sizeof(int)<<3;int t = x - y;x>>=(w-1);y>>=(w-1);t>>=(w-1);return(x != y) && (y == t);}顺便整理一下汇编中CF,OF的设定规则(个人总结,如有不对之处,欢迎指正)。

t = a + b;CF: (unsigned t) < (unsigned a) 进位标志OF: (a<0 == b<0) && (t<0 != a<0)t = a - b;CF: (a<0 && b>=0) || ((a<0 == b<0) && t<0) 退位标志OF: (a<0 != b<0) && (b<0 == t<0)汇编中,无符号和有符号运算对条件码(标志位)的设定应该是相同的,但是对于无符号比较和有符号比较,其返回值是根据不同的标志位进行的。

计算机系统结构(第2版(课后习题答案

计算机系统结构(第2版(课后习题答案

word 文档下载后可自由复制编辑你计算机系统结构清华第 2 版习题解答word 文档下载后可自由复制编辑1 目录1.1 第一章(P33)1.7-1.9 (透明性概念),1.12-1.18 (Amdahl定律),1.19、1.21 、1.24 (CPI/MIPS)1.2 第二章(P124)2.3 、2.5 、2.6 (浮点数性能),2.13 、2.15 (指令编码)1.3 第三章(P202)3.3 (存储层次性能), 3.5 (并行主存系统),3.15-3.15 加 1 题(堆栈模拟),3.19 中(3)(4)(6)(8)问(地址映象/ 替换算法-- 实存状况图)word 文档下载后可自由复制编辑1.4 第四章(P250)4.5 (中断屏蔽字表/中断过程示意图),4.8 (通道流量计算/通道时间图)1.5 第五章(P343)5.9 (流水线性能/ 时空图),5.15 (2种调度算法)1.6 第六章(P391)6.6 (向量流水时间计算),6.10 (Amdahl定律/MFLOPS)1.7 第七章(P446)7.3 、7.29(互连函数计算),7.6-7.14 (互连网性质),7.4 、7.5 、7.26(多级网寻径算法),word 文档下载后可自由复制编辑7.27 (寻径/ 选播算法)1.8 第八章(P498)8.12 ( SISD/SIMD 算法)1.9 第九章(P562)9.18 ( SISD/多功能部件/SIMD/MIMD 算法)(注:每章可选1-2 个主要知识点,每个知识点可只选 1 题。

有下划线者为推荐的主要知识点。

)word 文档 下载后可自由复制编辑2 例 , 习题2.1 第一章 (P33)例 1.1,p10假设将某系统的某一部件的处理速度加快到 10倍 ,但该部件的原处理时间仅为整个运行时间的40%,则采用加快措施后能使整个系统的性能提高多少?解:由题意可知: Fe=0.4, Se=10,根据 Amdahl 定律S n To T n1 (1Fe )S n 1 10.6 0.4100.64 Fe Se 1.56word 文档 下载后可自由复制编辑例 1.2,p10采用哪种实现技术来求浮点数平方根 FPSQR 的操作对系统的性能影响较大。

计算机操作系统第二版答案

计算机操作系统第二版答案

计算机操作系统第二版答案习题一1. 1. 什么是操作系统?它的主要功能是什么?什么是操作系统?它的主要功能是什么?答:操作系统是用来管理计算机系统的软、硬件资源,合理地组织计算机的工作流程,以方便用户使用的程序集合;其主要功能有进程管理、存储器管理、设备管理和文件管理功能。

管理功能。

2. 2. 2. 什么是多道程序设计技术?多道程序设计技什么是多道程序设计技术?多道程序设计技术的主要特点是什么?答:多道程序设计技术是把多个程序同时放入内存,使它们共享系统中的资源;特点:多道,即计算机内存中同时存放多道相互独立的程序;宏观上并行,是指同时进入系统的多道程序都处于运行过程中;微观上串行,是指在单处理机环境下,内存中的多道程序轮流占有CPU CPU,交替执行。

,交替执行。

3. 3. 批处理系统是怎样的一种操作系统?它的特点是什批处理系统是怎样的一种操作系统?它的特点是什么?答:批处理操作系统是一种基本的操作系统类型。

在该系统中,用户的作业被成批的输入到计算机中,然后在操作系统的控制下,用户的作业自动地执行;特点是:资源利用率高、系统吞吐量大、平均周转时间长、无交互能力。

长、无交互能力。

4. 4. 4. 什么是分时系统?什么是实时系统?什么是分时系统?什么是实时系统?试从交互性、及时性、独立性、多路性和可靠性几个方面比较分时系统和实时系统。

较分时系统和实时系统。

答:分时系统:一个计算机和许多终端设备连接,每个 答:分时系统:一个计算机和许多终端设备连接,每个用户可以通过终端向计算机发出指令,请求完成某项工作,在这样的系统中,用户感觉不到其他用户的存在,好像独占计算机一样。

计算机一样。

实时系统:对外部输入的信息,实时系统能够在规定的 实时系统:对外部输入的信息,实时系统能够在规定的时间内处理完毕并作出反应。

比较:交互性:实时系统具时间内处理完毕并作出反应。

有交互性,但人与系统的交互,仅限于访问系统中某些特定的专用服务程序。

深入理解计算机系统(第二版)家庭作业答案

深入理解计算机系统(第二版)家庭作业答案

深⼊理解计算机系统(第⼆版)家庭作业答案2.74对于有符号整数相减,溢出的规则可以总结为:t = a-b;如果a, b 同号,则肯定不会溢出。

如果a>=0 && b<0,则只有当t<=0时才算溢出。

如果a<0 && b>=0,则只有当t>=0时才算溢出。

不过,上述t肯定不会等于0,因为当a,b不同号时:1) a!=b,因此a-b不会等于0。

2) a-b <= abs(a) + abs(b) <= abs(TMax) + abs(TMin)=(2^w - 1)所以,a,b异号,t,b同号即可判定为溢出。

int tsub_ovf(int x, int y){int w = sizeof(int)<<3;int t = x - y;x>>=(w-1);y>>=(w-1);t>>=(w-1);return (x != y) && (y == t);}顺便整理⼀下汇编中CF,OF的设定规则(个⼈总结,如有不对之处,欢迎指正)。

t = a + b;CF: (unsigned t) < (unsigned a) 进位标志OF: (a<0 == b<0) && (t<0 != a<0)t = a - b;CF: (a<0 && b>=0) || ((a<0 == b<0) && t<0) 退位标志OF: (a<0 != b<0) && (b<0 == t<0)汇编中,⽆符号和有符号运算对条件码(标志位)的设定应该是相同的,但是对于⽆符号⽐较和有符号⽐较,其返回值是根据不同的标志位进⾏的。

详情可以参考第三章3.6.2节。

2.75根据2-18,不难推导, (x'*y')_h = (x*y)_h + x(w-1)*y + y(w-1)*x。

第1章 计算机系统概论第二版课后习题详细讲解

第1章 计算机系统概论第二版课后习题详细讲解

第1章计算机系统概论1. 什么是计算机系统、计算机硬件和计算机软件?硬件和软件哪个更重要?解:P3计算机系统:由计算机硬件系统和软件系统组成的综合体。

计算机硬件:指计算机中的电子线路和物理装置。

计算机软件:计算机运行所需的程序及相关资料。

硬件和软件在计算机系统中相互依存,缺一不可,因此同样重要。

2. 如何理解计算机的层次结构?答:计算机硬件、系统软件和应用软件构成了计算机系统的三个层次结构。

(1)硬件系统是最内层的,它是整个计算机系统的基础和核心。

(2)系统软件在硬件之外,为用户提供一个基本操作界面。

(3)应用软件在最外层,为用户提供解决具体问题的应用系统界面。

通常将硬件系统之外的其余层称为虚拟机。

各层次之间关系密切,上层是下层的扩展,下层是上层的基础,各层次的划分不是绝对的。

3. 说明高级语言、汇编语言和机器语言的差别及其联系。

答:机器语言是计算机硬件能够直接识别的语言,汇编语言是机器语言的符号表示,高级语言是面向算法的语言。

高级语言编写的程序(源程序)处于最高层,必须翻译成汇编语言,再由汇编程序汇编成机器语言(目标程序)之后才能被执行。

4. 如何理解计算机组成和计算机体系结构?答:计算机体系结构是指那些能够被程序员所见到的计算机系统的属性,如指令系统、数据类型、寻址技术组成及I/O 机理等。

计算机组成是指如何实现计算机体系结构所体现的属性,包含对程序员透明的硬件细节,如组成计算机系统的各个功能部件的结构和功能,及相互连接方法等。

5. 冯•诺依曼计算机的特点是什么?解:冯•诺依曼计算机的特点是:P8●计算机由运算器、控制器、存储器、输入设备、输出设备五大部件组成;●指令和数据以同同等地位存放于存储器内,并可以按地址访问;●指令和数据均用二进制表示;●指令由操作码、地址码两大部分组成,操作码用来表示操作的性质,地址码用来表示操作数在存储器中的位置;●指令在存储器中顺序存放,通常自动顺序取出执行;●机器以运算器为中心(原始冯•诺依曼机)。

实用文档之计算机组成原理第二版课后习题答案

实用文档之计算机组成原理第二版课后习题答案

实用文档之"第1章计算机系统概论"1. 什么是计算机系统、计算机硬件和计算机软件?硬件和软件哪个更重要?解:计算机系统:由计算机硬件系统和软件系统组成的综合体。

计算机硬件:指计算机中的电子线路和物理装置。

计算机软件:计算机运行所需的程序及相关资料。

硬件和软件在计算机系统中相互依存,缺一不可,因此同样重要。

2. 如何理解计算机的层次结构?答:计算机硬件、系统软件和应用软件构成了计算机系统的三个层次结构。

(1)硬件系统是最内层的,它是整个计算机系统的基础和核心。

(2)系统软件在硬件之外,为用户提供一个基本操作界面。

(3)应用软件在最外层,为用户提供解决具体问题的应用系统界面。

通常将硬件系统之外的其余层称为虚拟机。

各层次之间关系密切,上层是下层的扩展,下层是上层的基础,各层次的划分不是绝对的。

3. 说明高级语言、汇编语言和机器语言的差别及其联系。

答:机器语言是计算机硬件能够直接识别的语言,汇编语言是机器语言的符号表示,高级语言是面向算法的语言。

高级语言编写的程序(源程序)处于最高层,必须翻译成汇编语言,再由汇编程序汇编成机器语言(目标程序)之后才能被执行。

4. 如何理解计算机组成和计算机体系结构?答:计算机体系结构是指那些能够被程序员所见到的计算机系统的属性,如指令系统、数据类型、寻址技术组成及I/O 机理等。

计算机组成是指如何实现计算机体系结构所体现的属性,包含对程序员透明的硬件细节,如组成计算机系统的各个功能部件的结构和功能,及相互连接方法等。

5. 冯•诺依曼计算机的特点是什么?解:冯•诺依曼计算机的特点是:P8●计算机由运算器、控制器、存储器、输入设备、输出设备五大部件组成;●指令和数据以同同等地位存放于存储器内,并可以按地址访问;●指令和数据均用二进制表示;●指令由操作码、地址码两大部分组成,操作码用来表示操作的性质,地址码用来表示操作数在存储器中的位置;●指令在存储器中顺序存放,通常自动顺序取出执行;●机器以运算器为中心(原始冯•诺依曼机)。

深入理解计算机系统答案

深入理解计算机系统答案

深入理解计算机系统答案【篇一:深入理解计算机系统笔记】(1)对于一个无符号数字 x, 截断它到 k 位的结果就相当于计算 x mod 2^k.(2)在大多数的机器上 ,整数乘法指令相当地慢 ,需要 12 或者更多的始终周期 ,然而其他整数运算-例如加法、减法、位移运算和移位-只需要 1 个时钟周期 .因此 ,编译器使用的一项重要的优化就是试着使用移位和加法运算的组合来代替乘以常数因子的乘法.(3)在大多数的机器上 ,整数除法要比整数乘法更慢-需要 30 或者更多的始终周期 .除以 2 的幂也可以用移位运算来实现 ,只不过我们用的是右移 ,而不是左移 .对于无符号和二进制补码数 ,分别使用逻辑移位和算术移位来达到目的 .1. 注意系统的分类:主流的 ia32( 也就是 x86) ,以及 x86-64( 也就是x64) ,还有种 intel 的与原 32 位系统不兼容的 ia64 。

2. 编译系统由预处理器,编译器,汇编器和链接器组成。

3.单指令多数据并行称为 simd 并行,其扩展为 sse 指令集。

4.x64 上 long 为 8 字节,指针也为 8 字节。

5.无符号数右移必须采用逻辑右移,而有符号数一般采用算术右移。

6.有符号数遇见无符号数会默认强转为无符号数。

7.short 转为 unsigned 时,是先扩展大小再符号转换。

8. 补码非的计算:从左到右将第一个为 1 的位前的所有位取反。

9.负数的补码移位向下舍入。

10.正浮点数能使用整数排序函数来进行排序。

11.浮点加法和乘法不具备结合性,浮点乘法在加法上不具备分配性。

12.预处理器扩展源代码,然后编译器生成源代码的文本汇编代码,汇编器转成二进制汇编码,链接器生成exe 或 dll 或 lib 。

13.寄存器可以保存地址也可以保存值。

注意汇编中的加括号表示为取该地址指向的值,如 (%eax) 指 %eax 中保存的地址指向的值。

14.传送指令的两个操作符不能都指向存储器。

深入理解计算机系统答案(超高清电子版)

深入理解计算机系统答案(超高清电子版)

深⼊理解计算机系统答案(超⾼清电⼦版)Computer Systems:A Programmer’s Perspective Instructor’s Solution Manual1Randal E.BryantDavid R.O’HallaronDecember4,20032Chapter1Solutions to Homework ProblemsThe text uses two different kinds of exercises:Practice Problems.These are problems that are incorporated directly into the text,with explanatory solutions at the end of each chapter.Our intention is that students will work on these problems as they read the book.Each one highlights some particular concept.Homework Problems.These are found at the end of each chapter.They vary in complexity from simple drills to multi-week labs and are designed for instructors to give as assignments or to use as recitation examples.This document gives the solutions to the homework problems.1.1Chapter1:A Tour of Computer Systems1.2Chapter2:Representing and Manipulating InformationProblem2.40Solution:This exercise should be a straightforward variation on the existing code.2CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS1011void show_double(double x)12{13show_bytes((byte_pointer)&x,sizeof(double));14}code/data/show-ans.c 1int is_little_endian(void)3/*MSB=0,LSB=1*/4int x=1;56/*Return MSB when big-endian,LSB when little-endian*/7return(int)(*(char*)&x);8}1.2.CHAPTER2:REPRESENTING AND MANIPULATING INFORMATION3 There are many solutions to this problem,but it is a little bit tricky to write one that works for any word size.Here is our solution:code/data/shift-ans.c The above code peforms a right shift of a word in which all bits are set to1.If the shift is arithmetic,the resulting word will still have all bits set to1.Problem2.45Solution:This problem illustrates some of the challenges of writing portable code.The fact that1<<32yields0on some32-bit machines and1on others is common source of bugs.A.The C standard does not de?ne the effect of a shift by32of a32-bit datum.On the SPARC(andmany other machines),the expression x</doc/dde1f034f111f18583d05a59.html pute beyond_msb as2<<31.C.We cannot shift by more than15bits at a time,but we can compose multiple shifts to get thedesired effect.Thus,we can compute set_msb as2<<15<<15,and beyond_msb as set_msb<<1.Problem2.46Solution:This problem highlights the difference between zero extension and sign extension.It also provides an excuse to show an interesting trick that compilers often use to use shifting to perform masking and sign extension.A.The function does not perform any sign extension.For example,if we attempt to extract byte0fromword0xFF,we will get255,rather than.B.The following code uses a well-known trick for using shifts to isolate a particular range of bits and toperform sign extension at the same time.First,we perform a left shift so that the most signi?cant bit of the desired byte is at bit position31.Then we right shift by24,moving the byte into the proper position and peforming sign extension at the same time. 4CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS 3int left=word<<((3-bytenum)<<3);4return left>>24;5}Problem2.48Solution:This problem lets students rework the proof that complement plus increment performs negation.We make use of the property that two’s complement addition is associative,commutative,and has additive/doc/dde1f034f111f18583d05a59.html ing C notation,if we de?ne y to be x-1,then we have?y+1equal to-y,and hence?y equals -y+1.Substituting gives the expression-(x-1)+1,which equals-x.Problem2.49Solution:This problem requires a fairly deep understanding of two’s complement arithmetic.Some machines only provide one form of multiplication,and hence the trick shown in the code here is actually required to perform that actual form.As seen in Equation2.16we have.The?nal term has no effect on the-bit representation of,but the middle term represents acode/data/uhp-ans.c Problem2.50Solution:1.2.CHAPTER2:REPRESENTING AND MANIPULATING INFORMATION5A.:x+(x<<2)B.:x+(x<<3)C.:(x<<4)-(x<<1)D.:(x<<3)-(x<<6)Problem2.51Solution:Bit patterns similar to these arise in many applications.Many programmers provide them directly in hex-adecimal,but it would be better if they could express them in more abstract ways.A..((1<B..((1<Problem2.52Solution:Byte extraction and insertion code is useful in many contexts.Being able to write this sort of code is an important skill to foster.code/data/rbyte-ans.c Problem2.53Solution:These problems are fairly tricky.They require generating masks based on the shift amounts.Shift value k equal to0must be handled as a special case,since otherwise we would be generating the mask by performing a left shift by32.6CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS 1unsigned srl(unsigned x,int k)2{3/*Perform shift arithmetically*/4unsigned xsra=(int)x>>k;5/*Make mask of low order32-k bits*/6unsigned mask=k?((1<<(32-k))-1):?0;78return xsra&mask;9}code/data/rshift-ans.c 1int sra(int x,int k)2{3/*Perform shift logically*/4int xsrl=(unsigned)x>>k;5/*Make mask of high order k bits*/6unsigned mask=k??((1<<(32-k))-1):0;78return(x<0)?mask|xsrl:xsrl;1.2.CHAPTER2:REPRESENTING AND MANIPULATING INFORMATION7B.(a)For,we have,,code/data/?oatge-ans.c 1int float_ge(float x,float y)2{3unsigned ux=f2u(x);4unsigned uy=f2u(y);5unsigned sx=ux>>31;6unsigned sy=uy>>31;78return9(ux<<1==0&&uy<<1==0)||/*Both are zero*/10(!sx&&sy)||/*x>=0,y<0*/11(!sx&&!sy&&ux>=uy)||/*x>=0,y>=0*/12(sx&&sy&&ux<=uy);/*x<0,y<0*/13},8CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS This exercise is of practical value,since Intel-compatible processors perform all of their arithmetic in ex-tended precision.It is interesting to see how adding a few more bits to the exponent greatly increases the range of values that can be represented.Description Extended precisionValueSmallest denorm.Largest norm.Problem2.59Solution:We have found that working through?oating point representations for small word sizes is very instructive. Problems such as this one help make the description of IEEE?oating point more concrete.Description8000Smallest value4700Largest denormalized———1.3.CHAPTER3:MACHINE LEVEL REPRESENTATION OF C PROGRAMS91/*Compute2**x*/2float fpwr2(int x){4unsigned exp,sig;5unsigned u;67if(x<-149){8/*Too small.Return0.0*/9exp=0;10sig=0;11}else if(x<-126){12/*Denormalized result*/13exp=0;14sig=1<<(x+149);15}else if(x<128){16/*Normalized result.*/17exp=x+127;18sig=0;19}else{20/*Too big.Return+oo*/21exp=255;22sig=0;23}24u=exp<<23|sig;25return u2f(u);26}10CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS int decode2(int x,int y,int z){int t1=y-z;int t2=x*t1;int t3=(t1<<31)>>31;int t4=t3?t2;return t4;}Problem3.32Solution:This code example demonstrates one of the pedagogical challenges of using a compiler to generate assembly code examples.Seemingly insigni?cant changes in the C code can yield very different results.Of course, students will have to contend with this property as work with machine-generated assembly code anyhow. They will need to be able to decipher many different code patterns.This problem encourages them to think in abstract terms about one such pattern.1movl8(%ebp),%edx x2movl12(%ebp),%ecx y3movl%edx,%eax4subl%ecx,%eax result=x-y5cmpl%ecx,%edx Compare x:y6jge.L3if>=goto done:7movl%ecx,%eax8subl%edx,%eax result=y-x9.L3:done:A.When,it will compute?rst and then.When it just computes.B.The code for then-statement gets executed unconditionally.It then jumps over the code for else-statement if the test is false.C.then-statementt=test-expr;if(t)goto done;else-statementdone:D.The code in then-statement must not have any side effects,other than to set variables that are also set1.3.CHAPTER3:MACHINE LEVEL REPRESENTATION OF C PROGRAMS11Problem3.33Solution:This problem requires students to reason about the code fragments that implement the different branches of a switch statement.For this code,it also requires understanding different forms of pointer dereferencing.A.In line29,register%edx is copied to register%eax as the return value.From this,we can infer that%edx holds result.B.The original C code for the function is as follows:1/*Enumerated type creates set of constants numbered0and upward*/2typedef enum{MODE_A,MODE_B,MODE_C,MODE_D,MODE_E}mode_t;34int switch3(int*p1,int*p2,mode_t action)5{6int result=0;7switch(action){8case MODE_A:12case MODE_B:13*p2+=*p1;14result=*p2;15break;16case MODE_C:17*p2=15;18result=*p1;19break;20case MODE_D:21*p2=*p1;22/*Fall Through*/23case MODE_E:24result=17;25break;26default:27result=-1;28}29return result;30}Problem3.34Solution:This problem gives students practice analyzing disassembled code.The switch statement contains all the features one can imagine—cases with multiple labels,holes in the range of possible case values,and cases that fall through.12CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS 1int switch_prob(int x)2{3int result=x;45switch(x){6case50:7case52:8result<<=2;9break;10case53:11result>>=2;15/*Fall through*/16case55:17result*=result;18/*Fall through*/19default:20result+=10;21}2223return result;24}code/asm/varprod-ans.c 1int var_prod_ele_opt(var_matrix A,var_matrix B,int i,int k,int n) 2{3int*Aptr=&A[i*n];4int*Bptr=&B[k];5int result=0;6int cnt=n;78if(n<=0)9return result;1011do{12result+=(*Aptr)*(*Bptr);13Aptr+=1;14Bptr+=n;1.3.CHAPTER3:MACHINE LEVEL REPRESENTATION OF C PROGRAMS13 16}while(cnt); 1718return result;19}code/asm/structprob-ans.c 1typedef struct{2int idx;3int x[4];4}a_struct;14CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS 1/*Read input line and write it back*/ 2/*Code will work for any buffer size.Bigger is more time-efficient*/ 3#define BUFSIZE644void good_echo()5{6char buf[BUFSIZE];7int i;8while(1){9if(!fgets(buf,BUFSIZE,stdin))10return;/*End of file or error*/11/*Print characters in buffer*/12for(i=0;buf[i]&&buf[i]!=’\n’;i++)13if(putchar(buf[i])==EOF)14return;/*Error*/15if(buf[i]==’\n’){16/*Reached terminating newline*/17putchar(’\n’);18return;19}20}21}An alternative implementation is to use getchar to read the characters one at a time.Problem3.38Solution:Successfully mounting a buffer over?ow attack requires understanding many aspects of machine-level pro-grams.It is quite intriguing that by supplying a string to one function,we can alter the behavior of another function that should always return a? xed value.In assigning this problem,you should also give students a stern lecture about ethical computing practices and dispell any notion that hacking into systems is a desirable or even acceptable thing to do.Our solution starts by disassembling bufbomb,giving the following code for getbuf: 1080484f4:280484f4:55push%ebp380484f5:89e5mov%esp,%ebp480484f7:83ec18sub$0x18,%esp580484fa:83c4f4add$0xfffffff4,%esp680484fd:8d45f4lea0xfffffff4(%ebp),%eax78048500:50push%eax88048501:e86a ff ff ff call804847098048506:b801000000mov$0x1,%eax10804850b:89ec mov%ebp,%esp11804850d:5d pop%ebp12804850e:c3retWe can see on line6that the address of buf is12bytes below the saved value of%ebp,which is4bytes1.3.CHAPTER3:MACHINE LEVEL REPRESENTATION OF C PROGRAMS15 of%ebp,and the address of the start of the buffer.To determine the relevant values,we run GDB as follows:1.First,we set a breakpoint in getbuf and run the program to that point:(gdb)break getbuf(gdb)runComparing the stopping point to the disassembly,we see that it has already set up the stack frame.2.We get the value of buf by computing a value relative to%ebp:(gdb)print/x(%ebp+12)This gives0xbfffefbc.3.We?nd the saved value of register%ebp by dereferencing the current value of this register:(gdb)print/x*$ebpThis gives0xbfffefe8.4.We?nd the value of the return pointer on the stack,at offset4relative to%ebp:(gdb)print/x*((int*)$ebp+1)This gives0x8048528We can now put this information together to generate assembly code for our attack:1pushl$0x8048528Put correct return pointer back on stack2movl$0xdeadbeef,%eax Alter return value3ret Re-execute return4.align4Round up to125.long0xbfffefe8Saved value of%ebp6.long0xbfffefbc Location of buf7.long0x00000000PaddingNote that we have used the.align statement to get the assembler to insert enough extra bytes to use up twelve bytes for the code.We added an extra4bytes of0s at the end,because in some cases OBJDUMP would not generate the complete byte pattern for the data.These extra bytes(plus the termininating null byte)will over?ow into the stack frame for test,but they will not affect the program behavior. Assembling this code and disassembling the object code gives us the following:10:6828850408push$0x804852825:b8ef be ad de mov$0xdeadbeef,%eax3a:c3ret4b:90nop Byte inserted for alignment.5c:e8ef ff bf bc call0xbcc00000Invalid disassembly.611:ef out%eax,(%dx)Trying to diassemble712:ff(bad)data16CHAPTER1.SOLUTIONS TO HOMEWORK PROBLEMS From this we can read off the byte sequence:Problem3.39Solution:This problem is a variant on the asm examples in the text.The code is actually fairly simple.It relies on the fact that asm outputs can be arbitrary lvalues,and hence we can use dest[0]and dest[1]directly in the output list.code/asm/asmprobs-ans.c Problem3.40Solution:For this example,students essentially have to write the entire function in assembly.There is no(apparent) way to interface between the?oating point registers and the C code using extended asm.code/asm/fscale.c1.4.CHAPTER4:PROCESSOR ARCHITECTURE17 1.4Chapter4:Processor ArchitectureProblem4.32Solution:This problem makes students carefully examine the tables showing the computation stages for the different instructions.The steps for iaddl are a hybrid of those for irmovl and OPl.StageFetchrA:rB M PCvalP PCExecuteR rB valEPC updateleaveicode:ifun M PCDecodevalB RvalE valBMemoryWrite backR valMPC valPProblem4.34Solution:The following HCL code includes implementations of both the iaddl instruction and the leave instruc-tions.The implementations are fairly straightforward given the computation steps listed in the solutions to problems4.32and4.33.You can test the solutions using the test code in the ptest subdirectory.Make sure you use command line argument‘-i.’。

计算机组成原理第二版课后习题问题详解

计算机组成原理第二版课后习题问题详解

第1章计算机系统概论1. 什么是计算机系统、计算机硬件和计算机软件?硬件和软件哪个更重要?解:计算机系统:由计算机硬件系统和软件系统组成的综合体。

计算机硬件:指计算机中的电子线路和物理装置。

计算机软件:计算机运行所需的程序及相关资料。

硬件和软件在计算机系统中相互依存,缺一不可,因此同样重要。

2. 如何理解计算机的层次结构?答:计算机硬件、系统软件和应用软件构成了计算机系统的三个层次结构。

(1)硬件系统是最层的,它是整个计算机系统的基础和核心。

(2)系统软件在硬件之外,为用户提供一个基本操作界面。

(3)应用软件在最外层,为用户提供解决具体问题的应用系统界面。

通常将硬件系统之外的其余层称为虚拟机。

各层次之间关系密切,上层是下层的扩展,下层是上层的基础,各层次的划分不是绝对的。

3. 说明高级语言、汇编语言和机器语言的差别及其联系。

答:机器语言是计算机硬件能够直接识别的语言,汇编语言是机器语言的符号表示,高级语言是面向算法的语言。

高级语言编写的程序(源程序)处于最高层,必须翻译成汇编语言,再由汇编程序汇编成机器语言(目标程序)之后才能被执行。

4. 如何理解计算机组成和计算机体系结构?答:计算机体系结构是指那些能够被程序员所见到的计算机系统的属性,如指令系统、数据类型、寻址技术组成及I/O机理等。

计算机组成是指如何实现计算机体系结构所体现的属性,包含对程序员透明的硬件细节,如组成计算机系统的各个功能部件的结构和功能,及相互连接方法等。

5. •诺依曼计算机的特点是什么?解:•诺依曼计算机的特点是:P8●计算机由运算器、控制器、存储器、输入设备、输出设备五大部件组成;●指令和数据以同同等地位存放于存储器,并可以按地址访问;●指令和数据均用二进制表示;●指令由操作码、地址码两大部分组成,操作码用来表示操作的性质,地址码用来表示操作数在存储器中的位置;●指令在存储器中顺序存放,通常自动顺序取出执行;●机器以运算器为中心(原始•诺依曼机)。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

深入理解计算机系统(第二版)家庭作业第二章深入理解计算机系统二进制2.55-2.57略2.58int is_little_endian(){int a = 1;return *((char*)&a);}2.59(x&0xFF) | (y&~0xFF)2.60unsigned replace_byte(unsigned x, unsigned char b, int i){return (x & ~(0xFF<<(i<<3))) | (b << (i<<3));}2.61A. !~xB. !xC. !~(x>>((sizeof(int)-1)<<3))D. !(x&0xFF)注意,英文版中C是最低字节,D是最高字节。

中文版恰好反过来了。

这里是按中文版来做的。

2.62这里我感觉应该是英文版对的,int_shifts_are_arithmetic()int int_shifts_are_arithmetic(){int x = -1;return (x>>1) == -1;}2.63对于sra,主要的工作是将xrsl的第w-k-1位扩展到前面的高位。

这个可以利用取反加1来实现,不过这里的加1是加1<<(w-k-1)。

如果x的第w-k-1位为0,取反加1后,前面位全为0,如果为1,取反加1后就全是1。

最后再使用相应的掩码得到结果。

对于srl,注意工作就是将前面的高位清0,即xsra & (1<<(w-k) - 1)。

额外注意k==0时,不能使用1<<(w-k),于是改用2<<(w-k-1)。

int sra(int x, int k){int xsrl = (unsigned) x >> k;int w = sizeof(int) << 3;unsigned z = 1 << (w-k-1);unsigned mask = z - 1;unsigned right = mask & xsrl;unsigned left = ~mask & (~(z&xsrl) + z);return left | right;}int srl(unsigned x, int k){int xsra = (int) x >> k;int w = sizeof(int)*8;unsigned z = 2 << (w-k-1);return (z - 1) & xsra;}2.64int any_even_one(unsigned x){return !!(x & ());}2.65int even_ones(unsigned x){x ^= (x >> 16);x ^= (x >> 8);x ^= (x >> 4);x ^= (x >> 2);x ^= (x >> 1);return !(x&1);}x的每个位进行异或,如果为0就说明是偶数个1,如果为1就是奇数个1。

那么可以想到折半缩小规模。

最后一句也可以是 return (x^1)&12.66int leftmost_one(unsigned x){x |= (x >> 1);x |= (x >> 2);x |= (x >> 4);x |= (x >> 8);x |= (x >> 16);return x^(x>>1);}2.67A.32位机器上没有定义移位32次。

B.beyond_msb变为 2<<31。

C.定义 a = 1<<15; a<<=15; set_msb = a<<1; beyond_msb = a<<2;2.68感觉中文版有点问题,注释和函数有点对应不上,于是用英文版的了。

个人猜想应该是让x的最低n位变1。

int lower_one_mask(int n){return (2<<(n-1)) - 1;}2.69unsigned rotate_right(unsigned x, int n){int w = sizeof(unsigned)*8;return (x>>n) | (x<<(w-n-1)<<1);}2.70这一题是看x的值是否在 - 2^(n-1) 到 2^(n-1) - 1之间。

如果x满足这个条件,则其第n-1位就是符号位。

如果该位为0,则前面的w-n位均为0,如果该位为1,则前面的w-n位均为1。

所以本质是判断,x的高w-n+1位是否为0或者为-1。

int fits_bits(int x, int n){x >>= (n-1);return !x || !(~x);}2.71A.得到的结果是unsigned,而并非扩展为signed的结果。

B.使用int,将待抽取字节左移到最高字节,再右移到最低字节即可。

int xbyte(unsigned word, int bytenum){int ret = word << ((3 - bytenum)<<3);return ret >> 24;}2.72A.size_t是无符号整数,因此左边都会先转换为无符号整数,它肯定是大于等于0的。

B.判断条件改为if(maxbytes > 0 && maxbytes >= sizeof(val))2.73请先参考2.74题。

可知:t = a + b时,如果a,b异号(或者存在0),则肯定不会溢出。

如果a,b均大于等于0,则t<0就是正溢出,如果a,b均小于0,则t>=0就是负溢出。

于是,可以利用三个变量来表示是正溢出,负溢出还是无溢出。

int saturating_add(int x, int y){int w = sizeof(int)<<3;int t = x + y;int ans = x + y;x>>=(w-1);y>>=(w-1);t>>=(w-1);int pos_ovf = ~x&~y&t;int neg_ovf = x&y&~t;int novf = ~(pos_ovf|neg_ovf);return (pos_ovf & INT_MAX) | (novf & ans) | (neg_ovf & INT_MIN); }2.74对于有符号整数相减,溢出的规则可以总结为:t = a-b;如果a, b 同号,则肯定不会溢出。

如果a>=0 && b<0,则只有当t<=0时才算溢出。

如果a<0 && b>=0,则只有当t>=0时才算溢出。

不过,上述t肯定不会等于0,因为当a,b不同号时:1) a!=b,因此a-b不会等于0。

2) a-b <= abs(a) + abs(b) <= abs(TMax) + abs(TMin)=(2^w - 1)所以,a,b异号,t,b同号即可判定为溢出。

int tsub_ovf(int x, int y){int w = sizeof(int)<<3;int t = x - y;x>>=(w-1);y>>=(w-1);t>>=(w-1);return (x != y) && (y == t);}顺便整理一下汇编中CF,OF的设定规则(个人总结,如有不对之处,欢迎指正)。

t = a + b;CF: (unsigned t) < (unsigned a) 进位标志OF: (a<0 == b<0) && (t<0 != a<0)t = a - b;CF: (a<0 && b>=0) || ((a<0 == b<0) && t<0) 退位标志OF: (a<0 != b<0) && (b<0 == t<0)汇编中,无符号和有符号运算对条件码(标志位)的设定应该是相同的,但是对于无符号比较和有符号比较,其返回值是根据不同的标志位进行的。

2.75根据2-18,不难推导, (x'*y')_h = (x*y)_h + x(w-1)*y + y(w-1)*x。

unsigned unsigned_high_prod(unsigned x, unsigned y){int w = sizeof(int)<<3;return signed_high_prod(x, y) + (x>>(w-1))*y + x*(y>>(w-1));}当然,这里用了乘法,不属于整数位级编码规则,聪明的办法是使用int进行移位,并使用与运算。

即((int)x>>(w-1)) & y 和 ((int)y>>(w-1)) & x。

注:不使用long long来实现signed_high_prod(int x, int y)是一件比较复杂的工作,而且我不会只使用整数位级编码规则来实现,因为需要使用循环和条件判断。

下面的代码是计算两个整数相乘得到的高位和低位。

int uadd_ok(unsigned x, unsigned y){return x + y >= x;}void signed_prod_result(int x, int y, int &h, int &l){int w = sizeof(int)<<3;h = 0;l = (y&1)x:0;for(int i=1; i<w; i++){if( (y>>i)&1 ) {h += (unsigned)x>>(w-i);if(!uadd_ok(l, x<<i)) h++;l += (x<<i);}}h = h + ((x>>(w-1))*y) + ((y>>(w-1))*x);}最后一步计算之前的h即为unsigned相乘得到的高位。

相关文档
最新文档