Wayne
    • 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
    --- tags : Computer Architecture --- # Assignment2: RISC-V Toolchain [TOC] ## Introduction This is referenced from `段雅培` [Inner Dot Product](https://hackmd.io/@kaeteyaruyo/risc-v-hw1) ## Rewrite in C Code ``` clike= #include <stdio.h> ////// return floor(a / b) ////// int div(int a, int b){ int counter = 0; while(a>=0){ a = a-b; counter++; } return counter-1; } ////// return (a % b) ////// int mod(int a, int b){ int t = 0; while(a>=0){ t = a; a = a-b; } return t; } int _start(){ volatile char *tx = (volatile char*) 0x40002000; char *str1 = "The inner product of two vectors is "; int a[3] = {-1, -2, 3}; int b[3] = {4, -5, -6}; int length = 3; int t = 0; int sum = 0; for (int i = 0; i < length; i++){ sum += a[i] * b[i]; } //------------ print ------------// ////// print a[] ////// *tx = 'a'; *tx = ':'; for(int i = 0; i<length ; i++){ if((a[i]>>31) != 0){ *tx = '-'; t = ~(a[i]-1); } else t = a[i]; *tx = t + '0'; *tx = ' '; } *tx = '\n'; ////// print b[] ////// *tx = 'b'; *tx = ':'; for(int i = 0; i<length ; i++){ if((b[i]>>31) != 0){ *tx = '-'; t = ~(b[i]-1); } else t = b[i]; *tx = t + '0'; *tx = ' '; } *tx = '\n'; ////// print str1 ////// while(*str1!='\0') *tx = *str1++; ////// print sum ////// char ans[10]; int index = 0; int s = sum; if((s>>31) != 0){ *tx = '-'; s = ~(s-1); } while(s != 0){ t = mod(s, 10); s = div(s, 10); ans[index++] = t + '0'; } while(index>0) *tx = ans[--index]; *tx = '\n'; return 0; } ``` ## Execute it ### ``` cc -O3 -Wall -std=gnu99 -o emu-rv32i emu-rv32i.c -lelf riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -O3 -nostdlib -o test dot.c ./emu-rv32i test a:-1 -2 3 b:4 -5 -6 The inner product of two vectors is -12 >>> Execution time: 251504 ns >>> Instruction count: 300 (IPS=1192823) >>> Jumps: 47 (15.67%) - 8 forwards, 39 backwards >>> Branching T=41 (78.85%) F=11 (21.15%) ``` ## Toolchain ### $ riscv-none-embed-objdump -d test ``` test: file format elf32-littleriscv Disassembly of section .text: 00010054 <div>: 10054: 00050793 mv a5,a0 10058: 02054063 bltz a0,10078 <div+0x24> 1005c: 00000513 li a0,0 10060: 0080006f j 10068 <div+0x14> 10064: 00070513 mv a0,a4 10068: 40b787b3 sub a5,a5,a1 1006c: 00150713 addi a4,a0,1 10070: fe07dae3 bgez a5,10064 <div+0x10> 10074: 00008067 ret 10078: fff00513 li a0,-1 1007c: 00008067 ret 00010080 <mod>: 10080: 00055663 bgez a0,1008c <mod+0xc> 10084: 0140006f j 10098 <mod+0x18> 10088: 00078513 mv a0,a5 1008c: 40b507b3 sub a5,a0,a1 10090: fe07dce3 bgez a5,10088 <mod+0x8> 10094: 00008067 ret 10098: 00000513 li a0,0 1009c: 00008067 ret 000100a0 <_start>: 100a0: fd010113 addi sp,sp,-48 100a4: fff00793 li a5,-1 100a8: 00f12c23 sw a5,24(sp) 100ac: ffe00793 li a5,-2 100b0: 00f12e23 sw a5,28(sp) 100b4: 00300793 li a5,3 100b8: 02f12023 sw a5,32(sp) 100bc: 00400793 li a5,4 100c0: 02f12223 sw a5,36(sp) 100c4: ffb00793 li a5,-5 100c8: 02f12423 sw a5,40(sp) 100cc: ffa00793 li a5,-6 100d0: 02f12623 sw a5,44(sp) 100d4: 40002737 lui a4,0x40002 100d8: 06100793 li a5,97 100dc: 00f70023 sb a5,0(a4) # 40002000 <__global_pointer$+0x3fff0428> 100e0: 03a00793 li a5,58 100e4: 00f70023 sb a5,0(a4) 100e8: 01812783 lw a5,24(sp) 100ec: 0007da63 bgez a5,10100 <_start+0x60> 100f0: 02d00793 li a5,45 100f4: 00f70023 sb a5,0(a4) 100f8: 01812783 lw a5,24(sp) 100fc: 40f007b3 neg a5,a5 10100: 03078793 addi a5,a5,48 10104: 40002737 lui a4,0x40002 10108: 0ff7f793 andi a5,a5,255 1010c: 00f70023 sb a5,0(a4) # 40002000 <__global_pointer$+0x3fff0428> 10110: 02000793 li a5,32 10114: 00f70023 sb a5,0(a4) 10118: 01c12783 lw a5,28(sp) 1011c: 0007da63 bgez a5,10130 <_start+0x90> 10120: 02d00793 li a5,45 10124: 00f70023 sb a5,0(a4) 10128: 01c12783 lw a5,28(sp) 1012c: 40f007b3 neg a5,a5 10130: 03078793 addi a5,a5,48 10134: 40002737 lui a4,0x40002 10138: 0ff7f793 andi a5,a5,255 1013c: 00f70023 sb a5,0(a4) # 40002000 <__global_pointer$+0x3fff0428> 10140: 02000793 li a5,32 10144: 00f70023 sb a5,0(a4) 10148: 02012783 lw a5,32(sp) 1014c: 0007da63 bgez a5,10160 <_start+0xc0> 10150: 02d00793 li a5,45 10154: 00f70023 sb a5,0(a4) 10158: 02012783 lw a5,32(sp) 1015c: 40f007b3 neg a5,a5 10160: 03078793 addi a5,a5,48 10164: 40002737 lui a4,0x40002 10168: 0ff7f793 andi a5,a5,255 1016c: 00f70023 sb a5,0(a4) # 40002000 <__global_pointer$+0x3fff0428> 10170: 02000793 li a5,32 10174: 00f70023 sb a5,0(a4) 10178: 00a00793 li a5,10 1017c: 00f70023 sb a5,0(a4) 10180: 06200793 li a5,98 10184: 00f70023 sb a5,0(a4) 10188: 03a00793 li a5,58 1018c: 00f70023 sb a5,0(a4) 10190: 02412783 lw a5,36(sp) 10194: 0007da63 bgez a5,101a8 <_start+0x108> 10198: 02d00793 li a5,45 1019c: 00f70023 sb a5,0(a4) 101a0: 02412783 lw a5,36(sp) 101a4: 40f007b3 neg a5,a5 101a8: 03078793 addi a5,a5,48 101ac: 40002737 lui a4,0x40002 101b0: 0ff7f793 andi a5,a5,255 101b4: 00f70023 sb a5,0(a4) # 40002000 <__global_pointer$+0x3fff0428> 101b8: 02000793 li a5,32 101bc: 00f70023 sb a5,0(a4) 101c0: 02812783 lw a5,40(sp) 101c4: 0007da63 bgez a5,101d8 <_start+0x138> 101c8: 02d00793 li a5,45 101cc: 00f70023 sb a5,0(a4) 101d0: 02812783 lw a5,40(sp) 101d4: 40f007b3 neg a5,a5 101d8: 03078793 addi a5,a5,48 101dc: 40002737 lui a4,0x40002 101e0: 0ff7f793 andi a5,a5,255 101e4: 00f70023 sb a5,0(a4) # 40002000 <__global_pointer$+0x3fff0428> 101e8: 02000793 li a5,32 101ec: 00f70023 sb a5,0(a4) 101f0: 02c12783 lw a5,44(sp) 101f4: 0007da63 bgez a5,10208 <_start+0x168> 101f8: 02d00793 li a5,45 101fc: 00f70023 sb a5,0(a4) 10200: 02c12783 lw a5,44(sp) 10204: 40f007b3 neg a5,a5 10208: 03078793 addi a5,a5,48 1020c: 40002737 lui a4,0x40002 10210: 0ff7f793 andi a5,a5,255 10214: 00f70023 sb a5,0(a4) # 40002000 <__global_pointer$+0x3fff0428> 10218: 02000793 li a5,32 1021c: 00f70023 sb a5,0(a4) 10220: 00a00793 li a5,10 10224: 00f70023 sb a5,0(a4) 10228: 000107b7 lui a5,0x10 1022c: 3b078793 addi a5,a5,944 # 103b0 <_start+0x310> 10230: 05400713 li a4,84 10234: 400026b7 lui a3,0x40002 10238: 00178793 addi a5,a5,1 1023c: 00e68023 sb a4,0(a3) # 40002000 <__global_pointer$+0x3fff0428> 10240: 0007c703 lbu a4,0(a5) 10244: fe071ae3 bnez a4,10238 <_start+0x198> 10248: 02d00793 li a5,45 1024c: 00c10593 addi a1,sp,12 10250: 00100813 li a6,1 10254: 00f68023 sb a5,0(a3) 10258: 40b80833 sub a6,a6,a1 1025c: 00c00793 li a5,12 10260: 00b80533 add a0,a6,a1 10264: 00078693 mv a3,a5 10268: 0080006f j 10270 <_start+0x1d0> 1026c: 00070693 mv a3,a4 10270: ff668713 addi a4,a3,-10 10274: fe075ce3 bgez a4,1026c <_start+0x1cc> 10278: 00000713 li a4,0 1027c: 0080006f j 10284 <_start+0x1e4> 10280: 00060713 mv a4,a2 10284: ff678793 addi a5,a5,-10 10288: 00170613 addi a2,a4,1 1028c: fe07dae3 bgez a5,10280 <_start+0x1e0> 10290: 03068693 addi a3,a3,48 10294: 00d58023 sb a3,0(a1) 10298: 00070863 beqz a4,102a8 <_start+0x208> 1029c: 00158593 addi a1,a1,1 102a0: 00070793 mv a5,a4 102a4: fbdff06f j 10260 <_start+0x1c0> 102a8: fff50713 addi a4,a0,-1 102ac: 03010793 addi a5,sp,48 102b0: 00e787b3 add a5,a5,a4 102b4: fdc7c683 lbu a3,-36(a5) 102b8: 400027b7 lui a5,0x40002 102bc: 00d78023 sb a3,0(a5) # 40002000 <__global_pointer$+0x3fff0428> 102c0: 0c070c63 beqz a4,10398 <_start+0x2f8> 102c4: ffe50713 addi a4,a0,-2 102c8: 03010693 addi a3,sp,48 102cc: 00e686b3 add a3,a3,a4 102d0: fdc6c683 lbu a3,-36(a3) 102d4: 00d78023 sb a3,0(a5) 102d8: 0c070063 beqz a4,10398 <_start+0x2f8> 102dc: ffd50713 addi a4,a0,-3 102e0: 03010693 addi a3,sp,48 102e4: 00e686b3 add a3,a3,a4 102e8: fdc6c683 lbu a3,-36(a3) 102ec: 00d78023 sb a3,0(a5) 102f0: 0a070463 beqz a4,10398 <_start+0x2f8> 102f4: ffc50713 addi a4,a0,-4 102f8: 03010693 addi a3,sp,48 102fc: 00e686b3 add a3,a3,a4 10300: fdc6c683 lbu a3,-36(a3) 10304: 00d78023 sb a3,0(a5) 10308: 08070863 beqz a4,10398 <_start+0x2f8> 1030c: ffb50713 addi a4,a0,-5 10310: 03010693 addi a3,sp,48 10314: 00e686b3 add a3,a3,a4 10318: fdc6c683 lbu a3,-36(a3) 1031c: 00d78023 sb a3,0(a5) 10320: 06070c63 beqz a4,10398 <_start+0x2f8> 10324: ffa50713 addi a4,a0,-6 10328: 03010693 addi a3,sp,48 1032c: 00e686b3 add a3,a3,a4 10330: fdc6c683 lbu a3,-36(a3) 10334: 00d78023 sb a3,0(a5) 10338: 06070063 beqz a4,10398 <_start+0x2f8> 1033c: ff950713 addi a4,a0,-7 10340: 03010693 addi a3,sp,48 10344: 00e686b3 add a3,a3,a4 10348: fdc6c683 lbu a3,-36(a3) 1034c: 00d78023 sb a3,0(a5) 10350: 04070463 beqz a4,10398 <_start+0x2f8> 10354: ff850713 addi a4,a0,-8 10358: 03010693 addi a3,sp,48 1035c: 00e686b3 add a3,a3,a4 10360: fdc6c683 lbu a3,-36(a3) 10364: 00d78023 sb a3,0(a5) 10368: 02070863 beqz a4,10398 <_start+0x2f8> 1036c: ff750713 addi a4,a0,-9 10370: 03010793 addi a5,sp,48 10374: 00e787b3 add a5,a5,a4 10378: fdc7c683 lbu a3,-36(a5) 1037c: 400027b7 lui a5,0x40002 10380: 00d78023 sb a3,0(a5) # 40002000 <__global_pointer$+0x3fff0428> 10384: 00070a63 beqz a4,10398 <_start+0x2f8> 10388: 03010713 addi a4,sp,48 1038c: 00a70533 add a0,a4,a0 10390: fd254703 lbu a4,-46(a0) 10394: 00e78023 sb a4,0(a5) 10398: 400027b7 lui a5,0x40002 1039c: 00a00713 li a4,10 103a0: 00e78023 sb a4,0(a5) # 40002000 <__global_pointer$+0x3fff0428> 103a4: 00000513 li a0,0 103a8: 03010113 addi sp,sp,48 103ac: 00008067 ret ``` ### $ riscv-none-embed-readelf -h test ``` 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: 0x100a0 Start of program headers: 52 (bytes into file) Start of section headers: 1428 (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 ``` ### $ riscv-none-embed-size test ``` text data bss dec hex filename 900 0 0 900 384 test ```

    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