SPCC Exp2
SPCC Exp2
______________________________
Practical Incharge
EXPERIMENT NO-2
Theory:
2. Macro name [ ]
-----------
In the definition of the MACRO and MEND are the pseudo code that represents the
start and the end of the macro definition. Once the macro is defined then the name
of macro instruction now acts as a mnemonic in assembly language that is
equivalent to sequence of the statements. Whenever the macro instruction is
encountered, the whole of the macro gets substituted in place of the macro
instruction. This process of substitution is called as macro expansion. This
expansion takes place as the definition of the macro instruction is saved in the
macro processer and is used whenever the macro instruction is encountered. A
macro call represents the expansion of the macro in the source program.
There are four basic tasks that any macro processor is associated with are
as follows
2. Save the definition: The definitions of all the macro are saved onto a table so that
they can be expanded whenever in future they are called.
3. Recognize the calls: A macro call is similar to a machine instruction whose work
is predefined i.e. the definition is stored in some table.
4. Expand calls and substitute arguments: Each time a macro call is encountered a
table consisting of macro name is searched and all the code in the macro definition
except the MACRO, MEND and the macro name card is expanded in the main
program. The actual source program now looks like as it was intended by the
programmer.
Implementation Single Pass Algorithm
Definition of a macro within other macro is possible in case of one pass macro
processor. Here the inner macro is defined only after the outer one has been called:
in order to provide for any use of the inner macro, we would have to repeat the both
the macro definition and the macro call passes. This can be assumed by considering
that the macros are never called before they are defined. Here we make use of
additional data structures like macro definition indicator (MDI) and macro
definition level counter (MDLC). The MDI and MDLC are the switches used to
keep track of macro calls and macro definition.
The MDI has status “ON” during the expansion of macro call and the value “OFF”
all the other times. When its value is “ON” the cards are read from the MDT and
when it is “OFF” the cards are read from the input source card. The use of MDLC is
used keep track of the level of macros while defining the macros. Initially it is zero
and it is incremented each time a MACRO code is found within a macro. The
reverse process happens in case of MEND i.e. the valued of MDLC is decremented
by one each time it encounters a MEND and the process continues till the MDLC is
zero i.e. the completion of macro definition.
Algorithm:
READ: (Macro call expansion or read a next instruction form the source
input card)
1. If MDI =”OFF”, then
a. Read next source card from input file.
b. Return to MAIN algorithm.
2. Else increment MDT pointer to next entry MDTP<-MDTP+1.
3. Get next card from MDT.
4. Substitute arguments from macro call.
5. If MEND pseudo code
a. Then if MDLC=0,
i. Then MDI<-“OFF”.
ii. GOTO 1.a.
b. Else return to MAIN algorithm.
6. Else if AIF or AGO present
a. Then process AIF or AGO and update MDTP.
b. Return to MAIN algorithm.
7. Else return to MAIN algorithm.
Program:
#include<stdio.h>
#include<string.h>
struct mdt
{
char lab[10];
char opc[10];
char oper[10];
}d[10];
void main()
{
char label[10],opcode[10],operand[10];
char macroname[10];
int i,lines;
FILE *f1,*f2,*f3;
f1 = fopen("MACIN.txt","r");
f2 = fopen("MACOUT.txt","w");
f3 = fopen("MDT.txt","w");
fscanf(f1,"%s %s %s",label,opcode,operand);
while(strcmp(opcode,"END")!=0)
{
if(strcmp(opcode,"MACRO")==0)
{
strcpy(macroname,label);
fscanf(f1,"%s%s%s",label,opcode,operand);
lines = 0;
while(strcmp(opcode,"MEND")!=0)
{
fprintf(f3,"%s\t%s\t%s\n",label,opcode,operand);
strcpy(d[lines].lab,label);
strcpy(d[lines].opc,opcode);
strcpy(d[lines].oper,operand);
fscanf(f1,"%s %s %s",label,opcode,operand);
lines++;
}
}
else if(strcmp(opcode,macroname)==0)
{
for(i=0;i<lines;i++)
{
fprintf(f2,"%s\t%s\t%s\n",d[i].lab,d[i].opc,d[i].oper);
}
}
else
fprintf(f2,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(f1,"%s%s%s",label,opcode,operand);
}
fprintf(f2,"%s\t%s\t%s\n",label,opcode,operand);
fclose(f1);
fclose(f2);
fclose(f3);
printf("FINISHED");
}
Input:
CALC START 1000
SUM MACRO **
** LDA #5
** ADD #10
** sTA 2000
** MEND **
** LDA LENGTH
** COMP ZERO
** JEQ LOOP
** SUM **
LENGTH WORD S
ZERO WORD S
LOOP SUM **
** END **
Input File:
Output: