samidha 0 Newbie Poster
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define M 20
#define hr printf("\n----------------------------");
#define br printf("\n");

int mptr=0,pptr=0,sptr=0;

struct input_code
{
	char lbl[M];
	char instr[M];
	char op1[M];
	char op2[M];
}input[M];

struct opcode_table
{
	int num;
	char name[M];
	int code;
	struct opcode_table *chain;
}mot[26],pot[26];

struct symbol_table
{
	int num;
	char sym[M];
	int addr;
	struct symbol_table *chain;
}stab[26];

void read_input();
void process_optab(int);
void process_symtab();
void hash_optab(char *,int,int);
void hash_symtab(char *,int);
int check_symtab(char *,int *,int *);
int ismot(char *);
int ispot(char *);
int lit_val(char *);
int isreg(char *);
void print_littab();
void pass_1();

void main()
{
	clrscr();
	read_input();
	process_optab(0);
	process_optab(1);
	getch();
	pass_1();
	process_symtab();
	print_littab();
	getch();
}

void read_input()
{
	FILE *fp;
	int i=0;
	fp=fopen("INPUT2.TXT","r+");
	if(fp==NULL)
	{
		printf("\nInput file open error! Press a key to exit...");
		getch();
		exit(0);
	}
	hr
	printf("\nINPUT FILE");
	hr
	while(!feof(fp))
	{
		fscanf(fp,"%s%s%s%s",input[i].lbl,input[i].instr,input[i].op1,input[i].op2);
		flushall();
		printf("\n%-8s%-8s%-8s%-8s",input[i].lbl,input[i].instr,input[i].op1,input[i].op2);
		i++;
	}
	br
	fclose(fp);
}

void hash_optab(char *str,int code,int x)
{
	int key,i;
	struct opcode_table *temp,*temp2,*cur_tab[26];
	key=str[0]-65;
	if(x==0)
		for(i=0;i<26;i++)
			cur_tab[i]=&mot[i];
	else if(x==1)
		for(i=0;i<26;i++)
			cur_tab[i]=&pot[i];
	if(!strcmp(cur_tab[key]->name,""))
	{
		cur_tab[key]->num=mptr++;
		strcpy(cur_tab[key]->name,str);
		cur_tab[key]->code=code;
		cur_tab[key]->chain=NULL;
//		printf("\n%d\t%s\t%d\t%s",cur_tab[key]->num,cur_tab[key]->name,cur_tab[key]->code,cur_tab[key]->chain);
	}
	else
	{
		temp=cur_tab[key];
		while(temp->chain!=NULL)
			temp=temp->chain;
		temp2=(struct opcode_table *)malloc(sizeof(struct opcode_table));
		temp2->num=pptr++;
		strcpy(temp2->name,str);
		temp2->code=code;
		temp2->chain=NULL;
		temp->chain=temp2;
//		printf("\n%d\t%s\t%d\t%s\t***",temp->num,temp->name,temp->code,temp->chain);
	}
}

void process_optab(int x)
{
	FILE *tab;
	struct opcode_table *temp,*cur_tab[26];
	int i=0,code;
	char instr[M];
	hr
	if(x==0)
	{
		printf("\nMOT");
		hr
		tab=fopen("MTAB.TXT","r+");
		if(tab==NULL)
			printf("\nMOT open error!");
	}
	else if(x==1)
	{
		printf("\nPOT");
		hr
		tab=fopen("PTAB.TXT","r+");
		if(tab==NULL)
			printf("\nPOT open error!");

	}
	while(!feof(tab))
	{
		fscanf(tab,"%s%d",instr,&code);
		hash_optab(instr,code,x);
	}
	if(x==0)
		for(i=0;i<26;i++)
			cur_tab[i]=&mot[i];
	else if(x==1)
		for(i=0;i<26;i++)
			cur_tab[i]=&pot[i];
	i=0;
	while(i<26)
	{
		if(strcmp(cur_tab[i]->name,""))
		{
			printf("\n%-7s%d",cur_tab[i]->name,cur_tab[i]->code);
			temp=cur_tab[i]->chain;
			while(temp!=NULL)
			{
				printf("\n%-7s%d",temp->name,temp->code);
				temp=temp->chain;
			}
		}
		i++;
	}
	br
	fclose(tab);
}

