Wednesday, 6 March 2013

New 2 pass assembler Program

#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],character,add[10],objectcode[10];
int flag,flag1,locctr,location,loc;
clrscr();
fp1=fopen("out.dat","r");
fp2=fopen("p2out.dat","w");
fp3=fopen("optab.dat","r");
fp4=fopen("symtab.dat","r");
if(fp1==NULL)
{
printf("The file cannot be open");
}
if(fp2==NULL)
{
printf("The file cannot be open");
}
if(fp3==NULL)
{
printf("The file cannot be open");
}
if(fp4==NULL)
{
printf("The file cannot be open");
}
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)
{
flag=1;
break;
}
}
if(flag1==1)
{
itoa(loc,add,10);
strcpy(objectcode,strcat(mnemonic,add));
}
}
else if(strcmp(opcode,"BYTE")==0 || strcmp(opcode,"WORD")==0)
{
if((operand[0]=='C')||(operand[0]=='X'))
{
character=operand[2];
itoa(character,add,16);
strcpy(objectcode,add);
}
else
{
itoa(atoi(operand),add,10);
strcpy(objectcode,add);
}
}
strcpy(objectcode,"\0");
fprintf(fp2,"%s\t%s\t%s\t%s\n",label,opcode,operand,locctr,objectcode);
fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand);
}
fprintf(fp2,"%s\t%s\t%s\n",label,opcode,operand,locctr);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}
INPUT FILE:
Out.dat
** START 2000
2000 ** LDA FIVE
2003 ** STA ALPHA
2006 ** LDCH CHARZ
2009 ** STCH C1
2012 ALPHA RESW 1
2015 FIVE WORD 5
2018 CHARZ BYTE C’Z’
2019 C1 RESB 1
2020 ** END **
Optab.dat
START *
LDA 03
STA 0F
LDCH 53
STCH 57
END *
Symtab.dat
ALPHA 2012
FIVE 2015
CHARZ 2018
C1 2019
OUTPUT FILE:
P2out.dat
** START 2000
** LDA FIVE 2000 032015
** STA ALPHA 2003 0F2012
** LDCH CHARZ 2006 532018
** STCH C1 2009 572019
ALPHA RESW 1 2012
FIVE WORD 5 2015 5
CHARZ BYTE C'Z' 2018 5a
C1 RESB 1 2019
** END ** 2020

No comments:

Post a Comment

There was an error in this gadget