Here is a code for assembler; in which every-thing is gonna run except Origin directive..

#include<stdio.h>
#include<string.h>
#include<conio.h>

struct mottab        //declaring mot table structure
{
	char mn[6];
	int Class;
	char opcode[3];
};

struct symtab   //declaring structure for symbol table
{
	char symbol[8];
	int address;
	int size;
}st[20];

struct littab     //declaring structure for literal table
{
	int literal;
	int address;
}lt[10];

int pt[20];  //array for pool table

struct intcode  //structure for intermediate code
{
	int lc;
	int code1,type1;
	int code2,type2;
	int code3,type3;
}ic[30];
//mot table filled with necessory keywords and directives
static struct mottab mot[29]={{"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"},{"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"}};

int nmot=28;//number of entries in mot
int lc=0; //location counter
int ipt; // index of next entry in pool tabel
int ilt=0;//index of next entry in literal table
int ist=0; //index of next entry in symbol table
int iic=0; //index of next entry in intermidiate code tabel

int searchst(char symbol[]) //search symbol in symtab
{
int i;
	for(i=0;i<ist;i++)
		if(strcmp(st[i].symbol,symbol)==0)
			return(i);
		return(-1);

}

int searchmot(char symbol[]) //search mneumonic
{
 int i;
	for(i=0;i<nmot;i++)
		if(strcmp(mot[i].mn,symbol)==0)
			return(i);
		return(-1);
}

//insert symbol in symbol table
int insertst(char symbol[],int address,int size)
{
 strcpy(st[ist].symbol,symbol);
 st[ist].address=address;
 st[ist].size=size;
 ist++;
 return(ist-1);
}

void imperative(); //handle an executable statement
void declaration(); //handle declaration statement
void directive(); //handle an assembler directive
void print_symbol(); // display symbol table
void print_pool();//display pool table
void print_literal();//display literal table
void print_opcode();//display opcode table
void intermediate(); //display intermidiate code
void mcode(); //generate machine code

char s1[8],s2[8],s3[8],label[8];
void ltorg();   //for ltorg directive
void DC(); //handle declaration statement dc
void DS(); // handle declaration statement ds
void START(); // handle start directive
int tokencount; //total number of words in a statement

void main()
{
char file1[40],nextline[80];
int len,i,j,temp,errortype;
FILE *ptr1;
clrscr();
	printf("\nEnter source file name:");
	gets(file1);
	ptr1=fopen(file1,"r");
	while(!feof(ptr1))
	{
		i=0;
		nextline[i]=fgetc(ptr1);
		while(nextline[i]!='\n'&&nextline[i]!=EOF)
		{
			if(!isalnum(nextline[i]))
				nextline[i]=' ';
			else
				nextline[i]=toupper(nextline[i]);
			i++;
			nextline[i]=fgetc(ptr1);
		}
		nextline[i]='\0';

		//if next line is an END statement
		sscanf(nextline,"%s",s1); //read from the nextline in s1
		if(strcmp(s1,"END")==0)
			break;
		//if the nextline contains a label
		if(searchmot(s1)==-1)
		{
			if(searchst(s1)==-1)
				insertst(s1,lc,0);
			//separate opcode and operands
			tokencount=sscanf(nextline,"%s%s%s%s",label,s1,s2,s3);
			tokencount--;
		}
		else
		// separate opcode and operands
		tokencount=sscanf(nextline,"%s%s%s%",s1,s2,s3);
		if(tokencount==0)//blank line
			continue;//goto the beginning of the loop
		i=searchmot(s1);
		if(i==-1)
		{
			printf("\nWrong opcode..%s",s1);
			break;
		}
		switch(mot[i].Class)
		{
			case 1:imperative();
				break;
			case 2:declaration();
				break;
			case 3:directive();
				break;
			default: printf("\nWrong opcode..%s",s1);
				break;
		}

	}
	ltorg();
	printf("\n\n");
	print_symbol();
	getch();
	printf("\n\n");
	print_pool();
	getch();
	printf("\n\n");
	print_literal();
	getch();
	printf("\n\n");
	print_opcode();
	getch();
	printf("\n\n");
	intermediate();
	getch();
	printf("\n\n");
//	mcode();
	getch();
}

void imperative()
{
 int index;
 index=searchmot(s1);
 ic[iic].type1=ic[iic].type2=ic[iic].type3=0;//initialize
 ic[iic].lc=lc;
 ic[iic].code1=index;
 ic[iic].type1=mot[index].Class;
 lc=lc+1;
	if(tokencount>1)
	{
		index=searchmot(s2);
		if(index!=-1)
		{
			ic[iic].code2=index;
			ic[iic].type2=mot[index].Class;
		}
		else
		{
			//It is a variable
			index=searchst(s2);
			if(index==-1)
				index=insertst(s2,0,0);
			ic[iic].code2=index;
			ic[iic].type2=7; //value 7 is for variables
		}
	}
	if(tokencount>2)
	{
		if(isdigit(*s3))
		{
			lt[ilt].literal=atoi(s3);
			ic[iic].code3=ilt;
			ic[iic].type3=8; //value 8 is for literal type
			ilt++;
		}
		else
		{
			index=searchst(s3);
			if(index==-1)
				index=insertst(s3,0,0);
			ic[iic].code3=index;
			ic[iic].type3=7; // value 7 is for variables
		}
	}
	iic++;
}

void declaration() // intermidiate code for declarative statements
{
	if(strcmp(s1,"DC")==0)
	{
		DC();
		return;
	}
	if(strcmp(s1,"DS")==0)
		DS();
}