void process_symtab()
{
	FILE *stb;
	int i;
	struct symbol_table *temp;
	hr
	printf("\nSYMTAB");
	hr
	stb=fopen("STAB.TXT","w+");
	for(i=0;i<26;i++)
	{
		if(strcmp(stab[i].sym,""))
		{
			printf("\n%-8s%d\t%d",stab[i].sym,stab[i].num,stab[i].addr);
			fprintf(stb,"%s\t%d\t%d\n",stab[i].sym,stab[i].num,stab[i].addr);
		}
		temp=stab[i].chain;
		while(temp!=NULL)
		{
			if(strcmp(temp->sym,""))
			{
				printf("\n%-8s%d\t%d",temp->sym,temp->num,temp->addr);
				fprintf(stb,"%s%d\t%d\n",temp->sym,temp->num,temp->addr);
			}
			temp=temp->chain;
		}
	}
	br
	fclose(stb);
}

void hash_symtab(char *str,int lc)
{
	int key;
	struct symbol_table *temp,*temp2;
	key=str[0]-65;
	if(!strcmp(stab[key].sym,""))
	{
		stab[key].num=sptr++;
		strcpy(stab[key].sym,str);
		stab[key].addr=lc;
		stab[key].chain=NULL;
//		printf("\n%d\t%s\t%d\t%s",stab[key].num,stab[key].sym,stab[key].addr,stab[key].chain);
	}
	else
	{
		temp=&stab[key];
		while(temp->chain!=NULL)
			temp=temp->chain;
		temp2=(struct symbol_table *)malloc(sizeof(struct symbol_table));
		temp2->num=sptr++;
		strcpy(temp2->sym,str);
		temp2->addr=lc;
		temp2->chain=NULL;
		temp->chain=temp2;
//		printf("\n%d\t%s\t%d\t%s\t***",temp->num,temp->name,temp->code,temp->chain);
	}
}

int check_symtab(char *str,int *snum,int *saddr)
{
	int i;
	struct symbol_table *temp;
	for(i=0;i<26;i++)
	{
		if(!strcmp(stab[i].sym,str))
		{
			*snum=stab[i].num;
			*saddr=stab[i].addr;
			return 1;
		}
		temp=&stab[i];
		while(temp->chain!=NULL)
		{
			if(!strcmp(temp->sym,str))
			{
				*snum=temp->num;
				*saddr=temp->addr;
				return 1;
			}
			temp=temp->chain;
		}
	}
	return 0;
}

int ismot(char *str)
{
	if(!strcmp(str,"MOVER"))
		return 1;
	else if(!strcmp(str,"MOVEM"))
		return 2;
	else if(!strcmp(str,"ADD"))
		return 3;
	else if(!strcmp(str,"MUL"))
		return 4;
	else if(!strcmp(str,"SUB"))
		return 5;
	else if(!strcmp(str,"DIV"))
		return 6;
	else if(!strcmp(str,"BC"))
		return 7;
	else if(!strcmp(str,"COMP"))
		return 8;
	else return 0;
}

int ispot(char *str)
{
	if(!strcmp(str,"START"))
		return 1;
	else if(!strcmp(str,"END"))
		return 2;
	else if(!strcmp(str,"ORIGIN"))
		return 3;
	else if(!strcmp(str,"LTORG"))
		return 4;
	else if(!strcmp(str,"DC"))
		return 5;
	else if(!strcmp(str,"DS"))
		return 6;
	else return 0;
}

int lit_val(char *str)
{
	int i=0;
	char temp[M];
	while(str[i]!='\'')
	{
		temp[i]=str[i+2];
		i++;
	}
	temp[i]='\0';
	return atoi(temp);
}

int isreg(char *str)
{
	if(!strcmp(str,"AREG"))
		return 1;
	else if(!strcmp(str,"BREG"))
		return 2;
	else if(!strcmp(str,"CREG"))
		return 3;
	else if(!strcmp(str,"DREG"))
		return 4;
	else	return 0;
}

