Unit - 2
Macro Processor and Compilers
Q1) Introduce macros?
A1) Macros are single-line abbreviations for a certain group of instructions. Once the macro is defined, these groups of instructions can be used anywhere in a program. It is sometimes necessary for an assembly language programmer to repeat some blocks of code in the course of a program. The programmer needs to define a single machine instruction to represent a block of code for employing a macro in the program. The macro proves to be useful when instead of writing the entire block again and again, you can simply write the macro that you have already defined. An assembly language macro is an instruction that represents several other machine language instructions at once.
Macros are one-line acronyms for a specific set of commands. These collections of instructions can be used anywhere in a program once the macro has been defined.
During the course of a program, an assembly language programmer may need to repeat some blocks of code. To use a macro in a program, the programmer must define a single machine instruction to represent a block of code. Instead of writing the complete block over and over, the macro comes in handy when you can simply write the macro you've already defined. An assembly language macro is a single instruction that acts as a proxy for several machine language commands.
The macro facility allows you to provide a name to a sequence that appears multiple times in a program, and then use that name whenever that sequence is encountered. All you have to do now is use the macro instruction definition to give a sequence a name.
The structure shown below demonstrates how to define a macro in a program:
The first line of the macro definition is the MACRO pseudo-op, which is described by this structure. The name line for macro is the next line, which identifies the macro instruction name. Following the macro name is a line that contains the abbreviated sequence of instructions. The actual macro instruction is contained in each instruction. MEND pseudo-op is the last statement in the macro definition. This pseudo-op marks the end of the macro definition and the end of the macro instruction definition.
Q2) Explain Macro instruction arguments?
A2) In example 1 we have used one argument called as DATA. Suppose if we want to use more operands for same operation, it provides the facility for arguments or parameters in calls. Corresponding macro dummy arguments will appear in macro definition.
Consider the following example2
A 1, DATAl
A 2, DATAl
A 3, DATAl
A 1, DATA2
A 2, DATA2
A 3, DATA2
DATAl DC F’5”
DATA2 DC F’4”
In the above example DATAl and DATA2 parameters are used for performing add operations with its registers. In the above assembly code ADD operation is repeated two times with different operands. For the above case we can define macro in the following manner
SOURCE EXPANDED SOURCE
MACRO
INCR & arg
A l,<fcarg
A 2.< fcarg
A 3,< fcarg
MEND
INCR DATAl • use datal as operand A 1,DATAl
A A 2,DATAl
3,DATAl
INCR DATA2 use data2 as operand A
• •
DATA1 A
DC F’5” DATA2
DATA2 F’4” DATA2
DC
Performing the same operation with a variable argument is called as macro instruction argument or dummy argument. It is specified by means of & symbol. It is possible to supply more than one argument in a macro call. Each argument must correspond to a definition argument on the macro name line of the macro definition. When a macro call is processed, the arguments supplied are substituted for the respective dummy arguments in the macro definition.
Consider the following example3
Loopl A A5 DATAl
A 2,DATA2
A 3,DATA3
Loop2 A I 5 DATAl
A 2,DATA2
A 3,DATA3
DATAl F’5”
DC
DATA2 F’4”
DC
DATA3 F’2”
DC
This program could be written as
SOURCE EXPANDED SOURCE
MACRO
&1 INCR &arg15&arg2, &arg3
&1A l, &argl
A 2,&arg2
A 3,&arg3
MEND
Loopl INCR DATA1,DATA2,DATA3 Loopl A 1,DATAl
A 2,DATA2
A 3,DATA3
Loop2 INCR DATA3,DATA2,DATAl Loop2 A 1,DATA3
A 2,DATA2
A 3,DATAl
DATAl DC F’5’ DATAl DC F’5”
DATA2 DC F’4” DATA2 DC F’4”
DATA3 DC F’2” DATA3 DC Fc Z’
Here we have specified four arguments including label argument. Label arguments are treated as operands of a macroinstruction.
Q3) What do you mean by conditional macro expansion?
A3) For writing conditional macro two pseudo operations are used. They are
AIF
AGO
Consider the following example4
A 2,DATA2
A 3 ,DAT A3
Loop2 A 1,DATAl
A 2,DATA2
Loop3 A 1,DATAl
DATAl DC F’5”
DATA2 DC F’4”
DATA3 DC F’2”
In the above example under each loop variable number of arguments is used. The program could be written by using only conditional macro
MACRO
&argvary &count,&argl ,&arg2,&arg3
&argOA l,&argl
AIF (&count EQ 1).FINI
A 2,&arg2
AIF (&count EQ 2).FINI
A 3,&arg3
TINI MEND
EXPANDED SOURCE
Loopl vary 3,D AT A1,D AT A2,D AT A3 • •
Loopl A !,DAT A3
A 2,DATA2
A 3,DATAl
Loop2 vary 2,D AT A3 ,DAT A2 •
Loop2 A !,DATA3
A 2,DATA
Loop3 vary 1,DATAl
Looρ3 A !,DATA!
DATAl DC F’5”
DATAl DC F’5”
DATA2 DC F’4” DATA2 DC F’4”
DATA3 DC F’2” DATA3 DC F’2”
Labels starting with period (.) such as .FINI, are macro labels and do not appear in the output of the macro processor. The statement AIF directs the macro processor to skip to the statement labelled .FINI if the parameter corresponding to & COUNT is a 1; otherwise the macro processor is to continue with the statement following the AIF pseudo operation. AIF is a conditional branch pseudo operation. The AGO is
An unconditional branch or go to statement. It specifies a label appearing on some other statement in the macro instruction definition.
Q4) Describe macros calls within macros or nested macros?
A4) One macro within another macro is called a nested macro. Consider the following example 5 MACRO
ADDl &arg
L l,&arg
A 1 =FT’
ST l,<fcarg
MEND
MACRO
ADDS &argl,&arg2,&arg3
ADDl &argl
ADDl &arg2
ADDl &arg3
MEND
The above program could be written using multiple levels. Macro calls with in macros can involve several levels. The macro ADDS invoke another macro ADDl.
SOURCE EXPANDED SOURCE EXPANDED
SOURCE
(Level1) (Level2)
MACRO
ADDl &arg
L l,<fcarg
A I=FT’
ST 1
MEND ,<fcarg
MACRO
ADDS &argl,&arg2,&arg3
ADDl &argl
ADDl &arg2
ADDl &arg3
MEND SOURCE
Example of nested macro
TEST START 2000h
MACROS MACRO
CELTOFER MACRO &CEL &FER
LDA &CEL
MULT NINE
ADD THIRTYTWO
STA &FER
MEND
MEND
MACROF MACRO
CELTOFER MACRO &CEL &FER
LDAF &CEL
MULTF NINE
DIVF FIVE
ADD THIRTYTWO
STA &FER
MEND
MEND
Q5) Explain macros instruction?
A5) Consider the following example 1
A!,DATA //add contents of DATA to register1
A2,DATA //add contents of DATA to register2
A3,DATA //add contents of DATA to register3
A!,DATA //add contents of DATA to register 1
A2,DATA //add contents of DATAto register2
A3,DATA //add contents of DATAto register3
In the above example the following sequence is repeated two times.
A!,DATA
A2,DATA
A3,D AT A
Rather than typing these sequences more than one time, we can simply define a macro.
Q6) Define Macro.
A6) Macro definition starts with the key word MACRO and end with the keyword MEND i.e., it is defined in the following manner
MACRO
//macro name
//body of macro
MEN
D
The above example can be written using macro as follows
SOURCE EXPANDED SOURCE
MACRO
INCR
A !,DATA
A 2,DATA
A 3,D AT A
MEND
!,DAT
INCR A A
2,DAT
• A A
3,DAT
- A A
!,D
INCR A A T
2,DAT
• A A
3,DAT
• A A
DATADC F’5”
In the above case the macro processor replaces each macro call with the lines
A !,DATA
A 2,DATA
A 3,D AT A
The macro invocation can be done by using macro name, in this case it is INCR.
The process of replacement is called expanding the macro. In the above case the macro definition does not appear in the expanded source code.
Q7) Write the concept of single pass macro processor?
A7) “Macro in macro” can be handled by a one-pass macro processor that alternates between macro definition and macro expansion.
The definition of a macro must be in the source program before any statements that call that macro, due to the one-pass structure.
This is a sensible restriction (does not create any real inconvenience).
Global variables
In a one-pass macro processor, there are three basic data structures involved:
- DEFTAB
● The macro definition is saved here, together with the macro prototype and macro body.
● The comment lines have been eliminated.
● For ease of substituting arguments, references to macro instruction parameters are translated to positional notation.
2. NAMTAB
● Store macro names that function as an index to DEFTAB and contain references to the definition's start and end.
3. ARGTAB
● When macro invocations are expanded, this variable is used.
● The arguments are saved in this database according to their position in the argument list when a macro invocation statement is encountered.
Data structure
● The macro names are inserted into NAMTAB, which has two references to the start and finish of the DEFTAB definition.
● The third data structure is the ARGTAB argument table, which is used during macro invocation expansion.
● The arguments are saved in ARGTAB in the order in which they appear in the argument list.
Algorithm
Procedure DEFINE
● When the beginning of a macro definition is recognized, this function is called.
● Fill in the appropriate fields in DEFTAB and NAMTAB.
Procedure EXPAND
● Called to expand a macro invocation statement and set up the argument values in ARGTAB.
Procedure GETLINE
● Request that the next line be handled.
Algorithm for one pass macro processor
Begin {microprocessor}
EXPANDING : - FALSE
While OPCODE‘MIND’ do
Begin
CEPLINR
PROCESSLINE
End {while}
End {microprocessor}
Procedure PROCESSLINK
Begin
Search NAMTAB for OPCODE
If found then
Else if OPCODE=’MACRO’ then
DEFINE
Else write source line to expanded file
End {PROCESSLINK}
Procedure EXPAND
Begin
EXPANDING :=TRUE
Got first line of macro definition (prototype) from DEFTAB
Got up arguments from macro innovation in ARCTAB
Write macro invocation to expanded file as a constant
While not end of macro definition do
Begin
GETLINE
PROCESSLINE
End (while)
EXPANDING:=FALSE
End {EXPAND}
Procedure GETLINE
Begin
If EXPANDING then
Get next line of macro definition from DEFTAB
Multitude arguments from ARCTAB for positional notation
End (if)
Else
Read next line from input file
End {GETLINE}
Begin
Enter macro name into NAMTAB
Enter micro prtotype into DEFTAB
LEVEL t=1
While LEVEL>0 do
Begin
GETLINE
If this is not a comment line then
Begin
Substitute positonal notaiton for parameters
Enter line into DEFTAB
If OPCOD=’MACRO’ then
LEVEL:=LEVEL-1
Else if OPCODE=’MIND’ then
LEVEL:=LEVEL -1
End (if not comment)
End (while)
Store in NAMTAB pointers to beginning and end of definition
End {DEFINE}
Q8) Write the difference between two pass and one macro processor?
A8) Difference between two pass and one pass macro processor
Two Pass Macro Processor | One Pass Macro Processor |
(i) Pass1: Recognize macro definition (ii) Pass 2: Recognize macro calls |
|
|
|
Q9) Write contents of MNT, MDT and Argument list Array for following program?
A9)
MACRO | Pass 1 | ||
INCR & X, & Y,& Z | MNT | ||
MOVER & Z & X |
| ||
ADD & Z, &Y | Index | Macro Name | MDT Index |
MOVEM & Z, & X | 1 | INCR | 1 |
MEND | 2 | DECR | 6 |
MACRO | 3 | MNTC |
|
DECR & A,& B, & C = BREG |
|
|
|
SUB & C, & A | MDT | ||
MOVEM & C,& A | Index | MDT Instruction | |
MEND | 1 | INCR & X, & Y, & Z | |
START 100 | 2 | MOVER & Z, & X | |
READ N1 | 3 | ADD & Z, & Y | |
READ N2 | 4 | MOVEM & Z, & Y | |
INCR NI, N2, AREG | 5 | MEND | |
DECR NI, N2 | 6 | DECR & A,& B, & C = BREG | |
STOP | 7 | MOVER & C, & A | |
N1 DS 1 | 8 | SUB & C, & B | |
N2 DS 2 | 9 | MOVEM & C, & A | |
END | 10 | MEND | |
| 11 ← | MDTC |
Argument List Array for MACRO CALL:
INCR NI, N2, AREG |
| DECR N1, N2 | |||
Index | Formal Parameter | Actual Parameter | Index | Formal Parameter | Actual Parameter |
1 | X | N1 | 1 | A | N1 |
2 | Y | N2 | 2 | B | N2 |
3 | Z | AREG | 3 | C=CREG | CREG |
|
|
|
| Default | Value |
Pass 2 output for macro Expansion:-
START 100
READ N1
READ N2
MOVER AREG, N1
ADD AREG, N2
MOVEM AREG, N1
MOVER CREG, N1
SUB CREG, N2
MOVEM CREG, N1
Q10) Introduce compilers?
A10) A minimally useful compiler is a large project and a fully optimising compiler doing everything imaginable is impossibly large (a trip to the library or CiteSeer makes it even bigger).
There are several different compiler designs that could make sense for an object oriented dynamically typed language. The possible compiler designs run from compiling as quickly as an interpreter interprets to very slow compilers that produce very high quality code.
Most JIT (Just-in-Time) compilers compile fast but not as fast as interpreters, they stick to linear time worst case algorithms because compilation happens at run time where noticeable pauses are not acceptable. Most mature batch compilers tend to be very slow, aiming at fast execution times (at least at higher optimisation levels) though a lot are simpler to make them buildable.
JITs are a great strategy for very fast compiling compilers because they can recoup on the second execution. But they are much less appealing when execution time is important because the compile time pauses will get longer (potentially a lot longer, as compilers often use O(N2) - or greater - algorithms).
Compilers offer some strong benefits for up front design because of the rich body of literature, but they also have some strong disadvantages. It's likely that there are no successful projects working in the same design space: language implementation; optimisation style (fast compilation or fast execution); and basic framework (SSA, dataflow, simple tree walkers, one pass). It is also very likely that no-one on the team has done anything similar.
Judging design literature without having personally implemented similar projects is hard, if not impossible, but many key decisions have to be made early. These are really project-level design, not technical, but they'll influence the choice of algorithm and intermediate forms.
Q11) Write the difference between compiler and interpreter?
A11) Difference between compiler and interpreter
Compiler | Interpreter |
Scan the entire program first and then translate it into machine code. | Translate the program line-by-line. |
Execution time is less. | Execution time is more. |
Since source code is not required tampering with the source code is not possible. | Source code can be easily modified and hence no security of programs. |
Convert the entire program to machine code; when all the syntax errors have been removed execution takes place. | Each time the program is executed, every line is checked for syntax error and then converted to equivalent machine code. |
Slow for debugging. | Fast for debugging. |
Machine code can be saved and used; source code and compiler are no longer needed. | Machine code cannot be saved; an interpreter is always required for translation. |