Soonmyun Jang
    • 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
    # Assignment1: Prime Number ## Definition of prime number A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. (https://en.wikipedia.org/wiki/Prime_number) ## C code ```clike= #include <stdio.h> int IsPrimeNumber(int n); int main(void) { int i = 0; for (i = 1; i <= 20; i++) { if (IsPrimeNumber(i)) { printf("%3d ", i); } } printf("\n"); return 0; } int IsPrimeNumber(int n) { int i = 0; int last = n / 2; if (n <= 1) { return 0; } for (i = 2; i <= last; i++) { if ((n%i) == 0) { return 0; } } return 1; } ``` ## Assembly code ```clike= .data argument: .word 20 # Number to find the prime number str1: .string " is prime number" .text main: # set number lw a2, argument li a3, 0 jal ra, Loop1 # Exit program li a0, 10 ecall Loop1: addi a3, a3, 1 blt a3, a2, IsPrime ret IsPrime: li t3, 2 li t4, 1 div t5, a3, t3 blt a3, t3, Loop1 addi t5, t5, 1 j Loop2 Loop2: addi t4, t4, 1 bge t4, t5, PrintResult rem t6, a3, t4 beq t6, zero, Loop1 # return j Loop2 PrintResult: mv t0, a3 mv a1, t0 li a0, 1 ecall la a1, str1 li a0, 4 ecall j Loop1 ``` ## Explaination ### Fucntions #### main Main function is to excute the program #### Loop1 Loop1 function is to repeat codes(literally loop function). Before the function, target number for finding prime numbers should set. E.g.) If target number is 10, then the Loop1 function will repeat 10 times. #### IsPrime IsPrime function is to ascertain whether a number is prime or not. #### Loop2 Loop2 function is a loop function in IsPrime function. It checks numbers and when it finds prime number print results. #### PrintResult This function is to show results of prime numbers. ### Pipeline 1) IF: Instruction Fetch 2) ID: Instruction Decode / Register file read 3) EX: Execute / Address calculation 4) MEM: Memory Address 5) WB: Write Back At the first line in the main function. `lw x12 0(x12)` Firstly, in IF stage, the CPU fetches instruction. ![](https://i.imgur.com/YmmStCM.png) Here ID stage, data is loaded into `x12` as you see Read data `0xc` and Read data `0x0` in the Registers. ![](https://i.imgur.com/8iRCniR.png) And then it excuted in EX stage. ![](https://i.imgur.com/2wWA56p.png) In this instuction, data is `argument: .word 20"`. Therefore you can see this word data is saved into `x12`. ![](https://i.imgur.com/TAmTwb6.png) ![](https://i.imgur.com/HGi8vbx.png) However instuction `lw x12 0(x12)` is only for loading data, so there is no conditional statement or calculation. I will show you what happens in the system during other instruction. For example, here is `addi x13 x0 0`, which is next instruction. This is to save value that sum of `x0` and `0` into `x13`, so `x13` will be `0`. ![](https://i.imgur.com/yQkjx39.png) And if there is jump instruction, you can save current address into `ra(x1)`. After finishing function you can return to the previous address. And IF stage will be started in other position that you jumped. ![](https://i.imgur.com/OMwR5sv.png) ![](https://i.imgur.com/bjCFY03.png) In ID stage with blt instruction `blt x13 x12 8`, two values(`x13 and x12`) are compared and branched depending on the result. blt: branch if less than ~ bge: branch if grater than or equal ~ beq: branch if equal ~ bne: branch if not equal ~ ![](https://i.imgur.com/DIRNC12.png) ## Result I set 20 as a target number. It means the system will find prime numbers from 2 to 20. And results are 2, 3, 5, 7, 11, 13, 17 and 19. ![](https://i.imgur.com/AabFp0d.png) ## Writer Soonmyun Jang P76077133 jsm890803.3@gmail.com

    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