:::warning
必考題為107和111考古皆有出現
:::
## Marco
電腦科學裡的巨集是一種抽象(Abstraction),它根據一系列預定義的規則替換一定的文字模式。直譯器或編譯器在遇到巨集時會自動進行這一模式替換。
絕大多數情況下,「巨集」這個詞的使用暗示著將小命令或動作轉化為一系列指令(空間換取時間)。
> 引用自:https://zh.wikipedia.org/zh-tw/%E5%B7%A8%E9%9B%86
A macro represents a commonly used group of statements in the source programming language.
The macro processor **replaces each macro instruction with the corresponding group of source language statements**. This is called expanding the macros.
### 考古題
**107**

## Marco processer ERROR Handling
The macro processors that we have discussed so far might be called **preprocessors.**
They process macro **definitions and expand macro invocations**, producing an expanded version of the source program. This expanded program is then used as input to an assembler or compiler.
the **macro processor performs no analysis** of the text it handles.
The meaning of these statements, and their translation into machine language, are of no concern whatsoever during macro expansion
### 考古題
**111**

<h2 style="color:orange">Marco, Function比較[必考]</h2>
**Marco**
* 將巨集指令複製到程式中,節省PC移動位址的時間,但也較費空間
**Function**
* Function呼叫時會跳到指定的位址,節省程式碼的空間,但也較費時
### 考古題
**107**

**111**

<h2 style="color:orange">Marco label[必考]</h2>
在重複貼上Marco定義時,**會有重複Lablel出現**,會造成程式不知道要移動到哪個位址
However, the use of statements like **"JLT \*-14"** is generally considered to be a poor programming practice.
透過指定PC位移量的方式,會造成**程式不易維護**
technique for generating unique labels within a macro expansion:
The **character $ will be replaced by $XX**, where XX is a two-character alphanumeric counter of the number of macro instructions expanded, AA, AB, AC, etc.
```alt=
25 RDBUFF MACRO &INDEV,&BUFADR,&RECLTH
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
40 CLEAR S
45 +LDT #4096 SET MAXIMUM RECORD LENGTH
50 $LOOP TD =X'&INDEV' TEST INPUT DEVICE
55 JEQ $LOOP LOOP UNTIL READY
60 RD =X'&INDEV' READ CHARACTER INTO REG A
65 COMPR A,S TEST FOR END OF RECORD
70 JEQ $EXIT EXIT LOOP IF EOR
75 STCH &BUFADR,X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $LOOP HAS BEEN REACHED
90 $EXIT STX &RECLTH SAVE RECORD LENGTH
95 MEND
(a)
RDBUFF Fl,BUFFER,LENGTH
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
40 CLEAR S
45 +LDT #4096 SET MAXIMUM RECORD LENGTH
50 $AALOOP TD =X'Fl' TEST INPUT DEVICE
55 JEQ $AALOOP LOOP UNTIL READY
60 RD =X'Fl' READ CHARACTER INTO REG A
65 COMPR A,S TEST FOR END OF RECORD
70 JEQ $AAEXIT EXIT LOOP IF EOR
75 STCH BUFFER,X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $AALOOP HAS BEEN REACHED
90 $AAEXIT STX LENGTH SAVE REJORD LENGTH
(b)
```
### 考古題
**111, 107**

<h2 style="color:orange">Marco Data structure[必考]</h2>
**one-pass macro processor** that can alternate between macro definition and macro expansion
下層Macro可以定義不同的版本,取決於第一層Marco definition
```alt=
1 MACROS MACRO {Defines SIC standard version macros}
2 RDBUFF MACRO &INDEV,&BUFADR,&RECLTH
.
. {SIC standard version}
.
3 MEND {End of RDBUFF}
4 WRBUFF MACRO &OUTDEV,&BUFADR,&RECLTH
.
. {SIC standard version}
.
5 MEND {End of WRBUFF}
.
.
.
6 MEND {End of MACROS}
(a)
1 MACROX MACRO {Defines SIC/XE macros}
2 RDBUFF MACRO &INDEV,&BUFADR,&RECLTH
.
. {SIC/XE version}
.
3 MEND {End of RDBUFF}
4 WRBUFF MACRO &OUTDEV,&BUFADR,&RECLTH
.
. {SIC/XE version}
.
5 MEND {End of WRBUFF}
.
.
.
6 MEND {End of MACROX}
(b)
```
**There are three main data structures.**
* Definition table (DEFTAB)
- References to the macro instruction parameters are converted to a positional notation for efficiency in substituting arguments.
* Name table (NAMTAB)
- serving as an **index** to DEFTAB.
- 目錄
* Argument table (ARGTAB)
- 紀錄參數
### 考古題1
**111,107**

### 考古題2
**107**

## Recursive Macro Expansion
**PROCESSLINE and EXPAND** would probably not be procedures at all. Instead, the same logic would be incorporated into a looping structure, with data values being **saved on a stack**.
### 考古題
**111**

<h2 style="color:orange">Keyword Macro Parameters[必考]</h2>
### 考古題
**111,107**

<h2 style="color:orange">Marco程式[必考]</h2>
```alt=
1 SUM MACRO &ID
2 LDA X&ID-->1
3 ADD X&ID-->2
4 ADD X&ID-->3
5 STA X&ID-->S
6 MEND
(a)
SUM A
|
|
\|/
LDA XAl
ADD XA2
ADD XA3
STA XAS
(b)
```
**111,107**

### 考古題2
Boolean expression in IF statements occurs at the time macros are expanded, not during program execution.
```alt=
25 RDBUFF MACRO &INDEV, &BUFADR , &RECLTH ,&EOR ,&MAXLTH
26 IF (&EOR NE ")
27 &EORCK SET 1
28 ENDIF
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
38 IF (&EORCK EQ 1 )
40 LDCH =X'&EOR' SET EOR CHARACTER
42 RMO A,S
43 ENDIF
44 IF (&MAXLTH EQ '')
45 +LDT #4096 SET MAX LENGTH = 4096
46 ELSE
47 +LDT #&MAXLTH SET MAXIMUM RECORD LENGTH
48 ENDIF
50 $LOOP TD =X'&INDEV' TEST INPUT DEVICE
55 JEQ $LOOP LOOP UNTIL READY
60 RD =X'&INDEV' READ CHARACTER INTO REG A
63 IF (&EORCK EQ 1 )
65 COMPR A,S TEST FOR END OF RECORD
70 JEQ $EXIT EXIT LOOP IF EOR
73 ENDIF
75 STCH &BUFADR ,X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $LOOP HAS BEEN REACHED
90 $EXIT STX &RECLTH SAVE RECORD LENGTH
95 MEND
(a)
RDBUFF F3,BUF,RECL,04,2048
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
40 LOCH =X'04' SET EOR CHARACTER
42 RMO A,S
47 +LDT #2048 SET MAXIMUM RECORD LENGTH
50 $AALOOP TD =X'F3' TEST INPUT DEVICE
55 JEQ $AALOOP LOOP UNTIL READY
60 RD =X'F3' READ CHARACTER INTO REG A
65 COMPR A,S TEST FOR END OF RECORD
70 JEQ $AAEXIT EXIT LOOP IF EOR
75 STCH BUF,X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $AALOOP HAS BEEN REACHED
90 $AAEXIT STX RECL SAVE RECORD LENGTH
```
**111,107**
