FANFNA
    • 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    [TOC] # 2.1 Instruction 設計哲學 ## 簡單明瞭有助於一致性 (Simplicity favors regularity) :::info **Arithmetic Operations**: ``` =mips add a,b,c #a = b + c, ``` 所有的算術算子都是這個規格。 ::: ## 小就是快 (Smaller is faster) :::success - **Register Operands**: - arithmetic operations拿register operands來操作,因為register小所以快,MIPS擁有32 × 32-bit register file,也就是8*8 byte register,32bit又稱做一個word,t0 t9是暫存用,s0 s9是儲存用。 - **Memory Operands**: - 但有時也需要使用較大的空間,因此有memory operands,需要load和store因此較慢,compiler會盡量使用register operands。 ::: ## 讓常出現的部分加快 (Make the common case fast) Immediate Operands:因為小的常數操作常出現,因此有immediate operands避免掉一個load,例如addi s3,s3, 4。 The Zero Constants:MIPS register 0 (zero)裡面只裝常數「0」,不能被覆寫,常用來移動值,例如addt2, s1,zero。 ## 好的設計需要好的折衷辦法 (Good design demands good compromises) R-format、I-format、J-format:根據需求有不同的format,但都是32bit且各個format盡量相似,以下是各種format。 ## 以下是一些memory operands的特性: :::info ### byte addressed: 每1個位址代表1個byte (8 bits) ::: :::success ### Alignment: 為了加快執行速度,在讀 memory的時候會以一個 word 為單位,因此 data 在儲存時,memory offset 便會是 word size 倍數,藉此增加效能。 ::: :::info ### Big Endian and Small Endian: MIPS在一個word內的存放順序是Big Endian,most-significant byte在位址數目最小的位子 - ex:0x12345678 (word內的順序) ![](https://i.imgur.com/fDa1bwn.png =600x300) ::: :::success - 讓常出現的部分加快 (Make the common case fast) - **Immediate Operands**:因為小的常數操作常出現,因此有immediate operands避免掉一個load,例如`addi s3,s3, 4`。 - **The Zero Constants**:MIPS register 0 (*zero*)裡面只裝常數「0」,不能被覆寫,常用來移動值,例如`add $t2, $s1,$zero`。(標示數值) ::: :::danger - 好的設計需要好的折衷辦法 (Good design demands good compromises) **R-format**、**I-format**、**J-format**:根據需求有不同的format,但都是32bit且各個format盡量相似,以下是各種format。 ![](https://i.imgur.com/dOBR8TZ.png) ::: ### three big type review :::success 所有的BITS 加起來都要是32bits ::: R type ![](https://i.imgur.com/WsVUIve.png) I type ![](https://i.imgur.com/ehf0Sjq.png) J type ![](https://i.imgur.com/0nKMF7y.png) ## 2.2 MIPS Quick Look 重要暫存器一覽 ![](https://i.imgur.com/iJDRMBF.png) 指令格式一覽 ![](https://i.imgur.com/dvaZcSf.png) ### **重要的指令碼一覽** :::info - add :*op =0* ,**funct=32** ``` =mips add a,b,c #a = b + c, ``` - sub : *op =0* ,**funct=34** ``` =mips add a,b,c #a = b - c, ``` - addi: *op= 8*  #<font color="#f00">加法**(立即值用)**</font> ::: :::danger - lw : *op =35*  #<font color="#f00">讀檔</font> - <font color="blue">lw $t0, 20($a0) # $t0 = Memory[$a0 + 20]</font> - sw : *op =43*  #<font color="#f00">存檔</font> - <font color="blue">sw $t0, 20($a0) # Memory[$a0 + 20] = $t0</font> ::: :::info - move: ** - beq : *op =4*  #比較是否相等 - bne : *op =*5 #比較是否不相等 - sll : *op =0* ,**funct=0**  #左移 - slt : *op =0* ,**funct=42**  #比較大小 - j : *op= 2* ::: - 重要範例程式:procedure call以階乘為例,對照著activation record練習 - 進入procedure:下一行指令的位址放入$ra並跳到目標位址 - 離開procedure:把$ra裡的位址取出回到母函式 ![](https://i.imgur.com/1ICcmSS.png) ![](https://i.imgur.com/EREGqCu.png) ### **Addressing and Memory Accesses** ### leaf &noleaf :::warning - **Leaf** procedures: procedures that do not call others末端程序:本身是程序,而且不呼叫別的程序 - ![](https://i.imgur.com/H71Sjc9.png) :::spoiler ![](https://i.imgur.com/ydWg6G0.png) 1. 我們這邊會去算一次之後會去 POP 數值到 RIG 內部 2. 還原回原來的數值 ::: :::warning - **No leaf** Procedures: procedures that call other procedures非末端程序:本身是程序,而且也呼叫別的程序() **遞迴** - ![](https://i.imgur.com/Odp6cC2.png) :::spoiler ![](https://i.imgur.com/4htJENb.png) ::: ### loop(交大考題!!!) :::info ### loop1 ``` =c clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } ``` :::spoiler ``` mips move $t0,$zero # i = 0 loop1: sll $t1,$t0,2 # $t1 = i * 4 add $t2,$a0,$t1 # $t2 = address of array[i] sw $zero, 0($t2) # array[i] = 0 addi $t0,$t0,1 # i = i + 1 slt $t3,$t0,$a1 # $t3 = (i < size) bne $t3,$zero,loop1 # if (i < size) go to loop1 ``` ::: :::info ### loop 2 ``` c clear2(int *array, int size) { int *p; for (p = &array[0]; p < &array[size]; p = p + 1) *p = 0; } ``` :::spoiler ``` mips move $t0,$a0 # p = address of array[0] loop2: sw $zero,0($t0) # Memory[p] = 0 addi $t0,$t0,4 # p = p + 4 sll $t1,$a1,2 # $t1 = size * 4 add $t2,$a0,$t1 # $t2 = address of array[size] slt $t3,$t0,$t2 # $t3 = (p<&array[size]) bne $t3,$zero,loop2 # if (p<&array[size]) go to loop2 ``` ::: :::warning - Jump指令 (Pseudo-direct addressing) - 有限的32位指令長度對於大型程序的分支跳轉支持確實是個難題。MIPS指令中最小的操作碼域占6位,剩下的26位用於跳轉目標的編址。由於所有指令在記憶體中都是4 word對齊的,因此最低的2個地址位是無需存儲的,這样可供尋址範圍为**2^(26+2)=256** MB。分支跳轉地址被當做一個256 MB的段內絕對地址,而非PC相對尋址。這對於地址範圍超過256 MB的跳轉程序而言是無能为力的,所幸目前很少遇到這麼大的遠程跳轉需求。 - Branches out of segment - 段外分支跳轉可以使用暫存器跳轉指令實現,它可以跳轉到任一32位地址。 ``` mips beq $at, $0, L add $v1, $v0, $0 add $v1, $v1, $v1 j Somewhere L: add $v1, $v0, $v0 ``` - Conditional branches - 條件分支跳轉指令opcode的後16位branch offset是相對PC的有號偏移量,由於指令是4 word對齊的,因此可支持的跳轉範圍實際上是`2^(16+2)=256KB`**(相對PC的-128 KB~+128 KB)**。如果確定跳轉目標地址在分支指令前後的128KB範圍內,編譯器就可以編碼只生成一條簡單的條件分支指令。 :::

    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