Friday, April 4, 2014

Design suitable data structures and implement pass-I of a two-pass macro-processor.

SOFTWARE LABORATORY

First of all you will need a.asm file in same folder where your maro pass I program will be

a.asm
 
MACRO
INCR &X,&Y,&REG1=AREG
MOVER &REG1,&X
ADD &REG1,&Y
MOVEM &REG1,&X
MEND
MACRO
DECR &A,&B,&REG2=BREG
MOVER &REG2,&A
SUB &REG2,&B
MOVEM &REG2,&A
MEND
START 100
READ N1
READ N2
DECR N1,N2
STOP
N1 DS 1
N2 DS 2
END

now actual code :

MI.c


#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct mntab
{
    char name[8];
    int address;
    int argcnt;
} mnt[5];

struct mdtab
{
    char def[40];
}mdt[20];

struct ALA
{
    char formal[10];
    char actual[10];
    char keyword[10];
}ala[20];

int imdt=0;
int imnt=0;
int iala=0;
int main()
{
    char fname[10],buffer[80],s1[8],s2[8],s3[16],s4[16],ref[10];
    int i,j,k,cnt,flag;
    FILE * fin,*fout,*fmnt,*fmdt,*fala;
    printf("\n\t Enter File Name (for Input code ) : ");
    scanf("%s",fname);
    fin= fopen(fname,"r");
    fout= fopen("output.txt","w+");
   
    while(fgets(buffer,40,fin)!=0)
    {
        cnt=sscanf(buffer,"%s%s%s%s",s1,s2,s3,s4);
        if(strcmp(s1,"MACRO")!=0)
            fputs(buffer,fout);
        else
        {
            printf("\n\tMacro fond...");
            printf("\n\tProcessing macro name table  ");
            fgets(buffer,40,fin);
            for(i=0;i<40;i++)
                if(buffer[i]==',')
                    buffer[i]=' ';
            cnt=sscanf(buffer,"%s%s%s%s",s1,s2,s3,s4);
            i=(s1[1]-65)%5;
            strcpy(mnt[i].name,s1);
            mnt[i].address=imdt;
            mnt[i].argcnt=cnt-1;
            printf("\n\tProcessing Argument List ");
           
            if(cnt>1)
            {
                j=(s2[1]-65)%20;
                strcpy(ala[j].formal,s2);
            }
            if(cnt>2)
            {
                flag=0;
                j=(s3[1]-65)%20;
                for(i=0;i<16;i++)
                {
                    if(s3[i]=='=')
                    {
                        s3[i]=' ';
                        sscanf(s3,"%s%s",ref,s3);
                        flag=1;
                        break;
                    }
                }
                if(flag==1)
                {
                    strcpy(ala[j].formal,ref);
                    strcpy(ala[j].actual,s3);
                }
                else
                    strcpy(ala[j].formal,s3);
            }
            if(cnt>3)
            {
                flag=0;
                j=(s4[1]-65)%20;
                for(i=0;i<16;i++)
                {
                    if(s4[i]=='=')
                    {
                        s4[i]=' ';
                        sscanf(s4,"%s%s",ref,s4);
                        flag=1;
                        break;
                    }
                }
                if(flag==1)
                {
                    strcpy(ala[j].formal,ref);
                    strcpy(ala[j].actual,s4);
                }
                else
                    strcpy(ala[j].formal,s4);
            }
            printf("\n\tProcessing macro defination table....");
            sscanf(buffer,"%s%s%s%s",s1,s2,s3,s4);
            i=(s2[1]-65)%20;
            j=(s3[1]-65)%20;
            k=(s4[1]-65)%20;
            sprintf(buffer,"%s %d %d %d",s1,i,j,k);
            strcpy(mdt[imdt++].def,buffer);
            printf("\n\t %d ",imdt);
            while(1)
            {
                flag=0;
                strcpy(s1,"");
                strcpy(s2,"");
                strcpy(s3,"");
                strcpy(s4,"");
                fgets(buffer,40,fin);
                for(i=0;i<40;i++)
                    if(buffer[i]==',')
                        buffer[i]=' ';
                sscanf(buffer,"%s%s%s",s1,s2,s3);
                if(s2[0]=='&')
                {
                    j=((s2[1]-65)%20);
                    flag=1;
                    sprintf(buffer,"%s %d %s",s1,j,s3);
                }
                if(s3[0]=='&')
                {
                    k=((s3[1]-65)%20);
                    if(flag==1)
                        sprintf(buffer,"%s %d %d",s1,j,k);
                    else
                        sprintf(buffer,"%s %s %d",s1,s2,k);
                }
                if(strcmp(s1,"MEND")!=0)
                    strcpy(mdt[imdt++].def,buffer);
                else
                {
                    strcpy(mdt[imdt++].def,"MEND");
                    break;
                }
            }
        }
    }
    fseek(fin,0,0);
    fseek(fout,0,0);

    printf("\n\t====IP====");
    while(fgets(buffer,40,fin)!=0)
        puts(buffer);
    fclose(fin);
    printf("\n\t====OP of pass I ====");
    while(fgets(buffer,40,fout)!=0)
        puts(buffer);
    fclose(fout);
    fmnt= fopen("mnt.txt","w");
    printf("\n\t==== MNT ====");
    for(i=0;i<5;i++)
    {
        printf("\n\t %s\t%d\t%d",mnt[i].name,mnt[i].address,mnt[i].argcnt);
        fprintf(fmnt,"%s\t%d\t%d\n",mnt[i].name,mnt[i].address,mnt[i].argcnt);
    }
    fclose(fmnt);
    fmdt= fopen("mdt.txt","w");
    printf("\n\t==== MDT ====");
    for(i=0;i<imdt;i++)
    {
        printf("\n%d\t%s",i,mdt[i].def);
        fprintf(fmdt,"%s\n",mdt[i].def);
    }
    fclose(fmdt);
    fala= fopen("ala.txt","w");
    printf("\n\t==== ALA ====");
    for(i=0;i<20;i++)
    {
        printf("\n\t%s\t%s",ala[i].formal,ala[i].actual);
        fprintf(fala,"\t%s\t%s\n",ala[i].formal,ala[i].actual);
    }
    fclose(fala);
}


Execute it :


root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/macro I# gcc MI.c
root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/macro I# ./a.out

     Enter File Name (for Input code ) : a.asm

    Macro fond...
    Processing macro name table 
    Processing Argument List
    Processing macro defination table....
     1
    Macro fond...
    Processing macro name table 
    Processing Argument List
    Processing macro defination table....
     6
    ====IP====MACRO

INCR &X,&Y,&REG1=AREG

MOVER &REG1,&X

ADD &REG1,&Y

MOVEM &REG1,&X

MEND

MACRO

DECR &A,&B,&REG2=BREG

MOVER &REG2,&A

SUB &REG2,&B

MOVEM &REG2,&A

MEND

START 100

READ N1

READ N2

DECR N1,N2

STOP

N1 DS 1

N2 DS 2

END


    ====OP of pass I ====START 100

READ N1

READ N2

DECR N1,N2

STOP

N1 DS 1

N2 DS 2

END


    ==== MNT ====
         0    0
         0    0
         0    0
     INCR    0    3
     DECR    5    3
    ==== MDT ====
0    INCR 3 4 17
1    MOVER 17 3
2    ADD 17 4
3    MOVEM 17 3
4    MEND
5    DECR 0 1 17
6    MOVER 17 0
7    SUB 17 1
8    MOVEM 17 0
9    MEND
    ==== ALA ====
    &A   
    &B   
       
    &X   
    &Y   
      
    &REG2    BREG
       
        root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/macro I#


#################################################

On execution you will found ala.txt , mnt.txt ,mdt.txt , output.txt all this file require for macro pass II

ala.txt (actual and formal parameters list ala-aurgument list array)

    &A   
    &B   
       
    &X   
    &Y   
       
   &REG2    BREG
           
mdt.txt (Macro Defination Table)
INCR 3 4 17
MOVER 17 3
ADD 17 4
MOVEM 17 3
MEND
DECR 0 1 17
MOVER 17 0
SUB 17 1
MOVEM 17 0
MEND

mnt.txt (Macro Name Table)
    0    0
    0    0
    0    0
INCR    0    3
DECR    5    3

output.txt
START 100
READ N1
READ N2
DECR N1,N2
STOP
N1 DS 1
N2 DS 2
END

We will require this files while running macro pass II
+

No comments:

Post a Comment