Assembler Pass 2 C- code
//ASSEMBLER PASS 2 C-SOURCE CODE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
struct mot
{
char mn[6];
int class;
char op[3];
};
struct symt
{
char sym[10];
int address;
int length;
}st[20];
struct litt
{
int literal;
int address;
}lt[20];
int ist=0;
int ilt=0;
int lc;
static struct mot mt[28]={
{"STOP",1,"00"},{"ADD",1,"01"},{"SUB",1,"02"},
{"MULT",1,"03"},{"MOVER",1,"04"},{"MOVEM",1,"05"},
{"COMP",1,"06"},{"BC",1,"07"},{"DIV",1,"08"},
{"READ",1,"09"},{"PRINT",1,"10"},
{"START",3,"01"},{"END",3,"02"},{"ORIGIN",3,"03"},
{"EQU",3,"04"},{"LTORG",3,"05"},
{"DS",2,"01"},{"DC",2,"02"},
{"AREG",4,"01"},{"BREG",4,"02"},{"CREG",4,"03"},
{"EQ",5,"01"},{"LT",5,"02"},{"GT",5,"03"},{"LE",5,"04"},
{"GE",5,"05"},{"NE",5,"06"},{"ANY",5,"07"}
};
void main()
{
char fname[10],ch,buff[20],i,s1[8],s2[8],s3[8],k=0,s4[8],s5[8],s6[8],tokencount=0,j;
FILE *fp,*fp1;
clrscr();
printf("\nEnter no.of symbol table entries:");
scanf("%d",&ist);
for(i=0;i<ist;i++)
{
printf("\nEnter address of symbol no.%d:",i+1);
scanf("%d",&st[i].address);
}
printf("\nEnter no.of literal table entries:");
scanf("%d",&ilt);
for(i=0;i<ilt;i++)
{
printf("\nEnter address of literal no.%d:",i+1);
scanf("%d",<[i].address);
}
printf("\nEnter the name of file containing IC:");
scanf("%s",fname);
fp=fopen(fname,"r");
ch=fgetc(fp);
while(!feof(fp))
{
printf("%c",ch);
ch=fgetc(fp);
}
fclose(fp);
fp=fopen(fname,"r");
getch();
printf("\n\nMC code");
while(!feof(fp))
{ i=0;
buff[i]=fgetc(fp);
//printf("\nloop%d\n",k);
// getch();
k++;
fp1=fopen("mc.txt","w");
while(buff[i]!='\n' && buff[i]!=EOF)
{
if(!isalnum(buff[i]))
{
buff[i]=' ';
}
else
{
buff[i]=toupper(buff[i]);
}
i++;
buff[i]=fgetc(fp);
}
buff[i]='\0';
tokencount=sscanf(buff,"%s%s%s%s%s%s",s1,s2,s3,s4,s5,s6);
/*
for debugging
getch();
if(tokencount==6)
{
printf("\ncount:%d s1:%s s2:%s s3:%s s4:%s s5:%s s6:%s",tokencount,s1,s2,s3,s4,s5,s6);
}
else
if(tokencount==4)
{
printf("\ncount:%d s1:%s s2:%s s3:%s s4:%s",tokencount,s1,s2,s3,s4);
}
*/
fprintf(fp1,"\n");
printf("\n");
if((strcmp("AD",s1)==0) && (strcmp("01",s2)==0))
{
lc=atoi(s4);
continue;//jump back to start of while loop
}
if((strcmp("IS",s1)==0))
{
printf("%d\t%s",lc,s2);
fprintf(fp1,"%d\t%s",lc,s2);
if(tokencount==2)
{
printf("\t00\t00");
fprintf(fp1,"\t00\t00");
lc++;
continue;
}
else
{
if(tokencount==4)
{
strcpy(s5,s3);
strcpy(s6,s4);
printf("\t00");
fprintf(fp1,"\t00");
}
else
printf("\t%s",s4);
fprintf(fp1,"\t%s",s4);
if(strcmp("L",s5)==0)
{
printf("\t%d",lt[atoi(s6)].address);
fprintf(fp1,"\t%d",lt[atoi(s6)].address);
}
else
{
if(strcmp("S",s5)==0)
{
printf("\t%d",st[atoi(s6)].address);
fprintf(fp1,"\t%d",st[atoi(s6)].address);
}
}
}
lc++;
continue;
}
if((strcmp("DL",s1)==0))
{
printf("%d",lc);
fprintf(fp1,"%d",lc);
if(strcmp("01",s2)==0)
{
lc=lc+atoi(s4);
}
else
{
printf("\t00\t00\t%s",s4);
fprintf(fp1,"\t00\t00\t%s",s4);
lc=lc+1;
}
}
}//end of !feof while loop
fclose(fp1);
fclose(fp);
getch();
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
struct mot
{
char mn[6];
int class;
char op[3];
};
struct symt
{
char sym[10];
int address;
int length;
}st[20];
struct litt
{
int literal;
int address;
}lt[20];
int ist=0;
int ilt=0;
int lc;
static struct mot mt[28]={
{"STOP",1,"00"},{"ADD",1,"01"},{"SUB",1,"02"},
{"MULT",1,"03"},{"MOVER",1,"04"},{"MOVEM",1,"05"},
{"COMP",1,"06"},{"BC",1,"07"},{"DIV",1,"08"},
{"READ",1,"09"},{"PRINT",1,"10"},
{"START",3,"01"},{"END",3,"02"},{"ORIGIN",3,"03"},
{"EQU",3,"04"},{"LTORG",3,"05"},
{"DS",2,"01"},{"DC",2,"02"},
{"AREG",4,"01"},{"BREG",4,"02"},{"CREG",4,"03"},
{"EQ",5,"01"},{"LT",5,"02"},{"GT",5,"03"},{"LE",5,"04"},
{"GE",5,"05"},{"NE",5,"06"},{"ANY",5,"07"}
};
void main()
{
char fname[10],ch,buff[20],i,s1[8],s2[8],s3[8],k=0,s4[8],s5[8],s6[8],tokencount=0,j;
FILE *fp,*fp1;
clrscr();
printf("\nEnter no.of symbol table entries:");
scanf("%d",&ist);
for(i=0;i<ist;i++)
{
printf("\nEnter address of symbol no.%d:",i+1);
scanf("%d",&st[i].address);
}
printf("\nEnter no.of literal table entries:");
scanf("%d",&ilt);
for(i=0;i<ilt;i++)
{
printf("\nEnter address of literal no.%d:",i+1);
scanf("%d",<[i].address);
}
printf("\nEnter the name of file containing IC:");
scanf("%s",fname);
fp=fopen(fname,"r");
ch=fgetc(fp);
while(!feof(fp))
{
printf("%c",ch);
ch=fgetc(fp);
}
fclose(fp);
fp=fopen(fname,"r");
getch();
printf("\n\nMC code");
while(!feof(fp))
{ i=0;
buff[i]=fgetc(fp);
//printf("\nloop%d\n",k);
// getch();
k++;
fp1=fopen("mc.txt","w");
while(buff[i]!='\n' && buff[i]!=EOF)
{
if(!isalnum(buff[i]))
{
buff[i]=' ';
}
else
{
buff[i]=toupper(buff[i]);
}
i++;
buff[i]=fgetc(fp);
}
buff[i]='\0';
tokencount=sscanf(buff,"%s%s%s%s%s%s",s1,s2,s3,s4,s5,s6);
/*
for debugging
getch();
if(tokencount==6)
{
printf("\ncount:%d s1:%s s2:%s s3:%s s4:%s s5:%s s6:%s",tokencount,s1,s2,s3,s4,s5,s6);
}
else
if(tokencount==4)
{
printf("\ncount:%d s1:%s s2:%s s3:%s s4:%s",tokencount,s1,s2,s3,s4);
}
*/
fprintf(fp1,"\n");
printf("\n");
if((strcmp("AD",s1)==0) && (strcmp("01",s2)==0))
{
lc=atoi(s4);
continue;//jump back to start of while loop
}
if((strcmp("IS",s1)==0))
{
printf("%d\t%s",lc,s2);
fprintf(fp1,"%d\t%s",lc,s2);
if(tokencount==2)
{
printf("\t00\t00");
fprintf(fp1,"\t00\t00");
lc++;
continue;
}
else
{
if(tokencount==4)
{
strcpy(s5,s3);
strcpy(s6,s4);
printf("\t00");
fprintf(fp1,"\t00");
}
else
printf("\t%s",s4);
fprintf(fp1,"\t%s",s4);
if(strcmp("L",s5)==0)
{
printf("\t%d",lt[atoi(s6)].address);
fprintf(fp1,"\t%d",lt[atoi(s6)].address);
}
else
{
if(strcmp("S",s5)==0)
{
printf("\t%d",st[atoi(s6)].address);
fprintf(fp1,"\t%d",st[atoi(s6)].address);
}
}
}
lc++;
continue;
}
if((strcmp("DL",s1)==0))
{
printf("%d",lc);
fprintf(fp1,"%d",lc);
if(strcmp("01",s2)==0)
{
lc=lc+atoi(s4);
}
else
{
printf("\t00\t00\t%s",s4);
fprintf(fp1,"\t00\t00\t%s",s4);
lc=lc+1;
}
}
}//end of !feof while loop
fclose(fp1);
fclose(fp);
getch();
}
0 comments:
Post a Comment