You are on page 1of 4

PASS1 OF TWO PASS ASSEMBLER

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int count[20];
void main()
{
FILE *f1,*f2,*f3;
int linenumber,locctr;
char lbl[10],mne[10],ch,opd[10];
void wordcount();
clrscr();
printf("\nWORD COUNT FOR INPUT PROGRAM\n");
wordcount();
f1=fopen("INPUT.c","r");
f2=fopen("SYMTABl.c","w");
f3=fopen("INTERMED.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(f3,"%x\t%s\n",locctr,mne);
}
if(count[linenumber]==2)
{
fscanf(f1,"%s%s\n",mne,opd);
fprintf(f3,"%x\t%s\t%s\n",locctr,mne,opd);
}
if(count[linenumber]==3)
{
fscanf(f1,"%s%s%s\n",lbl,mne,opd);
fprintf(f3,"%x\t%s\t%s\t%s\n",locctr,lbl,mne,opd);
fprintf(f2,"\n%s\t%x\n",lbl,locctr);
}
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);
getch();
}
void wordcount()
{
FILE *f3;
char c;
int word=0,i=1;
f3=fopen("input.c","r");
c=fgetc(f3);
while(c!=EOF)
{
if(c==' ')
word+=1;
if(c=='\n')
{
word+=1;
count[i]=word;
printf("\nNo. of words in linenumber %d : %d\n",i,word);
i+=1;
word=0;
}
c=fgetc(f3);
}
fclose(f3);
}

INPUT:
INPUT.C
WC START 1000
LDA FIRST
ADD SECOND
STA THIRD
FIRST WORD 5
SECOND WORD 6
THIRD RESW 1
END
ENDL

OUTPUT:
WORD COUNT FOR INPUT PROGRAM
No. of words in linenumber 1:3
No. of words in linenumber 2:2
No. of words in linenumber 3:2
No. of words in linenumber 4:2
No. of words in linenumber 5:3
No. of words in linenumber 6:3
No. of words in linenumber 7:3
No. of words in linenumber 8:1
SYMBOLTABLE.C:
FIRST1009
SECOND 100c
THIRD

100f

INTERMEDIATE.C:
1000
1003
1006
1009
100c
100f
1012
11a1

LDA FIRST
ADD SECOND
STA THIRD
FIRSTWORD
5
SECOND WORD 6
THIRD
RESW1
END
ENDL

You might also like