Ryan.Y.C.Su
    • 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 No publishing access yet

      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.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      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
    • Make a copy
    • 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 Make a copy 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    # Shift-and-Subtract Division contributed by <`ryanycs`> :::danger Mention your GitHub account here. ::: ## Software Division RV32I does not include `div` instruction, which indicates integer division must be implemented in software. A common and efficient approach is the **shift-and-subtract division**, a simplified form of binary long division. In the following section, we will illustrate how this algorithm works, starting from the decimal long-division and eventually a complete RV32I implementation. ## Inspiration: Decimal Long Division We consider the division: $$ 100 \div 7 $$ We can perform the standard long division: ``` ┌────── 7 │ 100 ``` Let us break down the calculating process step by step: We begin by taking digits from **left to right**. The first digit is $1$, we set the *remainder* $R$ to $1$. Since the *remainder* $R = 1$ is less than the *diviser* $7$, no subtraction is performed at this stage, and the first *quotient* $Q$ is $0$. ``` 0 ┌────── 7 │ 100 ────── 1 0 ────── 1 ``` --- After that, we bring down the next digit from the dividend. This appends a $0$ to the current remainder, forming a new remainder: $$ R = 10 $$ Since $10 \ge 7$, we can perform a subtraction. We subtract $7$ from $10$ to obtain the new remainder: $$ R = 10 - 7 = 3 $$ Because we performed one substraction, the next quotient digit is $1$, giving: $$ Q = 01 = 1 $$ ``` 01 ┌────── 7 │ 100 ────── 1 0 ────── 10 7 ────── 3 ``` --- Next, we bring down the final digit of the dividend, forming: $$ R = 30 $$ Since $30 \ge 7$, we perform subtraction $4$ times, the reason to choice $4$ is that: - $7 \times 4 = 28 \le 30$ - $7 \times 5 = 23 > 30$ The remainder is: $$ R = 30 - 7 * 4 = 2 $$ The next quotient digit is $4$, giving: $$ Q = 14 $$ ``` 014 ┌────── 7 │ 100 ────── 1 0 ────── 10 7 ────── 30 28 ────── 2 ``` --- Therefore, the **quotient** is $14$, and the **remainder** is $2$. We can simulate this approch into binary long division. ## Binary Long Divison ### Example Taking $13 \div 3$ as the example. We first convert these number form decimal into binary representation: - Dividend = `1101` (13) - Divisor = `11` (3) ``` ┌─────── 11 │ 1101 ``` --- Take the digit of the dividend from **left to right**. The first digit is $1$, we set the remainder $R = 1$. Since the remainder $R = 1$ is less then the divisor $11$, there is no need of substraction. The quotient $Q$ is set to $0$. ``` 0 ┌─────── 11 │ 1101 ─────── 1 0 ─────── 1 ``` --- Next, bringing down the second digit of the dividend, which forming the new remainder: $$ R = (1 << 1) | 1 = 11 $$ Since remainder $R = 11_2 = 3$ is greater or equal to divisor $11_2 = 3$, we substract the divisor from remainder, obtain: $$ R = 11_2 - 11_2 = 0 $$ There is a substraction in this step, the quotient become: $$ Q = 01 = 1 $$ ``` 01 ┌─────── 11 │ 1101 ─────── 1 0 ─────── 11 11 ─────── 0 ``` --- After that, bringing down the third digit of the dividend, forming: $$ R = (0 << 1) | 0 = 0 $$ The new remainder $R = 0$ is less than divisor $11_2 = 3$, there is no need to do substracion. The quotient become: $$ Q = (1 << 1) | 0 = 10 $$ ``` 010 ┌─────── 11 │ 1101 ─────── 1 0 ─────── 11 11 ─────── 0 0 ─────── 0 ``` --- Finally, bringing down the last digit, the new remainder is: $$ R = (0 << 1) | 1 = 1 $$ The remainder $R = 1$ still less than the divisor $11_2 = 3$, there is no need to do substracion. The quotient become: $$ Q = (10 << 1) | 0 = 100 $$ ``` 0100 ┌─────── 11 │ 1101 ─────── 1 0 ─────── 11 11 ─────── 0 0 ─────── 1 0 ─────── 1 ``` --- Therefore, the **quotient** is `100` in binary, i.e. $4$ in decimal, and the **remainder** is `1` in brnary, i.e. $1$ in decimal: $$ 13 \div 3 = 4 \cdot\cdot\cdot 1 $$ ### Algorithm We can observe that binary long division relies only on three operations: **bit shifting**, **bitwise OR**, and **subtraction**, and that its processing steps follow a simple, repeating structure. We can write down the algorithm as follows: 1. initialize the remainder `R` and quotient `Q` to $0$. 2. **Shift left** the remainder `R` and **bitwise OR** the left-most bit of the dividend `Q` into `R` to form a new remainder. 3. **Shift left** the dividend to discard the bit that was just processed. 4. **Shift left** the quotient `Q` to prepare for the next quotient bit. 5. Compare the remainder `R` with the divisor `D`: - if `R` >= `D`, then do subtraction `R = R - D` and set LSB of `Q` to 1. - Otherwise, set LSB of `Q` to 0. 6. Repeat steps 2–5 for each bit of the dividend. ### C Implementation of Shift-and-Subtract Division The following function performs unsigned integer division using the shift-and-subtract algorithm: ```c void div(uint32_t dividend, uint32_t divisor, uint32_t *quotient, uint32_t *remainder) { int bit_count = 32; *quotient = 0; *remainder = 0; while (bit_count--) { uint32_t left_most = (dividend >> 31) & 1; // left-most bit of the dividend dividend <<= 1; // discard the bit that was just processed *remainder = (*remainder << 1) | left_most; *quotient <<= 1; // prepare for the next quotient bit. if (*remainder >= divisor) { *remainder -= divisor; *quotient |= 1; } } } ``` ### RISC-V Implementation of Shift-and-Subtract Division The following RISC-V program shows the unsigned integer division using the shift-and-subtract algorithm: ```asm # software_div: Software division implementation for RV32I # Algorithm: Simplified shift-and-subtract division # Input: a0 = dividend, a1 = divisor # Output: a0 = quotient, a1 = remainder # Constraints: Assumes positive inputs and divisor != 0 software_div: add t0, x0, x0 # quotient = 0 add t1, x0, x0 # remainder = 0 addi t2, x0, 32 # bit_count = 32 (process 32 bits) div_loop: slli t1, t1, 1 # remainder <<= 1 srli t3, a0, 31 # Extract MSB of dividend or t1, t1, t3 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, skip_sub # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) skip_sub: addi t2, t2, -1 # bit_count-- bne t2, x0, div_loop # Continue if bit_count != 0 add a0, t0, x0 # Return quotient in a0 add a1, t1, x0 # Return remainder in a1 jalr x0, ra, 0 # Return to caller ``` ## Handling Negative Value Handling negative input is simple. We can just using **bitwise XOR** on the sign bit of the both dividend and divisor. If the result is $1$, then the quotient is negative; otherwise, it is positive. The remainder has the same sign with the dividend. The shift-and-subtract division loop will use the absolute value of the dividend and divisor. The following RISC-V program shows the refinement of the previous prgram to support negative input. ```asm # software_div: Software division implementation for RV32I # Algorithm: Simplified shift-and-subtract division # Input: a0 = dividend, a1 = divisor # Output: a0 = quotient, a1 = remainder # Constraints: Assumes divisor != 0 software_div: add t0, x0, x0 # quotient = 0 add t1, x0, x0 # remainder = 0 addi t2, x0, 32 # bit_count = 32 (process 32 bits) srli t3, a1, 31 # sign bit of divisor srli t4, a0, 31 # negative_r = sign bit of dividend xor t3, t3, t4 # negative_q = dividend_sign ^ divisor_sign srai t5, a0, 31 # mask = a0 >> 31 xor t6, a0, t5 # temp = a0 ^ mask sub a0, t6, t5 # abs(a0) = temp - mask srai t5, a1, 31 # mask xor t6, a1, t5 # temp sub a1, t6, t5 # abs(a1) div_loop: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, skip_sub # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) skip_sub: addi t2, t2, -1 # bit_count-- bne t2, x0, div_loop # Continue if bit_count != 0 beq x0, t3, skip_neg_q sub t0, x0, t0 skip_neg_q: beq x0, t4, skip_neg_r sub t1, x0, t1 skip_neg_r: add a0, t0, x0 # Return quotient in a0 add a1, t1, x0 # Return remainder in a1 jalr x0, ra, 0 # Return to caller ``` I used the following test program and [Ripes](https://github.com/mortbopet/Ripes) simulator to measure the cycles count of software_div: ```asm .text main: li a0, 100 li a1, 3 call software_div li a7, 10 ecall ``` - Cycles: `419` :::danger Improve the above for less CPU cycles required. ::: #### Optimized RISC-V Program To improve the performance of the RISC-V implementation above, we note that the core division loop executes exactly 32 times; therefore, we can use **loop unrolling** to avoid using a branch to control the loop. Since the Ripes simulator does not include a branch predictor and treats every branch as not taken, removing branches can significantly reduce the execution overhead. - Cycles: `292` :::spoiler RISC-V Program ```asm # software_div: Software division implementation for RV32I # Algorithm: Simplified shift-and-subtract division # Input: a0 = dividend, a1 = divisor # Output: a0 = quotient, a1 = remainder # Constraints: Assumes divisor != 0 software_div: add t0, x0, x0 # quotient = 0 add t1, x0, x0 # remainder = 0 srli t3, a1, 31 # sign bit of divisor srli t4, a0, 31 # negative_r = sign bit of dividend xor t3, t3, t4 # negative_q = dividend_sign ^ divisor_sign srai t5, a0, 31 # mask = a0 >> 31 xor t6, a0, t5 # temp = a0 ^ mask sub a0, t6, t5 # abs(a0) = temp - mask srai t5, a1, 31 # mask xor t6, a1, t5 # temp sub a1, t6, t5 # abs(a1) div_loop: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, skip_sub # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) skip_sub: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: slli t1, t1, 1 # remainder <<= 1 srli t5, a0, 31 # Extract MSB of dividend or t1, t1, t5 # remainder |= extracted bit slli a0, a0, 1 # dividend <<= 1 (shift left) slli t0, t0, 1 # quotient <<= 1 (prepare for next bit) blt t1, a1, 1f # If remainder < divisor, skip subtraction sub t1, t1, a1 # remainder -= divisor ori t0, t0, 1 # quotient |= 1 (set LSB) 1: beq x0, t3, skip_neg_q sub t0, x0, t0 skip_neg_q: beq x0, t4, skip_neg_r sub t1, x0, t1 skip_neg_r: add a0, t0, x0 # Return quotient in a0 add a1, t1, x0 # Return remainder in a1 jalr x0, ra, 0 # Return to caller ``` ::: ## Referance [Quiz4 of Computer Architecture (2025 Fall) Problem-C](https://hackmd.io/@sysprog/arch2025-quiz4-sol#Problem-C) [你所不知道的 C 語言:bitwise 操作](https://hackmd.io/@sysprog/c-bitwise)

    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
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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