Single Pass Assembler Program
Single Pass Assembler Program
Question - Implement a single pass assembler.
Program
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int count[20];
void main()
{
FILE *f1,*f2,*f3,*f4;
int linenumber,locctr,f;
char lbl[10],mne[10],opd[10],ch,mneu1[10],sval[10];
char sadr[10],slbl[10],op1[10],label[10];
void wordcount();
printf("Word count for Input Program:");
wordcount();
printf("\n Output \n");
printf("\n Source Code \t Object code \n\n");
f1=fopen("INPUT.C","r");
f2=fopen("SYMTAB.C","w+");
f4=fopen("INTER.C","w");
fscanf(f1,"%s %s %x\n",lbl,mne,&locctr);
linenumber=2;
while(!feof(f1))
{
if(count[linenumber]==1)
{
fscanf(f1,"%s\n",mne);
fprintf(f4,"%x\t%s",locctr,mne);
}
if(count[linenumber]==2)
{
fscanf(f1,"%s%s\n",mne,opd);
fscanf(f4,"%X \t %s \t %s \n",locctr,mne,opd);
printf("%s\t%s\t",mne,opd);
f3=fopen("OPCODE.C","r");
while(!feof(f3))
{
fscanf(f3,"%s %s \n",mneu1,op1);
if(strcmp(mne,mneu1)==0)
printf("%s\t",op1);
}
fclose(f3);
f=0;
rewind(f2);
while(!feof(f2))
{
fscanf(f2,"%s %s %s \n",sadr,slbl,sval);
if(strcmp(opd,slbl)==0)
{
printf("%s\n\n",sadr);
f=1;
}
}
if(f==0)
printf("0000\n");
}
if(count[linenumber]==3)
{
fscanf(f1,"%s %s %s \n",lbl,mne,opd);
fprintf(f4,"%X \t %s \t %s \t %s \n",locctr,lbl,mne,opd);
fprintf(f2,"%X \t %s \t %s \n", locctr, lbl,opd);
if((strcmp(mne,"RESW")==0) || (strcmp(mne,"RESB")==0))
printf("%s \t %s \t 00 \t 000 %s \n\n",lbl,mne,opd);
else
printf("%s \t %s \t 00 \t 000 %s \n\n",lbl,mne,opd);
}
linenumber+=1;
if(strcmp(mne,"WORD")==0)
locctr+=3;
else if(strcmp(mne,"BYTE")==0)
locctr+=strlen(opd);
else if(strcmp(mne,"RESW)==0)
locctr+=3 * (atoi(opd));
else if(strcmp(mne,"RESB")==0)
locctr+=atoi(opd);
else
locctr+=3;
}
fclose(f1);
fclose(f2);
fclose(f4);
getch();
}
void wordcount()
{
FILE *f3;
char c;
int word=0,i=1;
printf("\n Word Count");
f3=fopen("INPUT.C","r");
c=fgetc(f3);
while(c!=EOF)
{
if(c==' ')
word+=1;
if(c=='\n')
{
word+=1;
count[i]=word;
printf("\n No.of words in %d:%d",r,word);
i+=1;
word=0;
}
c=fgetc(f3);
}
fclose(f3);
}
INPUT.c
WC START 1000
FIRST WORD 5
SECOND WORD 6
THIRD RESW 1
LDA FIRST
ADD SECOND
STA THIRD
END
OPCODE.c
LDA 00
ADD 18
SUB 1C
STA 0C
SYMTAB.c
FIRST 1009
SECOND 100C
THIRD 100F
INTER.c
1000 LDA FIRST
1003 ADD SECOND
1006 STA THIRD
1009 FIRST WORD
100C SECOND WORD
100F THIRD RESW
1012 END
1015 ENDL
Download Source Code
Note - Source code for two other files namely "SYMTAB.c" and "INTER.c" will be uploaded soon...
Program
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int count[20];
void main()
{
FILE *f1,*f2,*f3,*f4;
int linenumber,locctr,f;
char lbl[10],mne[10],opd[10],ch,mneu1[10],sval[10];
char sadr[10],slbl[10],op1[10],label[10];
void wordcount();
printf("Word count for Input Program:");
wordcount();
printf("\n Output \n");
printf("\n Source Code \t Object code \n\n");
f1=fopen("INPUT.C","r");
f2=fopen("SYMTAB.C","w+");
f4=fopen("INTER.C","w");
fscanf(f1,"%s %s %x\n",lbl,mne,&locctr);
linenumber=2;
while(!feof(f1))
{
if(count[linenumber]==1)
{
fscanf(f1,"%s\n",mne);
fprintf(f4,"%x\t%s",locctr,mne);
}
if(count[linenumber]==2)
{
fscanf(f1,"%s%s\n",mne,opd);
fscanf(f4,"%X \t %s \t %s \n",locctr,mne,opd);
printf("%s\t%s\t",mne,opd);
f3=fopen("OPCODE.C","r");
while(!feof(f3))
{
fscanf(f3,"%s %s \n",mneu1,op1);
if(strcmp(mne,mneu1)==0)
printf("%s\t",op1);
}
fclose(f3);
f=0;
rewind(f2);
while(!feof(f2))
{
fscanf(f2,"%s %s %s \n",sadr,slbl,sval);
if(strcmp(opd,slbl)==0)
{
printf("%s\n\n",sadr);
f=1;
}
}
if(f==0)
printf("0000\n");
}
if(count[linenumber]==3)
{
fscanf(f1,"%s %s %s \n",lbl,mne,opd);
fprintf(f4,"%X \t %s \t %s \t %s \n",locctr,lbl,mne,opd);
fprintf(f2,"%X \t %s \t %s \n", locctr, lbl,opd);
if((strcmp(mne,"RESW")==0) || (strcmp(mne,"RESB")==0))
printf("%s \t %s \t 00 \t 000 %s \n\n",lbl,mne,opd);
else
printf("%s \t %s \t 00 \t 000 %s \n\n",lbl,mne,opd);
}
linenumber+=1;
if(strcmp(mne,"WORD")==0)
locctr+=3;
else if(strcmp(mne,"BYTE")==0)
locctr+=strlen(opd);
else if(strcmp(mne,"RESW)==0)
locctr+=3 * (atoi(opd));
else if(strcmp(mne,"RESB")==0)
locctr+=atoi(opd);
else
locctr+=3;
}
fclose(f1);
fclose(f2);
fclose(f4);
getch();
}
void wordcount()
{
FILE *f3;
char c;
int word=0,i=1;
printf("\n Word Count");
f3=fopen("INPUT.C","r");
c=fgetc(f3);
while(c!=EOF)
{
if(c==' ')
word+=1;
if(c=='\n')
{
word+=1;
count[i]=word;
printf("\n No.of words in %d:%d",r,word);
i+=1;
word=0;
}
c=fgetc(f3);
}
fclose(f3);
}
INPUT.c
WC START 1000
FIRST WORD 5
SECOND WORD 6
THIRD RESW 1
LDA FIRST
ADD SECOND
STA THIRD
END
OPCODE.c
LDA 00
ADD 18
SUB 1C
STA 0C
SYMTAB.c
FIRST 1009
SECOND 100C
THIRD 100F
INTER.c
1000 LDA FIRST
1003 ADD SECOND
1006 STA THIRD
1009 FIRST WORD
100C SECOND WORD
100F THIRD RESW
1012 END
1015 ENDL
Download Source Code
Note - Source code for two other files namely "SYMTAB.c" and "INTER.c" will be uploaded soon...
0 comments: