資工系必修 共筆
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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 New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy 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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 計算機組織_劉一宇 ## Ch2. Instrution Set Architecture ## MIPS 組合語言 ## 前言 ::: info 組合語言 v.s. 處理器指令集 組合語言:大集合 指令集:小集合 需要 Assembler (組譯器) 把組合語言轉換成處理器可以執行的指令 ::: - 為什麼會這樣? - 有一些組語會轉成 2~3 個指令 - 如:le, ge... => xor, cmp, jump... - 為什麼要這樣? - 增加可讀性 ### Design priciple - priciple 1: simplicity favors regularity - 指令越簡單,對於硬體設計越好 - priciple 2: Smaller is faster - priciple 3: Make the common case fast - priciple 4: Good design demands good ompromises - 好的設計是需要折衷的 ## Outline - Operands - Register - Memory, data transfer - Immediate (Constants) - Signed and Unsigned numbers - 老師要我們自己去回憶 - Represnting instruction - 要怎麼呈現、要怎麼擺放 - Field - Operations - Logical - Decision making and branches - Supporting procedures in hardware - procedures call (function call) - 要怎麼做 recursion??? - 程式整合 (使用習慣 convension) - Addressing - Synchronization - 多處理器間同步議題 - 作業系統課程 - Compile -> Assembly -> Assembler - System software - 不同指令集比較 - Arm and x86 ## 開始 ### Instruction categories - Load/Store - Computation ### Instruction Formats - r format - i format - j format ### Oprends #### Add ```MIPS add $s0 $s1 $s2 # s0 = s1 + s2 ``` - $s0: destination - $s1: source 1 - $s2: source 2 ### Rigester - 通用暫存器 - $r0 - $r31 - ![[Pasted image 20230307112922.png|500]] - PC 指到下一個指令 - HI - LO > 為什麼暫存器 32 個bits? > design priciple 2: Smaller is faster - 16~23: $s0 ~ $s7 - 很像 C variable,因為有 scope 的概念 ### Memory 一個 word 32 bits - lw : load word - sw : save word #### 讀取記憶體位置 ```MIPS lw $t0, 8($s0) ``` $s0 是一個連續記憶體的位址起點(因為記憶體位址為 32bits,放進暫存器剛剛好) 前面的 8 是 offset,必須是常數(不可以是變數) lw 則是把 $s0 + 8 的位址放進 $t0 ``` $t0 = MEM[$s0 + 12] ``` #### 儲存記憶體位址 ```MIPS sw $t0, 12($s0) ``` ``` MEM[$s0 + 12] = $t0 ``` ## Signed ### 2's complement sign extension 把 3 bits 擴充成 4 bits 只要將空的位置填補上 3 bits 的最高位元 ## Representing Instructions 如何把組合語言轉成 01 的機器碼呢? 把指令切成不同欄位,放入資訊 - R-format: For Register - I-format: For immediate - J-format: For jump > RISC-V 的 Instruction type 切成了六種,切得更細,但變得更簡單 ### R-format - opcode 6bits:用於指定指令類型、對於所有 R 型指令該值均為 0,用 funct 來精確指定指令類型 - Rs (Source Register) 5bits:指定第一個來源操作數的暫存器編號 - Rt (Target Register) 5bits:指定目的操作數的暫存器編號 - Rd (Destination Register) 5bits:指定目的地的暫存器編號 - shatmt 5bits: 拿來做 shift 表示位移的位數 - funct 6bits > 為什麼要設定 opcode 都是 0,卻用 function 去定義? > 為什麼不要把 opcode 跟 function 合在一起? ### I-format I 型指令基本上與 R 型相同, opcode 仍然用於指定指令的操作類型 但這裡沒有 funct,所以不同的 I 型指令其 opcode 是不同的 - Rs (Source Register) 5bits:指定第一個來源操作數的暫存器編號 - Rt (Target Register) 5bits:指定來源操作數的暫存器編號或目的操作數的暫存器編號 - immediate 16bits:可以表示 2^16 的不同數值 (signed) > 如果我們存取的常數比 16bits 還大,應該怎麼辦? > Mars 聰明的會把指令拆成兩個 : ) ### Stored-Program Concept 現在的電腦系統有一個大 concept 不管是指令或是資料都存在一起,因此要怎麼做處理要決定於程式設計師(資料型態等) 但同樣會有一個問題是像 buffer overflow 這種沒有做 bonudry check 造成的問題 ## Operations ### Bitwise #### Shift 比較特別的是 sra,因為 shift 本身是除法,但如果我們把一個負數往右移 (111110000) 會變成正數(開頭填0),因此有了一個 sra,會將負數填入 1,正數填 0 ### Decision making and branches 電腦和計算機最大的差別,就是我們可以有不同的決策

    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