OliveLake
    • 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
    # Assignment1: RISC-V Assembly and Instruction Pipeline contributed by < [OliveLake](https://github.com/OliveLake) > ###### tags: `RISC-V` ###### tags: `computer architure 2021` ## [Leetcode 389. Find the Difference](https://leetcode.com/problems/find-the-difference/) Given two strings s and t. String t is generated by random shuffling string s and then add one more letter at a random position. Return the letter that was added to t. #### Example Input: s = "ae", t = "aae" Output: "a" ## C code ```cpp char findTheDifference(char *s, char *t){ int originalCount = 0; int newCount = 0; for (int i=0; i<strlen(s); i++) { originalCount+=(int)s[i]; //casting } for (int j=0; j<strlen(t); j++) { newCount += (int)t[j]; } return (char)(newCount - originalCount); } ``` ## RISC-V assembly code ```cpp= .data arr1: .word 97, 101 # s[2] = {a, e} arr2: .word 97, 97, 101 # t[3] = {a, a, e} str: .string "the added letter is " main: la s0, arr1 #s0 store the address of arr1 la s1, arr2 #s1 store the address of arr1 addi s2, s2, 2 #s2 always equal to 2 addi s3, s3, 3 #s3 alwaly equal to 3 add s4, x0, x0 add s5, x0, x0 add s6, x0, x0 add t0, x0, x0 jal ra, loop1 add t0, x0, x0 jal ra, loop2 j end loop1: lw s4, 0(s0) add s5, s5, s4 addi s0, s0, 4 addi t0, t0, 1 blt t0, s2, loop1 ret loop2: lw s4, 0(s1) add s6, s6, s4 addi s1, s1, 4 addi t0, t0, 1 blt t0, s3, loop2 ret end: la a0, str li a7, 4 ecall sub a0, s6, s5 li a7, 1 ecall ``` ## Analysis Using [Ripes](https://github.com/mortbopet/Ripes) simulator to test the assembly code. ### Pseudo Instruction While load assembly code into Ripes, it translates the original source code into machine readable form, and modifies name of registers from ABI name to sequencial one. ``` 00000000 <main>: 0: 10000417 auipc x8 0x10000 4: 00040413 addi x8 x8 0 8: 10000497 auipc x9 0x10000 c: 00048493 addi x9 x9 0 10: 00290913 addi x18 x18 2 14: 00398993 addi x19 x19 3 18: 00000a33 add x20 x0 x0 1c: 00000ab3 add x21 x0 x0 20: 00000b33 add x22 x0 x0 24: 000002b3 add x5 x0 x0 28: 010000ef jal x1 16 <loop1> 2c: 000002b3 add x5 x0 x0 30: 020000ef jal x1 32 <loop2> 34: 0340006f jal x0 52 <end> 00000038 <loop1>: 38: 00042a03 lw x20 0 x8 3c: 014a8ab3 add x21 x21 x20 40: 00440413 addi x8 x8 4 44: 00128293 addi x5 x5 1 48: ff22c8e3 blt x5 x18 -16 <loop1> 4c: 00008067 jalr x0 x1 0 00000050 <loop2>: 50: 0004aa03 lw x20 0 x9 54: 014b0b33 add x22 x22 x20 58: 00448493 addi x9 x9 4 5c: 00128293 addi x5 x5 1 60: ff32c8e3 blt x5 x19 -16 <loop2> 64: 00008067 jalr x0 x1 0 00000068 <end>: 68: 10000517 auipc x10 0x10000 6c: fac50513 addi x10 x10 -84 70: 00400893 addi x17 x0 4 74: 00000073 ecall 78: 415b0533 sub x10 x22 x21 7c: 00100893 addi x17 x0 1 80: 00000073 ecall ``` ### 5-stage pipelined processor ### Pipeline Hazard There three types of data hazard 1. Data Hazard Occurs when instructions exhibit data dependence modify data in different stages of a pipeline. There are three condictions in which a data hazard orrurs * RAW * WAR * WAW 3. Structure Hazard Occurs when two instructions that are already in pipeline need the same source. 3. Control Hazard Occurs when pipeline makes wrong decisions which brings instructions into the pipeline that must subsequently be discarded. ![](https://i.imgur.com/eojbkoD.png) ## Reference * [Ripes](https://github.com/mortbopet/Ripes) * [RISCV pipeline and hazards](https://robotics.shanghaitech.edu.cn/courses/ca/19s/notes/Discussion8_2.pdf)

    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