概述
選擇題 50 分(原題率 80%):http://tieba.baidu.com/p/1250021454?&share=9105&fr=sharewise&unique=967707B1DAECEF4A785B61D29AF36950&st=1639102957&client_type=1&client_version=12.10.1&sfc=copy&share_from=post
1
程序執(zhí)行的六個過程
E d i t ? → ? P r e ? P r o c e s s ? → ? C o m p i l e ? → ? L i n k ? → ? L o a d ? → ? E x e c u t e {\rm Edit} \space \rightarrow \space {\rm Pre \space Process} \space \rightarrow \space {\rm Compile} \space \rightarrow \space {\rm Link} \space \rightarrow \space {\rm Load} \space \rightarrow \space {\rm Execute} Edit?→?Pre?Process?→?Compile?→?Link?→?Load?→?Execute
2
浮點數(shù)編碼
Fixed Point Natation
使用 . . . 分割整數(shù)部分與小數(shù)部分
- 例如: 5.7 5 10 = 101.1 1 2 5.75_{10} = 101.11_2 5.7510?=101.112?
有理數(shù)的表示: ∑ k = ? j i b k ? 2 k \displaystyle \sum^i_{k=-j} b_k \cdot2^k k=?j∑i?bk??2k
BCD - Binary Coded Decimal
每個十進制的數(shù)字使用 4 bit 的二進制數(shù)存儲
Decimal: 0 1 2 3 4 5 6 7 8 9
BCD: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
Decimal: 127.33
BCD: 0001 0010 0111 .0011 0011
IEEE Floating Point
使用 V = ( ? 1 ) s × M × 2 E V=(-1)^s \times M \times 2^E V=(?1)s×M×2E 表示一個數(shù):
-
符號(sign):
s
s
s 決定這個數(shù)是負數(shù)(
s
=
1
s=1
s=1)還是正數(shù)(
s
=
0
s=0
s=0)
- 對于數(shù)值 0 0 0 的符號位解釋作為特殊情況處理
-
尾數(shù)(significand)
M
M
M 是一個二進制小數(shù)
- 范圍: 1 ~ 2 ? ? 1 \sim 2-\epsilon 1~2?? 或 0 ~ 1 ? ? 0 \sim 1 - \epsilon 0~1??
-
階碼(exponent)
E
E
E 的作用是對浮點數(shù)加權(quán)
- 權(quán)重是 2 2 2 的 E E E 次冪(可能是負數(shù))
將浮點數(shù)的位表示劃分為三個字段,分別對這些值進行編碼:
- 一個單獨的符號位 s s s 直接編碼符號 s s s
- k k k 位的階碼字段 e x p = e k ? 1 ? e 1 e 0 exp=e_{k-1} \cdots e_1 e_0 exp=ek?1??e1?e0? 編碼階碼 E E E
-
n
n
n 位小數(shù)字段
f
r
a
c
=
f
n
?
1
?
f
1
f
0
frac=f_{n-1} \cdots f_1 f_0
frac=fn?1??f1?f0? 編碼尾數(shù)
M
M
M
- 編碼出來的值 M M M 依賴于階碼字段 E E E 的值是否等于 0 0 0
單精度浮點數(shù)(float
,
32
32
32 bits):
s
=
1
??
&
??
k
=
8
??
&
??
n
=
32
s=1 \space \space \& \space \space k=8 \space \space \& \space \space n=32
s=1??&??k=8??&??n=32
雙精度浮點數(shù)(double
,
64
64
64 bits):
s
=
1
??
&
??
k
=
11
??
&
??
n
=
52
s=1 \space \space \& \space \space k=11 \space \space \& \space \space n=52
s=1??&??k=11??&??n=52
給定位表達,根據(jù) e x p exp exp 的值,被編碼的值可以分成三種不同的情況(最后一情況有兩個變種):
-
規(guī)格化的值
-
e
x
p
exp
exp 的位模式既不全為
0
0
0 也不全為
1
1
1
- 階碼
E
=
e
?
B
i
a
s
E = e - Bias
E=e?Bias
- e e e 是無符號數(shù),其位表示為 $e_{k-1} \cdots e_1 e_0 $
- B i a s = 2 k ? 1 ? 1 Bias = 2^{k-1} - 1 Bias=2k?1?1(單精度是 127 127 127,雙精度是 1023 1023 1023)
- 階碼
E
=
e
?
B
i
a
s
E = e - Bias
E=e?Bias
- 小數(shù)字段
f
r
a
c
frac
frac 被解釋為描述小數(shù)值,其中
0
≤
f
<
1
0 \le f < 1
0≤f<1,其二進制表示為
0.
f
n
?
1
?
f
1
f
0
0.f_{n-1}\cdots f_1 f_0
0.fn?1??f1?f0?
- 尾數(shù)定義為 M = 1 + f M = 1+f M=1+f
-
e
x
p
exp
exp 的位模式既不全為
0
0
0 也不全為
1
1
1
-
非規(guī)格化的值
- 階碼域全為 0 0 0
- 階碼值
E
=
1
?
B
i
a
s
E = 1 - Bias
E=1?Bias
- B i a s = 2 k ? 1 ? 1 Bias = 2^{k-1} - 1 Bias=2k?1?1(單精度是 127 127 127,雙精度是 1023 1023 1023)
- 尾數(shù) M = f M=f M=f
-
特殊值
- 階碼域全為 1 1 1
- 小數(shù)域
- 小數(shù)域全為零時,表示無窮
- 當 s = 0 s=0 s=0 時,值為 ? ∞ - \infty ?∞
- 當 $s = 1 $ 時,值為 + ∞ + \infty +∞
- 小數(shù)域為非零時,表示 N a N NaN NaN
- 小數(shù)域全為零時,表示無窮
例題
轉(zhuǎn)換 23.7 5 10 23.75_{10} 23.7510?
23.7 5 10 = 2 3 10 + 0.7 5 10 = 10111.1 1 2 = 1.01111 1 2 × 2 4 23.75_{10} = 23_{10} + 0.75_{10} = 10111.11_2 = 1.011111_2 \times 2^4 23.7510?=2310?+0.7510?=10111.112?=1.0111112?×24
因此:
- s = 0 s=0 s=0
- M = 1.01111 1 2 ?? ? ?? f = M ? 1 = 0.01111 1 2 ?? ? ?? f r a c = 0111110 … 0 M=1.011111_2 \space \space \Rightarrow \space \space f=M-1=0.011111_2 \space \space \Rightarrow \space \space frac=0111110\dots0 M=1.0111112??????f=M?1=0.0111112??????frac=0111110…0
- E = 4 ?? ? ?? e = E + B i a s = ( 4 + 127 ) 10 = 13 1 10 = 1000001 1 2 ?? ? ?? e x p = 10000011 E=4 \space \space \Rightarrow \space \space e = E + Bias = (4 + 127)_{10} = 131_{10} = 10000011_2 \space \space \Rightarrow \space \space exp = 10000011 E=4?????e=E+Bias=(4+127)10?=13110?=100000112??????exp=10000011
故: 23.7 5 10 = ( s ∣ e x p ∣ f r a c ) 2 = ( 0 ∣ 10000011 ∣ 01111100000000000000000 ) 2 23.75_{10} = (s|exp|frac)_2=(0|10000011|01111100000000000000000)_2 23.7510?=(s∣exp∣frac)2?=(0∣10000011∣01111100000000000000000)2?
轉(zhuǎn)換 1011 ? 1101 ? 0100 ? 0000 ? 0000 ? 0000 ? 0000 ? 000 0 2 1011 \space 1101 \space 0100 \space 0000 \space 0000 \space 0000 \space 0000 \space 0000_2 1011?1101?0100?0000?0000?0000?0000?00002?
- s = 1 s=1 s=1
- e x p = 01111010 ?? ? ?? E = e ? B i a s = ( 01111010 ) 2 ? 12 7 10 = ? 5 10 exp=01111010 \space \space \Rightarrow \space \space E=e-Bias = (01111010)_2 - 127_{10} = -5_{10} exp=01111010?????E=e?Bias=(01111010)2??12710?=?510?
- f r a c = 10 … 0 ?? ? ?? f = 0. 1 2 ?? ? ?? M = 1 + f = 1. 1 2 frac=10\dots0 \space \space \Rightarrow \space \space f=0.1_2 \space \space \Rightarrow \space \space M = 1 + f = 1.1_2 frac=10…0?????f=0.12??????M=1+f=1.12?
故: 1011 ? 1101 ? 0100 ? 0000 ? 0000 ? 0000 ? 0000 ? 000 0 2 = ( ? 1 ) s × M × 2 E = ? 0.04687 5 10 1011 \space 1101 \space 0100 \space 0000 \space 0000 \space 0000 \space 0000 \space 0000_2 = (-1)^s \times M \times 2^E = - 0.046875_{10} 1011?1101?0100?0000?0000?0000?0000?00002?=(?1)s×M×2E=?0.04687510?
位運算(必考,10分)
/*
* bitAnd - x&y using only ~ and |
* Example: bitAnd(6, 5) = 4
* Legal ops: ~ |
*/
int bitAnd(int x, int y) {
return ~((~x) | (~y));
}
/*
* bitOr - x|y using only ~ and &
* Example: bitOr(6, 5) = 7
* Legal ops: ~ &
*/
int bitOr(int x, int y) {
return ~((~x) & (~y));
}
/*
* isZero - returns 1 if x == 0, and 0 otherwise
* Examples: isZero(5) = 0, isZero(0) = 1
* Legal ops: ! ~ & ^ | + << >>
*/
int isZero(int x) {
return !x;
}
/*
* minusOne - return a value of -1
* Legal ops: ! ~ & ^ | + << >>
*/
int minusOne(void) {
return ~0;
}
/*
* TMax - return maximum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
*/
int tmax(void) {
return 0x7fffffff;
}
/*
* bitXor - x^y using only ~ and &
* Example: bitXor(4, 5) = 1
* Legal ops: ~ &
*/
int bitXor(int x, int y) {
return (~x) & y;
}
/*
* getByte - Extract byte n from word x
* Bytes numbered from 0 (LSB) to 3 (MSB)
* Examples: getByte(0x12345678,1) = 0x56
* Legal ops: ! ~ & ^ | + << >>
*/
int getByte(int x, int n) {
return (x >> (n << 3)) & 0xff;
}
/*
* isEqual - return 1 if x == y, and 0 otherwise
* Examples: isEqual(5,5) = 1, isEqual(4,5) = 0
* Legal ops: ! ~ & ^ | + << >>
*/
int isEqual(int x, int y) {
return !(x ^ y);
}
/*
* negate - return -x
* Example: negate(1) = -1.
* Legal ops: ! ~ & ^ | + << >>
*/
int negate(int x) {
return (~x) + 1;
}
/*
* isPositive - return 1 if x > 0, return 0 otherwise
* Example: isPositive(-1) = 0.
* Legal ops: ! ~ & ^ | + << >>
*/
int isPositive(int x) {
return !((x >> 31) | !x);
}
/* NegativeNum using only ~ and & , ignore 0
* Example: NegativeNum(-5) retrun -5 , NegativeNum(5) retrun -5, Negative(0) can
return any value
* Legal ops: ~ &
* Max ops: 8 */
int NegativeNum (int x) {
}
3
看懂指令(mov, jmp, add, call, ret, push, pop)
字節(jié)對齊、sizeof
數(shù)組 低地址指向高地址(會用)
寄存器
棧指針 %rsp
用于指明運行時棧的結(jié)束位置
四字(64 位) | 雙字(32 位) | 作用 |
---|---|---|
%rax |
%eax |
返回值 |
%rbx |
%ebx |
被調(diào)用者保存 |
%rcx |
%ecx |
第 4 個參數(shù) |
%rdx |
%edx |
第 3 個參數(shù) |
%rsi |
%esi |
第 2 個參數(shù) |
%rdi |
%edi |
第 1 個參數(shù) |
%rbp |
%ebp |
被調(diào)用者保存 |
%rsp |
%esp |
棧指針 |
%r8 |
%r8d |
第 5 個參數(shù) |
%r9 |
%r9d |
第 6 個參數(shù) |
指令
指令 | 作用 |
---|---|
MOV |
復(fù)制數(shù)據(jù) |
PUSH |
將數(shù)據(jù)壓入棧中 |
POP |
將數(shù)據(jù)從棧中彈出 |
LEA |
加載有效地址(Load Effective Address) |
ADD /SUB /MUL /DIV
|
加/減/乘/除 |
INC |
加一 |
JMP |
指令跳轉(zhuǎn) |
CALL /RET
|
調(diào)用/返回過程(Procedure) |
4
指針的三個問題:
- 指針的值
- 指針的地址
- 指針指向的地址/值
5
函數(shù)調(diào)用、函數(shù)返回過程(必考,15分)
棧幀(Stack Frame)
ESP, EBP, EIP
函數(shù)調(diào)用過程
當函數(shù)被調(diào)用時,編譯器與硬件:
- 將函數(shù)參數(shù)壓棧
- 將返回地址壓棧
- 將幀指針(Frame Pointer)壓棧
- 將幀指針(Frame Pointer)設(shè)置為等于棧指針(Stack Pointer)的值
- 將棧指針下移
函數(shù)返回過程
當函數(shù)返回時,編譯器與硬件:
- 將棧指針(Stack Pointer)設(shè)置為等于幀指針(Frame Pointer)的值
- 將舊的幀指針的值彈棧
- 使
%EBP
的值等于舊的幀指針的值 - 將返回地址彈棧,并將
%EIP
的值設(shè)置為其值 - 將參數(shù)彈棧
棧幀
棧幀通常包含以下元素:
- 傳給函數(shù)的參數(shù)
- 函數(shù)調(diào)用者的返回值
- 函數(shù)調(diào)用者的
%ebp
- 分配給函數(shù)本地變量的空間
- 已保存的寄存器
ESP、EBP、EIP
當調(diào)用函數(shù)時:
-
EIP
寄存器里存儲的是 CPU 下次要執(zhí)行的指令的地址 -
EBP
寄存器里存儲的是棧的棧底指針,通常叫?;?/strong>,是開始進行函數(shù)調(diào)用之前,由ESP
傳遞給EBP
的 -
ESP
寄存器里存儲的是在調(diào)用函數(shù)之后的棧頂,并且始終指向棧頂
當函數(shù)返回時:
- 系統(tǒng)根據(jù)
EIP
寄存器里存儲的地址,CPU 就能夠知道函數(shù)調(diào)用完,下一步應(yīng)該做什么 -
EBP
寄存器存儲的是棧底地址,而這個地址是由ESP
在函數(shù)調(diào)用前傳遞給EBP
的。等到調(diào)用結(jié)束,EBP
會把其地址再次傳回給ESP
。所以ESP
又一次指向了函數(shù)調(diào)用結(jié)束后的棧頂?shù)刂贰?/li>
6
緩沖區(qū)溢出
如何避免緩沖區(qū)攻擊:
-
使用限制長度的庫函數(shù)(不使用不好的函數(shù))
-
fgets
而不是gets
-
strncpy
而不是strcpy
-
- 棧使用隨機的偏移
-
數(shù)據(jù)執(zhí)行保護(不讓棧上指令執(zhí)行)
- 內(nèi)存要么可寫,要么可執(zhí)行,但二者不能兼得
-
檢查棧緩沖區(qū)溢出的標志
- Stack Canaries:在棧的返回地址的存儲位置之前放置一個整形值,該值在裝入程序時隨機確定;棧緩沖區(qū)攻擊時從低地址向高地址覆蓋??臻g,因此會在覆蓋返回地址之前就覆蓋了警惕標志;返回返回前會檢查該警惕標志是否被篡改。
7
動態(tài)內(nèi)存分配(堆/棧)
棧和堆的比較
靜態(tài)內(nèi)存分配:程序變量、靜態(tài)的局部變量、字符串常量
內(nèi)部/外部碎片
管理堆上內(nèi)存的數(shù)據(jù)結(jié)構(gòu):環(huán)形雙向鏈表(Circular Doubly-LinkedList)、分配樹(Allocation Tree)
靜態(tài)分配
靜態(tài)分配的變量
- 全局變量
- 被聲明為
static
的局部變量 - 顯式常量(例如字符串等)
動態(tài)分配
棧分配
- 最常用的方式
- FIFO
- 向低地址增長
- 寄存器
%ebp
保存棧頂?shù)刂?/li> - 寄存器
%esp
保存棧底地址
堆分配
- 使用
malloc
和free
釋放內(nèi)存空間
棧 v.s. 堆
- ??臻g是自動(隱式)分配的;堆空間是手動(顯式)分配的
- ??臻g在離開作用域時自動銷毀;堆空間需要使用
free
請求銷毀空間
8
定義 mymalloc
的數(shù)據(jù)結(jié)構(gòu)與算法
- 封裝
malloc
函數(shù)- 對結(jié)構(gòu)體(分配的空間)進行初始化
- 封裝
free
函數(shù)- 檢查錯誤(Fence 對不對、checkout 對不對)
- 修改 line number
9
垃圾回收概念、四種算法(標記清掃、復(fù)制、引用計數(shù)、分代式垃圾回收,掌握)、偽代碼(只有標記清掃有偽代碼)
標記清除(Mark and Sweep)
過程
- Mark:對 root nodes 可達的節(jié)點進行標記(Mark)
- Sweep:對未標記的節(jié)點進行清除(Sweep),并消除可達節(jié)點的標記
可以成為 root nodes(根節(jié)點)的元素
- 寄存器中的值
- 全局/靜態(tài)變量
- 棧中的局部變量
- 函數(shù)中的局部變量
優(yōu)點
- 不需要手動運行(會在堆滿了的時候自動運行)
- 能找到并釋放所有無用的內(nèi)存塊
缺點
- 標記清除算法會鎖住其他進程,導(dǎo)致一段明顯的停頓時間,影響性能
- 會產(chǎn)生碎片
復(fù)制法(Copying Collection)
使用 2 個堆:1 個給程序使用,另 1 個直到 GC 時都不使用
過程
- 從 root nodes 開始遍歷所有可達節(jié)點
- 將所有可達節(jié)點從正在使用的堆移動到另一個堆
- 刪除掉所有留在原來的堆里的數(shù)據(jù)
- 切換堆
優(yōu)點
- 速度比標記清除快(只需要一次遍歷)
缺點
- 空間利用率大幅下降
引用計數(shù)(Reference Counting)
- 為每個對象維護一個指向這個對象的指針的計數(shù)器
- 當計數(shù)器等于 0 時,即其是一個不可達節(jié)點(garbage)
缺點
- 維護引用計數(shù)器的開銷
- 無法解決循環(huán)引用
分代式(Generational Garbage Collection)
達爾文的進化論:新生物種是最容易被淘汰的
- 將對象分為多個年代
- 例如 G0、G1,其中 G0 包含所有的年輕的對象
- 不需要遍歷整個引用樹
- 大多數(shù)的回收在新生代完成
10
程序優(yōu)化的流程
- Create benchmark
- Find hotspots
- Investigate causes
- Modify application
- Retest using benchmark
Amdahl’s Law 概念與計算
主要思想:當對系統(tǒng)的某個部分加速時,其對系統(tǒng)整體性能影響取決于該部分的重要性和加速程序
若系統(tǒng)執(zhí)行某應(yīng)用程序需要時間
T
o
l
d
T_{old}
Told?;假設(shè)系統(tǒng)某部分所需執(zhí)行時間與該時間的比例為
α
\alpha
α,而該部分性能提升比例為
k
k
k(即該部分初始所需時間為
α
T
o
l
d
\alpha T_{old}
αTold?,現(xiàn)在所需時間為
(
α
T
o
l
d
)
/
k
(\alpha T_{old}) / k
(αTold?)/k),因此,總的執(zhí)行時間應(yīng)為:
T
n
e
w
=
(
1
?
α
)
T
o
l
d
+
(
α
T
o
l
d
)
/
k
T_{new} = (1 - \alpha)T_{old} + (\alpha T_{old})/k
Tnew?=(1?α)Told?+(αTold?)/k
由此,可以計算加速比:
S
=
T
o
l
d
T
n
e
w
=
1
(
1
?
α
)
+
α
/
k
S = \frac {T_{old}} {T_{new}} = \frac 1 {(1 - \alpha) + \alpha / k}
S=Tnew?Told??=(1?α)+α/k1?
Timer concept - multiplechoice
11
Optimization approaches(要能列出來)
看 ppt
最重要的優(yōu)化是算法上的優(yōu)化
- Code Motion(代碼移動)
- Reduction in Procedure Call(減少過程調(diào)用)
- Eliminate Unneeded Memory References(減少不需要的內(nèi)存訪問)
- Loop Unrolling(循環(huán)展開)
- Common Subexpression Elimination(消除公用子表達式)
- Avoiding Temporary Variables(避免臨時變量)
-
減少內(nèi)存分配
molloc
- 減少重分配,盡量重用
12
存儲器的層次結(jié)構(gòu)(Memory Hierarchy):
- Register
- L1/L2/L3
- Memory(內(nèi)存)
- 硬盤
- 分布式文件系統(tǒng)
金字塔結(jié)構(gòu)
云存儲(Cloud Storage)的概念、優(yōu)缺點(適當背一下)
Locality concept(時間、空間局部性)
云存儲
概念
把數(shù)據(jù)存放在通常由第三方托管的多臺虛擬服務(wù)器,而非專屬的服務(wù)器上。
優(yōu)點
- 企業(yè)只需要依實際使用的存儲空間支付費用。
- 企業(yè)并不需要在自己的數(shù)據(jù)中心或辦公室里安裝實體的存儲設(shè)備,大大減少 IT 和管理的成本。
- 日常維護工作,如備份、資料復(fù)制、或是增加存儲設(shè)備添購等工作,都轉(zhuǎn)移給托管的服務(wù)提供商,讓企業(yè)更可以專注在自己的核心業(yè)務(wù)上。
缺點
- 當所欲存儲的資料較為機密時,則對存放于云存儲服務(wù)提供商的安全性有疑慮。
- 訪問性能可能比本地端存儲設(shè)備的性能低。
- 資料的可靠性和可用性將取決于廣域網(wǎng),以及服務(wù)商所提供的預(yù)防措施好壞。
- 當用戶有特殊的資料使用記錄追蹤需求時(如公務(wù)部門依據(jù)規(guī)章和條例的要求,而需留存某些電磁記錄時),使用云計算及云存儲將使工作復(fù)雜度增加。
- 雖然可以一次提供給多人資料,或是傳遞資料給位于不同地方的人,但單人在轉(zhuǎn)移資料的時候(例如文件由手機發(fā)送至電腦,或是由電腦發(fā)送至手機)因為需要重新“上傳”與“下載”,會像是在繞遠路一般,不如使用傳輸線的快。
- 傳遞大型資料的話,若是互聯(lián)網(wǎng)斷線或是云服務(wù)供應(yīng)商出現(xiàn)差錯,小則需要重新傳輸,大則有可能會導(dǎo)致資料上的差錯或丟失。
Locality
時間局部性:程序在運行時,最近剛剛被引用過的一個內(nèi)存位置容易再次被引用,比如在調(diào)取一個函數(shù)的時候,前不久才調(diào)取過的本地參數(shù)容易再度被調(diào)取使用。
空間局部性:最近引用過的內(nèi)存位置以及其周邊的內(nèi)存位置容易再次被使用??臻g局部性比較常見于循環(huán)中,比如在一個數(shù)列中,如果第 3 個元素在上一個循環(huán)中使用,則本次循環(huán)中極有可能會使用第 4 個元素。
13
緩存(cache)的基本結(jié)構(gòu)
三種類型的緩存(全相聯(lián)、組相聯(lián)等)
Cache miss rate calculation(看清楚是什么類型的緩存)
緩存友好的代碼 cache- aware programming* (loop fission, Cache Collisions, Row-major order and Column-major order, Loop Tiling* code implement)
14
linker/loader 概念(要能講出這是干嘛的)
靜態(tài)鏈接過程 Static Linking Process(符號解析合并重定位)
Symbol Resolution concept / strong and week symbol
理解鏈接實驗的過程
linker 鏈接器
鏈接器(Linker)是一個程序,將一個或多個由編譯器或匯編器生成的目標文件外加庫鏈接為一個可執(zhí)行文件。
loader 加載器
加載程序:加載程序是將程序的機器代碼加載到系統(tǒng)內(nèi)存中的程序。
Static Linking Process(靜態(tài)鏈接過程)
-
Symbol Resolution(符號解析)
- Object files define and reference symbols.
- Linker associate each symbol reference with exactly one symbol definition.
- Combination(組合)
-
Relocation(重定位)
- Compiler and assemblers generate code and data sections that start at address 0.
- Linker reloacates theses sections by associating a memory location with each symbol defination and modifying references to those symbols so that they point to this memory location.
Symbol Resolution
確定符號引用關(guān)系,將每個模塊中引用的符號與某個目標模塊的定義符號建立關(guān)聯(lián)
每個定義符號在代碼段(函數(shù))和數(shù)據(jù)段(變量)都分配了存儲空間,將引用符號與定義符號建立關(guān)聯(lián)后,就可以在重定位時將引用符號的地址重定位為相關(guān)聯(lián)的定義符號的地址。
符號解析的整體過程
- 找到程序中有定義和引用的符號(包括函數(shù)和變量)
- 編譯器將定義的符號放在符號表中:
- 符號表是一個結(jié)構(gòu)數(shù)組
- 每個表項包含符號名、長度位置等信息
- 鏈接器將每個符號的引用都與一個確定的符號定義建立關(guān)聯(lián)
Strong & Week Symbol
- Strong Symbol - procedures and initialized globals
- Weak Symbol - uninitialized globals
符號鏈接的規(guī)則
- Strong Symbol 只能出現(xiàn)一次
- 同名的 Week Symbol 可以被 Strong Symbol 覆蓋
- 如果有多個 Week Symbol,鏈接器會隨機選擇一個
15
四種類型的異常、產(chǎn)生的原因是什么、怎么進行異常處理、處理完后返回到哪里
不要求進程、Windows編程等
4 種類型的異常與其產(chǎn)生的原因
- Interrupts: Signal from I/O device, INTR/NMI
- Traps: System Call
- Faults: Potentially recoverable error, Page Fault, 除零
- Aborts: Nonrecoverable error
異常處理
選擇題
1
1. Which of the following Visual C++ objects are contained within a “Project”?
I. Files, II. Visual C++ Solutions, III. Flow charts
2. In Visual C++, a Win32 Console Application is ()
a. the status window of the Visual C++ environment
b. built by using sophisticated “Application Wizards”
c. a program that is able to control the operating system of a windows computer
d. the simplest type of application Visual C++ can generate
3. Which of the following is able to describe a computation at the highest level of abstraction?
a. C++ code
b. logic Gates
c. machine code
d. C code
4. Consider the following fragment of C++ source code.
string msg;
unsigned int x; int y;
cin >> msg >> x >> y;
cout << x + y;
Which of the following is (are) true regarding execution of the segment?
1.The input statement will always take the same amount of time to execute.
2.The output statement will always be executed immediately after the input statement.
3.If x and y are both positive, an integer greater than both will be printed.
a. II and III only
b. I and II only
c. none
d. II only
5. Integrated programming environments make it difficult to mix and match tools from different sources. This is ()
a. good, because tools from different sources cannot be made to interact with each other
b. bad, because no single vendor is likely to be the source of all the best tools
c. bad, because all the tools will then have the same user interface
d. good, because it ensures compilation is not done incrementally by accident
6. Compared to a sequence of machine code instructions, a fragment of C code ()
a. may describe the same algorithm
b. is the native way to program most computers
c. describes the actions of the computer, not just of the CPU
d. does not engage any transistors during its execution
7. Which of the following does a debugger do?
- Analyze the source code to find programming errors.
- Decode machine code generated by a compiler.
- Stop execution of a program.
8. When using a debugger to find the cause of a program’s incorrect behavior, ()
a. it is often necessary to start the program multiple times under the debugger
b. the faulty code fragment must first be identified
c. the program is usually executed to the point at which the behavior occurs and then executed backwards to find the cause
d. it is fastest to start by stopping the debugger long before the behavior appears
2
1. In a computer with 4-byte words, which of the following C expressions tests whether ptr contains the address of a word?
I. (ptr & 3) == 0
II. (ptr | 3) == 0
III. (ptr % 4) == 0
a. III only
b. I only
c. I and III only
d. II only
看
ptr
指向的字節(jié)的最后兩位是不是0x11
(注:字節(jié)小端排序)
2. What happens in a C program when an addition would cause integer overflow?
a. An incorrect result is produced and execution continues.
b. An exception-handler is called with the two operands as parameters.
c. Execution is terminated.
d. The correct value is coerced to a floating point number.
3. In C, what is the following binary number 11010101
in hexadecimal?
a. 0xD5
b. 0x5D
c. 0xB5
d. 0xAB
4. What is the purpose of the exponent in floating point numbers?
a. to specify the base as binary, octal, or hexadecimal
b. the mantissa is raised to the power of the exponent
c. to indicate where the decimal or binary point should be
d. to specify the superscript
5. How is -10 (decimal) represented in an 8-bit 2’s complement binary format?
a. 11110110
b. 11110101
c. 10001010
d. 11111010
6. In C, using default floating point settings, what happens when a floating-point computation results in an overflow? 、
a. An erroneous value is computed and execution continues.
b. Program execution is halted.
c. A special value “infinity” is computed, testable with _finite().
d. An exception is raised unless disabled by calling _controlfp().
7. What is the value of the following C expression 0x1234 & 0x5432
?
a. 0x1111
b. 0x6666
c. 0x1030
d. 0x5636
8. Which of the following numerical operations is most likely to lead to loss of precision?
a. Floating-point multiplication
b. Integer addition
c. Floating-point addition
d. Integer multiplication
9. Which of the following could be represented by one bit of information?
a. the color of a single pixel on a true-color computer display
b. an ASCII character
c. the position of a light switch
d. the current channel of a television receiver
10. Which of the following statements about floating-point numbers in C is true?
I. Floating-point numbers are often only approximations of real numbers.
II. A 32-bit float only approximates decimal fractions, but a 64-bit double represents them exactly.
III. Floating-point numbers can represent any rational real number but not irrationals.
a. I only
b. I and III only
c. II only
d. I and II only
11. How is 46 (decimal) represented in an 8-bit 2’s complement binary format?
a. 00101110
b. 01000110
c. 00011110
d. 00101100
12. What is the value of the following C expression 0x1234 ^ 0x5432
?
a. 0x1030
b. 0x5434
c. 0x5636
d. 0x4606
3
1. The program counter contains
a. the number of CPU instructions a program has executed so far
b. the number of times a program has been executed
c. the amount of memory a program is currently using
d. the address of the CPU instruction that is about to be fetched
2. Which of the following is a good reason (are good reasons) to equip the CPU with small amounts of fast memory?
I.To make the design of the compiler simpler
II.To make some CPU instructions smaller
III.To make some CPU instructions faster
3. Which of the following must be true if a program is stopped at a specific line within the Visual C++ debugger?
I. There is at least one breakpoint enabled.
II. There is a breakpoint enabled on that line.
III. There is a breakpoint enabled on the line preceding that line
none
4. Programs compiled for an Intel Pentium processor do not execute properly on a SPARC processor from Sun Microsystems because ()
a. copyrights regarding code cannot be violated
b. the operation codes understood by the two processors are different
c. the assembly mnemonics for the same “opcode” are different in the two processors
d. the memory of a SPARC CPU is numbered from top to bottom
5. Within Visual C++, which of the following will reveal the value of variable when the program is stopped at a breakpoint?
I. Placing the mouse pointer over the variable name in the source file window.
II. Inserting a printf() in the program.
III. Typing the variable name on the “Watch” window.
6. Immediately after the CPU executes an instruction that is neither a branch nor a jump instruction, the program counter ()
a. remains unchanged
b. is incremented to point to the following instruction
c. has a value that cannot be determined without further information
d. is incremented by one
7. A CPU register is a word of CPU memory that ()
a. houses a critical variable for the duration of the execution of a program
b. records the results of periodic CPU diagnostics
c. is explicitly loaded and unloaded from normal memory by compiler-generated instructions
d. is automatically loaded when a CPU instruction refers to a word of normal memory
8. Which of the following computations may be performed by exactly one CPU instruction?
- a = 5;
- a = b + c * 5;
- for (i = 0; i < 10; i += a[i++]);
9. Suppose that, using a tool such as the memory window of Visual C++, we found that a certain set of contiguous memory locations contained the integer 0xC605CD623A8365000000
. What could these memory locations hold? (D)
- the integer 0xC605CD623A8365000000
- a string
- a CPU instruction
10. A branch instruction ()
a. sets the program counter to one of two possible values
b. increases the program counter by a fixed amount
c. sets the program counter to one of many possible values
d. unconditionally sets the program counter to its operand
11. A jump instruction ()
a. changes the program counter only if its operand is equal to zero
b. changes a pointer to point to the next element of an array
c. increases the program counter
d. unconditionally sets the program counter to its operand
12. The machine code generated from source code by a compiler ()
a. associates variable values with their names
b. executes more quickly than the source code
c. does not preserve all the information given in the source code
d. can be easily inspected to check the correctness of the compiler
13. Which of the following are true of the effect that optimizations have on the machine code generated by compilers?
I. The resulting code will be faster and/or smaller.
II. The resulting code will be clearer.
III. The resulting code will be harder to debug.
4
1. In C, assuming that an int
takes 4 bytes, if array a
is declared as follows and a
has the value 0x10000
, what is the value of the expression a + 2
?
int a[12];
a. 0x10004
b. 8
plus the contents of location 0x10000
c. 0x10002
d. 0x10008
2. The Visual C++ Memory window displays ()
a. the contents of memory, interpreted in one of several ways, without the associated variable names
b. the names and values of variables in memory, interpreted in one of several ways
c. the names and values of variables in memory, interpreted as 32-bit integers no matter what the variables’ types
d. the contents of memory, interpreted as 32-bit integers, without the associated variable name
3. Consider the following code fragment.
int a;
int b;
int main(int argc, char *argv[]) {
int c;
int d;
...
/* some code */
}
Which of the following must be true?
a. The value of *d
is closer to the value of *c
than to the value of *a
.
b. The value of &d
is closer to the value of &c
than to the value of &a
.
c. The values of *a
and *b
are closer to each other than the values of *c
and *d
.
d. The values of &a
and &b
are closer to each other than the values of &c
and &d
.
4. Consider the following code.
char a[100];
a[99] = *((char *) (((int) &a[0]) + 4))
If integers are 32 bits wide, which of the following values is equal to a[99]
?
a. a[4]
b. the integer stored in the bytes a[4], a[5], a[6] and a[7]
c. a[3]
d. a[0] + 4
5. Which of the following statements about alignment within C struct’s is true?
I. Alignment may cause the allocation of unused space.
II. Alignment is required by all modern processors.
III. Alignment can help processors access data more efficiently.
6. In C, assuming that an int takes 4 bytes, how many bytes are required to represent the following array int a[12];
?
a. 12
b. 52
c. 48
d. 44
7. Given the following declaration and initialization of char s[] = "string";
, what is the value of the expression s[6]
?
a. ‘\n’
b. an unpredictable value
c. ‘g’
d. ‘\0’
8. Given the address of a C struct at runtime, how is the address of a member element in the struct determined?
a. A linear search is made from the base address of the struct.
b. The element name is looked up in a symbol table.
c. A constant offset associated with the member is added to the address.
d. The struct consists of an array of pointers to the elements of the struct.
9:In one computer, the bytes with addresses A
, A+1
, A+2
and A+3
contain the integer 256, and the variable declared with int *a;
has the value A
. In a different computer, the bytes with addresses B
, B+1
, B+2
and B+3
also contain the integer 256, and the variable declared with int *b
has the value B
. Which of the following are necessarily true? (A)
- The contents of
A+1
are equal to the contents ofB+1
. - The contents of
A+1
are equal to the contents ofB+2
. *a == *b
10. We want the variable factorialfunc
to hold the address of the first instruction of the following function:
int factorial(int n) {
if (n == 1)
return n;
return n * factorial(n -1);
}
How would we declare the variable?
a. int (int) * factorialfunc;
b. int (*factorialfunc)(int);
c. factorial() * factorialfunc;
d. we can’t: C cannot extract the addresses of instructions.
5
1. Consider the program given below.
#include <stdio.h>
int callee(void) {
int count = 5;
printf("%d ", (int) &count);
return count;
}
int main (int argc, char *argv[]) {
int count = 4;
count = callee();
printf("%d ", (int) &count);
return 0;
}
Which of the following describes the output of the program?
a. Two different integers are printed, and the value of neither can be determined from the information given.
b. One integer is printed twice, and its value cannot be determined from the information given.
c. 5 is printed twice on the same line.
d. 5 and 4 are printed, in that order on the same line.
2. What does the following program print?
int callee(int* count) {
count++;
return *count;
}
int main(int argc, char *argv[]) {
int count = 4;
int retval;
retval = callee(&count);
printf("%d", retval);
return 0;
}
a. 8
b. 4
c. 5
d. cannot be determined from the information given.
3. At which of the following times is an activation record created?
I. When a program starts executing.
II.Every time a function is invoked.
III.When a variable is declared.
4. What does the following program print?
void callee(int* count) {
(*count)++;
}
int main (int argc, char *argv[]) {
int count = 4;
callee(count); // Wrong! => D. compile error
printf("%d", count);
return 0;
}
a. 5
b. 8
c. 4
d. nothing: it will not compile successfully
應(yīng)該是
callee(&count);
5. Consider the following program segment.
int factorial(int* arg) {
int n = *arg;
if (n == 1)
return n;
return n * factorial(n - 1);
}
When the segment is executed, the variable n is allocated to ()
a. just one address, and it is not known to the compiler
b. just one address, and it was chosen by the compiler
c. many addresses none of which is known to the compiler
d. many addresses that were chosen by the compiler
具體地址在編譯期不可知
6: Consider the following program.
int i;
int j = 1;
int callee(int number) {
int plusone;
plusone = number + 1;
return plusone;
}
int main(int argc, char *argv[]) {
if (j == 1)
return callee(i);
return j;
}
Which of the following are allocated in the activation record immediately after the function callee()
is invoked?
a. i
, j
and number
only.
b. i
only.
c. plusone
only.
d. plusone
and number
only.
7. Consider the following program.
int i;
int* jp = &i;
void main(int i, char * argv[]) {
printf("%d %d\n", (int) &i, (int) jp);
}
Which of the following describes what it prints?
a. two values, one 4 greater than the other
b. nothing: it will not compile because it is ambiguous
c. two very different integers
d. two integers that are exactly the same
8. Consider the following program.
int square(int* arg) {
int n = *arg;
return n * n;
}
int main(int argc, char * argv[]) {
int arg = strtol(argv[1], NULL, 0);
return square(arg);
}
When it is executed with the argument 5, the variable n is allocated to ()
a. exactly one address not known to the compiler.
b. many addresses chosen by the compiler.
c. exactly one address chosen by the compiler.
d. many addresses neither of which are known to the compile
9. What is printed as a result of execution of the following program?
#include <stdio.h>
void callee(int *count) {
(*count)++;
}
int main(int argc, char *argv[]) {
int count = 4;
callee(&count);
printf("%d", count);
return 0;
}
a. 8
b. 5
c. 4
d. It cannot be determined from the information given.
10. Consider the following segment of C source code.
int a = 8;
int b = *&a;
What is the value of variable b
at the end of execution of the segment?
a. &a
b. a
c. (int) &a
d. (int) &b
11. In a computer in which both addresses and integers are 32 bits wide, how many bytes of memory will the compiler allocate for following code fragment? ?
int a;
int *b = &a;
a. 0
b. 32
c. 8
d. 4
12. Activation records are organized in stacks because ()
a. they are seldom needed during program execution.
b. stacks are simple enough for the hardware to manage.
c. stacks allow activation records to be pushed and popped in any order.
d. functions need to access all the variables of the functions that call them.
13. When executing a function callee()
, which of the following are true regarding the value of the frame pointer?
I. It marks the top of the stack frame of the function that invoked callee()
.
II. It marks the bottom of the stack frame of callee()
III. It is the top of the stack.
14. Consider the following function.
int factorial(int n) {
if (n == 1) return n;
return n * factorial(n - 1);
}
How many activation records are “popped” when it is invoked by the expression factorial(4)
?
a. 0
b. 5
c. 4
d. 1
6
1. Which of the following are true about statically allocated data in C programs?
- Its location is chosen by the compiler.
- Its location may change during execution if more memory is required.
- Its location is not known directly but can be found in a static symbol table.
2. A memory leak is caused by a ()
a. function that allocates a large amount of memory from the heap
b. bug in which too much memory is allocated, causing internal fragmentation
c. bug in the memory allocator that fails to free memory
d. failure to free allocated memory
3. In C, local variables allocated inside functions are allocated ()
a. in a fifo
b. on the stack
c. in static storage
d. in the heap
4. Suppose a compiler uses static storage to store all variables, function parameters, saved registers, and return addresses. Which of the following language features can this compiler support?
I. Local variables.
II. Function calls.
III. Recursion.
遞歸是動態(tài)的
5. The key feature of implicit memory management is that memory is freed automatically. Which of the following features of C make(s) it difficult to add support for implicit memory management in C?
I.Pointers are not always initialized.
II.Type casting makes it impossible to know when a value could be a pointer.
III. C programs can allocate memory at runtime.
至少能看出來最后一個是錯的
6. Which of the following features apply to standard heap allocation in C?
I.The size of heap objects must be known at compile time.
II.Heap memory must be explicitly allocated.
III.Heap memory is deallocated when a function returns.
7. In this sequence of C statements
long a[10];
ptr = a + 5;
*ptr++ = x;
the last line could be rewritten as ()
a. a[6] = x;
b. ptr = x; *ptr++;
c. a[5] = x; ptr = ptr + 1;
d. ptr = ptr + 1; *ptr = x;
8. Consider the following fragment of C code.
int *p = (int *) calloc(100);
int *q = p;
free(p);
Immediately after executing it, which of the following are true about p
and q
?
I.p
and q
are identical pointers to freed storage.
II.p points to freed storage, and q points to an allocated block of size 100.
III.p should not be free()d again, but invoking free(q) is all right.
9. In C, to allocate an array of 100 longs on the heap you should write ()
a. long a[] = (long *) malloc(100);
b. long *a = (long *) malloc(100);
c. long *a = (long *) malloc(100 * sizeof(long));
d. long a[100] = (long *) malloc(sizeof(a));
10. What is the value of an uninitialized pointer variable declared within a function?
a. its last value from the previous call to the function
b. 0xDEADBEEF
c. the value is undefined
d. 0 (or NULL)
11. Consider a system in which memory consists of the following hole sizes in memory order:
H0 H1 H2 H3 H4 H5 H6 H7
10KB 4KB 20KB 18KB 7KB 9KB 12KB 15KB
and a successive segment request of
a) 12 KB
b) 10KB
c) 9KB
Which of the following sentences is true? (D)
I. First Fit algorithm allocates H2, H0, H3 for the mentioned request.
II. Worst Fit algorithm allocates H2, H3, H7 for the mentioned request.
III. Best Fit algorithm allocates H6, H0, H5 for the mentioned request.
Worst Fit:取最大的
12. Consider the malloc()
function. Which one of the following sentences is correct? ( D)
a. The malloc()
returns the amount of memory allocated
b. The malloc()
allocates the desired amount of memory on the stack
c. The allocated memory is only local to the function
d. The malloc()
allocates the desired amount of memory on the heap
只在堆 Heap 上分配內(nèi)存
7
1. what will be the output ?
void main(){
char *p="hello world!";
int *q;
p++;
q = (int *)p;
q++;
printf("%s%s\n",p,q);
}
A.hello world!hello world!
B.ello world! world!
C.error
D.ello world!llo world!
2. what properties of a variable are specified by the static keyword in c?
i.the variable will be statically allocated.
ii.the variable name will be visible only to functions defined within the same file.
iii.the variable’s value does not change very often. the compiler uses this fact to focus optimizations on other variables.(10.0分)
3. in c, calloc()
differs from malloc()
in that calloc()
A.detects memory allocation errors.
B.is faster.
C.sets the contents of the block to zero before returning.
D.allocates additional memory from the stack.
5. a static variable by default gets initialized to ()
A.1
B.blank space
C.0
D.garbage value
7. in c, when a struct
is freed, ()
A.only those pointers within the struct that point into the heap are freed automatically.
B.a destructor function is called automatically to clean up.
C.any pointers within the struct are also freed automatically.
D.no pointers within the struct are freed automatically.
8. why is it wrong to return the address of a local variable?
A.it allows illegal access to the variable from arbitrary functions.
B.the local variable may be in a machine register.
C.the variable address is invalid after the return.
D.it is faster to return the value of the variable.
9. in c, which of the following is the best way to detect when a pointer is freed twice?
A.set pointers to null after freeing them.
B.modify free() to set the freed data to zero.
C.flag all blocks as free or not, and check the flag when calling free().
D.keep a log of addresses that have been freed and scan the log before calling free().
10. to resolve memory leaks in c, one common approach is ()
A.to add padding before and after allocated memory blocks and to fill that memory with a known value.
B.to check whether the number of calls to malloc() is greater than the number of calls to free().
C.to ensure that memory blocks are allocated only on word boundaries.
D.to store the source code line whence each block is allocated.
8
1. How does JAVA handle memory allocation?
a. JAVA always uses a garbage collector.
b. JAVA has a garbage collector that can be used or turned off.
c. Allocation and deallocation is the responsibility of the programmer.
d. Allocation and deallocation is completely shielded from the programmer.
2. A garbage collector ()
a. frees memory blocks that cannot be reached by dereferencing pointers.
b. removes old versions of local variables from the stack .
c. frees memory blocks marked as “deleteable”.
d. frees all memory blocks that will not be accessed in the future.
3. A memory pool is a large block of memory from which small objects are allocated piecemeal by breaking them off from the pool as required. Under which of the following conditions would such a scheme result in greatly improved performance? (A)
I. All objects allocated from the pool are freed at around the same time.
II. All objects allocated from the pool are of similar sizes.
III. A garbage collector takes care of freeing memory.
4. Reference counts used in implementations of garbage collectors count ()
a. the number of times a block has been allocated.
b. the number of times a block has been accessed.
c. the number of pointers pointing to a block.
d. the number of times a datum has been referenced inside each block.
5. To quickly allocate and free many variables of a commonly used data type, we could ()
a. coalesce blocks when they are freed.
b. use sizes which are powers of two.
c. keep a linked list of free objects of that type’s size.
d. minimize the size of the data type.
6. the advantage of using copying gc including ()
i. A copying collector is generally more efficient than a non-copying collector
ii. The copying gc can make use of heap memory effectively.
7. A garbage collector starts from some “root” set of places that are always considered “reachable”, such as
i. CPU registers
ii. stack
iii. global variables
8. which statement is true?
A.The best- ?t method chooses the largest free block into which the requested segment ?ts.
B.For the best- ?t method, the list of free blocks should be ordered according to increasing memory addresses
C.Using the ?rst- ?t algorithm on a free list that is ordered according to decreasing block sizes results in low performance for allocations, but avoids external fragmentation.
D.Using the ?rst- ?t algorithm on a free list that is ordered according to increasing block sizes is equivalent to using the best- ?t algorithm.
9. Mark-and-sweep garbage collectors are called conservative if ()
A.they coalesce freed memory only when a memory request cannot be satis?ed
B.they perform garbage collection only when they run out of memory
C.they treat everything that looks like a pointer as a pointer
D.they do not free memory blocks forming a cyclic list
10. which of the following statements are true?
i.The ? rst- ? t memory allocation algorithm is slower than the best- ? t algorithm (on average).
ii.Deallocation using boundary tags is fast only when the list of free blocks is ordered according to increasing memory addresses.
D.none of them
9
1. Which of the following are useful for observing program performance?
I. Direct measurement with a stopwatch.
II. Statistical Sampling.
III. System Monitors
2. Which of the following approaches towards optimizing programs is most advisable?
a. “Optimize as you go”: make sure every function is optimized before writing the next one.
b. Optimize after all functions are written and debugged.
c. Optimize main()
first.
d. Optimize the more complex functions first.
3. Which of the following is likely to offer the best performance improvement for programs that spend 50% of their time comparing strings?
a. Store strings uniquely so that pointer comparison can be used.
b. Be sure to use hardware string-comparison instructions.
c. Write in-line code for string comparison to eliminate a procedure call.
d. Call a library function for string comparison.
4. In the process of Software Optimization Process, what should do first?
a. Investigate causes of Hotspots
b. find the Hotspots
c. Modify application.
d. think of better Algorithm or using better Data structure
5. “Wall time” measures ()
a. idle time.
b. the total duration of a program’s execution.
c. the time a program spends waiting for input and output.
d. the user time plus the system time.
6. What is TSC?
a. A timer mechanism of OS
b. A system call of OS
c. A timer mechanism of x86 platform, which is the shortname of Time stamp counter
d. A timer mechanism of C library
7. “CPU time” measures ()
a. the time spent executing system functions.
b. the time spent by a program executing program instructions
c. wall time
d. the percentage utilization of the CPU by the system.
8. Which is a function call of C library?
a. Clock()
b. SetTimer
c. gettimeofday()
d. GetLocalTime
9. Which of the following are advantages of using statistical sampling to profile programs?
I. Exact run times of all functions can be determined.
II. Code can be instrumented automatically.
III. The performance impact due to measurement can be minimal.
10. Amdahl’s law, applied to program optimization, says that ()
a. each optimization about doubles a program’s performance
b. program measurement is a prerequisite to optimization
c. algorithmic design is more important than code quality for performance
d. successive program optimizations tend to produce diminishing returns
11. General wisdom, expressed by the 80/20 rule, says that ()
a. 80% of the execution time is in the user interface, and 20% does the real work
b. algorithmic improvements account for the smallest amount of performance gain
c. most execution time is spent in a small amount of code
d. optimization can obtain between 20 and 80 percent improvement
10
1. Which of the following is likely to offer the best performance improvement for programs that spend 50% of their time comparing strings?
a. Be sure to use hardware string-comparison instructions.
b. Store strings uniquely so that pointer comparison can be used.
c. Write in-line code for string comparison to eliminate a procedure call.
d. Call a library function for string comparison.
2. Read the following code, and how can we optimize it?
void lower1(char *s)
{
int i;
for (i = 0; i < strlen(s); i++)
if (s[i] >= 'A' && s[i] <= 'Z')
s[i] -= ('A' - 'a');
}
a. Enhancing Parallelism
b. Loop Splitting
c. Reducing Procedure Calls
d. Converting to Pointer Code
3. Which of the following is/are related to optimizing program performance by making it running fast ()
I. By using faster algorithm
II. By not using pointer
III .By using data structure that occupy less memory space
4. Which of the following is normal skill of making program run faster ()
I. Reducing Procedure Calls
II. Enhancing Parallelism
III. Eliminating Unneeded Memory References
5. In order to optimizing program performance, we should know ()
I. What is the hot spot
II. Understanding features of that processor on which the program will run
III. All the system calls that the program uses.
6. To quickly allocate and free many variables of a commonly used data type, we could ()
a. keep a linked list of free objects of that type’s size.
b. minimize the size of the data type.
c. use sizes which are powers of two.
d. coalesce blocks when they are freed.
7. On the following opinions of optimizing C programs, which is/are right?
I. Just config the compiler in its optimizing setting, then nothing else need to
II. Understanding the feature of CPU is needless
III. Everything can be done in the C level, so it is needless to know the assembly code
none
8. On profiling, which is/are wrong?
I.GPROF is the profilling tool on Linux platform
II. it can be used to estimate where time is spent in the program
III. it can incorporate instrumentation code to determine how much time the different parts of the program require.
none
9. Which of the following approaches towards optimizing programs is most advisable?
a. Optimize the more complex functions first.
b. “Optimize as you go”: make sure every function is optimized before writing the next one.
c. Optimize after all functions are written and debugged.
d. Optimize main() first.
10. Which of the following is not optimization technique?
a. constant folding
b. code motion
c. memory aliasing
d. loop unrolling
11
1. what can loader do?
i. translate the c code into machine code (by Compiler)
ii.resolution (by Linker)
iii.load or map the executable object file from the disk to memory
2. which section is used for resolution(符號解析)
i. elf header
ii. section header tables
iii. .symtab
iv. .rel.text and .rel.data (10.0分)
3. where the field, which describes whether relocatable object file is using little endian or big endian, locates?
A.elf header
B…text
C.section header tables
D…bss
5. which variable will be put into bss
? 未初始化或被初始化為0的全局或靜態(tài)變量
int printf( const char* format, ... );
int global_init_var = 84;
int global_uninit_var;
void func1(int i) {
printf("%d\n", i);
}
int main(void) {
static int static_var = 85;
static int static_var2;
int a = 1;
int b;
func1( static_var + static_var2 + a + b );
return a;
}
i a and b
ii static_var
iii global_init_var
iv global_uninit_var
6. which file format is used for executable object file
i. pe
ii. coff
iii.elf
iv. a.out
7. what can linker do?
i. resolution
ii.relocation
iii.take the same kind of sections from relocatable object files, and put them together according to their types
8. at what time can linking happen? 這是廣義上的鏈接
i.compile time
ii.load time
iii.run time
9. which section is used for relocation 重定位
i. elf header
ii. section header tables
iii. .symtab
iv. .rel.text and .rel.data
12
1. Which of the following is (are) true of the concept of locality of reference?
I. It is used to predict future memory references precisely, with the help of the compiler.
II. It is a quality of typical programs.
III. It has been mathematically proven.
2. Which of the following levels of a typical memory hierarchy transfers data in chunks of biggest size?
a. cache <–> main memory.
b. CPU registers <–> cache.
c. they all transfer one byte at a time.
d. main memory <–> disk.
3. Current technology trends suggest that the need for memory hierarchies ()
a. will disappear when “broadband” communications start delivering data over the internet at speeds greater than 1Mbps.
b. will disappear once processors reach clock frequencies greater than about 1000MHz.
c. will disappear once DRAM speeds improve.
d. will never disappear.
4. Which of the following is necessarily true regarding the following code fragment?
a = b;
c = d;
if (e == 1) return;
a. It exhibits no locality of reference.
b. It exhibits locality of reference no matter where the variables are allocated.
c. It exhibits locality of reference but only when a == b.
d. It exhibits locality of reference because the variables are allocated near each other.
5. Which of the following manages the transfer of data between the cache and main memory?
a. Compiler.
b. Operating System.
c. Hardware.
d. Registry.
6. Which of the following levels of a typical memory hierarchy transfers data in chunks of smallest size?
a. they all transfer one byte at a time.
b. main memory <–> disk.
c. cache <–> main memory.
d. CPU registers <–> cache.
7. Compared to dynamic RAM (SRAM), disks are ()
I. more expensive per megabyte.
II. slower per word access.
III. more persistent.
8. Which of the following manages the transfer of data between the CPU registers and the cache?
a. Hardware.
b. Operating System.
c. Registry.
d. Compiler.
9. A memory hierarchy ()
a. limits programs’ size but allows them to execute more quickly.
b. takes advantage of the speed of SRAM and the capacity of disk.
c. makes programs execute more slowly but allows them to be bigger.
d. is a way of structuring memory allocation decisions.
10. Compared to static RAM (SRAM), dynamic RAM (DRAM) is ?
- more expensive per megabyte.
- slower per word access.
- more persistent.
13
1. lru is an effective cache replacement strategy primarily because programs ()
A.exhibit locality of reference
B.read data much more frequently than write data
C.usually have small working sets
D.none of the above
2. when a cache is full and a new cache line needs to be fetched into it, which of the following is a pretty good, practical approach?
A.choosing the cache location currently occupied by the least-recently-used data.
B.randomly selecting a cache location for the new line.
C.choosing always the same cache location for the new line.
D.denying the memory operation that caused the fetch of the new line.
3. a program whose code and data together occupy fewer than 256 kbytes is executed on a computer with a 512 kbyte direct cache. which of the following is true? (10.0分)
A.there is no telling, from the information given, how many bytes will be fetched from main memory.
B.no bytes will be fetched from main memory
C.every instruction fetch will cause a cache miss.
D.some bytes, but at most 256 kbytes, will be fetched from main memory.
4. when the following code fragment is executed on a computer with 32-bit integers and a fully-associative cache with 32-byte cache lines, how many bytes of the array a will be fetched into the cache from main memory?
int a[100];
for (i = 0; i < 17; sum += a[i], i++);
A.at most 96.
B.at most 68.
C.exactly 17.
D.exactly 32.
5. your computer has 32-bit integers and a direct cache containing 128 32-byte cache lines. in the following code fragment, the compiler allocates a
at address 0x800000 and b
at address 0x801000. before the execution of the code fragment, the arrays a
and b
have never been used, so they are not in the cache. what is the minimum number of bytes from each of the arrays a and b that could be fetched into the cache from main memory, during the execution of the code?
int b[1024];
int a[1024];
for (i = 0; i < 17; sum += a[i] + b[i], i++);
A.96
B.17
C.68
D.1088
6. which facts about the cache can be determined by calling the following function?
int data[1 << 20];
void callee(int x) {
int i, result;
for (i = 0; i < (1 << 20); i += x) {
result += data[i];
}
}
i cache line size
ii cache size
iii cache speed
7. a certain program is found to execute with a cache hit ratio of 0.90 on computer a, and of 0.95 on computer b. however, because of other design parameters of these computers, its wall time is the same in both a and b. then, a clever programmer finds a way to improve the locality of the program, so that it now executes with a hit ratio of 0.92 on a, and of 0.97 on b. which of the following statements is valid?
A.the wall time is now smaller on b than on a.
B.the wall time is now greater on b than on a.
C.the wall time is still the same on a and b, though it is smaller than before on both of them.
D.it is impossible to change the hit ratio of a program.
8. consider the following fragments from two versions of a program. version a version b
// version a
for (i = 0 ; i < n ; i++ ) {
read(i);
calculate(i);
write(i);
}
// version b
for (i = 0 ; i < n ; i++ ) {
read(i);
}
for (i = 0 ; i < n ; i++ ) {
calculate(i);
}
for (i = 0 ; i < n ; i++ ) {
write(i);
}
which of the following are true of version b, compared to version a?
i b may be faster because of cache effects.
ii b may be slower because of cache effects.
iii b may execute at essentially the same speed as a.
9. about the cache in a computer system, which is true
(a) every computer system has 3 level cache, that is l1, l2, l3 cache
(b) every computer systems’ cache system have data cache and instruction cache
? every computer systems’ cache system has 2 level cache, that is l1, and l2 cache
(d) every computer system’s cache system has l1 and l2 cache inside cpu chip(10.0分)
A.ii and iii only
B.none
C.i only
D.i and iii only
10. two computers a and b with a cache in the cpu chip differ only in that a has an l2 cache and b does not. which of the following are possible?
i.b executes a program more quickly than a.
ii.a executes a program more quickly than b.
iii.while executing a program, a fetches more data from main memory than does b.
A.i and ii only.
B.i, ii and iii.
C.i and iii only.文章來源:http://www.zghlxwxcb.cn/news/detail-789421.html
D.ii only.文章來源地址http://www.zghlxwxcb.cn/news/detail-789421.html
到了這里,關(guān)于四川大學(xué)軟件學(xué)院|系統(tǒng)級編程期末復(fù)習(xí)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!