Systems Programming: Tutorial Eleven: Sheet-Five-Solutions
Systems Programming: Tutorial Eleven: Sheet-Five-Solutions
Tutorial Eleven:
Sheet-Five-Solutions
1
Exercises(1)(a)
1. a) Describe briefly the data structures used by a macro-facility.
2
Solution
Definition Table
ϕThe macro definitions are stored in a definition table DEFTAB containing the
macro prototype and the statements making up the body with some editing
to simplify expansion of calls : “each parameter in the body is replaced by a
positional indicator which gives the position of the parameter in the
prototype parameter list” .
ϕThe symbolic parameters in the prototype are retained to enable the macro
processor to handle parameter replacement by keyword rather than by
position.
3
Solution(2)
Name Table
ϕThe macro names are entered into Name Table, NAMTAB which serves as
index to DEFTAB.
ϕ An entry in NAMTAB contains pointer to the beginning of the definition of
the macro in DEFTAB.
4
Solution(3)
ARGTAB
ϕA table ARGTAB is used during expansion of a macro call to store the
arguments in that call according to their position in the argument list.
ϕAs the macro is expanded, arguments from ARGTAB are substituted for the
corresponding parameters in the macro body.
5
Solution(4)
Copy File
ϕTo store a copy of the source file to be input during Pass II.
6
Solution(5)
Expanded File
ϕTo store the expanded source program.
7
Exercises(1)(b)
1. b) Why ordinary symbols are not permitted in defining labels within
macros? Explain using an example.
8
Solution
It is not possible for the body of the macro definition to contain
normal labels. If a label appears in the body, it will be generated with
each macro call, resulting in ‘duplicate label definition’.
Example consider following macro definition
ALPHA MACRO &X, &U
LDA &X
COMP #0
JLT LABEL
ADD #1
LABEL STA &U
MEND
9
Solution(2)
Consider the following successive subroutine calls
CALL1 ALPHA XX1, UU1
CALL2 ALPHA XX2, UU2
10
Solution(2)
This yields the following expansion:
CALL1 LDA XX1
COMP #0
JLT LABEL
ADD #1
LABEL STA UU1
CALL2 LDA XX1
COMP #0
JLT LABEL
ADD #1
LABEL STA UU1
(Duplicate label “LABEL” as a result of the macro expansions)
11
Exercises(1)(c)
1. c) Explain briefly the difference between subroutines/functions and
macros.
12
Solution
The definition of a macro causes no machine code to be generated
by the macro-assembler, while the definition of a subroutine does.
Use of macros causes assembler to generate code for macro body
with arguments substituted for the parameters (on-line code). Use of
subroutines causes instructions for binding of call with definition to
be generated (off-line code). That is, only one copy of the subroutine
body is saved in memory.
ϕThus, macros execute faster than subroutines because there is no linkage
overhead, but they require more space because each reference to a macro
causes the assembly of a separate copy of the code of the body.
13
Exercises(2)
ALPHA MACRO &P, &Q
2. Given the macro &M SET %ITEMS(&P)
definition: IF (&M EQ 0)
LDA #0
give the expansion of the STA
ELSE
&Q
14
Solution
Label Mnemonic Operand
XXX LDA P1
ADD P2
ADD P3
ADD P4
STA Q
15
Exercises(3)
3. Write the definition of the macro:
ABS &P, &Q
that stores in &Q the absolute value of &P.
16
Solution
Label Mnemonic Operand
ABSOLUTE MACRO &P, &Q
LDA &P
COMP #0
JLT $L1
J $L2
$L1 MUL #-1
$L2 STA &Q
MEND
17
Exercises(4)
4. Write the definition of the macro:
SQR &P, &Q
that stores in &Q the square value of &P.
18
Solution
Label Mnemonic Operand
SQR MACRO &P, &Q
LDA &P
MUL &P
STA &Q
MEND
19
Exercises(5)
5. Given the macro
definition:
give the expansion of the FS MACRO &P, &Q
macro call: &N
LDA
SET
#0
1
LBL FS (A1, A2, A3), D WHILE (&N LE %ITEMS(&P))
ADD &P(&N)
&N SET &N+1
ENDW
STA &Q
MEND
20
Solution
Label Mnemonic Operand
LBL LDA #0
ADD A1
ADD A2
ADD A3
STA D
21
Exercises (6)
6. Write the definition of the macro:
TOTAL &Q, &R
that stores in &R the sum of elements in the list &Q. Assume that the
list has at least one element.
22
Solution-Macro
Label Mnemonic Operand
TOTAL MACRO &Q, &R
LDA &LIST[1]
&N SET 2
WHILE (&N LE %ITEMS(&Q))
ADD &Q[&N]
&N SET &N+1
ENDW
STA &R
MEND
23