goodfrank1688
    • 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
    # Assignment:RISC-V Assembly and Instruction Pipeline ## Valid Perfect Square Given a positive integer num, write a function which returns True if num is a perfect square else False. ## implementation the source code link: [click me](https://github.com/gfrank1688/ca_hw1) ### C code ```cpp= #include <stdio.h> bool isPerfectSquare(int num){ int k = 1; while( num > 0) { num = num - k; k = k + 2; } return num == 0; } int main(void){ int number1 = 14 , number2 = 16; printf("is %d a perfect square number?%d\n",number1,isPerfectSquare(number1)); printf("is %d a perfect square number?%d\n",number2,isPerfectSquare(number2)); } ``` ### logic because every perfectsquare number can be a sum of 1+3+5+7+....+(2n+1)by this property,we can observe that it is come from (n+1)^2 - n^2 = 2n+1,so we can use a while loop to keep on subtracting input from a getting bigger odd number from 1,and check whether your input number will be 0 finally,if yes and it isa perfect square number,otherwise ,it's not. * Example:16 = 1+3+5+7,we can check whether 16 is a perfect square numer by first subtract 16 from 1 16-1=15,and now 15 still > 0,so we keep going, 15-(1+2) = 15-3=12 12-(3+2) = 12-5=7 7-(5+2) = 7-7=0 so we can make sure that 16 = 1+3+5+7 ,and it's a perfect square number. ### Assembly ```asm== .data str1: .string "16 is a perfect square number(Yes = 1/No = 0) " str2: .string "\n14 is a perfect square number(Yes = 1/No = 0) " num1: .word 16 num2: .word 14 .text main: lw a0,num1 jal ra,isPerfectSquare la a0,str1 li a7,4 ecall mv a0,a1 li a7,1 ecall lw a0,num2 jal ra,isPerfectSquare la a0,str2 li a7,4 ecall mv a0,a1 li a7,1 ecall li a7,10 ecall isPerfectSquare: addi t0,zero,1 #add t2,a0,zero mv t2, a0 loop: slti t1,t2,1 bne t1,zero,exit sub t2,t2,t0 addi t0,t0,2 j loop exit: bne t2,zero,else addi a1,zero,1 ret else: addi a1,zero,0 ret ``` ### Assembly without pseudo this code is changed from Ripes's editor with the instructions which RISC-V machine can actually execute,that is, it replaced the pseudo instruction to the real instruction. ```arm== 00000000 <main>: 0: 10000517 auipc x10 0x10000 4: 03552503 lw x10 53 x10 8: 050000ef jal x1 80 <isPerfectSquare> c: 10000517 auipc x10 0x10000 10: ff450513 addi x10 x10 -12 14: 00400893 addi x17 x0 4 18: 00000073 ecall 1c: 00058513 addi x10 x11 0 20: 00100893 addi x17 x0 1 24: 00000073 ecall 28: 10000517 auipc x10 0x10000 2c: 00952503 lw x10 9 x10 30: 028000ef jal x1 40 <isPerfectSquare> 34: 10000517 auipc x10 0x10000 38: fcc50513 addi x10 x10 -52 3c: 00400893 addi x17 x0 4 40: 00000073 ecall 44: 00058513 addi x10 x11 0 48: 00100893 addi x17 x0 1 4c: 00000073 ecall 50: 00a00893 addi x17 x0 10 54: 00000073 ecall 00000058 <isPerfectSquare>: 58: 00100293 addi x5 x0 1 5c: 00050393 addi x7 x10 0 00000060 <loop>: 60: 0013a313 slti x6 x7 1 64: 00031863 bne x6 x0 16 <exit> 68: 405383b3 sub x7 x7 x5 6c: 00228293 addi x5 x5 2 70: ff1ff06f jal x0 -16 <loop> 00000074 <exit>: 74: 00039663 bne x7 x0 12 <else> 78: 00100593 addi x11 x0 1 7c: 00008067 jalr x0 x1 0 00000080 <else>: 80: 00000593 addi x11 x0 0 84: 00008067 jalr x0 x1 0 ``` ### Analysis I choose an R-TYPE subtract instruction to be a sample to show how the PIPELINE work *`sub x7 x7 x5` ![](https://i.imgur.com/s11Kbzs.png) * IF-Stage * next instruction's pc = pc + 4 * `sub x7 x7 x5` it's machine code is 0x405383b3 ![](https://i.imgur.com/2ZAkqw1.png) * ID-Stage * rs1: 0x07 (x7) * rs2: 0x05 (x5) * rs1_data = 0x0000000e = 14(decimal) * rs2_data = 0x00000001 = 1(decimal) ![](https://i.imgur.com/fAnK3e6.png) * EX-Stage * you can see the leftist two MUXs's resource came from rs1_data/rs2_data(cause don't have data hazard here) * cause it is R-TYPE instruction , so the final input of ALU are camd from the front MUXs * ALU_in1:0x0000000e * ALU_in2:0x00000001 * ALU_out:0x0000000d is the right output ( 15 - 1 = 14) ![](https://i.imgur.com/ysHQrrB.png) * MEM-Stage * cause `sub x7 x7 x5` is R-TYPE,so I don't care about the value of addr/DI/DO(although they have value) ![](https://i.imgur.com/Zebhaj6.png) * WB-Stage * the input came from ALU_out in EX-Stage and just pass through MEM-Stage * look at the yellow line,you can see the output of WB's MUX go out the Pipeline and then write into Regfile,so now the instruction is actually done. ## Hazard issue when I run the code of below instruction , suffer from a Control Hazard ,because the BranchCtrl are in the EX-Stage so the penalty is 2 clocks,that is,we need to flush two instructions in IF and ID stages. `jal x1 80` ![](https://i.imgur.com/O1DYnNm.png) ![](https://i.imgur.com/SaGXVBT.png) ![](https://i.imgur.com/b5JILzc.png)

    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