Homework Assignment #3

Solutions:

  1. Describe the three major parts of a central processing unit and explain how they work together to execute a program. (Write your own answer. Do not copy from course notes and web materials.)

    Answer:

    中央處理單元的三個主要部分:暫存器、算術邏輯單元、控制單元。

    1. 暫存器 (registers): 暫存器是固定長度的小記憶空間,主要是用來暫時儲存資料,功能好比於記憶體。資料暫存器也可叫累積器,一個中央處理單元可以有多個累積器,它們可被算數邏輯單元所運行。位址暫存器管理這些允許中央處理單元從指定的記憶位置讀取資料的記憶體位置。特殊種類的位址暫存器叫做索引暫存器,專門管理索引中的陣列或向量,而指令暫存器主要是管理編碼指令。

    2. 算術及邏輯單元 (arithmetic and logical unit,簡稱 ALU): 算術單元主要就是做加、減、乘、除等運算功能,而在邏輯單元則是執行比較、選擇、判斷等指令。

    3. 控制單元 (control unit): 從記憶體讀入指令,然後解碼,再依據解碼的結果執行,這是控制單元的主要功能。若需要做資料運算,則再從主記憶體讀入運算式,並送到算術邏輯單元進行運算。其他功能像輸入單元從輸入設備讀入資料並存放到記憶體,以及將記憶體的資料經輸出單元寫到輸出設備等動作,都是由控制單元所控制。 要執行程式主要為這四個循環動作:抓取、解碼、執行、和回存。

    (1) 抓取 (fetch):控制單元先從記憶體讀取指令,再由程式計數器指出該項並將這些項目儲存在指令暫存器內。

    (2) 解碼 (decode):控制單元將指令暫存器中的指令解碼後,在累積器或在記憶體內得到指定的運算式和符合的運算域。

    (3) 執行 (execute):算數及邏輯單元執行解碼之後的運算式,這是它的主要機能。

    (4) 回存 (restore):將執行的結果存回累積器或記憶體位置。 然後當某個程式執行到第四步驟時,其他的程式再依照這四種步驟這樣一直循環下去,不過這四個步驟是幾乎是同時的,也就是說每個程式都是同步執行的。

  2. The following program segment contains a collection of variable declarations. Point out and correct all invalid declarations.

    1

    2

    3

    4

    5

    6

    char character, CHAR;

    short a1, a, b;

    int 1a, ab, a&b

    long aaaaaaaaaaaa1, bbbbbb0ccccccc, unsigned;

    long unsigned l0, O1, abc;

    float float, real, real-number;

    Answer:

    1. Line 3, "1a" 第一個字母不能為數字,可更正為 "la";"a&b" 變數不能有  "&"  的符號,可更正為 "aANDb";變數宣告結束要有一個分號 ";"。

    2. Line 4: "unsigned" 是保留字,不可以當作變數名稱,可更正為其他變數名稱。

    3. Line 6: "float" 是保留字,不可以當作變數名稱,可更正為 其他變數名稱;"real-number" 變數名稱不能用 "-" 連接,可更正為 "real_number"。

  3. Consider the following C program.

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    #include <stdio.h>

    int main(void) {
     

      int i=6, j=35, k=10;
      float r=2.98, s=1.1, t=3.21;

      k = s * s + j / i * r - k % i * 3;

      t = (r + s) * s - (t * j / 10);


      return 0;
    }

    Suppose the operands of a binary operation are evaluated from left to right. For each assignment of Lines 8 and 9, indicate the evaluation order of operations (including loading of operand values and execution of operators) in the right-hand-side expression. (Copy the expression to your solution and mark numbers 1, 2, 3, ¼, etc., under each operator and operand.) Also, for each operation indicate whether it is an integer operation or a floating-point operation. What are the final values of variables k and t?

    Answer:

    Line 8:

    k = s * s + j / i * r - k % i * 3 ;
        1 3 2 9 4 6 5 8 7 15 10 12 11 14 13  

    1. s * s = 1.21

        floating-point ´ floating-point ® floating-point

    2. j / i = 5 ... 5

        integer ´ integer ® integer

    3. (j / i) * r = 14.9

        integer ´ floating-point ® floating-point

    4. k % i = 1 ...4

        integer ´ integer ® integer

    5.(k % i) * 3 = 12

        integer ´ integer ® integer

    6. (s * s) + (j / i * r) = 16.11

        floating-point ´ floating-point ® floating-point

    7. (s * s + j / i * r ) - (k % i * 3) = 4.11

         floating-point ´ integer ® floating-point

    由於 k 是整數    ∴ k = 4

    Line 9:

    t = ( r + s ) * s - ( t * j / 10 ) ;
          1 3 2   5 4 11   6 8 7 10  9    

     1.  r + s = 4.08

        floating-point ´ floating-point ® floating-point

     2. (r + s) * s = 4.488

        floating-point ´ floating-point ® floating-point

     3.  t * j = 112.35

        floating-point ´ integer ® floating-point

     4.  (t * j / 10 ) = 11.235

        floating-point ´ integer ® floating-point

     5.  (r + s) * s - (t * j / 10) = -6.747

        floating-point ´ floating-point ® floating-point

    t = -6.747

  4. Suppose the operands of a binary operation are evaluated from left to right. Rewrite the following assignments to remove side effect in the expressions.

    (a)

    a = (++b * --c) + (c++ - d++);

    (b)

    r = (s = ++t / s--) * t;

    (c)

    w = (u = t - r * u) + (t = w / u + u) - (w = t * u);

    Answer:

    (a)

    b = b + 1;

    c = c - 1;

    temp1 = b * c;

    temp2 = c - d;

    c = c + 1;

    d = d + 1;

    a = temp1 + temp2;

    (b)

    t = t + 1;

    temp1 = t / s;

    s = s - 1;     // This assignment has no computational effect and can be omitted.

    s = temp1;

    r = s * t ;

    (c)

    temp1 = r * u ;

    u = t - temp1 ;

    temp2 = w / u ;

    t = temp2 + u ;

    w = t * u ;

    temp3 = u + t ;

    w = temp3 - w ;