Aisu
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    2
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 計算機結構 <!-- --- ###### tags: `NTNU CSIE`, `大二下課程`, `必修`, `大二下修習` --> --- > [TOC] --- ## 第一章 - Computer Abstractions and Technology ### 1.1 - Introduction 現代計算機的分類: 1. 個人電腦(**Personal computers**) - 普通用途、多種軟體 - 價格與性能(CP 值)很重要 2. 伺服器電腦(**Server computers**) - 以網路連線為基礎 - 高容量、高性能、高可靠性 - Range from small servers to building sized 3. 超級電腦(**Supercomputers**) - 用於高階科學 & 工程計算 - 有著最高的性能、最低的市場需求 4. 嵌入式電腦(**Embedded computers**) - Hidden as components of systems - 嚴格限制權限、性能、價格 ### 1.2 - Eight Great Ideas in Computer Architecture 計算機系統結構中的 8 個 Great Ideas。 1. 設計考量摩爾定律(**Design for Moore's Law**) 系統結構設計的周期通常都比較長,由於摩爾定律的存在,很有可能項目開始時和項目結束時能夠提供的原件在工藝方面的性能就有很大的差距了,因此做系統結構設計需要考慮到這方面的因素。 p.s. 但到了 2017 年,各方都開始覺得摩爾定律要走到盡頭了。 2. 以抽象概念簡化設計(**Use Abstraction to Simplify Design**) 分層次設計、模組化(作業系統的 Layer approach、loadable kernel module),底層的細節對上層不透明,只要提供接口即可。 3. 優化常見情況(**Make the Common Case Fast**) 做優化要注重在常見情況上,找到瓶頸、最耗時的點才有用。 想找出常見情況則需要做 [針對性的測試(1.6)](https://hackmd.io/0GYhppFQS1-U5o9klxSbUw?both#16---Performance)。 4. 平行運算(**Performance via Parallelism**) 5. 管線運算 / 流水線運算(**Performance via Pipelining**) 多級流水可以提高資源利用率、隱藏延遲等等。 6. 預測運算(**Performance via Prediction**) 處理器指令預測、指令預取。 7. 記憶體分級(**Hierarchy of Memories**) 最突出的體現是在 cache 上。 p.s. 現在主流的處理器上 cache 已經做到了 L3,可能過幾年就有 L4 了。 8. 冗餘設計(**Dependability via Redundancy**) 通過冗餘設計來保證系統可靠性,實現容災備份。 ### 1.3 - Below Your Program 程式運行架構 & 過程: - 應用程式(**Application software**) - 以高機語言(high-level language, HLL)撰寫。 - 系統程式(**System software**) - 將高階語言編譯成組合語言(Assembly language)。 - 作業系統(Operating system, OS)則負責: - Handle input / output - Manage memory and storage - Schedule tasks & sharing resources - 硬體(**Hardware**) - Processor, memory, I/O controllers ### 1.4 - Under the Covers ### 1.5 - Technologies for Building Processors and Memory 電子元件的發展演變: > | 年分 | 電子元件技術 | 單位資源消耗能得到的相對性能 | > | ---- | ----------------------- | ----------------------- | > | 1951 | 電子管(Vacuum tube) | 1 | > | 1965 | 晶體管(Transistor) | 35 | > | 1975 | 集成電路(Integrated circuit, IC)| 900 | > | 1995 | Very large scale IC, VLSI | 2,400,000 | > | 2013 | Ultra large scale IC | 250,000,000,000 | CPU 芯片製造: > ![](https://i.imgur.com/UH3GRO0.png =650x) 延伸資料: - [CPU 製造的那些事之一:i7 和 i5 其實是孿生兄弟!?](https://zhuanlan.zhihu.com/p/29743431) - [CPU 製造的那些事之二:Die 的大小和良品率](https://zhuanlan.zhihu.com/p/29767262) - [為什麼晶圓都是圓的不是方的?](https://zhuanlan.zhihu.com/p/30513730) - [為什麼「電路」要鋪滿整個晶圓?](https://zhuanlan.zhihu.com/p/30549356) ### <span style="color: red;">**1.6 - Performance**</span> > - $Performance_x = \dfrac{1}{Execution\ time_x}$ > > - $Performance_x > Performance_y = \dfrac{1}{Execution\ time_x} > \dfrac{1}{Execution\ time_y}$ > > - $Execution\ time_y > Execution\ time_x$ - 效能比較:X 與 Y 的效能比數 n > $n = \dfrac{Performance_x}{Performance_y} = \dfrac{Execution\ time_y}{Execution\ time_x}$ - 執行時間(Execution Time): 系統效能針對的時間計量方式是前者,而 CPU 效能針對的時間計算方式是後者。 - Elapsed time / Response time / Wall clock time 跑某個任務實際花費的時間,包含了 I/O、訪存、操作系統 overhead... 等等。 - <span style="color: red;">**CPU time / CPU execution time**</span> CPU 跑某個任務消耗的時間,實際上是 CPU 跑了多少個時鐘週期,不包含其他部分消耗所花費的時間(I/O、線程切換... 等等)。 > --- > $CPU\ time = CPU\ clock\ cycles × Clock\ cycle\ time = \dfrac{CPU\ clock\ cycles}{Clock\ rate}$ > > $Clock\ rate = \dfrac{1}{CPU\ cycle\ time} = \dfrac{CPU\ clock\ cycles}{CPU\ time}$ > > --- > $Clock\ cycles = Instruction\ count × Clock\ cycles\ per\ instruction\ (CPI)$ > > $\begin{split} CPU\ time &= Instruction\ count × CPI × Clock\ cycle\ time \\\\&= \dfrac{Instruction\ count × CPI}{Clock\ rate} \end{split}$ > > --- > | 指令總數 | 計算指令 | 記億體載入指令 | 記憶體貯存指令 | 分支指令 | > | :-----: | :----: | :----------: | :---------: | :-----: | > | 20 萬 | 45% | 20% | 15% | 20% | > > | 機器 | 時脈頻率 | 計算 CPI | 記億體載入 CPI | 記憶體貯存 CPI | 分支 CPI | > | :-: | :-----: | :------------: | :----------: | :-----------: | :-----: | > | P1 | 1 GHz | 1 | 8 | 8 | 2 | > | P2 | 1.5 GHz | 80%:1 20%:2 | 10 | 10 | 2 | > - <span style="color: blue;">期中考題目(2013 - 1): > 一個程式在時脈頻率分別是 1 GHz 和 1.5 GHz 的兩部計算機(P1 和 P2)上執行,共執行了 20 萬個指令。其中,計算指令佔 45%,記億體載入指令佔 20%,記憶體貯存指令佔 15%,分支指令佔 20%。P1 的計算指令的 CPI 是 1,記憶體載人或貯存指令是 8,分支指令是 2。P2 的計算指令中有 80% 的 CPI 仍是 1,剩餘的計算指令的 CPI 增為 2,記億體載入或貯存指令是 10,分支指令也是 2。 > 請計算 P1 和 P2 的: > (1) 總執行時間。 > (2) CPI。 > (3) 比較兩機器的效能。 > - 解答: > 1. - P1 的總執行時間:$(45%×1+20%×8+15%×8+20%×2)\\×(2×10^5)÷(1×10^9)\\=0.00073\ 秒$ > - P2 的總執行時間:$(45%×(80%×1+20%×2)+20%×10+15%×10+20%×2)\\×(2×10^5)÷(1.5×10^9)\\=0.000592\ 秒$ > 1. - P1 的 CPI:$\\(45%×1+20%×8+15%×8+20%×2)\\=3.65$ > - P2 的 CPI:$\\(45%×(80%×1+20%×2)+20%×10+15%×10+20%×2)\\=4.44$ > 1. $效能比數\ n\\n=\dfrac{Performance_{P1}}{Performance_{P2}}=\dfrac{Execution\ time_{P2}}{Execution\ time_{P1}}=\dfrac{0.000592}{0.00073}\\=0.8109589$</span> ### 1.7 - The Power Wall 功耗與主頻幾乎是同等上升的,主頻越高功耗越高。電腦發展前期主頻提升很快,但到了最近,處理器的主頻基本不再提升,因為功耗已經達到了一個相當高的程度,在散熱等其他方面還沒辦法跟上的時候只能限制主頻的提升。 - 動態功耗 CPU 的功耗主要是動態功耗,來源於晶體管的開關切換,即高低電平(0 和 1)之間的翻轉,這中間本質上是個充放電的過程,所以這部分能量消耗是無法避免的。 > $Power = Capacitive\ load × Voltage ^ 2 × Frequency$ > (動態功耗)(電容負載)   (電壓)  (切換頻率) - 靜態功耗 主要來自於晶體管的漏電流,幾乎無法避免,只能通過工藝改進來減少。 ### 1.8 - The Sea Change(巨變):The Switch from Uniprocessors to Multiprocessors 受到工藝、功耗等的限制,已經無法再單純地提升主頻來提高處理器的性能,因此轉向提高處理器的並行能力來繼續發展 CPU,即單核心開始向多核心轉變。 之前由於性能的提升主要在工藝、硬體層面,對於軟體來說影響很小。但是多核處理器出現之後,也推動了平行演算法的發展,因為只有從演算法上進行改良才能夠更好地利用多核心處理器的優勢。 ### 1.9 - Real Stuff:Benchmarking(基準測試) the Intel Core i7 ### 1.10 - Fallacies and Pitfalls(謬誤 & 隱患) 提及:每秒百萬指令(Millions of Instructions Per Second, MIPS)。 > Amdahl's law(阿姆達爾定律):$T_{improved} = \dfrac{T_{affected}}{Improvement\ factor} + T_{unaffected}$ ### 1.11 - Concluding Remarks > $\dfrac{Seconds}{Program} = \dfrac{Instructions}{Program} × \dfrac{Clock\ cycles}{Instruction} × \dfrac{Seconds}{Clock\ cycle}$ ### 1.12 - Historical Perspective and Further Reading > [PDF](https://booksite.elsevier.com/9780124077263/downloads/historial%20perspectives/section_1.12.pdf) ### 1.13 - Exercises > [解答](http://jcf94.com/download/2015-09-09-cod-CH01_Solution.pdf) --- ## 第二章 - Instructions:Language of the Computer ### 2.1 - Introduction 第二章將以 MIPS 為例,介紹計算機指令。 ### 2.2 - Operations of the Computer Hardware **設計原則 1:簡單有助於規整(Simplicity favors regularity**)。 ### 2.3 - Operands of the Computer Hardware **設計原則 2:越少越快(Smaller is faster**)。 MIPS 有 32 個 32 位元的暫存器。 **設計原則 3:優化常見情況(Make the Common Case Fast**) - 記憶體運算元(**Memory Operands**) > Example:**A[12] = h + A[8];**(h in $s2, base address of A in $s3) ```Assembly= lw $t0, 32($s3) # load word add $t0, $s2, $t0 sw $t0, 48($s3) # store word ``` - 立即運算元(**Immediate Operands**) - Constant data specified in an instruction ```Assembly= addi $s3, $s3, 4 ``` - No subtract immediate instruction (Just use a negative constant) ```Assembly= addi $s2, $s1, -1 ``` - 常數 0(**Constant Zero**) - Useful for common operations ex:move between registers ```Assembly= add $t2, $s1, $zero ``` ### 2.4 - Signed and Unsigned Numbers - 2s-Complement Signed Integers > $x + \bar{x} = 1111...1111_2 = -1$ > $\bar{x} + 1 = -x$ > Example:negate +2 > > $+2 = 0000\ 0000\ ...\ 0010_2$ > $\begin{split} -2 &= 1111\ 1111\ ...\ 1101_2 + 1 > \\&= 1111\ 1111\ ...\ 1110_2 \end{split}$ - Sign Extension > Example:8-bit → 16-bit > > $+2:0000\ 0010 → {\color{LimeGreen}{0000\ 0000}}\ {\color{Blue}{0}}000\ 0010_2$ > $-2:1111\ 1110 → {\color{LimeGreen}{1111\ 1111}}\ {\color{Blue}{1}}111\ 1110_2$ ### 2.5 - Representing Instructions in the Computer 指令將以二進位呈現(所謂的機器語言, machine code) 以 MIPS 為例,指令以 32 位元的二進位呈現 - MIPS R-format Instructions(R:Register) | op | rs | rt | rd | shamt | funct | | :----: | :----: | :----: | :----: | :----: | :----: | | 6 bits | 5 bits | 5 bits | 5 bits | 5 bits | 6 bits | - op:operation code(opcode) - rs:first source register number - rt:second source register number - rd:destination register number - shamt:shift amount(00000 for now) - funct:function code(extends opcode) - Example:**add $t0, $s1, $s2** | special | $s1 | $s2 | $t0 | 0 | add | | :-----: | :---: | :---: | :---: | :---: | :----: | | 0 | 17 | 18 | 8 | 0 | 32 | | 000000 | 10001 | 10010 | 01000 | 00000 | 100000 | $000000\ 10001\ 10010\ 01000\ 00000\ 100000_2\\= 0000\ 0010\ 0011\ 0010\ 0100\ 0000\ 0010\ 0000_2 = 0232\ 4020_{16}$ - MIPS I-format Instructions(I:Immediate) | op | rs | rt | constant or address | | :----: | :----: | :----: | :-----------------: | | 6 bits | 5 bits | 5 bits | 16 bits | **設計原則 4:好的設計需要適宜的折衷方案        (Good design demands good compromises)** - Examples: | Instruction | Format | op | rs | rt | rd | shamt | funct | address | | :-------------: | :----: | :-------: | :-: | :-: | :--: | :---: | :-------: | :------: | | add | R | 0 | reg | reg | reg | 0 | $32_{10}$ | n.a. | | sub (subtract) | R | 0 | reg | reg | reg | 0 | $34_{10}$ | n.a. | | add immediate | I | $8_{10}$ | reg | reg | n.a. | n.a. | n.a. | constant | | lw (load word) | I | $35_{10}$ | reg | reg | n.a. | n.a. | n.a. | address | | sw (store word) | I | $43_{10}$ | reg | reg | n.a. | n.a. | n.a. | address | ### 2.6 - Logical Operations | Operation | C | Java | MIPS | | :---------: | :-: | :--: | :-------: | | Shift left | << | << | sll | | Shift right | >> | >>> | srl | | Bitwise AND | & | & | and, andi | | Bitwise OR | \| | \| | or, ori | | Bitwise NOT | ~ | ~ | nor | - AND Operations:**$t0 = $t1 & $t2;** ```Assembly= and $t0, $t1, $t2 ``` - OR Operations:**$t0 = $t1 | $t2;** ```Assembly= or $t0, $t1, $t2 ``` - Special in MIPS ─ NOR Operations:**a NOR b** == NOT(a OR b) ```Assembly= nor $t0, $t1, $zero # The last is always Register 0 ``` ### <span style="color: red;">**2.7 - Instructions for Making Decisions**</span> - if (rs == rt) branch to instruction labeled L1 ```Assembly= beq rs, rt, L1 # beq = branch if equal ``` - if (rs != rt) branch to instruction labeled L1 ```Assembly= bne rs, rt, L1 # bne = branch if not equal ``` > Question: >   Why don't we use blt, bge, …? > Answer: >   Hardware for "<, ≥, …" is slower than "=, ≠". >   On the other hand, we can make beq and bne common case. - unconditional jump to instruction labeled L1 ```Assembly= j L1 ``` - if (rs < rt) rd = 1; else rd = 0; ```Assembly= slt rd, rs, rt # slt = set on less than ``` - if (rs < constant) rd = 1; else rd = 0; ```Assembly= slti rd, rs, constant # slti = set on less than immediate ``` ### 2.8 - Supporting Procedures in Computer Hardware > | 暫存器號 | 符號名 | 用途 | > | ---------- | --------- | ------------------------------------ | > | 0 | zero | 看起來象浪費,其實很有用 | > | 1 | at | 保留給彙編器使用 | > | 2~3 | v0、v1 | 函式返回值 | > | 4~7 | a0~a3 | 前頭幾個函式引數 | > | <span style="color: red;">**8~15**</span> | <span style="color: red;">**t0~t7**</span> | <span style="color: red;">臨時暫存器,子過程可以不儲存就使用</span> | > | <span style="color: red;">**16~23**</span> | <span style="color: red;">**s0~s7**</span> | <span style="color: red;">暫存器變數</span> | > | <span style="color: red;">**24~25**</span> | <span style="color: red;">**t8、t9**</span> | <span style="color: red;">同 **8~15(t0~t7)**,臨時暫存器</span> | > | 26、27 | k0、k1 | 保留給異常處理函式使用 | > | 28 | gp | global pointer:方便存取全域或靜態變數 | > | 29 | sp | stack pointer | > | 30 | s8 / fp | 第 9 個暫存器變數,也可用做 frame pointer | > | 31 | ra | 返回地址 | - <span style="color: red;">**Procedure Call Instructions**</span> - Procedure call:jal(Jump And Link) ```Assembly= jal ProcedureLabel ``` - Procedure return:jr(Jump Register) ```Assembly= jr $ra ``` ### 2.9 - Communicating with People ### 2.10 - MIPS Addressing for 32-Bit Immediates and Addresses - Byte / Halfword Operations - Byte:8 bits / Halfword:16 bits (2 bytes) - MIPS byte / halfword load / store - String processing is a common case - Sign extend to 32 bits in rt ```Assembly= lb rt, offset(rs) lh rt, offset(rs) ``` - Zero(Unsigned) extend to 32 bits in rt ```Assembly= lbu rt, offset(rs) lhu rt, offset(rs) ``` - Store just rightmost byte / halfword ```Assembly= sb rt, offset(rs) sh rt, offset(rs) ``` - MIPS J-format Instructions(J:Jump) | op | address | | :----: | :-----: | | 6 bits | 26 bits | ### 2.11 - Parallelism and Instructions:Synchronization 提及:原子操作(**Atomic read / write memory operation**) - Synchronization(同步) in MIPS - Load linked: ```Assembly= ll rt, offset(rs) ``` - Store conditional: ```Assembly= sc rt, offset(rs) ``` - Succeed if location not changed since the ll → Returns 1 in rt - Fail if location is changed since the ll → Returns 0 in rt ### 2.12 - Translating and Starting a Program > ![](https://i.imgur.com/DSN2iXh.png =700x) - The object file(for UNIX systems)typically contains six distinct pieces, provide information for building a complete program: 1. Header(object file header): described contents of object module 2. Text segment: translated instructions(machine language codes) 3. Static data segment: data allocated for the life of the program 4. Relocation information: for contents that depend on absolute location of loaded program 5. Symbol table: undefined labels, ex:Global definitions and external references 6. Debug information: for associating with source code ### 2.13 - A C Sort Example to Put It All Together ### 2.14 - Arrays versus Pointers ### 2.15 - Advanced Material:Compiling C and Interpreting Java ### 2.16 - Real Stuff:ARMv7 (32-bit) Instructions ### 2.17 - Real Stuff:x86 Instructions ### 2.18 - Real Stuff:ARMv8 (64-bit) Instructions ### 2.19 - Fallacies and Pitfalls ### <span style="color: red;">**2.20 - Concluding Remarks**</span> - 設計原則(Design principles) - 簡單有助於規整(Simplicity favors regularity) - 越少越快(Smaller is faster) - 優化常見情況(Make the common case fast) - 好的設計需要適宜的折衷方案 (Good design demands good compromises) - <span style="color: red;">**在本章中應該要學會的指令**</span> | Instruction class | MIPS examples | | :---------------------- | :-------------------------------- | | 算數 / Arithmetic | add、sub、addi | | 資料 / Data transfer | lw、sw、lb、lbu、lh、lhu、sb、lui | | 邏輯 / Logical | and、or、nor、andi、ori、sll、srl | | 條件 / Condition branch | beq、bne、slt、slti、sltiu | | 跳轉 / Jump | j、jr、jal | ### 2.21 - Historical Perspective and Further Reading > [PDF](http://booksite.elsevier.com/9780124077263/downloads/historial%20perspectives/section_2.21.pdf) ### 2.22 - Exercises > [解答](http://jcf94.com/download/2015-09-09-cod-CH02_Solution.pdf) --- ## 第三章 - Arithmetic for Computers ### 3.1 Introduction - 實數:加、減、乘、除,以及溢位處置。 - 浮點數:表示法 & 運算。 ### 3.2 Addition and Subtraction ### 3.3 Multiplication 提及:算術邏輯單元(Arithmetic Logic Unit, ALU)。 名詞:**multiplicand**(被除數)、**multiplier**(除數)。 - 使用 2 個 32 位元的暫存器儲存 **product**(積) - HI:most-significant 32 bits - LO:least-significant 32 bits - MIPS Multiplication Instructions - 64-bit product in HI/LO (用 rs & rt 做乘法,存入 HI/LO) ```Assembly= mult rs, rt multu rs, rt ``` - Move from HI/LO to rd Can test HI value to see if product overflows 32 bits ```Assembly= mfhi rd mflo rd ``` - Least-significant 32 bits of product → rd (只取積的後 32 位元,存入 rd) ```Assembly= mul rd, rs, rt # not in the textbook, FYI ``` - 硬體運作圖: > ![](https://i.imgur.com/uEeHzUl.png =550x) - 邏輯流程圖: > ![](https://i.imgur.com/WrdpDrA.png =550x) ### 3.4 Division - 使用 2 個 32 位元的暫存器儲存 **remainder**(餘數)和 **quotient**(商數) - HI:32-bit remainder - LO:32-bit quotient - MIPS Division Instructions No overflow or divide-by-0 checking. Software must perform checks if required. - access result ```Assembly= mfhi rd mflo rd ``` - divide ```Assembly= div rs, rt divu rs, rt ``` - 硬體運作圖: > ![](https://i.imgur.com/wUeFbyJ.png =550x) - 邏輯流程圖: > ![](https://i.imgur.com/PlzASxp.png =550x) ### 3.5 Floating Point - $x = (-1)^s × (1 + Fraction)_2 × 2_{10}^{\ (Exponent_{10} - Bias_{10})}$ - S:sign bit(0 => non-negative / 1 => negative) - Normalize significand:1.0 ≤ | significand | < 2.0 - Always have a leading pre-binary-point 1 bit, so no need to represent it explicitly(hidden bit) - Significand is Fraction with the “1.” restored - Exponent:excess representation:actual exponent + Bias - Ensure exponent is unsigned - Bias:Single = 127 / Double = 1023 - Two representations - Single precision(32-bit) | S | Exponent | Fraction | | :---: | :------: | :------: | | 1 bit | 8 bits | 23 bits | - Double precision(64-bit) | S | Exponent | Fraction | | :---: | :------: | :------: | | 1 bit | 11 bits | 52 bits | - Single-Precision Range - Exponent:00000000 and 11111111 are reserved.(Inf. & NaN.) - Smallest value - Exponent:00000001 → actual exponent = 1 – 127 = –126 - Fraction:000… 0000 → significand = 1.0 - $±1.0 × 2^{–126} ≈ ±1.2 × 10^{–38}$ - Largest value - Exponent:11111110 → actual exponent = 254 – 127 = +127 - Fraction:111… 1111 → significand $≈$ 2.0 - $±2.0 × 2^{+127} ≈ ±3.4 × 10^{+38}$ - Double-Precision Range - Exponent:00000000000 and 11111111111 are reserved.(Inf. & NaN.) - Smallest value - Exponent:00000000001 → actual exponent = 1 – 1023 = –1022 - Fraction:00000… 00000 → significand = 1.0 - $±1.0 × 2^{–1022} ≈ ±2.2 × 10^{–308}$ - Largest value - Exponent:11111111110 → actual exponent = 2046 – 1023 = +1023 - Fraction:1111… 111111 → significand $≈$ 2.0 - $±2.0 × 2^{+1023} ≈ ±1.8 × 10^{+308}$ - Floating-Point Examples: > Represent –0.75: > $–0.75 = (–1)^1 × 1.1_2 × 2^{–1}$ > - S = 1 > - Fraction = $100… 000_2$ > - Exponent = –1 + Bias > Single:–1 + 127 = 126 = $01111110_2$(8 bits) > Double:–1 + 1023 = 1022 = $01111111110_2$(11 bits) > - Answer = > Single:$\ \ 1\ \ 01111110\ \ \ \ \ \ \ \ 1000…00$ > Double:$1\ \ 01111111110\ \ 1000…00$ > What number is represented by this single-precision float: > $11000000101000…00$($1\ \ 10000001\ \ 01000…00$) > - S = 1 > - Fraction = $0100…000_2$(23 bits) > - Exponent = $10000001_2$(8 bits) = $129_{10}$ > - Answer > $= (–1)^1 × (1_2 + 0.01_2) × 2^{(129 – 127)}\\= (–1) × 1.25 × 2^2\\=\ –5.0$ - Special cases: - Exponent = 000...000 → hidden bit is 0 $x = (-1)^S × (0 + Fraction) × 2^{−Bias}$ - Exponent & Fraction are both 000...000 $x = (-1)^S × (0 + 0) × 2^{−Bias} = ±0.0$(Two representations of 0.0!) - Exponent = 111...111 / Fraction = 000...000 ±Infinity - Exponent = 111...111 / Fraction ≠ 000...000 Not-a-Number (NaN.) - FP Instructions: - Load and Store:lwc1、ldc1、swc1、sdc1 l = "Load", s = "Store" / w = "Word", d = "Double" / c1 = "Coprocessor 1" Ex: ```Assembly= ldc1 $f8, 32($sp) # Load Double-precision $f8 to Coprocessor 1 ``` - Single-precision arithmetic:add.s、sub.s、mul.s、div.s Ex: ```Assembly= add.s $f0, $f1, $f6 ``` - Double-precision arithmetic:add.d、sub.d、mul.d、div.d Ex: ```Assembly= mul.d $f4, $f4, $f6 ``` - Single- and Double-precision comparison: c.XX.s、c.XX.d(XX is eq, lt, le, …) c = "Compare" / XX = {condition} eq = "EQual" / lt = "Less Than" / le = "Less Equal"... ```Assembly= c.lt.s $f3, $f4 # Compare $f3 & $f4 then Set condition bit ``` - Branch on FP condition code true or false:bc1t、bc1f b = "Branch" / c1 = "Coprocessor 1" / t = "True", f = "False" Ex: ```Assembly= bc1t TargetLabel # do TargetLabel if condition bit == True ``` ### 3.6 Parallelism and Computer Arithmetic:Subword Parallelism ### 3.7 Real Stuff:Streaming SIMD Extensions and Advanced Vector Extensions in x86 --- # 期中考範圍到此,以下為期末考範圍 --- ### 3.8 Going Faster:Subword Parallelism and Matrix Multiply ### 3.9 Fallacies and Pitfalls - Right Shift and Division Only for unsigned integers - Assumptions of associativity may fail > ![](https://i.imgur.com/x5lqS32.png =300x) ### 3.10 Concluding Remarks ### 3.11 Historical Perspective and Further Reading ### 3.12 Exercises --- ## 第四章 - The Processor ### 4.1 - Introduction - Instruction Execution - CPU Overview - Multiplexers - Control > ![](https://i.imgur.com/CCZQIRv.png =550x) ### 4.2 - Logic Design Conventions - Basics - Combinational Elements - Sequential Elements - Clocking Methodology ### 4.3 - Building a Datapath - Instruction Fetch > ![](https://i.imgur.com/ugQjDAl.png) 處理指令需要的幾個基本元素: - 指令寄存器(Instruction Register, IR) 用於存儲所有的程序指令,並且給它們編上地址。 - 程式計數器(Program Counter, PC) 一個地址寄存器,用於存放指令地址,即指向指令存儲器中當前正在執行的指令。 - 加法器 用於改變 PC 的值,指向下一條指令,以使程序可以繼續向後執行。 - R-format Instructions > ![](https://i.imgur.com/iwhiAdu.png =550x) - 通常需要讀取 2 個寄存器的內容,經過 ALU 運算後寫入另一個寄存器。 - 寄存器訪問需要 4 個輸入(2 個要讀的寄存器號,1 個要寫的寄存器號,1 個要寫入的寄存器值),2 個輸出(分別是輸出 2 個要讀的寄存器的內容),以及一個寫信號脈衝。 - 寄存器號的 3 個口需要 5 位的數據寬度([第二章中 rs、rt、rd 的長度都是 5 位](https://hackmd.io/0GYhppFQS1-U5o9klxSbUw#25---Representing-Instructions-in-the-Computer)),寫入與讀出的 3 個口則分別需要 32 位的數據寬度。 - I-format Instructions > ![](https://i.imgur.com/VtObZSx.png =400x) - ```Assembly= lw $t1, offset($t2) ``` 存儲地址是將 $t2 中的內容(32位基地址)加上 offset(16 位偏移地址)作為目標地址,然後將內容寫入寄存器 $t1。 這裡需要的是一個地址加法器(加 16 位和 32 位的數)以及數據存儲器。 - ```Assembly= beq $t1, $t2, offset ``` 首先讀取 $t1 和 $t2 的內容到 ALU 進行對比,若通過,則將 offset(16 位偏移地址)加到 PC 上作為下一次的目標地址。 - Branch Instructions > ![](https://i.imgur.com/JcCxLLA.png =550x) - R-Type/Load/Store Datapath > ![](https://i.imgur.com/QtSmBME.png =650x) - Full Datapath > ![](https://i.imgur.com/KWCchcd.png =650x) ### 4.4 - A Simple Implementation Scheme - ALU Control - 實現簡單的指令,包含: - load word(lw) - store word(sw) - branch equal(beq) - 算數邏輯運算:add、sub、AND、OR - set on less than(slt) - 要實現上面的這些功能,只需要 2 位數: > ![](https://i.imgur.com/0f1rkv1.png =650x) - 00 表示 lw 或者 sw,這裡對於 ALU 來說只需要作地址的相加即可。 - 01 表示 beq,需要用 ALU 對兩個寄存器相減。 - 10 表示 R-format 操作,需要配合 6 位的 Funct 再進行各種情況的選擇。 - 根據上述的 2 位 ALUOp 和 6 位 Funct,生成出真正的 4 位 ALU 操作碼 - Main Control Unit - Datapath With Control - R-Type Instruction - Load Instruction - Branch-on-Equal Instruction - Datapath With Jumps Added ### 4.5 - An Overview of Pipelining - Pipelining Analogy - MIPS Pipeline - Five stages: 1. IF(Instruction Fetch) Instruction fetch from memory. 1. ID(Instruction Decode) Instruction decode & register read. 1. EX(EXecute) Execute operation or calculate address. 1. MEM(MEMory) Access memory operand. 1. WB(Write Back) Write result back to register. - Pipeline Performance - lw:IF + ID + EX + MEM + WB - sw:IF + ID + EX + MEM - R-format:IF + ID + EX + WB - beq:IF + ID + EXE - Pipeline Speedup - $Speed\ up = \dfrac{Time_{pipelined}}{Time_{non-pipelined}} ≈ number\ of\ stages$ (when there are many instructions) - Hazards(冒險) - Structure hazards(結構冒險) 兩條指令在不同的階段需要同時訪問相同的寄存器造成的。 - Data hazards(數據冒險) 下一條指令需要的數據,上一條指令還在計算中。 - Control hazards(控制 / 分支冒險) 遇到例如 beq 這類指令時,指令正在譯碼,但是流水線就要緊接著讀入下一條指令,而此時下一條指令的 PC 還沒有確定,因此產生矛盾。(Stall on Branch) - Branch Prediction 隨便預測一個方向先執行,如果不對再忽略前面執行的指令轉去執行正確的指令,這樣如果預測是正確的,則流水線任然是全速運行,如果是錯誤的那麼與原來不採用預測的方式耗時一樣。 - Forwarding(轉發)(a.k.a. Bypassing, 旁路) - Load-Use Data Hazard ### 4.6 - Pipelined Datapath and Control - MIPS Pipelined Datapath > ![](https://i.imgur.com/ArkELp7.png =650x) - Pipeline registers 為了使得一個數據通路里面的五個部分之間相互不影響,解決方法就是在兩個相鄰的部分之間使用額外的寄存器來傳遞數據,保證上一條指令的結果能夠保存下來傳給下一個部分來執行,同時能繼續執行下一條指令的該部分。 > ![](https://i.imgur.com/n6Fnvem.png =650x) - Pipelined Control ### 4.7 - Data Hazards:Forwarding v.s. Stalling - Data Hazards in ALU Instructions - Dependencies & Forwarding - Detecting the Need to Forward - Forwarding Paths - Forwarding Conditions - Double Data Hazard - Revised Forwarding Condition - Datapath with Forwarding > ![](https://i.imgur.com/pcWB8CO.png =650x) - Load-Use Data Hazard 如果遇到 lw 造成的 Hazard 必須 stall。 > ![](https://i.imgur.com/8kES57W.png =650x) > ![](https://i.imgur.com/QMdbwPZ.png =650x) - Datapath with Hazard Detection - Stalls and Performance ### 4.8 - Control Hazards - Branch Hazards - Reducing Branch Delay - Data Hazards for Branches - Dynamic Branch Prediction ### 4.9 - Exceptions - Exceptions and Interrupts - Exception arise within the CPU Ex:undefined opcode, overflow, syscall... - Interrupt from an external I/O controller - Handling Exceptions - An Alternate Mechanism - Handler Actions - Exceptions in a Pipeline - Pipeline with Exceptions > ![](https://i.imgur.com/56yydF2.png =650x) - Exception Properties - Multiple Exceptions - Imprecise Exceptions ### 4.10 - Parallelism and Advanced Instruction Level Parallelism - Instruction-Level Parallelism (ILP) - Multiple Issue(指令多發射 / 單週期多指令並行) - Static multiple issue(靜態多發射) - Dynamic multiple issue / Superscalar(動態多發射 / 超標量) - Speculation(推測) - Compiler/Hardware Speculation - Speculation and Exceptions - Static Multiple Issue - Scheduling Static Multiple Issue - MIPS with Static Dual Issue - Hazards in the Dual-Issue MIPS - Scheduling Example - Loop Unrolling - Dynamic Multiple Issue - Dynamic Pipeline Scheduling - Dynamically Scheduled CPU - Register Renaming - Speculation - Why Do Dynamic Scheduling? - Does Multiple Issue Work? - Power Efficiency ### 4.11 - Real Stuff:The ARM Cortex-A8 and Intel Core i7 Pipelines ### 4.12 - Instruction-Level Parallelism and Matrix Multiply ### 4.13 - Advanced Topic:An Introduction to Digital Design Using a Hardware Design Language to Describe and Model a Pipeline and More Pipelining Illustrations > [PDF](http://booksite.elsevier.com/9780124077263/downloads/advance_contents_and_appendices/section_4.13.pdf) ### 4.14 - Fallacies and Pitfalls ### 4.15 - Conduding Remarks ### 4.16 - Historical Perspective and Further Reading > [PDF](http://booksite.elsevier.com/9780124077263/downloads/historial%20perspectives/section_4.16.pdf) ### 4.17 - Exercises > [解答](http://jcf94.com/download/2015-09-09-cod-CH04_Solution.pdf) --- ## 第五章 - Large and Fast:Exploiting Memory Hierarchy ### 5.1 - Introduction - Principle of Locality(局部性) - Temporal locality(時間局部性) - Spatial locality(空間局部性) - Take Advantage of Locality - Memory Hierarchy Level > ![](https://i.imgur.com/p9LudsR.png =400x) ### 5.2 - Memory Technologies | 技術 | 訪問時間 | 價格 / GB | 特點 | | :------: | :--------: | :------: | :--: | | SRAM | 0.5~2.5 ns | $500~1000 | 數據用晶體管存儲,可直接讀取數據。 | | DRAM | 50~70 ns | $10~20 | 數據用電容存儲,會不斷漏電,需要定期刷新。(充電)| | FLASH | 5k~50k ns | $0.75~1.00 | EEPROM,電擦除可編程存儲器,有讀寫次數上限。 | | HDD | 5M~20M ns | $0.05~0.10 | 用磁盤、磁頭等進行磁性存儲和讀寫。 | - Flash Storage - Flash Type - NOR flash - NAND flash - Disk Storage - Disk Sectors and Access - Disk Access Example - Disk Performance Issues ### 5.3 - The Basics of Caches - Cache Memory - Direct Mapped Cache - Tags and Valid Bits - Cache Example - Address Subdivision - Example:Larger Block Size - Block Size Considerations - Cache Misses - Write-Through - write buffer - Write-Back - dirty block - Write Allocation - Example:Intrinsity FastMATH - Main Memory Supporting Caches - Increasing Memory Bandwidth ### 5.4 - Measuring and Improving Cache Performance - Measure Cache Performance - $\begin{split} Memory\ stall\ cycles &= \dfrac{Memory\ accesses}{Program} × Miss\ rate × Miss\ penalty \\\\&= \dfrac{Instructions}{Program} × \dfrac{Misses}{Instruction} × Miss\ penalty \end{split}$ - Cache Performance Example - Average Access Time - Average memory access time(AMAT) $AMAT = Hit\ time + Miss\ rate × Miss\ penalty$ - Performance Summary - Associative Caches - Directed mapped(直接匹配) - Set associatative(組相聯) - Fully associative(全相聯) - Example of Associative Cache - Spectrum of Associativity - Associativity Example - How Much Associativity - Set Associative Cache Organization - Replacement Policy - Multilevel Caches ### 5.5 - Dependable Memory Hierarchy - Dependability - Dependability Measures - 故障(Failure):機器從正常的運行狀態中被中斷,跳到對應的故障解決服務中。若不能從故障中恢復就是永久性故障,故障還可能是間歇性的。 - 可靠性(Reliability):估量機器從某一點開始能夠持續正常服務的時長。 - 平均故障時間(Mean Time To Failure, MTTF):平均持續正常工作不發生故障的時長。 - 年故障率(Annual Failure Rate):給定 MTTF 之後,一年中發生故障的時間比率。 - 平均修復時間(Mean Time To Repair, MTTR):一旦發生故障之後,從故障中恢復平均需要花費的時間。 - 平均故障間時間(Mean Time Between Failure, MTBF):MTTF 和 MTTR 的總和。 - 有效性(Availability):用於估量機器在整個故障及恢復過程中正常工作市場的比率。 > $有效性(Availability) = \dfrac{MTTF}{MTBF} = \dfrac{MTTF}{MTTF + MTTR}$ - 為了提高MTTF 的三種方案: 1. 從設計上避免故障發生。 1. 依靠冗餘備份,使得即使故障發生了也能夠正常運行。 1. 提前預測可能發生故障的情況,並提前修正錯誤。 ### 5.6 - Virtual Machine(VM) ### 5.7 - Virtual Memory --- ### 5.8 - A Common Framework for Memory Hierarchy ### 5.9 - Using a Finite-State Machine to Control a Simple Cache ### 5.10 - Parallelism and Memory Hierarchies: Cache Coherence ### 5.11 - Real Stuff:The ARM Cortex-A8 and Intel Core i7 Memory Hierarchies ### 5.12 - Going Faster:Cache Blocking and Matrix Multiply ### 5.13 - Fallacies and Pitfalls

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully