Friday, September 6, 2013

Pass1 of Two Pass-assembler stimulation in C


Pass1 of Two Pass-assembler stimulation in C language
Program:
#include<stdio.h>
#include<conio.h>
struct node
{
char str[100];
}n[100];
void main()
{
int v,r,i=0,k=0,c=0;
int q=0,a=0;
int p,num=0;
char fil[100];
char op[100],dig[100];
char line[100],num1[100];
char str[100];
char str1[100];
char ch;
FILE *f1,*f2,*f3,*fl;
clrscr();
printf("\nEnter the assembly language program...");
scanf("%s",fil);
f1=fopen(fil,"r+");
fl=fopen(fil,"r");
f2=fopen("optab.txt","w");
f3=fopen("symtab.txt","w");
if(f1==NULL)
            printf("File cannot be opened...");
else
{
while((ch=fgetc(f1))!=EOF)
            {
            if((isalpha(ch))&&(isupper(ch)))
                        {
                        op[i]=ch;
                        i++;
                        }
            if(isdigit(ch))
                        {
                        dig[k]=ch;
                        k++;
                        }
            if(ch=='\n' && c==0)
                        {
                        dig[k]='\0';
                        op[i]='\0';
                        if(strcmp(op,"START")==0)
                                    {
                                    v=atoi(dig);
                                    }
                        c=1;
                        k=0;
                        i=0;
                        }
            if(c==1)
                        {
                        if(ch==' ' || ch=='\n')
                                    {
                                    op[i]='\0';
                                    dig[k]='\0';
                                    if(strcmp(op,"LDA")==0)
                                                {
                                                fprintf(f2,"%s %d\n",op,v);
                                                }
                                    if(strcmp(op,"ADD")==0)
                                                {
                                                v=v+3;
                                                fprintf(f2,"%s %d\n",op,v);
                                                }
                                    if(strcmp(op,"STA")==0)
                                                {
                                                v=v+3;
                                                fprintf(f2,"%s %d\n",op,v);
                                                }
                                    if(strcmp(op,"SUB")==0)
                                                {
                                                v=v+3;
                                                fprintf(f2,"%s %d\n",op,v);
                                                }
                                    if(strcmp(op,"END")==0)
                                                {
                                                fprintf(f2,"%s %d\n",op,v);
                                                }
                                    if(strcmp(op,dig)!=0 && strcmp(op,"A")!=0 && strcmp(op,"B")!=0 && strcmp(op,"SUB")!=0 && strcmp(op,"LDA")!=0 && strcmp(op,"STA")!=0 && strcmp(op,"ADD")!=0 && strcmp(op,"BYTE")!=0 && strcmp(op,"WORD")!=0 && strcmp(op,"RESB")!=0 && strcmp(op,"RESW")!=0 && strcmp(op,"END")!=0)
                                                {
                                                strcpy(n[q].str,op);
                                                q++;
                                                }
                                    i=0;
                                    k=0;
                                    r=0;
                                    }
                        }
            }
}
while(fgets(line,sizeof(line),fl))
            {
            if(strstr(line,"WORD"))
                        {
                        v=v+3;
                        for(a=0;a<q;a++)
                                    {
                                    if(strstr(line,n[a].str))
                                                {
                                                fprintf(f3,"%s %d\n",n[a].str,v);
                                                break;
                                                }
                                    }
                        }
            if(strstr(line,"BYTE"))
                        {
                        for(a=0;a<q;a++)
                                    {
                                    if(strstr(line,n[a].str))
                                                {
                                                for(r=0;str[r]!='\n';r++)
                                                            {
                                                            if(isdigit(str[r]))
                                                                        {
                                                                        num=atoi(&str[r]);
                                                                        break;
                                                                        }
                                                            }
                                                v=v+num;
                                                fprintf(f3,"%s %d\n",n[a].str,v);
                                                break;
                                                }
                                    }
                        }
            if(strstr(line,"RESB"))
                        {
                        for(a=0;a<q;a++)
                                    {
                                    if(strstr(line,n[a].str))
                                                {
                                                for(r=0;str[r]!='\n';r++)
                                                            {
                                                            if(isdigit(str[r]))
                                                                        {
                                                                        num=atoi(&str[r]);
                                                                        break;
                                                                        }
                                                            }
                                                v=(v+num)*3;
                                                fprintf(f3,"%s %d\n",n[a].str,v);
                                                break;
                                                }
                                    }
                        }
            if(strstr(line,"RESW"))
                        {
                        v=(v+3)*3;
                        for(a=0;a<q;a++)
                                    {
                                    if(strstr(line,n[a].str))
                                                {
                                                fprintf(f3,"%s %d\n",n[a].str,v);
                                                break;
                                                }
                                    }
                        }
            }
printf("\nSymbol table and Optable was created successfully...");
fclose(fl);
fclose(f3);
fclose(f2);
fclose(f1);
getch();
}
Output:
Enter the assembly language program...alp.txt
Symbol table and Optable was created successfully...

//ALP.txt
START 2000
LDA FIRST
ADD A
STA SECOND
SUB B
FIRST WORD 5
SECOND WORD 6

//SYMTAB.txt
FIRST 2012
SECOND 2015

//OPTAB.txt
LDA 2000
ADD 2003
STA 2006
SUB 2009





If you want to add additional Keywords just include them in the if condition
For e.g, to include the keyword "MUL", just add an if condition along with other keywords...like
 if(strcmp(op,"MUL")==0)
                                                {
                                                fprintf(f2,"%s %d\n",op,v);
                                                }

Also if you want to add additional registers include them in the if condition along with other registers...
Here I have included registers A and B...For register C add the statement like strcmp(op,"B")!=0 in the if condition.

Don't forget to comment.....

No comments:

Post a Comment