113 年 國立成功大學工程科學系碩士班丙組《計算機概論》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊

第 1 題15 分

The following is the recursive syntax of Hanoi Tower. Please change the following recursive method to a non-recursive one.

Hanoi Tower (N,A,B,C)
do
  If N>0
    then do
      Hanoi Tower(N-1,A,C,B)
      Move the Nth tray, from A to B
      Hanoi Tower(N-1,C,B,A)
    Finish
Finish

Parameters: N means the number of plates to be moved, A means the starting point, B means the end point, C means the emp point. In the other words, N plates will be moved from A to B with the temp point C.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查:

  1. 遞迴程序的執行順序。
  2. Stack(堆疊)如何模擬函式呼叫與返回。
  3. Hanoi Tower 的遞迴關係。

原遞迴程序:

  • 先將 N−1N-1 個盤子由 AA 移至 CC。
  • 將第 NN 個盤子由 AA 移至 BB。
  • 再將 N−1N-1 個盤子由 CC 移至 BB。

其移動次數滿足:

T(N)=2T(N−1)+1T(N)=2T(N-1)+1

且 T(0)=0T(0)=0,因此:

T(N)=2N−1T(N)=2^N-1

解題方法:以明確 Stack 模擬遞迴

每次遞迴呼叫都建立一個 stack frame,記錄:

  • NN:目前要移動的盤子數
  • AA:起點
  • BB:終點
  • CC:暫存柱
  • stage:目前執行到哪個階段

一個 Hanoi Tower 呼叫可分成三個階段:

stage工作
0執行 Hanoi(N-1,A,C,B)
1移動第 NN 個盤子,再執行第二個遞迴呼叫
2第二個遞迴呼叫完成,返回上一層

遞迴呼叫會先處理後加入的工作,因此使用 Stack 的 push 與 pop 即可保留正確順序。

非遞迴版本

HanoiTower(N, A, B, C)

    if N <= 0
        return

    stack.push((N, A, B, C, 0))

    while stack is not empty

        frame = stack.top()

        if frame.N <= 0
            stack.pop()

        else if frame.stage == 0

            // 原本要執行:
            // HanoiTower(N-1, A, C, B)

            frame.stage = 1

            stack.push(
                frame.N - 1,
                frame.A,
                frame.C,
                frame.B,
                0
            )

        else if frame.stage == 1

            // 原本的遞迴呼叫已完成
            Move frame.N-th plate
                from frame.A to frame.B

            frame.stage = 2

            // 原本要執行:
            // HanoiTower(N-1, C, B, A)

            stack.push(
                frame.N - 1,
                frame.C,
                frame.B,
                frame.A,
                0
            )

        else

            // 第二個遞迴呼叫完成,返回上一層
            stack.pop()

其中:

stack.push(N, A, B, C, stage)

表示建立一個新的遞迴呼叫紀錄。

關鍵流程說明

以目前 frame:

HanoiTower(N, A, B, C)

為例。

第一階段

先將目前 frame 的 stage 改為 11,再壓入:

HanoiTower(N-1, A, C, B)

如此可確保子問題完成後,程式會回到原本的 frame,繼續執行移動第 NN 個盤子的工作。

第二階段

第一個子問題完成後,執行:

Move Nth plate from A to B
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題10 分

There is a two-dimensional array A. Suppose the address of A(1, 1) is 644 and the address of A(3, 3) is 676. What is the address of A (14, 14)? Please briefly explain your answer.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

二維陣列的位址計算,必須先知道:

  • 儲存方式:row-major(列優先)或 column-major(行優先)
  • 每個元素所占的位址單位
  • 主要維度的大小

本題由 A(1,1)A(1,1) 與 A(3,3)A(3,3) 的位址差,反推出陣列每列(或每行)包含 1515 個元素。以下採用一般考題的假設:陣列連續儲存,且每個元素占一個位址單位。

