113 年 國立成功大學工程科學系碩士班乙組《計算機概論》
第 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.
登入後即可作答並保存紀錄。
核心觀念
本題考查:
- 遞迴程序的執行順序。
- Stack(堆疊)如何模擬函式呼叫與返回。
- Hanoi Tower 的遞迴關係。
原遞迴程序:
- 先將 個盤子由 移至 。
- 將第 個盤子由 移至 。
- 再將 個盤子由 移至 。
其移動次數滿足:
且 ,因此:
解題方法:以明確 Stack 模擬遞迴
每次遞迴呼叫都建立一個 stack frame,記錄:
- :目前要移動的盤子數
- :起點
- :終點
- :暫存柱
stage:目前執行到哪個階段
一個 Hanoi Tower 呼叫可分成三個階段:
stage | 工作 |
|---|---|
| 0 | 執行 Hanoi(N-1,A,C,B) |
| 1 | 移動第 個盤子,再執行第二個遞迴呼叫 |
| 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 改為 ,再壓入:
HanoiTower(N-1, A, C, B)
如此可確保子問題完成後,程式會回到原本的 frame,繼續執行移動第 個盤子的工作。
第二階段
第一個子問題完成後,執行:
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(行優先)
- 每個元素所占的位址單位
- 主要維度的大小
本題由 與 的位址差,反推出陣列每列(或每行)包含 個元素。以下採用一般考題的假設:陣列連續儲存,且每個元素占一個位址單位。
解題方法
以 row-major 為例,若每列有 個元素,則:
由題目可得:
從 移動到 ,共跨越:
- 前進兩列: 個元素
- 每列再向右前進兩格: 個元素
因此:
接著計算 相對於 的位移:
所以:
第 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 and output .
The weights are .
The calculation for is given by:
The activation function is given by:
The neural network is defined as below:
The weights are .
What is the result and when .
登入後即可作答並保存紀錄。
核心觀念
本題考查前饋神經網路的逐層計算。每個神經元先將輸入乘上連到該節點的權重後加總,再套用 sigmoid 函數:
計算時要依箭頭方向傳遞數值,並以箭頭終點判定權重屬於哪個神經元。
解題方法
圖中第一層由 連到 : 分別連入 , 分別連入 。第二層由 連到 ,權重依序為 ;最後 分別以 連到 。各節點皆套用 sigmoid 函數。
已知 ,依照圖上的箭頭逐層計算。
第一層:
第二層:
第 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]);
}
登入後即可作答並保存紀錄。
核心觀念
本題考查兩種參數傳遞方式:
- Call by value(傳值呼叫):先計算實際參數的值,再將該值複製給形式參數。
- Call by address(傳址呼叫):將實際參數所對應的記憶體位址傳給形式參數,使形式參數與實際參數指向同一個儲存位置。
題目開頭寫「three modes」,但實際只列出上述兩種模式,以下依題目列出的兩種模式作答。
一、兩種參數傳遞方式
1. Call by value
procedure increase(x)
{
x := x + 1;
}
a := 5;
increase(a);
執行時,形式參數 x 取得 a 的複本:
函式內執行:
但 x 只是區域複本,因此函式結束後:
所以,函式內修改形式參數,不會影響呼叫端的實際參數。
2. Call by address
procedure increase(x)
{
x := x + 1;
}
a := 5;
increase(a);
傳址呼叫會將 a 的位址傳給 x,因此 x 與 a 指向同一個儲存位置。
函式內執行:
等同於直接修改 a:
所以,函式內修改形式參數,會同步修改呼叫端的實際參數。
二、本題的關鍵判斷
在 Main 中,先執行:
test[1] := 5;
test[2] := 6;
element := 7;
因此呼叫函式前的狀態為:
| 變數 | 值 |
|---|---|
test[1] | 5 |
test[2] | 6 |
element | 7 |
接著呼叫:
call_by_Mode(test[element]);
將 element=7 代入索引:
然而,test 宣告為大小為 2 的整數陣列,只存在:
test[7] 超出陣列合法範圍,因此實際參數在函式呼叫前便已經是非法存取。
三、Call by value 的結果
傳值呼叫必須先計算實際參數的值:
由於 test[7] 不存在,計算實際參數時即發生陣列索引越界。
因此:
- 無法取得合法的值傳給
x call_by_Mode的函式本體不會正常執行test[1] := 7、element := 4、x := x+3都不會成為有效結果
四、Call by address 的結果
傳址呼叫必須先取得實際參數的位址:
但 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 個位元,因此四位十六進位數中:
- 第一位:操作碼(opcode)
- 後三位:運算元,通常代表記憶體位址或條件碼
本題未附上「language description table」,以下依 MARIE 常見指令集解碼:
| 操作碼 | 指令 |
|---|---|
Store | |
Subt | |
Skipcond | |
AddI |
其中 Skipcond 的條件碼為:
000:若累加器 ,跳過下一個指令400:若 ,跳過下一個指令800:若 ,跳過下一個指令
解題方法
先取每組指令的第一個十六進位數字判斷操作碼,再將後三個十六進位數字解讀為運算元。
A. 4034
第一個十六進位數字為 ,對應 Subt。
後三位為 034,代表記憶體位址 。
因此:
其意義為:
也就是從累加器中減去記憶體位址 034 內的內容。
B. 8023
第一個十六進位數字為 ,對應 Skipcond。
後三位為 023。Skipcond 實際判斷的是運算元最前面的條件欄位;023 的高位條件碼為 000,因此代表:
其意義為:若 ,跳過下一個指令。
023 中低位的數字不影響條件判斷,因此此指令的有效條件是「累加器小於零」。
C. B288
第一個十六進位數字為 ,對應 AddI,即間接加法。
後三位為 288,代表記憶體位址 。
因此:
其意義為:
執行時先讀取 M[288] 作為另一個記憶體位址,再將該位址中的內容加到累加器。
D. 2345
第一個十六進位數字為 ,對應 Store。
後三位為 345,代表記憶體位址 。
因此:
第 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.
| address | content |
|---|---|
| 00 | 25 |
| 01 | 03 |
| 02 | 20 |
| 03 | F9 |
| 04 | 53 |
| 05 | 05 |
| 06 | 33 |
| 07 | 00 |
| 08 | C0 |
| 09 | 00 |
| 0A | C0 |
| 0B | 00 |
| 0C | C0 |
| 0D | 00 |
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 會指向目前指令的第一個位元組,執行後通常增加 。
依語言描述表:
2RXX:將立即值XX載入暫存器53XY:將暫存器 與暫存器 相加,結果存入暫存器3RXX:將暫存器 的內容存入記憶體位址XXC000:停止執行
解題方法
由於 PC 初值為 00,依序讀取每兩個位元組:
| PC | 指令 | 動作 |
|---|---|---|
00 | 25 03 | |
02 | 20 F9 | |
04 | 53 05 | |
06 | 33 00 | |
08 | C0 00 | 停止 |
逐步計算如下。
第一步:執行 25 03
指令 25 03 表示將立即值 03 載入暫存器 :
因此:
第二步:執行 20 F9
指令 20 F9 表示將立即值 F9 載入暫存器 :
因此:
第三步:執行 53 05
此指令將 與 相加,結果存入 :
代入目前內容:
以十六進位計算:
所以:
第四步:執行 33 00
指令 33 00 表示將 的內容存入記憶體位址 00: