ebook img

Chapter 3 Arithmetic for Computers PDF

82 Pages·2009·5.17 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Chapter 3 Arithmetic for Computers

Chapter 3 Arithmetic for Computers 授課教師: 張傳育 博士 (Chuan-Yu Chang Ph.D.) E-mail: [email protected] Tel: (05)5342601 ext. 4337 1 IInnttrroodduuccttiioonn 自然數字的二進位表示法 二進位與十進位的轉換(binary to decimal conversion) 二進位與十六進位的轉換(binary to hexadecimal conversion) 十六進位與十進位的轉換 負數表示法 二進位數的邏輯運算 (and, or) 二進位數的算術運算(+, -, *, /) 分數(fraction)及浮點表示法 硬體如何實際執行加、減、乘、除。 2 Signed and Unsigned Numbers In any number base, the value of ith digit d is d × Base i Binary numbers (base 2) least significant bit: the rightmost bit most significant bit: the leftmost bit 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001... decimal: 0...2n-1 Of course it gets more complicated: numbers are finite (overflow) fractions and real numbers negative numbers e.g., no MIPS subi instruction; addi can add a negative number How do we represent negative numbers? i.e., which bit patterns will represent which numbers? 3 EExxaammppllee:: AASSCCIIII VVss.. BBiinnaarryy NNuummbbeerrss What is the expansion in storage if the number 1 billion is represented in ASCII versus a 32 bit integer? 1 billion = 1000000000 (10個ASCII符號) 1 ASCII = 8 bits 所以需要8*10=80 bits 因此80/32=2.5 4 Possible Representations of Negative Numbers Sign and Magnitude: One's Complement Two's Complement 000 = +0 000 = +0 000 = +0 001 = +1 001 = +1 001 = +1 010 = +2 010 = +2 010 = +2 011 = +3 011 = +3 011 = +3 100 = -0 100 = -3 100 = -4 101 = -1 101 = -2 101 = -3 110 = -2 110 = -1 110 = -2 111 = -3 111 = -0 111 = -1 Issues: number of zeros, ease of operations Which one is best? Why? The drawbacks of the sign-magnitude representation: It’s not obvious where to put the sign bit. Adders for sign and magnitude may need an extra step to set the sign because we can’t know in advance what the proper sign will be. Two zeros, positive and negative zero. 5 MIPS: 2’’’’s complement 以0為前導的數為正值 32 bit signed numbers: 0000 0000 0000 0000 0000 0000 0000 0000 = 0 two ten 0000 0000 0000 0000 0000 0000 0000 0001 = + 1 two ten 0000 0000 0000 0000 0000 0000 0000 0010 = + 2 two ten ... maxint 0111 1111 1111 1111 1111 1111 1111 1110 = + 2,147,483,646 two ten 0111 1111 1111 1111 1111 1111 1111 1111 = + 2,147,483,647 two ten 1000 0000 0000 0000 0000 0000 0000 0000 = – 2,147,483,648 two ten minint 1000 0000 0000 0000 0000 0000 0000 0001 = – 2,147,483,647 two ten 1000 0000 0000 0000 0000 0000 0000 0010 = – 2,147,483,646 two ten ... 1111 1111 1111 1111 1111 1111 1111 1101 = – 3 two ten 1111 1111 1111 1111 1111 1111 1111 1110 = – 2 two ten 1111 1111 1111 1111 1111 1111 1111 1111 = – 1 two ten 以1為前導的數為負值 6 EExxaammppllee:: BBiinnaarryy ttoo DDeecciimmaall ccoonnvveerrssiioonn What is the decimal value of this 32-bits two’s complement number? 1111 1111 1111 1111 1111 1111 1111 1100 (2) ( ) ( ) ( ) ( ) ( ) ( ) 1× −231 + 1× 230 + 1× 229 + ... + 1× 22 + 0 × 21 + 0 × 20 = −231 + 230 + 229 + ... + +22 + 0 + 0 = −2147483648 + 2147483644 = −4 7 Two's Complement Operations Negating a two's complement number: invert all bits and add 1 Converting n bit numbers into numbers with more than n bits: MIPS 16 bit immediate gets converted to 32 bits for arithmetic copy the most significant bit (the sign bit) into the other bits 0010 -> 0000 0010 1010 -> 1111 1010 "sign extension" lbu: load byte unsigned lb: load byte 8 SSiiggnneedd vveerrssuuss UUnnssiiggnneedd CCoommppaarriissoonn •MIPS offers two versions of the set on less than comparison •有號數的若小於則設定(slt, slti): set on less than, set on less than immediate •無號數的若小於則設定(sltu, sltiu): set on less than unsigned, set on less than immediate unsigned • 範例. 假設暫存器 $s0 含有以下的二進位數 1111 1111 1111 1111 1111 1111 1111 1111 而暫存器 $s1含有以下的二進位數 0000 0000 0000 0000 0000 0000 0000 0001 經由以下兩個指令運算後,暫存器 $t0及 $t1 的值為何? slt $t0, $s0, $s1 sltu $t1, $s0, $s1 » 解答 $s0: signed=-1, unsigned=4294967295 $s1: signed=1, unsigned=1 得到 $t0 = 1 及 $t1 = 0. 9 EExxaammppllee :: NNeeggaattiioonn SShhoorrttccuutt Negate 2 , and then check the result by negating -2 . (10) (10) 2 = 0000 0000 0000 0000 0000 0000 0000 0010 (10) Negating this number by inverting the bits and adding one. 1111 1111 1111 1111 1111 1111 1111 1101 + 1 1111 1111 1111 1111 1111 1111 1111 1110 = -2 0變1,1變0 0000 0000 0000 0000 0000 0000 0000 0001 + 1 0000 0000 0000 0000 0000 0000 0000 0010 = 2 10

Description:
Chapter 3. Arithmetic for Computers. 授課教師: 張傳育博士(Chuan-Yu Chang Ph. D.) E-mail: . MIPS 16 bit immediate gets converted to 32 bits for arithmetic.
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.