解題方法

以 row-major 為例,若每列有 NN 個元素,則:

Addr⁡(A(i,j))=Addr⁡(A(1,1))+((i−1)N+(j−1))\operatorname{Addr}(A(i,j)) = \operatorname{Addr}(A(1,1)) + \bigl((i-1)N+(j-1)\bigr)

由題目可得:

Addr⁡(A(3,3))−Addr⁡(A(1,1))=676−644=32\operatorname{Addr}(A(3,3))-\operatorname{Addr}(A(1,1)) = 676-644=32

從 A(1,1)A(1,1) 移動到 A(3,3)A(3,3),共跨越:

  • 前進兩列:2N2N 個元素
  • 每列再向右前進兩格:22 個元素

因此:

2N+2=322N+2=32 2N=302N=30 N=15N=15

接著計算 A(14,14)A(14,14) 相對於 A(1,1)A(1,1) 的位移:

(14−1)×15+(14−1)=13×15+13=208(14-1)\times 15+(14-1) = 13\times 15+13 = 208

所以:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3 題10 分

Please calculate the result after forward propagation, and each cells' calculation is implemented in the figure.

🖼️【此處有附圖,請對照原卷】
The figure shows a neural network with input nodes X1,X2,...,XNX_1, X_2, ..., X_N and output aa.
The weights are W1,W2,...,WNW_1, W_2, ..., W_N.
The calculation for aia_i is given by:
ai=∑i=1NXiWia_i = \sum_{i=1}^{N} X_i W_i
The activation function σ(x)\sigma(x) is given by:
σ(x)=11+e−x\sigma(x) = \frac{1}{1 + e^{-x}}

The neural network is defined as below:
The weights are (W1,W2,W3,W4,W5,W6,W7,W8,W9,W10)=(−1,−2,−4,−5,5,−4,−5,6,3,−5)(W_1, W_2, W_3, W_4, W_5, W_6, W_7, W_8, W_9, W_{10}) = (-1, -2, -4, -5, 5, -4, -5, 6, 3, -5).
What is the result a1∼a4a_1 \sim a_4 and YY when (x1,x2)=(1,1)(x_1, x_2) = (1, 1).

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 2 頁原卷第 3 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查前饋神經網路的逐層計算。每個神經元先將輸入乘上連到該節點的權重後加總,再套用 sigmoid 函數:

σ(z)=11+e−z\sigma(z)=\frac{1}{1+e^{-z}}

計算時要依箭頭方向傳遞數值,並以箭頭終點判定權重屬於哪個神經元。

解題方法

圖中第一層由 X1、X2X_1、X_2 連到 a1、a2a_1、a_2:w1、w2w_1、w_2 分別連入 a1a_1,w3、w4w_3、w_4 分別連入 a2a_2。第二層由 a1、a2a_1、a_2 連到 a3、a4a_3、a_4,權重依序為 w5、w6、w7、w8w_5、w_6、w_7、w_8;最後 a3、a4a_3、a_4 分別以 w9、w10w_9、w_{10} 連到 YY。各節點皆套用 sigmoid 函數。

已知 (x1,x2)=(1,1)(x_1,x_2)=(1,1),依照圖上的箭頭逐層計算。

第一層:

a1=σ(x1w1+x2w2)=σ(1(−1)+1(−2))=σ(−3)≈0.04743a_1=\sigma(x_1w_1+x_2w_2) =\sigma\bigl(1(-1)+1(-2)\bigr) =\sigma(-3) \approx 0.04743 a2=σ(x1w3+x2w4)=σ(1(−4)+1(−5))=σ(−9)≈0.0001234a_2=\sigma(x_1w_3+x_2w_4) =\sigma\bigl(1(-4)+1(-5)\bigr) =\sigma(-9) \approx 0.0001234

第二層:

