# Ch3 Arithmetic for Computers 本章節主要在探討電腦如何用硬體電路來執行加減乘除與浮點數運算,並延伸到「平行化算術加速」與錯誤處理。這一章的內容是從「數字如何表示」一路講到「怎麼加快算術運算效率」。 ## 3-2 加法與減法 :::warning 本章節的整數皆使用 2's complement ::: - **加法**:直接加起來就好  - **減法**:**加上負數**就是減法  - **Overflow**: - 當計算結果超過儲存大小,就會產生 overflow - **Saturating Operation**: - 當 overflow 產生時,只儲存最大的資料型態 - e.g. 7(4-bits) +12(4-bits) = 15(4-bits, saturating operations) ## 3-3 乘法 > 可以參考這位大佬寫的筆記:[連結](https://ithelp.ithome.com.tw/articles/10160710) ### 一般乘法器  - **流程**: - 先初始化 multiplicand(被除數)與 multiplier(除數),product 預設為 0  - 當 multiplier 的最低位是 1 時,product 加上 multiplicand 的值 - 如果是 0,則跳過這一步  - 計算完成後,multiplicand 左移一位,multiplier 右移一位  - 會執行 **n** 次循環:n = multiplicand 的位數  - **優化版本**: - 直接將 multiplier 存在 product 中 - 每次做完一個循環,multiplicand 左移一位,product 右移一位  ### 快速乘法器 - 使用多重的 ALU:  - 可以透過**平行化處理**來加快運算 ### MIPS 實作 - **64-bits Product**: - 因為 MIPS register 只能存 32 bit,因此要分兩個 register 存放 - **HI**:most-significant 32 bits - **LO**:least-significant 32 bits - **指令**: - **mult, multu**: ```mips= mult rs, rt multu rs, rt ``` - 64-bit product - **mfhi, mflo**: ```mips= mfhi rd mflo rd ``` - 把 **HI/LO** 存到 `rd` - **mul**: ```mips= mul rd, rs, rt ``` - least-significant 32 bits of product -> `rd` ## 3-4 除法 ### 一般除法器  - **流程**: :::warning 直接參考表格會比較好理解,就不多加贅述了 ::: - 循環會經過 n 次(n = divisor 的次數)  - **優化版本**:   ### MIPS 除法指令 - 使用 HI/LO register 來儲存結果 - **div**: ```mips= div rs, rt ``` - **divu**: ```mips= divu rs, rt ``` ## 3-5 Float Point - 用來表示**非整數型態**的數字 - 使用**科學記號**表示 - e.g. $1.45 \times 10^{50}$ - In binary:$\mathrm{\pm 1.xxxxxxx_2 \times 2^y}$ - 使用 IEEE 754 標準 ### Single precision  - **Sign** = 0, 1 - **Exponent** = 次方數 + **127** - **Fraction** = 小數點後的數 ### Double precision  - **Sign** = 0, 1 - **Exponent** = 次方數 + **1023** - **Fraction** = 小數點後的數
×
Sign in
Email
Password
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