Friday 17 August 2012

System Software program 2:


Implementation of Two pass of a two pass Assembler:

Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char a[10],ad[10],label[10],opcode[10],operand[10],mnemonic[10],symbol[10];
int i, address,code,add,len,actuallen;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("AssmList.txt","w");
fp2=fopen("symtab.txt","r");
fp3=fopen("intermediate.txt","r");
fp4=fopen("optab.txt","r");
fscanf(fp3,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"start")==0)
{
fprintf(fp1,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);
}
while(strcmp(opcode,"end")!=0)
{
if(strcmp(opcode,"byte")==0)
{
fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);
len=strlen(operand);
actuallen=len-3;
for(i=2;i<(actuallen+2);i++)
{
itopa(operand[i],ad,16);
fprintf(fp1,"%s",ad);
}
fprintf(fp1,"\n");
}
else if(strcmp(opcode,"word")==0)
{
len=strlen(operand);
itoa(atoi(operand),a,10);
fprintf(fp1,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a);
}
else if((strcmp(opcode,"resb")==0)||(strcmp(opcode,"resw")==0))
{
fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
}
else
{
rewind(fp4);
fscanf(fp4,"%s%d",mnemonic,&code);
while(strcmp(opcode,mnemonic)!=0)
fscanf(fp4,"%s%d",mnemonic,&code);
if(strcmp(operand,"**")==0)
{
fprintf(fp1,"%d\t%s\t%s\t0000%d\n",address,label,opcode,operand,code);
}
else
{
rewind(fp2);
fscanf(fp2,"%s%d",symbol,&add);
while(strcmp(operand,symbol)!=0)
fscanf(fp2,"%s%d",symbol,&code);
}
fprintf(fp1,"%d\t%s\%s\t%s\t%d\n",address,label,opcode,operand,code);
}
}
fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);
}
fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);
printf("FINISHED");
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}

No comments:

Post a Comment