a3=σ(a1w5+a2w6)=σ(0.04743(5)+0.0001234(−4))=σ(0.23664)≈0.5589a_3=\sigma(a_1w_5+a_2w_6) =\sigma\bigl(0.04743(5)+0.0001234(-4)\bigr) =\sigma(0.23664) \approx 0.5589
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4 題10 分

Parameters are classified into three modes, 1. Call by address and 2. Call by value.
(1) Please explain parameter passing by examples of two modes briefly (10%)
(2) What would be the results of call_by_Mode function (test [element]) when the parameters are passed by two modes. (15%)

element : Integer
test    : Integer array of size 2

function call_by_Mode(x: Integer)
{
  test[1] := 7;
  element := 4;
  x       := x+3;
}

function Main ()
{
  test[1] := 5;
  test[2] := 6;
  element := 7;
  call_by_Mode (test[element]);
}

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查兩種參數傳遞方式:

  1. Call by value(傳值呼叫):先計算實際參數的值,再將該值複製給形式參數。
  2. Call by address(傳址呼叫):將實際參數所對應的記憶體位址傳給形式參數,使形式參數與實際參數指向同一個儲存位置。

題目開頭寫「three modes」,但實際只列出上述兩種模式,以下依題目列出的兩種模式作答。


一、兩種參數傳遞方式

1. Call by value

procedure increase(x)
{
    x := x + 1;
}

a := 5;
increase(a);

執行時,形式參數 x 取得 a 的複本:

x=5x=5

函式內執行:

x:=x+1=6x:=x+1=6

但 x 只是區域複本,因此函式結束後:

a=5a=5

所以,函式內修改形式參數,不會影響呼叫端的實際參數。


2. Call by address

procedure increase(x)
{
    x := x + 1;
}

a := 5;
increase(a);

傳址呼叫會將 a 的位址傳給 x,因此 x 與 a 指向同一個儲存位置。

函式內執行:

x:=x+1x:=x+1

等同於直接修改 a:

a=5+1=6a=5+1=6

所以,函式內修改形式參數,會同步修改呼叫端的實際參數。


二、本題的關鍵判斷

在 Main 中,先執行:

test[1] := 5;
test[2] := 6;
element := 7;

因此呼叫函式前的狀態為:

變數值
test[1]5
test[2]6
element7

接著呼叫:

call_by_Mode(test[element]);

將 element=7 代入索引:

test[element]=test[7]test[element]=test[7]

然而,test 宣告為大小為 2 的整數陣列,只存在:

test[1], test[2]test[1],\ test[2]

test[7] 超出陣列合法範圍,因此實際參數在函式呼叫前便已經是非法存取。


三、Call by value 的結果

傳值呼叫必須先計算實際參數的值:

test[element]=test[7]test[element]=test[7]

由於 test[7] 不存在,計算實際參數時即發生陣列索引越界。

因此:

  • 無法取得合法的值傳給 x
  • call_by_Mode 的函式本體不會正常執行
  • test[1] := 7、element := 4、x := x+3 都不會成為有效結果

四、Call by address 的結果

傳址呼叫必須先取得實際參數的位址:

address⁡(test[element])=address⁡(test[7])\operatorname{address}(test[element]) =\operatorname{address}(test[7])

但 test[7] 並不是陣列中的合法元素,因此無法取得合法位址。

因此:

  • 無法將合法位址傳給 x
  • call_by_Mode 的函式本體不會正常執行
  • 函式內的三個指定敘述不會產生定義明確的結果

傳址呼叫雖然會讓 x 與實際參數共用儲存位置,但前提是實際參數必須先是合法的變數或合法的陣列元素。本題的 test[7] 不符合此條件。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 5 題12 分

Encode each of the following commands in terms of the machine language described in the language description table.

A. ROTATE the contents of register 7 to the right 5 bit positions.
B. JUMP to the instruction at address B2 if the content of register 2 equals that of register 0.
C. ADD the contents of registers 5 and 6 as thought they were values in floating-point notation and leave the result in register 4.
D. AND the contents of registers 5 and 6, leaving the result in register 4.

