魏晉成
    • 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
    --- title: "Lab2: RISC-V RV32I[MA] emulator with ELF support" --- Lab2: RISC-V RV32I[MA] emulator with ELF support for Count Leading Zeros === ###### tags: `RISC-V` `Computer Architeture` [TOC] **[Original Document](https://hackmd.io/@WeiCheng14159/rkUifs2Hw)** ## Rewritten Code in C Above original program is rewritten in `CLZ.c` by myself as below. ```c= unsigned char CLZ(unsigned int src){ unsigned char result; unsigned int mask = 0x80000000; for(result = 0; !(src & mask) && (result < 32); result ++){ mask >>= 1; } return result; } int _start(){ const unsigned int toCount = 0xf70; unsigned char result = CLZ(toCount); // Printing Part volatile char* tx = (volatile char*) 0x40002000; char toPrint; *tx = '0'; *tx = 'x'; toPrint = (result&0xf0)>>4; if(toPrint > 9) *tx = toPrint + 'A'; else *tx = toPrint + '0'; toPrint = result & 0xf; if(toPrint > 9) *tx = toPrint + 'A'; else *tx = toPrint + '0'; return 0; } ``` ## Execution with rv32emu 1. By using `riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -O3 -nostdlib ./CLZ.c -o CLZ3` to compile `CLZ.c` above with optimization in favor of speed and executing with `emu-rv32i CLZ3`, I had the result below. ```bash 0x14 >>> Execution time: 18295 ns >>> Instruction count: 137 (IPS=7488384) >>> Jumps: 22 (16.06%) - 2 forwards, 20 backwards >>> Branching T=19 (50.00%) F=19 (50.00%) ``` 2. By using `riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -Os -nostdlib ./CLZ.c -o CLZs` to compile `CLZ.c` above with optimization in favor of code size and executing with `emu-rv32i CLZs`, I had the result below. ```bash 0x14 >>> Execution time: 21801 ns >>> Instruction count: 177 (IPS=8118893) >>> Jumps: 48 (27.12%) - 24 forwards, 24 backwards >>> Branching T=23 (53.49%) F=20 (46.51%) ``` ## Checking Code Size By using `riscv-none-objdump -h CLZ3 > CLZ3h` and `riscv-none-objdump -h CLZs > CLZsh` to check the segments of code, I have below result. For `-Os` the compiler did a great job with a significant reduction in code size. * CLZ with -O3 ```bash CLZ3: file format elf32-littleriscv Sections: Idx Name Size VMA LMA File off Algn 0 .text 000000e0 00010054 00010054 00000054 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 1 .comment 00000033 00000000 00000000 00000134 2**0 CONTENTS, READONLY ``` * CLZ with -Os ```bash CLZs: file format elf32-littleriscv Sections: Idx Name Size VMA LMA File off Algn 0 .text 000000ac 00010054 00010054 00000054 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 1 .comment 00000033 00000000 00000000 00000100 2**0 CONTENTS, READONLY ``` ## Checking Disassembled Program By using `riscv-none-embed-objdump -d CLZ3 > CLZ3.as ` and `riscv-none-embed-objdump -d CLZs > CLZs.as` to check the disassembled code of the two elf files, I have below result. * CLZ with -O3 ```bash CLZ3: file format elf32-littleriscv Disassembly of section .text: 00010054 <CLZ>: 10054: 02054a63 bltz a0,10088 <CLZ+0x34> 10058: 80000737 lui a4,0x80000 1005c: 00000793 li a5,0 10060: 02000613 li a2,32 10064: 0080006f j 1006c <CLZ+0x18> 10068: 00c78c63 beq a5,a2,10080 <CLZ+0x2c> 1006c: 00175713 srli a4,a4,0x1 10070: 00178793 addi a5,a5,1 10074: 00e576b3 and a3,a0,a4 10078: 0ff7f793 andi a5,a5,255 1007c: fe0686e3 beqz a3,10068 <CLZ+0x14> 10080: 00078513 mv a0,a5 10084: 00008067 ret 10088: 00000793 li a5,0 1008c: ff5ff06f j 10080 <CLZ+0x2c> 00010090 <_start>: 10090: 00001637 lui a2,0x1 10094: 00100793 li a5,1 10098: 40000737 lui a4,0x40000 1009c: f7060613 addi a2,a2,-144 # f70 <CLZ-0xf0e4> 100a0: 02000593 li a1,32 100a4: 0080006f j 100ac <_start+0x1c> 100a8: 04b78a63 beq a5,a1,100fc <_start+0x6c> 100ac: 00175713 srli a4,a4,0x1 100b0: 00178793 addi a5,a5,1 100b4: 00c776b3 and a3,a4,a2 100b8: 0ff7f793 andi a5,a5,255 100bc: fe0686e3 beqz a3,100a8 <_start+0x18> 100c0: 40002737 lui a4,0x40002 100c4: 03000693 li a3,48 100c8: 00d70023 sb a3,0(a4) # 40002000 <__global_pointer$+0x3fff06cc> 100cc: 07800613 li a2,120 100d0: 0047d693 srli a3,a5,0x4 100d4: 03068693 addi a3,a3,48 100d8: 00c70023 sb a2,0(a4) 100dc: 00d70023 sb a3,0(a4) 100e0: 00f7f793 andi a5,a5,15 100e4: 00900693 li a3,9 100e8: 04f6f263 bgeu a3,a5,1012c <_start+0x9c> 100ec: 04178793 addi a5,a5,65 100f0: 00f70023 sb a5,0(a4) 100f4: 00000513 li a0,0 100f8: 00008067 ret 100fc: 400027b7 lui a5,0x40002 10100: 03000713 li a4,48 10104: 00e78023 sb a4,0(a5) # 40002000 <__global_pointer$+0x3fff06cc> 10108: 07800713 li a4,120 1010c: 00e78023 sb a4,0(a5) 10110: 03200713 li a4,50 10114: 00e78023 sb a4,0(a5) 10118: 03000793 li a5,48 1011c: 40002737 lui a4,0x40002 10120: 00f70023 sb a5,0(a4) # 40002000 <__global_pointer$+0x3fff06cc> 10124: 00000513 li a0,0 10128: 00008067 ret 1012c: 03078793 addi a5,a5,48 10130: fedff06f j 1011c <_start+0x8c> ``` * CLZ with -Os ```bash CLZs: file format elf32-littleriscv Disassembly of section .text: 00010054 <CLZ>: 10054: 80000737 lui a4,0x80000 10058: 00000793 li a5,0 1005c: 02000693 li a3,32 10060: 00a77633 and a2,a4,a0 10064: 00061463 bnez a2,1006c <CLZ+0x18> 10068: 00d79663 bne a5,a3,10074 <CLZ+0x20> 1006c: 00078513 mv a0,a5 10070: 00008067 ret 10074: 00178793 addi a5,a5,1 10078: 00175713 srli a4,a4,0x1 1007c: 0ff7f793 andi a5,a5,255 10080: fe1ff06f j 10060 <CLZ+0xc> 00010084 <_start>: 10084: 00001537 lui a0,0x1 10088: ff010113 addi sp,sp,-16 1008c: f7050513 addi a0,a0,-144 # f70 <CLZ-0xf0e4> 10090: 00112623 sw ra,12(sp) 10094: fc1ff0ef jal ra,10054 <CLZ> 10098: 40002737 lui a4,0x40002 1009c: 03000793 li a5,48 100a0: 00f70023 sb a5,0(a4) # 40002000 <__global_pointer$+0x3fff0700> 100a4: 07800793 li a5,120 100a8: 00f70023 sb a5,0(a4) 100ac: 00455793 srli a5,a0,0x4 100b0: 0ff7f793 andi a5,a5,255 100b4: 00900693 li a3,9 100b8: 02f6fc63 bgeu a3,a5,100f0 <_start+0x6c> 100bc: 04178793 addi a5,a5,65 100c0: 0ff7f793 andi a5,a5,255 100c4: 00f70023 sb a5,0(a4) 100c8: 00f57513 andi a0,a0,15 100cc: 00900793 li a5,9 100d0: 02a7f463 bgeu a5,a0,100f8 <_start+0x74> 100d4: 04150513 addi a0,a0,65 100d8: 400027b7 lui a5,0x40002 100dc: 00a78023 sb a0,0(a5) # 40002000 <__global_pointer$+0x3fff0700> 100e0: 00c12083 lw ra,12(sp) 100e4: 00000513 li a0,0 100e8: 01010113 addi sp,sp,16 100ec: 00008067 ret 100f0: 03078793 addi a5,a5,48 100f4: fcdff06f j 100c0 <_start+0x3c> 100f8: 03050513 addi a0,a0,48 100fc: fddff06f j 100d8 <_start+0x54> ``` ## Interesting Findings and Further Tests * Comparison with inline subroutine Because I found that under `-O3` optimization option, the compiler would inline the function `CLZ` into `_start`, thus I want to compare the generated elfs with and without inlining function`CLZ`. Below is the execution result with `riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -O3 -nostdlib ./CLZ.c -o CLZ3_inline` command to compile the c program with inlined function and `emu-rv32i CLZ3_inline` command to have below result. ```bash 0x14 >>> Execution time: 8677 ns >>> Instruction count: 137 (IPS=15788867) >>> Jumps: 22 (16.06%) - 2 forwards, 20 backwards >>> Branching T=19 (50.00%) F=19 (50.00%) ``` In comparison with the result of `emu-rv32i CLZ3` below. They in fact has the same program and performance, but with the overhead of the loader and memory, the execution time can even differ between two execution results with the same elf. ```bash 0x14 >>> Execution time: 18295 ns >>> Instruction count: 137 (IPS=7488384) >>> Jumps: 22 (16.06%) - 2 forwards, 20 backwards >>> Branching T=19 (50.00%) F=19 (50.00%) ``` By using `riscv-none-embed-objdump -d CLZ3_inline > CLZ3_inline.as`, we have the disassembled file of program compiled with `-O3` flag and `inline` function below. ```bash CLZ3_inline: file format elf32-littleriscv Disassembly of section .text: 00010054 <_start>: 10054: 00001637 lui a2,0x1 10058: 00100793 li a5,1 1005c: 40000737 lui a4,0x40000 10060: f7060613 addi a2,a2,-144 # f70 <_start-0xf0e4> 10064: 02000593 li a1,32 10068: 0080006f j 10070 <_start+0x1c> 1006c: 04b78a63 beq a5,a1,100c0 <_start+0x6c> 10070: 00175713 srli a4,a4,0x1 10074: 00178793 addi a5,a5,1 10078: 00c776b3 and a3,a4,a2 1007c: 0ff7f793 andi a5,a5,255 10080: fe0686e3 beqz a3,1006c <_start+0x18> 10084: 40002737 lui a4,0x40002 10088: 03000693 li a3,48 1008c: 00d70023 sb a3,0(a4) # 40002000 <__global_pointer$+0x3fff0708> 10090: 07800613 li a2,120 10094: 0047d693 srli a3,a5,0x4 10098: 03068693 addi a3,a3,48 1009c: 00c70023 sb a2,0(a4) 100a0: 00d70023 sb a3,0(a4) 100a4: 00f7f793 andi a5,a5,15 100a8: 00900693 li a3,9 100ac: 04f6f263 bgeu a3,a5,100f0 <_start+0x9c> 100b0: 04178793 addi a5,a5,65 100b4: 00f70023 sb a5,0(a4) 100b8: 00000513 li a0,0 100bc: 00008067 ret 100c0: 400027b7 lui a5,0x40002 100c4: 03000713 li a4,48 100c8: 00e78023 sb a4,0(a5) # 40002000 <__global_pointer$+0x3fff0708> 100cc: 07800713 li a4,120 100d0: 00e78023 sb a4,0(a5) 100d4: 03200713 li a4,50 100d8: 00e78023 sb a4,0(a5) 100dc: 03000793 li a5,48 100e0: 40002737 lui a4,0x40002 100e4: 00f70023 sb a5,0(a4) # 40002000 <__global_pointer$+0x3fff0708> 100e8: 00000513 li a0,0 100ec: 00008067 ret 100f0: 03078793 addi a5,a5,48 100f4: fedff06f j 100e0 <_start+0x8c> ``` ![](https://i.imgur.com/nh6Q3SW.png) Magically, every instruction and registers used CLZ3_inline.as and CLZ3.as are identical in `_start` function, and the only difference is address number for jump and branch. * Unsigned Char Causing Further Instructions In the result of CLZ function, I think of using `unsigned char` type (same as `uint8_t`) would save the system some space. However in a 32-bit system, compiler generates code that mask lower 8 bit of register that stores `result` in while loop (`andi a5, a5, 255`). On average this action would cause 16 more instruction in execution time. Thus, using uint8_t would be a trade off between time and space in this case.

    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