void directive() //intermidiate code for directive
{
	if(strcmp(s1,"START")==0)
	{
		START();
		return;
	}
	if(strcmp(s1,"LTORG")==0)
		ltorg();
}

void ltorg()  //intermidiate code for literals
{
 int i,index;
 for(i=pt[ipt];i<ilt;i++)
 {
  lt[i].address=lc;
  index=searchmot("DC");
  ic[iic].type1=ic[iic].type2=ic[iic].type3=0;//initialize
  ic[iic].lc=lc;
  ic[iic].code1=index;
  ic[iic].type1=mot[index].Class;
  ic[iic].type2=6; //6 is type for constants
  ic[iic].code2=lt[i].literal;
  lc=lc+1;
  iic++;
 }
 ipt++;
 pt[ipt]=ilt;
}

void DC()
{
	int index;
	index=searchmot(s1);
	ic[iic].type1=ic[iic].type2=ic[iic].type3=0;//initialize
	ic[iic].lc=lc;
	ic[iic].code1=index;
	ic[iic].type1=mot[index].Class;
	ic[iic].type2=6; //6 is type for constants
	ic[iic].code2=atoi(s2);
	index=searchst(label);
	if(index==-1)
		index=insertst(label,0,0);
		st[index].address=lc;
		st[index].size=1;
		lc=lc+1;     //incrementing lc count
		iic++;
}

void DS()                   //process declarative statement
{
 int index;
	index=searchmot(s1);
	ic[iic].type1=ic[iic].type2=ic[iic].type3=0;//initialize
	ic[iic].lc=lc;
	ic[iic].code1=index;
	ic[iic].type1=mot[index].Class;
	ic[iic].type2=6; //6 is type for constants
	ic[iic].code2=atoi(s2);
	index=searchst(label);
	if(index==-1)
		index=insertst(label,0,0);
	st[index].address=lc;
	st[index].size=atoi(s2);
	lc=lc+atoi(s2);
	iic++;
}

void START() //process start
{
 int index;
 index=searchmot(s1);
 ic[iic].type1=ic[iic].type2=ic[iic].type3=0;//initialize
  ic[iic].lc=lc;
  ic[iic].code1=index;
  ic[iic].type1=mot[index].Class;
  ic[iic].type2=6; //6 is type for constants
  ic[iic].code2=atoi(s2);
  lc=atoi(s2);            //convert to integer
  iic++;
}

void intermediate()     //forming of intermidiate code
{
 int i;
 char decode[9][3]={" ","IS","DL","AD","RG","CC","C","S","L"};
 printf("\n inter code::");
 for(i=0;i<iic;i++)
 {
 printf("\n%3d(%s,%2s)",ic[i].lc,decode[ic[i].type1],mot[ic[i].code1].opcode);
 if(ic[i].type2!=0)
 {
	if(ic[i].type2<6)
		printf("(%s,%2s)",decode[ic[i].type2],mot[ic[i].code2].opcode);
	else
		printf("(%s,%2d)",decode[ic[i].type3],ic[i].code3);

 }
 if(ic[i].type3!=0)
	printf("(%s,%2d)",decode[ic[i].type3],ic[i].code3);

 }
}


void print_symbol()
{
 int i;
	printf("\nSymbol table::");
	for(i=0;i<ist;i++)
		printf("\n%10s  %3d  %3d",st[i].symbol,st[i].address,st[i].size);

}

void print_pool()
{
	int i;
	printf("\nPool Tabel::");
	for(i=0;i<ipt;i++)
		printf("\n%d",pt[i]);

}
void print_literal()
{
 int i;
 printf("\nLT Tabel::");
 for(i=0;i<ilt;i++)
	printf("\n%5d\t%5d",lt[i].literal,lt[i].address);

}

void print_opcode()
{
 int i;
 printf("\n Opcode Tabel::");
	for(i=0;i<nmot;i++)
		if(mot[i].Class==1)
			printf("\n%6s\t%2s",mot[i].mn,mot[i].opcode);

}

void mcode()
{
 int i;
 printf("\nMachine Code::");
 for(i=0;i<iic;i++)
 {
	if(ic[i].type1==1)
	{
		printf("\n%3d	%s",ic[i].lc,mot[ic[i].code1].opcode);
		if(ic[i].type2==0)
			printf("00 000");
		else
			if(ic[i].type2>6)
				printf("00%3d",st[ic[i].code2].address);
			else
			{
				printf("%2s",mot[ic[i].code2].opcode);
				if(ic[i].type3==7)
					printf("%3d",st[ic[i].code3].address);
				else
					printf("%3d",lt[ic[i].code3].address);
			}
	}
	else
		if(ic[i].type1==2 && strcmp(mot[ic[i].code1].mn,"DC")==0)
		{
			printf("\n%3d",ic[i].lc);
			printf("00 00 %3d",ic[i].code2);
		}
 }
}

If someone have solution then plz tell me where i am going wrong.....

Maybe because

struct mottab //declaring mot table structure
{
char mn[6];
int Class;
char opcode[3];
};

mn[] is of size 6 and ORIGIN is of size 7 including '\0'. Try changing the size of mn and see if it works

Maybe because


mn[] is of size 6 and ORIGIN is of size 7 including '\0'. Try changing the size of mn and see if it works

Yahh, you are right... A silly mistake.. Thanx !! :)

Be a part of the DaniWeb community

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