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.....

Program to implement PASS 1 of a two pass Assembler.




#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
struct symtab
{
            char sym[10];
            int  symadd;
};
struct symtab st[20];
void main()
{
            char opcode[10],mnemonic[3],op1[10],op2[10],label[10],code[10],ic[10];
            int locctr,start,length,symcnt=0,symid=0;
            FILE *finput,*foutput,*fsymtab,*foptab;
            clrscr();
            finput=fopen("C:/SP_Prog/SP/INPUT.txt","r");
            fsymtab=fopen("C:/SP_Prog/SP/SYMTAB.txt","w");
            foutput=fopen("C:/SP_Prog/SP/OUTPUT.txt","w");
            foptab=fopen("C:/SP_Prog/SP/OPTAB.txt","r");
            fscanf(finput,"%s%s%s%s",label,opcode,op1,op2);
             char icop1[10],icop2[10];
            if(strcmp(opcode,"START")==0)
            {
                        start=atoi(op1);
                        locctr=start;
                        strcpy(icop1,"(C,");
                        strcat(icop1,op1);
                        strcat(icop1,")");
                        fprintf(foutput,"%s\t\t%s\t\t%s\n","(AD-01)",icop1,"");
                        fscanf(finput,"%s%s%s%s",label,opcode,op1,op2);
            }
            else
            locctr=0;
            int i;
            while(strcmp(opcode,"END")!=0)
            {
                        if(strcmp(opcode,"ORIGIN")==0)
                        {
                                    locctr=atoi(op1);
                                    strcpy(ic,"(AD,03)");

                        }

                        if(strcmp(label,"**")!=0)
                        {
                                    symcnt=symcnt + 1;

                                    if(strcmp(opcode,"EQU")==0)
                                    {
                                                if(isdigit(op1[0]));
                                                {
                                                            fprintf(fsymtab,"%s\t%d\n",label,locctr);

                                                }
                                    }
                                    symid=symid+1;
                                    fprintf(fsymtab,"%d\t\t%s\t\t%d\n",symid,label,locctr);
                          }


                        rewind(foptab);
                        fscanf(foptab,"%s%s",code,ic);

                        while(strcmp(code,NULL)!=0)
                        {

                                    if(strcmp(opcode,code)==0)
                                    {
                                                locctr+=1;
                                                break;
                                    }
                                    fscanf(foptab,"%s%s",code,ic);


                        }
                        char icop1[5];
                        if(isdigit(op1[0])!=0)
                        {
                                    strcpy(icop1,"(C,");
                                    strcat(icop1,op1);
                                    strcat(icop1,")");
                        }

                        else if(strcmp(op1,"AREG")==0)
                        {
                                    strcpy(icop1,"(1)");
                        }
                        else if(strcmp(op1,"BREG")==0)
                        {
                                    strcpy(icop1,"(2)");
                        }
                        else if(strcmp(op1,"CREG")==0)

                        {
                                    strcpy(icop1,"(3)");
                        }
                        else if(strcmp(op1,"DREG")==0)
                        {
                                    strcpy(icop1,"(4)");
                        }
                        else
                        {
                                    strcpy(icop1,op1);
                        }
                        if(strcmp(op2,"**")==0)
                                    strcpy(icop2,"");
                         else
                                    strcpy(icop2,op2);

                        fprintf(foutput,"%s\t\t%s\t\t%s\n",ic,icop1,icop2);

                        fscanf(finput,"%s%s%s%s",label,opcode,op1,op2);
            }
                        fscanf(foutput,"%s%s%s%s",label,opcode,op1,op2);

            length=locctr-start;
            printf("The length of the program is %d",length);
            fclose(finput);
            fclose(fsymtab);
            fclose(foutput);
            fclose(foptab);
            getch();

}

MASM for windows7

Hi Guys Here is the Masm for Win7  64bit and other 64 bit computers.
Just Download 2 file as given in below and follow the instructions . Enjoyyyyy



INSTRUCTIONS:
1:Download both
DOSBox0.74-win32-installer.exe & 8086_Assembler.zip
2:Run 8086 assembler.zip&extracted in c:\ drive.
3:Then run DOSBOX0.74 &install it.
4:After installing the DOSBOX0.74 application a black scrren will seen.
5:Inside this screen type mount c c:\8086 after enter key press and type c:
6:then type edit (your program name).asm
7:A blue screen will seen.type your program. That's it..................
Hi