登入後即可作答並保存紀錄。

這一題的完整詳解

這題考查將指令轉換為機器語言的編碼過程,需要根據提供的「語言描述表」來找出對應的 Op-code 和 Operand,並組合出正確的機器指令格式。

核心觀念:
機器語言指令通常由 Op-code (操作碼) 和 Operand (操作數) 組成。Op-code 決定了執行的操作,Operand 則指定了操作的對象(例如寄存器、記憶體位址、立即數)。「語言描述表」提供了 Op-code 和其對應的操作類型。

解題步驟:
我們需要逐一分析每個指令,並從語言描述表中找到對應的條目。

語言描述表中的相關條目:

  • Op-code A (ROX ROTATE): the bit pattern in register R one bit to the right X times. Each time place the bit that started at the low-order end at the high-order end. Example: A403 would cause the contents of register 4 to be rotated 3 bits to the right in a circular fashion.

    • 格式:A (Op-code) + R (寄存器號) + X (旋轉次數)
    • 例子:A403 表示 R=4, X=3。
  • Op-code B (BRXY JUMP): to the instruction located in the memory cell at address XY if the bit pattern in register R is equal to the bit pattern in register number 0. Otherwise, continue with the normal sequence of execution. (The jump is implemented by copying XY into the program counter during the execute phase.) Example: B43C would first compare the contents of register 4 with the contents of register 0. If the two were equal, the pattern 3C would be placed in the program counter so that the next instruction executed would be the one located at that memory address. Otherwise, nothing would be done and program execution would continue in its normal sequence.

    • 格式:B (Op-code) + R (比較寄存器號) + XY (目標位址)
    • 例子:B43C 表示 R=4, XY=3C。
  • Op-code 6 (RST ADD): the bit patterns in registers S and T as though they represented values in floating-point notation and leave the floating-point result in register R. Example: 634E would cause the values in registers 4 and E to be added as floating-point values and the result to be placed in register 3.

    • 格式:6 (Op-code) + R (結果寄存器號) + S (操作數1寄存器號) + T (操作數2寄存器號)
    • 例子:634E 表示 R=3, S=4, T=E。
  • Op-code 8 (RST AND): the bit patterns in register S and T and place the result in register R. Example: 8045 would cause the result of ANDing the contents of registers 4 and 5 to be placed in register 0.

    • 格式:8 (Op-code) + R (結果寄存器號) + S (操作數1寄存器號) + T (操作數2寄存器號)
    • 例子:8045 表示 R=0, S=4, T=5。

分析各小題:

A. ROTATE the contents of register 7 to the right 5 bit positions.

  • 操作:ROTATE,右旋轉。
  • 寄存器:Register 7 (R=7)。
  • 旋轉次數:5 bit positions (X=5)。
  • 對應 Op-code:A (ROX ROTATE)。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 6 題12 分

Decode each of the following instructions that were encoded using the language description table.

A. 4034
B. 8023
C. B288
D. 2345

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題要求將 16 位元機器指令轉換成組合語言指令。依 MARIE 指令集的格式:

指令=4 位元操作碼+12 位元運算元\text{指令}=\text{4 位元操作碼}+\text{12 位元運算元}

每個十六進位數字代表 4 個位元,因此四位十六進位數中:

  • 第一位:操作碼(opcode)
  • 後三位:運算元,通常代表記憶體位址或條件碼

本題未附上「language description table」,以下依 MARIE 常見指令集解碼:

操作碼指令
22Store
44Subt
88Skipcond
BBAddI

其中 Skipcond 的條件碼為:

  • 000:若累加器 AC<0AC<0,跳過下一個指令
  • 400:若 AC=0AC=0,跳過下一個指令
  • 800:若 AC>0AC>0,跳過下一個指令

解題方法

