---
# System prepended metadata

title: 計算機結構作業三

---

#    計算機結構作業三
110590007白宸安
## 1
![](https://i.imgur.com/vkKxN31.png)
---
![](https://i.imgur.com/OhXzZDQ.png)
---
![](https://i.imgur.com/uI6XYiX.png)




## 2
(16 points) Consider two unsigned binary numbers: M = 0111 and N = 1101. 
(a) (8 points) Write down each step of M × N according to version 1 of the multiply algorithm.
(b) (8 points) Write down each step of M × N according to version 2 of the multiply algorithm.

ans : 
(a) 0111 * 1101 = 0101 1011
```
Multiplier  Multiplicand  Product
1101        0000 0111     0000 0000
0110        0000 1110     0000 0111
0011        0001 1100     0000 0111
0001        0011 1000     0010 0011
0000        0111 0000     0101 1011          
```

(b) 0111 * 1101 = 0101 1011
```
Multiplicand   Product
0111           0000 1101
               0111 1101
0111           00111 110
0111           000111 11
               100011 11
0111           0100011 1
               1011011 1
0111           0101 1011
```

##    3
(20 points) Consider two signed binary numbers: M = 0111 and N = 0110.
(a) (10 points) Write down each step of M ÷ N according to version 1 of the divide algorithm.
(b) (10 points) Write down each step of M ÷ N according to version 2 of the divide algorithm.

ans : 
(a) 0111 / 0110 
=> quot = 0001
=> remainder = 0000 0001
注意：直接開始
```
quot    divisor       remainder
0000    0110 0000     0000 0111
                      1010 0111
000 0                 0000 0111
000 0    0 0110 000   0000 0111
                      1101 0111
00 00                 0000 0111
00 00    00 0110 00   0000 0111
                      1110 1111
0 000                 0000 0111
0 000    000 0110 0   0000 0111
                      1111 1011
 0000                 0000 0111
 0000    0000 0110    0000 0111
                      0000 0001
 0001                 0000 0001
 0001    0000 0011    0000 0001

```

(b) 0111 / 0110
=> quot = 0001
=> remainder = 0000 0001
注意：最後要shift left half remainder 1 bit回來
```
remainder    divisor
0000 0111    0110
0000 111 0    
1010 111 0
0001 11 00
1011 11 00
0011 1 000
1101 1 000
0111 0000
0001 0000
0010 0001
0001 0001(shift left half remainder)

```

##    4
(20 points) Consider two decimal numbers: X = 0.3125 and Y = -15.96875.
(a) (10 points) Write down X and Y in the IEEE 754 single-precision format. You must detail how you get the answers, or you will receive 0 points.
(b) (5 points) Assuming X and Y are given in the IEEE 754 single precision format, show all the steps to perform X + Y and write the result in the IEEE 754 single precision format.
( c) (5 points) Assuming X and Y are given in the IEEE 754 single precision format, show all the steps to perform X × Y and write the result in the IEEE 754 single precision format.



###    (a)
x = (-1)^s * (1+fraction) * 2^(exponent - bias)

1、先把x轉換為二進位
0.3125 * 2 = 0.625，整數部分為0
0.625 * 2 = 1.25，整數部分為1
0.25 * 2 = 0.5，整數部分為0
0.5 * 2 = 1，整數部分為1
所以二進位是0.0101
左移兩格為1.01 * $2^{-2}$

sign bit(1 bit) : 0
exponent(8 bits) : -2 + 127 = 125 = 01111101
fraction(23 bits) : 01 000000000000000000000

所以x = 0 01111101 01 000000000000000000000
       
2、先把y轉換為二進位
15 = 1111，左半邊是1111

0.96875 * 2 = 1.9375，整數部分為1
0.9375 * 2 = 1.875，整數部分為1
0.875 * 2 = 1.75，整數部分為1
0.75 * 2 = 1.5，整數部分為1
0.5 * 2 = 1，整數部分為1

所以二進位是-1111.11111
右移三格是-1.11111111 * $2^{3}$

sign bit(1 bit) : 1
exponent(8 bits) : 3 + 127 = 130 = 10000010
fraction(23 bits) : 11111111000000000000000

所以y = 1 10000010 11111111000000000000000


###    (b)
x + y : 

1.對齊bits

x = 1.01 * $2^{-2}$ = 0.0000101 * $2^{3}$
y = -1.11111111 * $2^{3}$

2.加法

```    
    -1.11111111 * $2^{3}$
 +   0.0000101  * $2^{3}$
 -------------------------
    -1.11110101 * $2^{3}$          
```
x + y = -1.11110101 * $2^{3}$

sign bit(1 bit) : 1
exponent(8 bits) : 3 + 127 = 130 = 10000010
fraction(23 bits) : 11110101000000000000000

ans = 1 10000010 11110101000000000000000

###    ( c)
x * y : 

x = 1.01 * $2^{-2}$ = 0.0000101 * $2^{3}$
y = -1.11111111 * $2^{3}$

1.add exponent
-2 + 3 = 1
所以新的exponent是$2^{1}$

2.乘法
先忽略正負號和小數點
```
         111111111
      *        101
      ---------------
         111111111
       111111111         
 --------------------
      100111111011
```
-(1.11111111) * 1.01 = -10.0111111011

3.整理
-10.0111111011 = -1.00111111011 * $2^{1}$

結果 = -1.00111111011 * $2^{1+1}$
結果 = -1.00111111011 * $2^{2}$

sign bit(1 bit) : 1
exponent(8 bits) : 2 + 127 = 129 = 10000001
fraction(23 bits) : 00111111011000000000000 

ans = 1 10000001 00111111011000000000000

##    5
(20 points) Consider a new floating-point number representation that has only 8 bits containing the sign bit, exponent, and fraction from left to right. The leftmost bit is still the sign bit, the exponent is 3 bits wide and has a bias of 3, and the faction is 4 bits long. A hidden 1 to the left of the binary point is assumed. In this representation, any 8-bit binary pattern having 000 in the exponent field and a non-zero fraction indicates a denormalized number: (−1)Sign × (0 + Fraction) × 2-2. Write the answers of (a), (b), and (c ) in scientific notation, e.g., 1.0101 × 22.
(a) (3 points) What is the largest negative “normalized” number, denoted as a0?
(b) (6 points) What is the smallest negative “denormalized” number, denoted as
a1? What is the second smallest negative “denormalized” number, denoted as
a2?
(c ) (4 points) Find the differences between a0 and a1, and between a1 and a2.
Also, describe what you observe and any implications from them.
(d) (3 points) What decimal number does the Hexadecimal pattern 0x5C
represent?
(e) (4 points) Let U be the nearest representation of the decimal number -5.7; that
is, U has the smallest approximation error. What is the approximation error between U and -5.7? What is the actual decimal number represented by U?

###    (a)
largest negative normalized number a0 : 

sign bit : 1
exponent : 001 = 1 - 3 = -2
fraction : 0000

a0 = -1.0 * $2^{-2}$

###    (b)
smallest negative denormalized number a1 : 

sign bit : 1
exponent : 000 = 0 - 3 = -3
fraction : 1111

a1 = -0.1111 * $2^{-3}$

second smallest negative denormalized number a2 : 

sign bit : 1
exponent : 000 = 0 - 3 = -3
fraction : 1110

a2 = -0.111 * $2^{-3}$

###    (c )
difference between a0 and a1 : 
a0 = -1.0 * $2^{-2}$
a1 = -0.1111 * $2^{-3}$

a0 - a1 = { (-10.0) - (-0.1111) } * $2^{-3}$
        = -1.0001 * $2^{-3}$
        
difference between a1 and a2 : 
a1 = -0.1111 * $2^{-3}$
a2 = -0.111 * $2^{-3}$

a1 - a2 = { (-0.1111) - (-0.111) } * $2^{-3}$
        = -0.0001 * $2^{-3}$
        
observations and implications from them : 
一、減完都變成denormalize格式
二、a0和a1其實相差蠻大的，而a1和a2其實蠻接近的
三、都是$2^{-3}$

###    (d)
What decimal number does the Hexadecimal pattern 0x5C
represent : 

0x5C = 0101 1100

sign bit : 0
exponent : 101 = 5 - 3 = 2
fraction : 1100

1.11 * $2^{2}$ = $111_2$ = $7_{10}$


###    (e)
Let U be the nearest representation of the decimal number -5.7; that
is, U has the smallest approximation error. What is the approximation error between U and -5.7? What is the actual decimal number represented by U?



x = (-1)^s * (1+fraction) * 2^(exponent - bias)
先把-5.7處理成binary表示
左邊
5 = 101

右邊
0.7 * 2 = 1.4，整數部分為1
0.4 * 2 = 0.8，整數部分為0
0.8 * 2 = 1.6，整數部分為1
0.6 * 2 = 1.2，整數部分為1
0.2 * 2 = 0.4，整數部分為0
0.4 * 2 = 0.8，整數部分為0
0.8 * 2 = 1.6，整數部分為1
0.6 * 2 = 1.2，整數部分為1
0.2 * 2 = 0.4，整數部分為0
0.4 * 2 = 0.8，整數部分為0
0.8 * 2 = 1.6，整數部分為1
0.6 * 2 = 1.2，整數部分為1
...

所以
-5.7 = $-101.1\overline{0110}$
        = $-1.011\overline{0110}$ * $2^2$
        = -1.0111 * $2^2$ (round up)
    
sign bit : 1
exponent : 2 + 3 = 5 = 101
fraction : 0111

U = 1 101 0111 
  = -1.0111 * $2^2$
  = $-101.11_{2}$ = $-5.75_{10}$
  
approximation error between U and -5.7 is $0.05_{10}$
and U's actual decimal number is $-5.75_{10}$


##    6
John說 greater_or_equal 會回傳1，if X >= Y。回傳0，if X < Y
假設X, Y不包含Nan，且0和-0被視為相同。

John說的是否正確？

ux : unsigned x
uy : unsigned y

sx : sign bit of x
sy : sign bit of y

(ux << 1 == 0 && uy << 1 == 0) : 確認x, y是否都為0或-0

(!sx && sy) : 只有當sx=0和sy=1時才return 1，也就是如果x是正數，y是負數，直接return 1。


(!sx && !sy && ux >= uy)：當x和y都是正數時，且x>=y時return 1

(sx && sy && ux <= uy)：當x和y都是負數時，且x<=y時return 1

錯誤點：(sx && sy && ux <= uy)
當x和y都是負數時，也要ux >= uy才能return 1。因為都是負數時，較大的數依然較大。
ex：
x = 111 = -1
y = 110 = -2

x = 011 = 3
y = 010 = 2

所以John說的是錯的