1 / 54

第二章

第二章. 數值表示法. 數值表示法. 符號大小值 (sign magnitude) 1 補數 (1 ’ s complement) 2 補數 (2 ’ s complement). 補數的加、減法. 1 ’ s complement 進位要加到最後一個 bit( 校正 ) 較不方便 2 ’ s complement 進位捨去 比較自然 減法以加法方式計算. 0. 1. N. -. 1. N. -. 2. 2. ( a) 整數取 N 模數的圓表示法. 0000. 1111. 0001. 1110. 0010. 0. -. 1.

Download Presentation

第二章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第二章 • 數值表示法

  2. 數值表示法 • 符號大小值(sign magnitude) • 1補數(1’s complement) • 2補數(2’s complement)

  3. 補數的加、減法 • 1’s complement • 進位要加到最後一個bit(校正) • 較不方便 • 2’s complement • 進位捨去 • 比較自然 • 減法以加法方式計算

  4. 0 1 N - 1 N - 2 2 (a)整數取N模數的圓表示法 0000 1111 0001 1110 0010 0 - 1 + 1 - 2 + 2 1101 0011 - 3 + 3 - 4 + 4 1100 0100 - 5 + 5 1011 0101 - 6 + 6 - 7 + 7 - 8 1010 0110 1001 0111 1000 (b)2的補數之mod 16系統 圖 2.3 模數系統與 2 的補數系統

  5. ( + 4 ) 0 0 1 0 ( + 2 ) 0 1 0 0 ( ) 0 0 1 1 + 3 1 0 1 0 ( - 6 ) ( - 2 ) 0 1 0 1 ( + 5 ) 1 1 1 0 ( ) + 7 1 0 1 1 ( - 5 ) 0 1 1 1 1 1 1 0 ( - 2 ) 1 1 0 1 ( - 3 ) 1 0 0 1 ( - 7 ) 0 1 0 0 ( + 4 ) 1 1 0 1 1 1 0 1 ( - 3 ) 1 0 0 1 0 1 1 1 ( - 7 ) 0 1 0 0 ( + 4 ) 0 0 1 0 ( + 2 ) 0 0 1 0 ( ) 0 1 0 0 + 4 1 1 0 0 1 1 1 0 ( - 2 ) 0 1 1 0 0 1 1 0 ( + 6 ) ( + 3 ) 0 0 1 1 1 1 0 1 0 0 1 1 ( + 3 ) 1 0 0 1 1 0 0 1 ( - 7 ) 0 1 0 1 1 0 1 1 ( - 5 ) 1 1 1 0 ( - 2 ) 1 0 0 1 ( - 7 ) 1 0 0 1 0 0 0 1 ( + 1 ) 1 1 1 1 1 0 0 0 ( - 8 ) 0 0 1 0 0 0 1 0 ( + 2 ) 1 1 0 1 0 0 1 1 ( - 3 ) ( ) 0 1 0 1 + 5 (a) (b) + + (c) (d) + + (e) + - (f) - + (g) - + (h) - + (i) - + (j) - + 圖 2.4 2 的補數之加減法運算

  6. Sign extension • 正數左邊延伸加0 • 0111  7 • 00000111  7 • 負數左邊延伸加1 • 1001  -7 • 11111001  -7

  7. 算數溢位 • 考慮4-bit • 表示範圍-8 ~ +7 • +7 + (+4) = 0111 + 0100 = 1011(-5) • -4+(-6)=1100+1010=0110(+6) • 原因 11與-10超過4-bit的表示範圍 • 結論:sign bit不應該有進位 • 偵測方式:同號數相加與其和的正負號不同表示有溢位產生

  8. n 個位元 第1個字組 第2個字組 • • • 第1個字組 • • • 最後字組 圖 2.5 記憶體字組

  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(對其位址) • 影響存取速度

  11. 字串表示法 • 特殊結束字元 • 例如 C 的 ‘\0’ • 紀錄字串長度 • 需要另外的記憶體存放此長度

  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

  16. 位址 內容 i 從這裡開始執行 Move A,R0 i + 4 Add B,R0 三指令程式區段 i + 8 Move R0,C A B 程式的資料 C 圖 2.8 C ← [A] + [B] 的程式

  17. Move NUM1,R0 i i + 4 Add NUM2,R0 i + 8 Add NUM3,R0 • • • i Add NUM n ,R0 + 4 n - 4 i 4 n + Move R0,SUM • • • SUM NUM1 NUM2 • • • NUM n 圖 2.9 把 n 個數相加的直線順序程式

  18. Move N,R1 Clear R0 LOOP 決定“下一個”數值的 位址並且把“下一個” 數值加到R0 程式迴圈 Decrement R1 Branch>0 LOOP Move R0,SUM • • • SUM N n NUM1 NUM2 • • • NUM n 圖 2.10 使用迴圈來把 n 個數相加

  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. Add 20(R1),R2 R1 1000 1000 20 = 偏移量 1020 遲算元 (a) 偏移量是以常數來表示 Add 1000(R1),R2 1000 20 R1 20 = 偏移量 遲算元 1020 (b) 偏移量位於暫存器中 圖 2.13 索引定址

  21. n N Student ID LIST LIST + 4 Test 1 學生1 LIST + 8 Test 2 LIST + 12 Test 3 LIST + 16 Student ID Test 1 學生2 Test 2 Test 3 • • • 圖 2.14 學生成績列表

  22. 100 Move N,R1 104 Move #NUM1,R2 108 Clear R0 LOOP 112 Add (R2),R0 116 Add #4,R2 120 Decrement R1 124 Branch>0 LOOP 128 Move R0,SUM 132 SUM 200 N 204 100 NUM1 208 NUM2 212 NUM n 604 圖 2.17 圖 2.12 中的程式之記憶體配置 Trace fig 2.18 p 2-46

  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

  26. 0 • • • 堆疊指標 暫存器 目前的 頂端元素 SP - 28 17 739 堆疊 • • • 底端元素 43 底端 • • • k 2 - 1 圖 2.21 記憶體中內容為字組的堆疊

  27. SP 19 - 28 - 28 17 SP 17 739 739 堆疊 • • • • • • 43 43 NEWITEM 19 ITEM - 28 (b) 取出 ITEM 之後 (a) 推入 NEWITEM 之後 圖 2.22 堆疊運作對圖 2.21 中的堆疊所造成的影響

  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. 記憶體 位置 記憶體 位置 呼叫程式 副常式SUB 呼叫SUB 200 1000 第一個指令 204 下一個指令 傳回 1000 PC 204 204 Link register 呼叫 傳回 圖 2.24 使用連結暫存器的子常式連結

  31. Subroutine nesting • Link register corrupt • Solution • Using stack

  32. Parameter passing • Using registers to accept argument • Trace fig 2.25 p2-63 • Drawback • How to handle many parameter? • Solution • Using stack

  33. 假設堆疊頂端位於第一層之下 – Mo v e #NUM1, (SP) 把參數推入堆疊 – Mo v e N, (SP) Call LIST ADD 呼叫副常式 (堆疊第二層的頂端) Mo v e 4(SP),SUM 儲存結果 Add #8,SP 回復堆疊的頂端 (堆疊第一層的頂端) . . . – – LIST ADD Mo v eMultiple R0 R2, (SP) 儲存暫存器 (堆疊第三層的頂端) Mo v e 16(SP),R1 把計數器初始成 n Mo v e 20(SP),R2 讓指標指向串列開頭 p Clear R0 把總和初始成 n LOOP Add (R2)+,R0 把元素加入串列中 Decremen t R1 Branc h LOOP > 0 Mo v e R0,20(SP) 把結果放入堆疊 – Mo v eMultiple (SP)+,R0 R2 回復暫存器 Return 回到呼叫程式 (a) 呼叫程式與副常式  [R2] 第三層 [R1] [R0]  第二層 回傳位址 n NUM1  第一層 (b) 不同時期的堆疊頂端 圖 2.26 把圖 2.16 的程式寫成副常式;堆疊中傳遞的參數

  34. Stack Frame Call sub Move sp, bp Bp 指向sp Push bp

  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. saved [R1] 堆疊指標 saved [R0] localvar3 New FP Move FP,-(SP) localvar2 Move SP,FP localvar1 被呼叫副常式 的堆疊窗格 窗格指標 saved [FP] Call 指令 Return address param1 param2 呼叫者參數 param3 param4 舊的TOS 圖 2.27 副常式的堆疊窗格範例

  37. 記憶體位置 指令 註解 主程式 . . . – 2000 Mo v e P ARAM2, (SP) 把參數放到堆疊 – 2004 Mo v e P ARAM1, (SP) 2008 Call SUB1 將param1存入RESULT 2012 Mo v e (SP),RESUL T 儲存結果 Pop param1,param2 2016 Add #8,SP 回復堆疊的層級 2020 下一個指令 此時指向param1 . . . 此時SP指向舊的TOS,回復到call sub1,sub2之前的狀態 第一層副常式 – 2100 SUB1 Mo v e FP , (SP) 儲存窗格指標暫存器 2104 Mo v e SP ,FP 載入窗格指標 FP 指向stack頂端 – – 2108 Mo v eMultiple R0 R3, (SP) 儲存暫存器 2112 Mo v e 8(FP),R0 取得第一個參數 param1 Mo v e 12(FP),R1 取得第二個參數 . . . param2 – Mo v e P ARAM3, (SP) 把參數放到堆疊 . 2160 Call SUB2 Pop param3 2164 Mo v e (SP)+,R2 取出 SUB2的結果放入 . 此時指向param3 . . 存入param1 Mo v e R3,8(FP) 把答案放到堆疊 Pop R3~R0 – Mo v eMultiple (SP)+,R0 R3 回復暫存器 Mo v e (SP)+,FP 回復窗格指標暫存器 Pop fp (of main) Return 回到主程式 Pop 2012 to PC 第二層子常式 – 3000 SUB2 Mo v e FP , (SP) 儲存窗格指標暫存器 v FP 指向stack頂端 Mo v e SP ,FP 載入窗格指標 – – Mo v eMultiple R0 R1, (SP) 儲存暫存器 R0 與 R1 Mo v e 8(FP),R0 取得參數 param3 . . . 存入param3 Mo v e R1,8(FP) 把 SUB2 的結果放到堆疊 – Mo v eMultiple (SP)+,R0 R1 回復暫存器 R0 與 R1 Mo v e (SP)+,FP 回復窗格指標暫存器 Return 回到子常式 1 Pop fp (of Sub1) Pop 2164 to PC 圖 2.28 巢狀子常式

  38. 低記憶體位址 [R1] from SUB1 [R0] from SUB1 第二個副常式 的堆疊窗格 [FP] from SUB1 FP 2164 param3 [R3] from Main [R2] from Main [R1] from Main [R0] from Main 第一個副常式 的堆疊窗格 FP [FP] from Main 2012 param1 param2 舊的TOS 高記憶體位址 圖 2.29 圖 2.28 的堆疊窗格

  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. C R0 0 . . . before: 0 0 1 1 1 0 0 1 1 . . . after: 1 1 1 0 0 1 1 0 0 (a) 邏輯左移 LShiftL #2,R0 0 R0 C . . . 0 1 1 1 0 0 1 1 before: 0 . . . after: 0 0 0 1 1 1 0 0 1 (b) 邏輯右移 LShiftR #2,R0 R0 C . . . before: 0 1 0 0 1 1 0 1 0 . . . after: 1 1 1 0 0 1 1 0 1 (c) 算術右移 AShiftR #2,R0 圖 2.30 邏輯與算術移位指令

  41. C R0 . . . before: 0 1 1 1 0 0 1 1 0 . . . after: 1 1 1 0 0 1 1 0 1 (a) 不含進位的向左旋轉 RotateL #2,R0 C R0 . . . before: 0 0 1 1 1 0 0 1 1 . . . after: 1 1 1 0 0 1 1 0 0 (b) 包含進位的向左旋轉 RotateLC #2,R0 R0 C . . . 0 before: 0 1 1 1 0 0 1 1 . . . after: 1 1 0 1 1 1 0 0 1 (c) 不含進位的向右旋轉 RotateR #2,R0 R0 C . . . 0 1 1 1 0 0 1 1 0 before: . . . 1 after: 1 0 0 1 1 1 0 0 (d) 包含進位的向右旋轉 RotateRC #2,R0 圖 2.32 旋轉指令

  42. XXXXYYYY LOC LOC+4 ZZZZWWWW 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

  43. – (j = n 1; j > 0; j = j 1) for – – { ( k = j 1; k > = 0; k = k 1 ) for { (LIST[ k ] > LIST[ j ]) if { TEMP = LIST[ k ]; LIST[ k ] = LIST[ ]; j LIST[ j ] = TEMP; } } } (a) 排序處理的 C 語言程式 Mo v e #LIST,R0 把 LIST 載入基底暫存器 R0。 Mo v e N,R1 初始化外部迴圈的索引暫存器 R1 成 j = n - 1。 Subtract #1,R1 OUTER Mo v e R1,R2 初始化內部迴圈的索引暫存器 R2 成 k = j - 1。 Subtract #1,R2 Mo v eByte (R0,R1),R3 把 LIST(j) 載入記錄子串列目 前最大的數之 R3。 INNER CompareByte R3,(R0,R2) 若是 LIST(k)  [R3] 則不互相交換。  Branc h 0 NEXT Mo v eByte (R0,R2),R4 否則交換 LIST(k) 與 LIST(j), 並且把新的最大數載入 R3。 Mo v eByte R3,(R0,R2) Mo v eByte R4,(R0,R1) 暫存器 R4 扮演 TEMP 的角色。 Mo v eByte R4,R3 NEXT Decremen t R2 遞減也充當迴圈計數器的索引 暫存器 R2 與 R1,若是迴圈尚未 完成則跳回。  Branc h 0 INNER Decremen t R1 Branc h > 0 OUTER (b) 排序處理的組合語言程式 圖 2.34 使用直接選擇排序法的位元組排序程式

  44. Link list • 使用連續記憶體的缺點 • 新增刪除資料後會產生缺口 • 須做記憶體搬移以避免浪費記憶體 • Solution • Link list • 紀錄不必位於連續記憶體 • 新增刪除容易達成

  45. 鍵欄位 鏈結欄位 資料欄位 記憶體位址 (ID) (測驗分數) 1個字組 1個字組 3個字組 第一筆記錄 2320 27243 1040 首端 1040 28106 1200 第二筆記錄 第三筆記錄 1200 28370 2880 • • • 倒數第二筆 記錄 2720 40632 1280 1280 47871 0 末端 最後一筆記錄 圖 2.36 在記憶體中以鏈結串列來組織的學生測驗成績

  46. INSER TION Compare #0, RHEAD Branch>0 HEAD 新紀錄變為 一個包含一個 元素的串列 Mo v e RNEWREC, RHEAD 不為空 Return Compare (RHEAD), (RNEWREC) HEAD Branch>0 SEARCH Mo v e RHEAD, 4(RNEWREC) 新紀錄變為 新的首端 將新紀錄插入目前 首端之最後的某一地方 Mo v e RNEWREC, RHEAD Return Mo v e RHEAD, RCURRENT SEARCH Mo v e 4(RCURRENT), RNEXT LOOP Compare #0, RNEXT Branch=0 T AIL Compare (RNEXT), (RNEWREC) 新紀錄變為新的尾端 Branch<0 INSER T Mo v e RNEXT , RCURRENT 將新紀錄插入 內部位置中 Branch LOOP INSER T Mo v e RNEXT , 4(RNEWREC) Mo v e RNEWREC, 4(RCURRENT) T AIL Return 圖 2.37 用來把一筆新記錄插入鏈結串列的副常式 若將RHEAD,RNEWREC,RCURRENT,RNEXT改成一般的Registers,則此程式幾乎無法閱讀

  47. 刪除一筆紀錄 • Trace fig2.38 p2-88

  48. 8 4+3 4+3 10 OP碼 來源 目的 其他資訊 索引值或立即運算元 (a) 包含一個運算元的指令 OP碼 來源 目的 其他資訊 記憶體位址/立即運算元 2nd word (b) 包含兩個運算元的指令 OP碼 R i R j R k 其他資訊 (c) 包含三個運算元的指令 圖 2.39 把指令編入長度為 32 個位元的字組中

  49. 機器指令的長度 • Suppose we have 16 registers 256 opcode and 8 addressing mode • Consider Add R1,R2 • Opcode:8 bits, source registers:4 bits, destination registers: 4 bits • 3 bits to encode addressing mode • Thus 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

More Related