void print_littab()
{
	int num=999,val,pool,addr;
	FILE *fp;
	hr
	printf("\nLITTAB");
	hr
	fp=fopen("LTAB.TXT","r+");
	while(!feof(fp))
	{
		if(num!=999)
			printf("\n%d\t%d\t%d\t%d",num,val,pool,addr);
		fscanf(fp,"%d%d%d%d",&num,&val,&pool,&addr);
	}
	br
	fclose(fp);
}

void pass_1()
{
	FILE *icf,*ltfp,*poolt,*pass2ip;
	int address,length,this_addr,code,size,this_literal,this_entry;
	char this_label[M];
	struct symbol_table	*temp,*sym_str;

// 1. Initialization
	int i=0,j,sym_num,sym_addr,lit_found=0,cur_ltp,value,LITADDR[M];
	int LC=0,PTP=0,LTP=0,RC=0;
//	int STP=0;
	int POOLTAB[M],LITTAB[M];
	POOLTAB[0]=0;

// Heading of IC
	hr
	printf("\nINTERMEDIATE CODE");
	hr

// Input file for Pass 2
	pass2ip=fopen("PASS2IP.TXT","w+");

// Open Intermediate Code file and renew LITTAB file
	icf=fopen("ICODE.TXT","w+");
	ltfp=fopen("LTAB.TXT","w+");
	fclose(ltfp);

// 2. While next statement is not an end statement
	while(strcmp(input[i].instr,"END"))
	{

// a. If label is present
		if(strcmp(input[i].lbl,"-"))
		{
			strcpy(this_label,input[i].lbl);
			if(check_symtab(this_label,&sym_num,&sym_addr)==0)
				hash_symtab(this_label,LC);
		}

		// if next mnemonic then go to step 2f

// b. If an LTORG
		if(!strcmp(input[i].instr,"LTORG"))
		{
			ltfp=fopen("LTAB.TXT","a+");
			for(j=POOLTAB[PTP];j<LTP;j++)
			{
				//process literals to allocate memory
				fprintf(ltfp,"%d\t%d\t%d\t%d\n",j,LITTAB[j],PTP,LC++);
				LITADDR[j]=LC;
			}
			PTP++;
			POOLTAB[PTP]=LTP;
			fclose(ltfp);
			printf("\n(AD,4)");
			fprintf(icf,"(AD,4)\n");
			fprintf(pass2ip,"-\tAD\t4\t-\t-\t-\n");
		}

// c. If a START or ORIGIN statement then
		if(!strcmp(input[i].instr,"START"))
		{
			if(!strcmp(input[i].op2,"-"))
			{
				printf("\n(AD,1)");
				fprintf(icf,"(AD,1)\n");
				fprintf(pass2ip,"-\tAD\t1\t-\t-\t-\n");
			}
			else
			{
				LC=atoi(input[i].op2);
				printf("\n(AD,1)\t(C,%d)",LC);
				fprintf(icf,"(AD,1)\t(C,%d)\n",LC);
				fprintf(pass2ip,"-\tAD\t1\t-\tC\t%d\n",LC);
			}
		}
		else if(!strcmp(input[i].instr,"ORIGIN"))
		{
			check_symtab(input[i].op2,&sym_num,&sym_addr);
			LC=sym_addr;
			printf("\n(AD,3)\t(S,%d)",sym_num);
			fprintf(icf,"(AD,3)\t(S,%d)\n",sym_num);
			fprintf(pass2ip,"-\tAD\t3\t-\tS\t%d\n",sym_num);
		}

// d. If an EQU statement then
		if(!strcmp(input[i].instr,"EQU"))
		{
//			strcpy(this_label,input[i].op2);
			check_symtab(input[i].op2,&sym_num,&sym_addr);
			this_addr=sym_addr;
			j=this_label[0]-65;
			temp=&stab[j];
			while(strcmp(temp->sym,this_label))
			{
				temp=temp->chain;
				if(temp==NULL)
				{
					printf("\nSymbol not found!");
					break;
				}
			}
			temp->addr=this_addr;
		}

// e. If a declaration statement then
		if(!strcmp(input[i].instr,"DC")||!strcmp(input[i].instr,"DS"))
		{
			if(!strcmp(input[i].instr,"DC"))
			{
				code=0;
				size=1;
			}
			else if(!strcmp(input[i].instr,"DS"))
			{
				code=1;
				size=atoi(input[i].op2);
			}
			LC+=size;
			for(j=0;j<=sptr;j++)
			{
				if(!strcmp(stab[j].sym,input[i].lbl))
				{
					stab[j].addr=LC-1;
					break;
				}
			}
			value=atoi(input[i].op2);
			printf("\n(DL,%d)\t(C,%d)",code,value);
			printf("\t\t%d",LC-1);
			fprintf(icf,"(DL,%d)\t(C,%d)\n",code,value);
			fprintf(pass2ip,"%d\tDL\t%d\t-\tC\t%d\n",LC-1,code,value);
		}

// f. If an imperative statement then
		else if(ismot(input[i].instr))
		{
			code=ismot(input[i].instr);
			if(input[i].op2[0]=='=')
			{
				this_literal=lit_val(input[i].op2);
				lit_found=0;
				for(j=POOLTAB[PTP];j<LTP;j++)
				{
					if(this_literal==LITTAB[j])
					{
						lit_found=1;
						break;
					}
				}
				if(lit_found)
					cur_ltp=j;
				else
				{
					LITTAB[LTP]=this_literal;
					cur_ltp=LTP;
					LTP=LTP+1;
				}
				RC=isreg(input[i].op1);
				if(RC)
				{
					printf("\n(IS,%d)\t(%d)(L,%d)",code,RC,cur_ltp);
					fprintf(icf,"(IS,%d)\t(%d)(L,%d)\n",code,RC,cur_ltp);
					fprintf(pass2ip,"%d\tIS\t%d\t%d\tL\t%d\n",LC,code,RC,cur_ltp);
				}
				else
				{
					printf("\n(IS,%d)\t(L,%d)",code,cur_ltp);
					fprintf(icf,"(IS,%d)\t(L,%d)\n",code,cur_ltp);
					fprintf(pass2ip,"%d\tIS\t%d\t-\tL\t%d\n",LC,code,cur_ltp);
				}
			}
			else
			{
				if(!check_symtab(input[i].op2,&sym_num,&sym_addr))
				{
					hash_symtab(input[i].op2,-1);
					check_symtab(input[i].op2,&sym_num,&sym_addr);
				}
				this_entry=sym_num;
				RC=isreg(input[i].op1);
				if(RC)
				{
					printf("\n(IS,%d)\t(%d)(S,%d)",code,RC,this_entry);
					fprintf(icf,"(IS,%d)\t(%d)(S,%d)\n",code,RC,this_entry);
					fprintf(pass2ip,"%d\tIS\t%d\t%d\tS\t%d\n",LC,code,RC,this_entry);
				}
				else
				{
					printf("\n(IS,%d)\t(S,%d)",code,this_entry);
					fprintf(icf,"(IS,%d)\t(S,%d)\n",code,this_entry);
					fprintf(pass2ip,"%d\tIS\t%d\t-\tS\t%d\n",LC,code,this_entry);
				}
			}
			printf("\t%d",LC);
			LC=LC+1;
		}
		i++;
	}

// 3. Processing of END statement
	// perform step 2b
	ltfp=fopen("LTAB.TXT","a+");
	for(j=POOLTAB[PTP];j<LTP;j++)
	{
		//process literals to allocate memory
		fprintf(ltfp,"%d\t%d\t%d\t%d\n",j,LITTAB[j],PTP,LC++);
		LITADDR[j]=LC;
	}
	PTP++;
	POOLTAB[PTP]=LTP;
	fclose(ltfp);
	printf("\n(AD,2)");
	fprintf(icf,"(AD,2)");
	fprintf(pass2ip,"-\tAD\t2\t-\t-\t-\n");

// Write to POOLT.TXT
	poolt=fopen("POOLT.TXT","w+");
	for(j=0;j<PTP;j++)
		fprintf(poolt,"%d\n",POOLTAB[j]);
	fclose(poolt);
	br

// Close Intermediate Code file
	fclose(icf);
	fclose(pass2ip);
}

having some problem while creating MOT table in it... Is that code is clear?? there is input of one assembly code containing file I have taken as INPUT.TXT....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.