先取每組指令的第一個十六進位數字判斷操作碼,再將後三個十六進位數字解讀為運算元。


A. 4034

第一個十六進位數字為 44,對應 Subt。

後三位為 034,代表記憶體位址 034034。

因此:

Subt 034\boxed{\texttt{Subt 034}}

其意義為:

AC←AC−M[034]AC \leftarrow AC-\text{M}[034]

也就是從累加器中減去記憶體位址 034 內的內容。


B. 8023

第一個十六進位數字為 88,對應 Skipcond。

後三位為 023。Skipcond 實際判斷的是運算元最前面的條件欄位;023 的高位條件碼為 000,因此代表:

Skipcond 000\boxed{\texttt{Skipcond 000}}

其意義為:若 AC<0AC<0,跳過下一個指令。

023 中低位的數字不影響條件判斷,因此此指令的有效條件是「累加器小於零」。


C. B288

第一個十六進位數字為 BB,對應 AddI,即間接加法。

後三位為 288,代表記憶體位址 288288。

因此:

AddI 288\boxed{\texttt{AddI 288}}

其意義為:

AC←AC+M[M[288]]AC \leftarrow AC+\text{M}[\text{M}[288]]

執行時先讀取 M[288] 作為另一個記憶體位址,再將該位址中的內容加到累加器。


D. 2345

第一個十六進位數字為 22,對應 Store。

後三位為 345,代表記憶體位址 345345。

因此:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 7 題16 分

The following table shows a portion of a machine's memory containing a program written in the language described in the language description table. Answer the questions below assuming that the machine is started with its program counter containing 00.

addresscontent
0025
0103
0220
03F9
0453
0505
0633
0700
08C0
0900
0AC0
0B00
0CC0
0D00

A. What bit pattern will be in register 5 when the machine halts?
B. What bit pattern will be in register 0 when the machine halts?
C. What bit pattern will be in register 3 when the machine halts?
D. What bit pattern will be at memory location 00 when the machine halts?

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查簡化機器的:

  • 程式計數器(Program Counter, PC)與指令擷取
  • 立即定址(immediate addressing)
  • 暫存器加法
  • 記憶體直接定址(direct addressing)
  • 8 位元資料的十六進位運算

每條指令占用連續兩個記憶體位址。PC 會指向目前指令的第一個位元組,執行後通常增加 22。

依語言描述表:

  • 2RXX:將立即值 XX 載入暫存器 RR
  • 53XY:將暫存器 XX 與暫存器 YY 相加,結果存入暫存器 33
  • 3RXX:將暫存器 RR 的內容存入記憶體位址 XX
  • C000:停止執行

解題方法

由於 PC 初值為 00,依序讀取每兩個位元組:

PC指令動作
0025 03R5←03R_5 \leftarrow 03
0220 F9R0←F9R_0 \leftarrow F9
0453 05R3←R0+R5R_3 \leftarrow R_0+R_5
0633 00M[00]←R3M[00]\leftarrow R_3
08C0 00停止

逐步計算如下。

第一步:執行 25 03

指令 25 03 表示將立即值 03 載入暫存器 55:

R5←03R_5 \leftarrow 03

因此:

R5=03R_5=03

第二步:執行 20 F9

指令 20 F9 表示將立即值 F9 載入暫存器 00:

R0←F9R_0 \leftarrow F9

因此:

R0=F9R_0=F9

第三步:執行 53 05

此指令將 R0R_0 與 R5R_5 相加,結果存入 R3R_3:

R3←R0+R5R_3 \leftarrow R_0+R_5

代入目前內容:

R3=F9+03R_3=F9+03

以十六進位計算:

F9+03=FC\texttt{F9}+\texttt{03}=\texttt{FC}

所以:

R3=FCR_3=FC

第四步:執行 33 00

指令 33 00 表示將 R3R_3 的內容存入記憶體位址 00:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題