IMLEMENTATION OF A SIMPLE MACROPROCESSOR

Aim
To perform the Simple Macro processor using c program in CS1207 SYSTEM SOFTWARE LAB
Algorithm
Start the macro processor program
Include the necessary header files and variable
Open the three files
f1=macin.dat with read privilege
f2=macout.dat with write privilege
f3= deftab.dat with write privilege
Get the variable form f1 file macin.dat for label,opcode,operand
Read the variable until the opcode is not is equal to zero
Then check if the opcode is equal to Macro if Macro
Then
Copy macroname=label
Get the variable label ,opcode ,operand
In these if condition perform the while loop until opcode is not equal to MEND
Copy the variable
d[lines].lab=label
d[lines].opc=opcode
d[lines].oper=operand
and increase lines++;
close while loop and if condtion
else if opcode is equal to macro name
Perform the for loop from 0 to length
fprint for d[i].lab,d[i].opc,d[i].oper
else if it is not match
fprintf(f2,"%s\t%s\t%s\n",label,opcode,operand);
Finally terminate the program

Source code in c program for perform Simple macro processor
# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <stdlib.h>
struct deftab
{
char lab[10];
char opc[10];
char oper[10];
}d[10];
void main()
{
char label[10],opcode[10],operand[10],newlabel[10],newoperand[10];
char macroname[10];
int i,lines;
FILE *f1,*f2,*f3;
clrscr();
f1=fopen("macin.dat","r");
f2=fopen("macout.dat","w");
f3=fopen("deftab.dat","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)
{
printf("Lines = %d\n",lines);
for(i=0;i<lines;i++)
{
fprintf(f2,"%s\t%s\t%s\n",d[i].lab,d[i].opc,d[i].oper);
printf("DLAB = %s\nDOPC = %s\nDOPER = %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");
getch();
}

INPUT FILE :

MACIN.DAT
CALC START 1000
SUM MACRO **
** LDA #5
** ADD #10
** STA 2000
** MEND **
** LDA LENGTH
** COMP ZERO
** JEQ LOOP
** SUM **
LENGTH WORD 5
ZERO WORD 0
LOOP SUM **
** END **

OUTPUT FILES :
MACOUT.DAT
CALC START 1000
** LDA LENGTH
** COMP ZERO
** JEQ LOOP
** LDA #5
** ADD #10
** STA 2000
LENGTH WORD 5
ZERO WORD 0
** LDA #5
** ADD #10
** STA 2000
** END **

DEFTAB.DAT
** LDA #5
** ADD #10
** STA 2000





Post a Comment

0 Comments