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 即可保留正確順序。
非遞迴版本
第 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 .
登入後即可作答並保存紀錄。
這題考查神經網路的前向傳播 (Forward Propagation) 計算。需要根據給定的網路結構、權重、輸入值以及激活函數,逐步計算每一層的輸出。
核心觀念:
神經網路的前向傳播是將輸入數據通過網路的各層,根據權重和激活函數計算輸出。
計算步驟通常是:
- 計算加權總和(或稱輸入): (其中 是偏置項,此題沒有給出偏置項,假設為 0)。
- 將加權總和通過激活函數:。
解題步驟:
首先,觀察網路結構圖:
- 輸入層有 。
- 第一層隱藏層有節點 。
- 第二層隱藏層有節點 。
- 輸出層為 。
根據圖示,我們可以推導出各節點的計算公式:
輸入層:
第一層隱藏層:
- 節點 :接收來自 的輸入,權重為 ;接收來自 的輸入,權重為 。
- 節點 :接收來自 的輸入,權重為 ;接收來自 的輸入,權重為 。
第二層隱藏層:
- 節點 :接收來自 的輸入,權重為 ;接收來自 的輸入,權重為 。
- 節點 :接收來自 的輸入,權重為 ;接收來自 的輸入,權重為 。
輸出層:
- 節點 :接收來自 的輸入,權重為 ;接收來自 的輸入,權重為 。
權重值:
計算過程:
1. 計算第一層隱藏層的輸出 ():
2. 計算第二層隱藏層的輸出 ():
3. 計算輸出層的輸出 ():
精確計算(避免中間四捨五入):
使用 和 。
-
第一層:
-
第二層:
第 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] 不存在,計算實際參數時即發生陣列索引越界。
因此:
第 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 題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 中低位的數字不影響條件判斷,因此此指令的有效條件是「累加器小於零」。
第 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 載入暫存器 :
因此: