第二章
-
Upload
lance-oconnor -
Category
Documents
-
view
17 -
download
0
description
Transcript of 第二章
4
N 2-N 1-
01
2
(a) 整數取 N 模數的圓表示法
00000001
0010
0011
0100
0101
01100111
10001001
1010
1011
1100
1101
11101111
1+1-2+
3+
4+
5+6+
7+
2-3-
4-
5-6-
7- 8-
0
(b)2 的補數之 mod 16 系統
圖 2.3 模數系統與 2 的補數系統
5
1 0 1 11 1 1 0
1 0 0 1
1 1 0 11 0 0 1
0 0 1 00 1 0 0
0 1 1 00 0 1 1
1 0 0 11 0 1 1
1 0 0 10 0 0 1
0 0 1 01 1 0 1
1 1 1 0
0 1 0 01 0 1 0
0 1 1 11 1 0 1
0 1 0 0
1 1 0 10 1 1 1
0 1 0 0
0 0 1 01 1 0 0
1 1 1 0
0 1 1 01 1 0 1
0 0 1 1
1 0 0 10 1 0 1
1 1 1 0
1 0 0 11 1 1 1
1 0 0 0
0 0 1 00 0 1 1
0 1 0 1
0 1 0 1
0 0 1 00 0 1 1
5-
2+( )3+( )
5+( )
2+( )4+( )
2-
7-
3- 7-
6+( )3+( )
1+( )
7- 5-
7-
2+( )3-
6-
2-
4+( )
3-
4+( )
7+( )
4+( )
2-
3+( )
2-
8-
5+( )
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
(a)
(c)
(b)
(d)
(e)
(f)
(g)
(h)
(i)
(j)
圖 2.4 2 的補數之加減法運算
7
算數溢位
考慮 4-bit表示範圍 -8 ~ +7+7 + (+4) = 0111 + 0100 =
1011(-5)-4+(-6)=1100+1010=0110(+6)原因 11 與 -10 超過 4-bit 的表示
範圍 結論 :sign bit 不應該有進位 偵測方式 : 同號數相加與其和的
正負號不同表示有溢位產生
9
Address space( 位址空間 )
CPU 有 24 條定址線則有 224 (16M) Address space
位元組 (byte)固定 8 bits
字組 (word)16~64 bits
定址基本單位為位元組 (byte)Byte-addressable memory( 可定
址位元組的記憶體 )
10
跨字組之位元組排列方式(Endian Order )
little-endian( 正放指派 ) The little end comes first the low-order byte of the number is stored in
memory at the lowest address, and the high-order byte at the highest address ( 低位元組在低位址 , 高位元組在高位址 )
Byte3 Byte2 Byte1 Byte0 (4 bytes 整數 ) Base Address+0 Byte0 Base Address+1 Byte1 Base Address+2 Byte2 Base Address+3 Byte3 Intel processors (those used in PC's)
big-endian ( 倒放指派 ) The big end comes first Motorola processors
(those used in Mac's) Alignment( 對其位址 )
影響存取速度
12
Register Transfer Notation (RTN)
記憶體位置位址名稱 LOC 、 PLACE 、 A 、 VAR2(MEMORY) R0 、 R5(REGISTER) DATAIN 、 OUTSTATUS(I/O)
RTN 右邊表示值 左邊表示位置名稱
記憶體內容 R1 [LOC] R3 [R1]+[R2]
13
Assembly 表示法
Move LOC, R1 Add R1, R2, R3 Intex Syntax (op dest, sour)
mov eax,1 mov ebx,0ffh int 80h
AT&T Syntax (op sour, dest)movl $1,%eax movl $0xff,%ebx int $0x80
14
指令長度
C = A + B C [A] + [B] Three operands
Add A,B,C Two operands
Move B,C Add A,C
Single operand (accumulator register implied) Load A Add A Store C
15
算數運算的運算元考量
C = A + B 算數運算的運算元只能用 registers
指令數目變多 , 指令長度變短 Move A, Ri Move B, Rj Add Ri, Rj Move Rj, C
算數運算的運算元只能用一個記憶體(or register) 指令數目變少 , 指令長度變長 Move A,Ri Add B, Ri Move Ri,C
17
NUM n
NUM2
NUM1
R0,SUM
NUM n,R0
NUM3,R0
NUM2,R0
NUM1,R0
圖 2.9 把 n 個數相加的直線順序程式
Add
Add
Move
SUM
i
Move
Add
i 4n+
i 4n 4-+
i 8+
i 4+
•••
•••
•••
18
N,R1Move
NUM n
NUM2
NUM1
R0,SUM
R1
圖 2.10 使用迴圈來把 n 個數相加
LOOP
Decrement
Move
LOOP
程式迴圈
決定“下一個”數值的位址並且把“下一個”數值加到 R0
N
SUM
n
R0Clear
Branch>0
•••
•••
19
定址模式 (Addressing mode)
各種指定運算位元位置的不同方式 立即定址 move #200,R0 (moveI 200,R0) 暫存器定址 move R1, R2 絕對 ( 直接 ) 定址 move LOC, R2 間接定址 (pointer)
Add (R1), R0 Add (A),R0
(trace fig 2.12 p2-33) 索引定址 X(Ri)
Add 20(R1), R2 ( 常見 ) Add 1000(R1),R2
索引基底 (Ri,Rj) 索引偏移量定址 X(Ri,Rj) 相對定址 X(PC) 自動遞增 (Ri)+ 先提取 (Ri), 再將 Ri 加一個單位 自動遞減 – (Ri) 再將 Ri 減一個單位 , 先提取 (Ri)
20
遲算元1020
圖 2.13 索引定址
Add 1000(R1),R2
R1
R1
Add 20(R1),R2
遲算元1020
201000
20 = 偏移量
20 = 偏移量
10001000
(a) 偏移量是以常數來表示
(b) 偏移量位於暫存器中
21
圖 2.14 學生成績列表
學生 1
學生 2
Test 3
Test 2
Test 1
Student ID
Test 3
Test 2
Student ID
nN
LIST
Test 1LIST + 4
LIST + 8
LIST + 12
LIST + 16
•••
22
NUM2
NUMn
NUM1
R0Clear
R0,SUM
R1
#4,R2
(R2),R0
圖 2.17 圖 2.12 中的程式之記憶體配置Trace fig 2.18 p 2-46
100
132
604
212
208
204
200
128
124
120
116
112
108
104
100
SUM
N
LOOP
LOOP
Decrement
Add
Add
Move
#NUM1,R2
N,R1Move
Move
Branch>0
23
assebmler Symbol table Two-pass assembler
1st pass builds symbol table Eg. Forward branch refer to
undefined yet symbol, this symbol is not scanned yet.
24
Input/Output Techniques Program-controller I/O
CPU has direct control over I/O Sensing status Read/write commands Transferring data
CPU waits for I/O module to complete operation Wastes CPU time CPU polling peripheral
Interrupted I/O Overcomes CPU waiting No repeated CPU checking of device I/O module interrupts when ready CPU interrupted by hardware
Direct Memory Access (DMA) Interrupt driven and programmed I/O require active CPU
intervention Transfer rate is limited CPU is tied up
Additional Module (hardware) on bus DMA controller takes over from CPU for I/O
25
I/O addressing
Memory-mapped I/O The use of the same instructions and
bus to communicate with both main memory and input/output devices
uses a section of memory for I/O Don’t need special I/O instructions
port-mapped I/O (Direct I/O access) the 8086 used IN and OUT to read
and write to I/O devices Need special I/O instructions
27
圖 2.22 堆疊運作對圖 2.21 中的堆疊所造成的影響
(b) 取出 ITEM 之後(a) 推入 NEWITEM 之後
17
739
43
ITEM
SP
堆疊
SP
NEWITEM
19
17
739
19
43
28-
28-28-
•••
•••
28
Stack operations
LIFO(last-in-first-out) BP – base pointer (bottom) SP – stack pointer (top) push
Subtract #4, SP Move newitem, (SP) Or move newitem, -(SP)
Pop Move (SP), item Add #4, SP Or move (SP)+, item
29
Queue
FIFO(first-in-first-out) En-queue
Add item De-queue
Remove item Need tow pointers to point to
head and tail Move forward to high memory Circular buffer
30
傳回呼叫
圖 2.24 使用連結暫存器的子常式連結
1000
204
204
Link register
PC
傳回
1000
記憶體 位置呼叫程式記憶體
位置
200204
呼叫 SUB下一個指令
副常式 SUB
第一個指令
32
Parameter passing
Using registers to accept argument
Trace fig 2.25 p2-63 Drawback
How to handle many parameter? Solution
Using stack
33
假設堆疊頂端位於第一層之下
Move #NUM1, (SP) 把參數推入堆疊Move N, (SP)Call LISTADD 呼叫副常式
( 堆疊第二層的頂端 )Move 4(SP),SUM 儲存結果Add #8,SP 回復堆疊的頂端
( 堆疊第一層的頂端 )...
LISTADD MoveMultiple R0 R2, (SP) 儲存暫存器( 堆疊第三層的頂端 )
Move 16(SP),R1 把計數器初始成 nMove 20(SP),R2 讓指標指向串列開頭pClear R0 把總和初始成 n
LOOP Add (R2)+,R0 把元素加入串列中Decrement R1Branch>0 LOOPMove R0,20(SP) 把結果放入堆疊MoveMultiple (SP)+,R0 R2 回復暫存器Return 回到呼叫程式
[R2]
[R1]
[R0]
回傳位址n
NUM1
第三層
第二層
第一層
––
– –
圖 2.26 把圖 2.16 的程式寫成副常式;堆疊中傳遞的參數
(a) 呼叫程式與副常式
(b) 不同時期的堆疊頂端
–
35
Procedure prologue & epilogue
prologue Move fp, -(sp) ( 將 sp – 4 後 , 再將 fp copy 到
stack, 將舊 fp(calling proc) 存起來 ) Move sp,fp (fp sp) 建立新的 frame pointer OR Push bp ( 將舊 bp(calling proc) 存起來 ) Move sp,bp (bp sp), 建立新的 frame
pointer Epilogue
Move fp,sp (sp fp) Move fp,(sp)+ RET Or Move bp,sp (sp bp) Pop bp RET
36
圖 2.27 副常式的堆疊窗格範例
堆疊指標
窗格指標
saved [R1]
saved [R0]
被呼叫副常式的堆疊窗格
Return address
localvar3
localvar2
localvar1
saved [FP]
舊的 TOS
param2
param1
param3
param4
呼叫者參數
Call 指令
Move FP,-(SP)Move SP,FP
New FP
37
記憶體位置 指令 註解
主程式...
2000 Move PARAM2, (SP) 把參數放到堆疊2004 Move PARAM1, (SP)2008 Call SUB12012 Move (SP),RESULT 儲存結果2016 Add #8,SP 回復堆疊的層級2020 下一個指令
...
第一層副常式2100 SUB1 Move FP , (SP) 儲存窗格指標暫存器2104 Move SP,FP 載入窗格指標2108 MoveMultiple R0 R3, (SP) 儲存暫存器2112 Move 8(FP),R0 取得第一個參數
Move 12(FP),R1 取得第二個參數...Move PARAM3, (SP) 把參數放到堆疊 .
2160 Call SUB22164 Move (SP)+,R2 取出 SUB2 的結果放入
...Move R3,8(FP) 把答案放到堆疊MoveMultiple (SP)+,R0 R3 回復暫存器Move (SP)+,FP 回復窗格指標暫存器Return 回到主程式
第二層子常式3000 SUB2 Move FP , (SP) 儲存窗格指標暫存器v
Move SP ,FP 載入窗格指標MoveMultiple R0 R1, (SP) 儲存暫存器 R0 與 R1Move 8(FP),R0 取得參數...Move R1,8(FP) 把 SUB2 的結果放到堆疊MoveMultiple (SP)+,R0R1 回復暫存器 R0 與 R1Move (SP)+,FP 回復窗格指標暫存器Return 回到子常式 1
––
–
– –
–
–
–
– –
–
圖 2.28 巢狀子常式
FP 指向stack 頂端
FP 指向stack 頂端
param1
param2
param3
存入param3
Pop fp (of Sub1)
Pop 2164 to PC
此時指向 param3 存入param1
Pop param3
Pop R3~R0
Pop fp (of main)
Pop 2012 to PC
此時指向 param1
將 param1 存入 RESULT
Pop param1,param2
此時 SP 指向舊的 TOS, 回復到call sub1,sub2 之前的狀態
38
圖 2.29 圖 2.28 的堆疊窗格
FP
FP
[FP] from SUB1
2164
第一個副常式的堆疊窗格
[R0] from Main
param3
[R3] from Main
[R2] from Main
[R1] from Main
舊的 TOS
2012
[FP] from Main
param1
param2
[R0] from SUB1
[R1] from SUB1
第二個副常式的堆疊窗格
高記憶體位址
低記憶體位址
39
Logical instructions
R0 contains 4 ASCII We want to check if the left
most byte (MSB) is Z(5A) or not
And #$FF000000,R0 Compare #$5A000000,R0 Branch=0 YES And instruction is used to set
uninterested bits to 0
40
圖 2.30 邏輯與算術移位指令
CR00
before:
after:
0
1
0 0 01 1 1 . . . 11
0 0 1 1 1 000
(b) 邏輯右移 LShiftR #2,R0
(a) 邏輯左移 LShiftL #2,R0
C R0 0
before:
after:
0
1
0 0 01 1 1 . . . 11
1 10 . . . 00101
C
before:
after:
0
1
1 1 00 0 1 . . . 01
1 1 0 0 1 011
(c) 算術右移 AShiftR #2,R0
R0
. . .
. . .
41
圖 2.32 旋轉指令
CR0
before:
after:
0
1
0 0 01 1 1 . . . 11
1 0 1 1 1 001
(c) 不含進位的向右旋轉 RotateR #2,R0
(a) 不含進位的向左旋轉 RotateL #2,R0
C R0
before:
after:
0
1
0 0 01 1 1 . . . 11
1 10 . . . 10101
C
before:
after:
0
1
0 0 01 1 1 . . . 11
1 0 1 1 1 000
(d) 包含進位的向右旋轉 RotateRC #2,R0
R0
. . .
. . .
(b) 包含進位的向左旋轉 RotateLC #2,R0
C R0
before:
after:
0
1
0 0 01 1 1 . . . 11
1 10 . . . 00101
42
Packed-bcd number
LOC = xxxxyyyy LOC+4=zzzzwwww We want to pack yyyywwww Move #LOC,R0 MoveByte (R0)+,R1 LShiftL #4,R1 MoveByte (R0),R2 And #$F,R2 Or R1,R2 MoveByte R2, PACKED
LOC+4
XXXXYYYY
ZZZZWWWW
LOC
43
for (j = n 1; j > 0; j = j 1){ for ( k = j 1; k > = 0; k = k 1 )
{ if (LIST[k] > LIST[ j]){ TEMP = LIST[k];
LIST[k] = LIST[j];LIST[ j] = TEMP;
}}
}
Move #LIST,R0 把 LIST 載入基底暫存器 R0 。Move N,R1 初始化外部迴圈的索引暫存器
R1 成 j = n - 1 。Subtract #1,R1OUTER Move R1,R2 初始化內部迴圈的索引暫存器
R2 成 k = j - 1 。Subtract #1,R2MoveByte (R0,R1),R3 把 LIST(j) 載入記錄子串列目
前最大的數之 R3 。INNER CompareByte R3,(R0,R2) 若是 LIST(k) [R3] 則不互相交換。
Branch 0 NEXTMoveByte (R0,R2),R4 否則交換 LIST(k) 與 LIST(j) ,
並且把新的最大數載入 R3 。MoveByte R3,(R0,R2)MoveByte R4,(R0,R1) 暫存器 R4 扮演 TEMP 的角色。MoveByte R4,R3
NEXT Decrement R2 遞減也充當迴圈計數器的索引暫存器 R2 與 R1,若是迴圈尚未完成則跳回。
Branch 0 INNERDecrement R1Branch> 0 OUTER
(b) 排序處理的組合語言程式
圖 2.34 使用直接選擇排序法的位元組排序程式
––––
(a) 排序處理的 C 語言程式
45
圖 2.36 在記憶體中以鏈結串列來組織的學生測驗成績
第一筆記錄
28106 12001040
1 個字組 1 個字組 3 個字組首端
(ID) ( 測驗分數 )記憶體位址 鍵欄位 鏈結欄位 資料欄位
27243 10402320
40632 12802720
28370 28801200
47871 01280
第二筆記錄
第三筆記錄
倒數第二筆 記錄
最後一筆記錄 末端
•••
46
INSERTION Compare
Branch>0Move RNEWREC, RHEAD
Return
Compare (RHEAD), (RNEWREC)
Branch>0 SEARCH
Move RHEAD, 4(RNEWREC)
Move RNEWREC, RHEAD
Return
Move RHEAD, RCURRENT
Move 4(RCURRENT), RNEXT
Compare
Branch=0 TAIL
(RNEXT), (RNEWREC)
Branch<0
#0, RNEXT
Compare
HEAD
不為空
#0, RHEAD
HEAD
INSERTMove RNEXT, RCURRENT
Move
Branch
RNEXT, 4(RNEWREC)Move RNEWREC, 4(RCURRENT)
Return
SEARCH
將新紀錄插入目前首端之最後的某一地方
LOOP
將新紀錄插入 內部位置中
新紀錄變為新的尾端
INSERT
TAIL
LOOP
新紀錄變為一個包含一個元素的串列
新紀錄變為新的首端
圖 2.37 用來把一筆新記錄插入鏈結串列的副常式
若將 RHEAD,RNEWREC,RCURRENT,RNEXT改成一般的 Registers, 則此程式幾乎無法閱讀
48
OP碼
圖 2.39 把指令編入長度為 32 個位元的字組中
來源 目的 其他資訊
8 4+3 4+3 10
(b) 包含兩個運算元的指令
記憶體位址 / 立即運算元
OP碼
(c) 包含三個運算元的指令
R i R j 其他資訊R k
OP碼 來源 目的 其他資訊
(a) 包含一個運算元的指令 索引值或立即運算元
2nd word
49
機器指令的長度
Suppose we have 16 registers 256 opcode and 8 addressing mode
Consider Add R1,R2Opcode:8 bits, source registers:4
bits, destination registers: 4 bits3 bits to encode addressing modeThus we need 8+4+3+4+3=22
bits at least to encode the machine instruction Add R1, R2
50
機器指令的長度 (cont)
Consider move 24(R0),R5 Opcode:8 bits, source registers:4 bits,
destination registers: 4 bits 3 bits to encode addressing mode Thus we leave 10 bits(32-8-4-3-4-3)
for index value(24), that is we can index 210 = 1024 items
Consider LShiftR #2,R0 and move #$3A, R1 Opcode:8 bits, source register:4 bits,
source addressing:3 bits, immediate addressing: 3 bits
Thus we use 8+4+3+3=18 bits and leave 14 bits for the size of immediate value
51
機器指令的長度 (cont)
Consider Branch>0 LOOP Opcode:8 bits We have 24 bits for branch offset,
consider positive and negative offset then we have 223 offset
Consider Move R2,LOC Opcode:8 bits, source registers:4 bits,
source addressing:3 bits, absolute addressing(LOC) 3 bits
We use 8+4+3+3=18 bits and leave 14 bits for representing the address of LOC (cannot address 32 bits memory space)
Solution:use 2nd word to represent the whole address then we can encode And #$FF000000,R2 where FF000000 is in the 2nd word
52
機器指令的長度 (cont)
If we allow memory to memory operation(rarely seen in modern processor) then we need more longer instruction
Consider move LOC1, LOC2We need 2nd for LOC1, and 3rd
for LOC2
53
CISC vs. RISC
CISC 允許machine instruction 的長度不一 , 例如
Intel machine instructions 的長度可以有 1 byte, 2 bytes, 3 bytes, 4 bytes
使用多個字組可以實作較複雜 , 較接近高階程式語言作業方式的指令
包含較多且較複雜的指令但完成一件事所需指令數較少
RISC 20% of the instructions in a computer did 80%
of the work (by John Cocke of IBM Research ) 所有指令長度固定 ( 例如 32 bits) 所有的 ALU 計算只能作用在 register-register
( 不能有 register-memory) Memory reference 只能用 Load (for read),
Store (for write) 指令 包含較少且較簡單的指令但完成一件事所需指
令數較多
54
表 2.1 通用的定址模式
名稱 組合語言語法 定址函式
立即 #Value 運算元= Value
暫存器 Ri EA= Ri
絕對 ( 直接 ) LOC EA= LOC
間接 (Ri ) EA= [Ri](LOC) EA= [LOC]
索引 X(Ri) EA= [Ri]+ X
以索引為基底 (Ri ,Rj ) EA= [Ri]+ [Rj]
以索引與偏移量為基底 X(Ri,Rj ) EA= [Ri]+ [Rj] + X
相對 X(PC) EA= [PC] + X
自動遞增 (Ri )+ EA= [Ri] ;Increment R i
自動遞減 (Ri ) Decrement R i ;EA= [Ri]
EA =有效位址 (effectiveaddress)
Value = 有號數