E94079029
    • 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
    # Assignment2: RISC-V Toolchain contributed by < `Cheng Yu` > ###### tags: `Computer Architure` ## Bubble sort Choose bubble sort assembly code from [王傑世](https://hackmd.io/4-oWQOprRnCLu3ZeJy31kA?view) ### Rewrite C code ```cpp void BBSort(int *arr, int size) { for (int i = 0; i < size; ++i) for (int j = 0; j < size - i; ++j) if (arr[j] < arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } int _start() { int arr[] = {2, 3, 7, 4, 1}; int size = 5; BBSort(arr, size - 1); volatile char* tx = (volatile char*) 0x40002000; for (int i = 0; i < size; ++i) { *tx = arr[i] + '0'; *tx = '\n'; } return 0; } ``` Then we can run the following instructions ``` $ riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -O3 -nostdlib bubble_sort.c -o bubble_sort $ ./emu-rv32i bubble_sort ``` O0 ![](https://i.imgur.com/dfMNeLa.png) ELF file header ![](https://i.imgur.com/CZKkhCy.png) ``` Disassembly of section .text: 00010054 <BBSort>: 10054: fd010113 addi sp,sp,-48 10058: 02812623 sw s0,44(sp) 1005c: 03010413 addi s0,sp,48 10060: fca42e23 sw a0,-36(s0) 10064: fcb42c23 sw a1,-40(s0) 10068: fe042623 sw zero,-20(s0) 1006c: 0c80006f j 10134 <BBSort+0xe0> 10070: fe042423 sw zero,-24(s0) 10074: 0a00006f j 10114 <BBSort+0xc0> 10078: fe842783 lw a5,-24(s0) 1007c: 00279793 slli a5,a5,0x2 10080: fdc42703 lw a4,-36(s0) 10084: 00f707b3 add a5,a4,a5 10088: 0007a703 lw a4,0(a5) 1008c: fe842783 lw a5,-24(s0) 10090: 00178793 addi a5,a5,1 10094: 00279793 slli a5,a5,0x2 10098: fdc42683 lw a3,-36(s0) 1009c: 00f687b3 add a5,a3,a5 100a0: 0007a783 lw a5,0(a5) 100a4: 06f75263 bge a4,a5,10108 <BBSort+0xb4> 100a8: fe842783 lw a5,-24(s0) 100ac: 00279793 slli a5,a5,0x2 100b0: fdc42703 lw a4,-36(s0) 100b4: 00f707b3 add a5,a4,a5 100b8: 0007a783 lw a5,0(a5) 100bc: fef42223 sw a5,-28(s0) 100c0: fe842783 lw a5,-24(s0) 100c4: 00178793 addi a5,a5,1 100c8: 00279793 slli a5,a5,0x2 100cc: fdc42703 lw a4,-36(s0) 100d0: 00f70733 add a4,a4,a5 100d4: fe842783 lw a5,-24(s0) 100d8: 00279793 slli a5,a5,0x2 100dc: fdc42683 lw a3,-36(s0) 100e0: 00f687b3 add a5,a3,a5 100e4: 00072703 lw a4,0(a4) 100e8: 00e7a023 sw a4,0(a5) 100ec: fe842783 lw a5,-24(s0) 100f0: 00178793 addi a5,a5,1 100f4: 00279793 slli a5,a5,0x2 100f8: fdc42703 lw a4,-36(s0) 100fc: 00f707b3 add a5,a4,a5 10100: fe442703 lw a4,-28(s0) 10104: 00e7a023 sw a4,0(a5) 10108: fe842783 lw a5,-24(s0) 1010c: 00178793 addi a5,a5,1 10110: fef42423 sw a5,-24(s0) 10114: fd842703 lw a4,-40(s0) 10118: fec42783 lw a5,-20(s0) 1011c: 40f707b3 sub a5,a4,a5 10120: fe842703 lw a4,-24(s0) 10124: f4f74ae3 blt a4,a5,10078 <BBSort+0x24> 10128: fec42783 lw a5,-20(s0) 1012c: 00178793 addi a5,a5,1 10130: fef42623 sw a5,-20(s0) 10134: fec42703 lw a4,-20(s0) 10138: fd842783 lw a5,-40(s0) 1013c: f2f74ae3 blt a4,a5,10070 <BBSort+0x1c> 10140: 00000013 nop 10144: 02c12403 lw s0,44(sp) 10148: 03010113 addi sp,sp,48 1014c: 00008067 ret 00010150 <_start>: 10150: fd010113 addi sp,sp,-48 10154: 02112623 sw ra,44(sp) 10158: 02812423 sw s0,40(sp) 1015c: 03010413 addi s0,sp,48 10160: 000107b7 lui a5,0x10 10164: 2307a583 lw a1,560(a5) # 10230 <_start+0xe0> 10168: 23078713 addi a4,a5,560 1016c: 00472603 lw a2,4(a4) 10170: 23078713 addi a4,a5,560 10174: 00872683 lw a3,8(a4) 10178: 23078713 addi a4,a5,560 1017c: 00c72703 lw a4,12(a4) 10180: 23078793 addi a5,a5,560 10184: 0107a783 lw a5,16(a5) 10188: fcb42823 sw a1,-48(s0) 1018c: fcc42a23 sw a2,-44(s0) 10190: fcd42c23 sw a3,-40(s0) 10194: fce42e23 sw a4,-36(s0) 10198: fef42023 sw a5,-32(s0) 1019c: 00500793 li a5,5 101a0: fef42423 sw a5,-24(s0) 101a4: fe842783 lw a5,-24(s0) 101a8: fff78713 addi a4,a5,-1 101ac: fd040793 addi a5,s0,-48 101b0: 00070593 mv a1,a4 101b4: 00078513 mv a0,a5 101b8: e9dff0ef jal ra,10054 <BBSort> 101bc: 400027b7 lui a5,0x40002 101c0: fef42223 sw a5,-28(s0) 101c4: fe042623 sw zero,-20(s0) 101c8: 0440006f j 1020c <_start+0xbc> 101cc: fec42783 lw a5,-20(s0) 101d0: 00279793 slli a5,a5,0x2 101d4: ff040713 addi a4,s0,-16 101d8: 00f707b3 add a5,a4,a5 101dc: fe07a783 lw a5,-32(a5) # 40001fe0 <__global_pointer$+0x3fff059c> 101e0: 0ff7f793 andi a5,a5,255 101e4: 03078793 addi a5,a5,48 101e8: 0ff7f713 andi a4,a5,255 101ec: fe442783 lw a5,-28(s0) 101f0: 00e78023 sb a4,0(a5) 101f4: fe442783 lw a5,-28(s0) 101f8: 00a00713 li a4,10 101fc: 00e78023 sb a4,0(a5) 10200: fec42783 lw a5,-20(s0) 10204: 00178793 addi a5,a5,1 10208: fef42623 sw a5,-20(s0) 1020c: fec42703 lw a4,-20(s0) 10210: fe842783 lw a5,-24(s0) 10214: faf74ce3 blt a4,a5,101cc <_start+0x7c> 10218: 00000793 li a5,0 1021c: 00078513 mv a0,a5 10220: 02c12083 lw ra,44(sp) 10224: 02812403 lw s0,40(sp) 10228: 03010113 addi sp,sp,48 1022c: 00008067 ret ``` size ``` text data bss dec hex filename 496 0 0 496 1f0 bubble_sort ``` O3 ![](https://i.imgur.com/CzfkXHm.png) ``` Disassembly of section .text: 00010054 <BBSort>: 10054: 02b05a63 blez a1,10088 <BBSort+0x34> 10058: 00259613 slli a2,a1,0x2 1005c: 00c50633 add a2,a0,a2 10060: 00050793 mv a5,a0 10064: 0007a703 lw a4,0(a5) 10068: 0047a683 lw a3,4(a5) 1006c: 00d75663 bge a4,a3,10078 <BBSort+0x24> 10070: 00d7a023 sw a3,0(a5) 10074: 00e7a223 sw a4,4(a5) 10078: 00478793 addi a5,a5,4 1007c: fec794e3 bne a5,a2,10064 <BBSort+0x10> 10080: ffc60613 addi a2,a2,-4 10084: fcc51ee3 bne a0,a2,10060 <BBSort+0xc> 10088: 00008067 ret 0001008c <_start>: 1008c: 000107b7 lui a5,0x10 10090: 21478793 addi a5,a5,532 # 10214 <_start+0x188> 10094: 0007a703 lw a4,0(a5) 10098: 0047a683 lw a3,4(a5) 1009c: 0087a583 lw a1,8(a5) 100a0: 00c7a603 lw a2,12(a5) 100a4: 0107a783 lw a5,16(a5) 100a8: fe010113 addi sp,sp,-32 100ac: 00e12623 sw a4,12(sp) 100b0: 00d12823 sw a3,16(sp) 100b4: 00b12a23 sw a1,20(sp) 100b8: 00c12c23 sw a2,24(sp) 100bc: 00f12e23 sw a5,28(sp) 100c0: 00d75663 bge a4,a3,100cc <_start+0x40> 100c4: 00d12623 sw a3,12(sp) 100c8: 00e12823 sw a4,16(sp) 100cc: 01012783 lw a5,16(sp) 100d0: 01412703 lw a4,20(sp) 100d4: 00e7dc63 bge a5,a4,100ec <_start+0x60> 100d8: 00078693 mv a3,a5 100dc: 00e12823 sw a4,16(sp) 100e0: 00f12a23 sw a5,20(sp) 100e4: 00070793 mv a5,a4 100e8: 00068713 mv a4,a3 100ec: 01812603 lw a2,24(sp) 100f0: 00c75c63 bge a4,a2,10108 <_start+0x7c> 100f4: 00070693 mv a3,a4 100f8: 00c12a23 sw a2,20(sp) 100fc: 00e12c23 sw a4,24(sp) 10100: 00060713 mv a4,a2 10104: 00068613 mv a2,a3 10108: 01c12683 lw a3,28(sp) 1010c: 0ed64c63 blt a2,a3,10204 <_start+0x178> 10110: 00c12683 lw a3,12(sp) 10114: 00f6dc63 bge a3,a5,1012c <_start+0xa0> 10118: 00078593 mv a1,a5 1011c: 00f12623 sw a5,12(sp) 10120: 00d12823 sw a3,16(sp) 10124: 00068793 mv a5,a3 10128: 00058693 mv a3,a1 1012c: 00e7dc63 bge a5,a4,10144 <_start+0xb8> 10130: 00078593 mv a1,a5 10134: 00e12823 sw a4,16(sp) 10138: 00f12a23 sw a5,20(sp) 1013c: 00070793 mv a5,a4 10140: 00058713 mv a4,a1 10144: 0ac74863 blt a4,a2,101f4 <_start+0x168> 10148: 00f6dc63 bge a3,a5,10160 <_start+0xd4> 1014c: 00078613 mv a2,a5 10150: 00f12623 sw a5,12(sp) 10154: 00d12823 sw a3,16(sp) 10158: 00068793 mv a5,a3 1015c: 00060693 mv a3,a2 10160: 00e7d863 bge a5,a4,10170 <_start+0xe4> 10164: 00f12a23 sw a5,20(sp) 10168: 00e12823 sw a4,16(sp) 1016c: 00070793 mv a5,a4 10170: 00f6d663 bge a3,a5,1017c <_start+0xf0> 10174: 00f12623 sw a5,12(sp) 10178: 00d12823 sw a3,16(sp) 1017c: 00c12683 lw a3,12(sp) 10180: 400027b7 lui a5,0x40002 10184: 00a00713 li a4,10 10188: 03068693 addi a3,a3,48 1018c: 0ff6f693 andi a3,a3,255 10190: 00d78023 sb a3,0(a5) # 40002000 <__global_pointer$+0x3fff05d8> 10194: 00e78023 sb a4,0(a5) 10198: 01012683 lw a3,16(sp) 1019c: 00000513 li a0,0 101a0: 03068693 addi a3,a3,48 101a4: 0ff6f693 andi a3,a3,255 101a8: 00d78023 sb a3,0(a5) 101ac: 00e78023 sb a4,0(a5) 101b0: 01412683 lw a3,20(sp) 101b4: 03068693 addi a3,a3,48 101b8: 0ff6f693 andi a3,a3,255 101bc: 00d78023 sb a3,0(a5) 101c0: 00e78023 sb a4,0(a5) 101c4: 01812683 lw a3,24(sp) 101c8: 03068693 addi a3,a3,48 101cc: 0ff6f693 andi a3,a3,255 101d0: 00d78023 sb a3,0(a5) 101d4: 00e78023 sb a4,0(a5) 101d8: 01c12683 lw a3,28(sp) 101dc: 03068693 addi a3,a3,48 101e0: 0ff6f693 andi a3,a3,255 101e4: 00d78023 sb a3,0(a5) 101e8: 00e78023 sb a4,0(a5) 101ec: 02010113 addi sp,sp,32 101f0: 00008067 ret 101f4: 00e12c23 sw a4,24(sp) 101f8: 00c12a23 sw a2,20(sp) 101fc: 00060713 mv a4,a2 10200: f49ff06f j 10148 <_start+0xbc> 10204: 00c12e23 sw a2,28(sp) 10208: 00d12c23 sw a3,24(sp) 1020c: 00068613 mv a2,a3 10210: f01ff06f j 10110 <_start+0x84> ``` size ``` text data bss dec hex filename 468 0 0 468 1d4 bubble_sort ```

    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