IMPLEMENTATION OF PASS TWO OF A TWO PASS ASSEMBLER
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE
*fp1,*fp2,*fp3,*fp4;
char
label[10],opcode[10],operand[10],symbol[10],code[10];
char mnemonic[5],ch,add[10],objcode[10];
int
flag,flag1,locctr,location,loc;
clrscr();
printf("\nImplementation
Of pass Two Of A Two Pass Assembler:");
printf("\n---------------------------------------------------\n\n");
fp1=fopen("out.txt","r");
fp2=fopen("twoout.txt","w");
fp3=fopen("optab.txt","r");
fp4=fopen("symtb1.txt","r");
fscanf(fp1,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
fprintf(fp2,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand);
}
while(strcmp(opcode,"END")!=0)
{
flag=0;
fscanf(fp3,"%s%s",code,mnemonic);
while(strcmp(code,"END")!=0)
{
if((strcmp(opcode,code)==0) &&
(strcmp(mnemonic,"*"))!=0)
{
flag=1; break;
}
fscanf(fp3,"%s%s",code,mnemonic);
}
if(flag==1)
{
flag1=0;
rewind(fp4);
while(!feof(fp4))
{
fscanf(fp4,"%s%d",symbol,&loc);
if(strcmp(symbol,operand)==0)
{
flag1=1;
break;
}
}
if(flag1==1)
{
itoa(loc,add,10);
strcpy(objcode,strcat(mnemonic,add));
}
}
else if(strcmp(opcode,"BYTE")==0 ||
strcmp(opcode,"WORD")==0)
{
if((operand[0]=='C') || (operand[0]=='X'))
{
ch=operand[2];
itoa(ch,add,16);
strcpy(objcode,add);
}
else
{
itoa(atoi(operand),add,10);
strcpy(objcode,add);
}
}
else
strcpy(objcode,"\0");
fprintf(fp2,"%s\t%s\t%s\t%d\t%s\n",label,opcode,operand,locctr,objcode);
fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand);
}
fprintf(fp2,"%s\t%s\t%s\t%d\n",label,opcode,operand,locctr);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
printf("Program
Completed Successfully...\n");
getch();
}
OUTPUT:
Implementation Of pass Two Of A Two Pass Assembler:
---------------------------------------------------------------------
Program Completed Successfully...
INPUT FILES:
OUT.TXT
** START 1000
1000 ** LDA VAR1
1003 ** MUL VAR2
1006 **
STA ANS
1009 VAR1
WORD 11
1012 VAR2
WORD 12
1015 ANS
RESW 1
1018 ** END **
OPTAB.TXT
START *
ADD 18
DIV
24
LDA 00
LDCH 50
MUL 20
STA 0C
STCH 54
SUB 1C
END *
SYMTB1.TXT
VAR1 1009
VAR2 1012
ANS 1015
OUTPUT FILES:
TWOOUT.TXT
** START 1000
** LDA VAR1
1000
001009
** MUL VAR2
1003
201012
** STA ANS
1006
0C1015
VAR1 WORD 11 1009 11
VAR2 WORD 12 1012 12
ANS RESW 1 1015
** END ** 1018
No comments:
Post a Comment
Thanks for your valuable comments