黃瀚群
    • 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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Tower Of Hanoi contributed by < `hanqun0723` > ###### tags: `RISC-V` ## Program ```clike= .data argument: .word 3 #the number of disks str1: .string " \n Number of disk " str2: .string " \n Move disk from " str3: .string " to " .text main: li a0, 4 #print the number of disks la a1, str1 ecall li a0, 1 lw a1, argument ecall lw a0, argument #load arguments li a1, 1 #a1 = A li a2, 2 #a2 = B li a3, 3 #a3 = C jal ra,hanoi #Exit program li a0, 10 ecall hanoi: li t4, 1 bne a0, t4, Else #if(disk == 1) mv t0, a0 #else(disk > 1) mv t1, a1 mv t2, a2 mv t3, a3 li a0, 4 la a1, str2 ecall li a0, 1 mv a1, t1 ecall li a0, 4 la a1, str3 ecall li a0, 1 mv a1, t3 ecall jalr ra, ra, 0 Else: addi sp, sp, -20 #hanoi(n-1,A,C,B) sw ra, 16(sp) sw a3, 12(sp) sw a2, 8(sp) sw a1, 4(sp) sw a0, 0(sp) add t0, zero, a3 mv a3, a2 mv a2, t0 addi a0, a0, -1 jal ra, hanoi #ra = pc+4 lw ra, 16(sp) lw a3, 12(sp) lw a2, 8(sp) lw a1, 4(sp) lw a0, 0(sp) mv t0, a0 #move a disk from A to C mv t1, a1 #save a0,a1 values in t0,t1 li a0, 4 la a1, str2 ecall li a0, 1 mv a1, t1 ecall li a0, 4 la a1, str3 ecall li a0, 1 mv a1, a3 ecall mv a0, t0 #hanoi(n-1,B,A,C) mv a1, t1 add t0, zero, a2 mv a2, a1 mv a1, t0 addi a0, a0, -1 jal ra, hanoi lw ra, 16(sp) addi sp, sp, 20 jalr ra, ra, 0 ``` ## Instruction Memory 根據 assembled code 顯示總共有 79 條 instruction, 所以 instruction memory 從 0x00000000 依序存放至 0x00000000 + (hex(79*4-1=315)=0x13b) = 0x0000013b ![](https://i.imgur.com/9FbPbiE.png) ![](https://i.imgur.com/nTWVkUs.png) 第一條執行的指令為 ```clike addi x10 x0 4 ``` 根據I-type 的instruction format ![](https://i.imgur.com/MCV0NlR.png) immediate = 000000000100 rs = 00000 funct = 000 rd = 01010 op code = 0010011 將上述 32 bits 的二進位值轉用十六進位表示 00000000010000000000010100010011 = 0x00400513 * 從PC位址 0x00000000 可以看到指令有被正確存放 ![](https://i.imgur.com/4NllfB2.png) ## Data Memory Data 的部分會先儲存於Data memory中, 再利用 lw , sw來讀取與儲存資料 ![](https://i.imgur.com/QX0TL6q.png) ![](https://i.imgur.com/IsoH5ZY.png) ## Stack 在遞迴的過程中需先將原本的 arguments 先儲存至 stack 存放, 由於此函數有 4 個 arguments 和他的 return address, 所以我們需要將 stack 挪出 5 個 32bits 的空間來存放 . ![](https://i.imgur.com/szLFJZY.png) 圖為stack pointer的起始位置 ![](https://i.imgur.com/Iim2YN9.png) sp : 0x7ffffff0 sp-20 (5個32bits空間) : 0x7fffffdc 將需要儲存的值依序存放至stack ra (return address) -> 0x7fffffec a3 (C tower)-> 0x7fffffe8 a2 (B tower)-> 0x7fffffe4 a1 (A tower)-> 0x7fffffe0 a0 (number of disks)-> 0x7fffffdc ![](https://i.imgur.com/ECRM3w3.png) 結果如下圖: 根據第一次call function的argunents做驗證, a0 = 3 ,a1 = 1 , a2 = 2 , a3 = 3 , ra, 顯示有將值正確存放至 stack ![](https://i.imgur.com/PV9snpk.png) * function結束後須將 stack pointer 值還原 ```clike addi sp, sp ,20 ``` ## 5-stage pipeline 這裡一樣以程式碼第一行為例子來觀察pipeline如何運作及資料流 ``` addi x10 x0 4 ``` * IF stage 此階段會到 instruction memory 去抓指令 ![](https://i.imgur.com/ODlNyjf.png) * ID stage 對 instruction 做 decode, 並根據 decode 出來的結果來看要做什麼樣子的運算及到相對應的 register 讀取資料 ![](https://i.imgur.com/c3JhNuq.png) * EXE stage 將前一階段從 register 讀出來的值與常數部分做相加 ![](https://i.imgur.com/Hiytwpd.png) * MEM stage 這個階段因為沒有使用到lw, sw指令, 所以不會對data memory做存取 ![](https://i.imgur.com/pQQA0Oz.png) * WB stage 最後在將計算過後的數值write back到 rd 暫存器 ![](https://i.imgur.com/yFTiexe.png) ## Results 結果會顯示 Input 的 disk 數量及每個步驟移動的過程 ![](https://i.imgur.com/wOi8CAo.png) ## Reference 1. https://riscv.org/specifications/

    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