sam2468sam
    • 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
    # Homework2: RISC-V Toolchain ## Dot Product ### Pick up a assembly program from Homework1 * choose Dot Product from [段雅培](https://hackmd.io/@kaeteyaruyo/risc-v-hw1) ### Rewrite the C code ```cpp= void _start() { int a[3] = {1,2,3}; int b[3] = {-4,-5,6}; int sum = 0; int i; for(i=0;i<3;i++) sum=sum+a[i]*b[i]; volatile char *tx = (volatile char *)0x40002000; const char *result = "The inner product of two vectors is "; while (*result){ *tx = *result; result++; } *tx = (char)((sum +'0')); *tx = '\n'; } ``` :::spoiler ### Handwritten Assembly code from 段雅培 ```cpp= .data arr1: .word 1, 2, 3 # a[3] = {2, 4, 6} arr2: .word 4, 5, 6 # b[3] = {8, 10, 12} len: .word 3 # array length = 3 str: .string "The inner product of two vectors is " .text # s1 = arr1 base address # s2 = arr2 base address # s3 = array length # s4 = sum # t0 = i # t1 = a[i] # t2 = b[i] # t3 = a[i] * b[i] (assume no overflow, lower 32 bits) main: la s1, arr1 # s1 = a la s2, arr2 # s2 = b lw s3, len # s3 = 3 add s4, x0, x0 # sum = 0 add t0, x0, x0 # i = 0 jal ra, loop # jump to for loop jal ra, print # jump to for loop li a7, 10 # end program ecall loop: lw t1, 0(s1) # t1 = a[i] addi s1, s1, 4 # ++a (address move forward) lw t2, 0(s2) # t2 = b[i] addi s2, s2, 4 # ++b mul t3, t1, t2 # t3 = a[i] * b[i] add s4, s4, t3 # sum += a[i] * b[i] addi t0, t0, 1 # ++i blt t0, s3, loop # if i < 3, go to loop ret # else, return to main print: la a0, str # load string li a7, 4 # print string ecall add a0, s4, x0 # load result li a7, 1 # print integer ecall ret # go back to main ``` ::: :::spoiler ### Compiler optimized Assembly code by O3 ```cpp= DotProduct: file format elf32-littleriscv Disassembly of section .text: 00010054 <_start>: 10054: 000107b7 lui a5,0x10 10058: 08878793 addi a5,a5,136 # 10088 <_start+0x34> 1005c: 05400713 li a4,84 10060: 400026b7 lui a3,0x40002 10064: 00e68023 sb a4,0(a3) # 40002000 <__global_pointer$+0x3fff0750> 10068: 00178793 addi a5,a5,1 1006c: 0007c703 lbu a4,0(a5) 10070: fe071ae3 bnez a4,10064 <_start+0x10> 10074: 03400793 li a5,52 10078: 00f68023 sb a5,0(a3) 1007c: 00a00793 li a5,10 10080: 00f68023 sb a5,0(a3) 10084: 00008067 ret ``` ::: :::spoiler ### Compiler optimized Assembly code by Os ```cpp= DotProduct: file format elf32-littleriscv Disassembly of section .text: 00010054 <_start>: 10054: 000107b7 lui a5,0x10 10058: 08c78793 addi a5,a5,140 # 1008c <_start+0x38> 1005c: 400026b7 lui a3,0x40002 10060: 0007c703 lbu a4,0(a5) 10064: 00071e63 bnez a4,10080 <_start+0x2c> 10068: 400027b7 lui a5,0x40002 1006c: 03400713 li a4,52 10070: 00e78023 sb a4,0(a5) # 40002000 <__global_pointer$+0x3fff074c> 10074: 00a00713 li a4,10 10078: 00e78023 sb a4,0(a5) 1007c: 00008067 ret 10080: 00e68023 sb a4,0(a3) # 40002000 <__global_pointer$+0x3fff074c> 10084: 00178793 addi a5,a5,1 10088: fd9ff06f j 10060 <_start+0xc> ``` ::: ### Result by O3 ``` ./emu-rv32i DotProduct The inner product of two vectors is 4 >>> Execution time: 3523200 ns >>> Instruction count: 154 (IPS=43710) >>> Jumps: 36 (23.38%) - 0 forwards, 36 backwards >>> Branching T=35 (97.22%) F=1 (2.78%) ``` ### Result by Os ``` ./emu-rv32i DotProduct The inner product of two vectors is 4 >>> Execution time: 3628300 ns >>> Instruction count: 192 (IPS=52917) >>> Jumps: 73 (38.02%) - 36 forwards, 37 backwards >>> Branching T=36 (97.30%) F=1 (2.70%) ``` :::spoiler ### Readelf by O3 ``` ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: RISC-V Version: 0x1 Entry point address: 0x10054 Start of program headers: 52 (bytes into file) Start of section headers: 588 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 1 Size of section headers: 40 (bytes) Number of section headers: 7 Section header string table index: 6 ``` ::: :::spoiler ### Readelf by Os ``` ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: RISC-V Version: 0x1 Entry point address: 0x10054 Start of program headers: 52 (bytes into file) Start of section headers: 592 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 1 Size of section headers: 40 (bytes) Number of section headers: 7 Section header string table index: 6 ``` ::: ### Size by O3 ``` text data bss dec hex filename 92 0 0 92 5c DotProduct ``` ### Size by Os ``` text data bss dec hex filename 96 0 0 96 60 DotProduct ```

    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