0% found this document useful (0 votes)
399 views

C Code For Assembler Pass-1

This C program performs lexical analysis, syntax analysis, and code generation for a simple assembly language. It includes struct definitions for symbol, literal, and intermediate code tables. Functions search and insert symbols, handle directives like START and LTORG, and generate intermediate code by calling functions for imperative statements, declarations, and directives. The main function opens a file, tokenizes each line, and calls the appropriate functions to populate the tables and generate intermediate code which is then printed.

Uploaded by

Chaman Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
399 views

C Code For Assembler Pass-1

This C program performs lexical analysis, syntax analysis, and code generation for a simple assembly language. It includes struct definitions for symbol, literal, and intermediate code tables. Functions search and insert symbols, handle directives like START and LTORG, and generate intermediate code by calling functions for imperative statements, declarations, and directives. The main function opens a file, tokenizes each line, and calls the appropriate functions to populate the tables and generate intermediate code which is then printed.

Uploaded by

Chaman Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include<stdio.

h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
struct mottab
{
char mnemonic[6];
int clas;
char opcode[3];
}mottab;
struct symbltab
{
char symb[8];
int address;
int size;
}ST[20];
struct literaltab
{
int litrl;
int address;
}LT[20];
int PT[10];
struct intermed
{
int lc;
int code1,type1;
int code2,type2;
int code3,type3;
}IC[30];
static struct mottab MOT[30]={{"STOP",1,"00"},{"ADD",1,"01"},{"SUB",1,"02"},{"MU
LT",1,"03"},{"MOVER",1,"04"},{"MOVEM",1,"05"},{"COMP",1,"06"},{"BC",1,"07"},{"DI
V",1,"08"},{"READ",1,"09"},{"PRINT",1,"10"},{"START",3,"01"},{"END",3,"02"},{"OR
IGIN",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,"0
3"},{"LE",5,"04"},{"GE",5,"05"},{"NE",5,"06"},{"ANY",5,"07"}};

int nMOT=28;
int lc=0;
int iPT;
int iLT=0;
int iST=0;
int iIC=0;
int searchST(char symbol[])
{
int i;
for(i=0;i<iST;i++)
if(strcmp(ST[i].symb,symbol)==0)
return(i);
return(-1);
}
int searchMOT(char symbol[])
{
int i;
for(i=0;i<nMOT;i++)
if(strcmp(MOT[i].mnemonic,symbol)==0)
return(i);
return(-1);
}
int insertST(char symbol[],int add,int siz)
{
strcpy(ST[iST].symb,symbol);
ST[iST].address=add;
ST[iST].size=siz;
iST++;
return(iST-1);
}
void imperative();
void declaration();
void directive();
void print_symbol();
void print_pool();
void print_literal();
void print_opcode();
void intermediate();
char s1[8],s2[8],s3[8],label[8];
void ltorg();
void dc();
void ds();
void start();
int tokencount;
void main()
{
int i;
char file1[40],nextline[80];
FILE *ptr1;
clrscr();
printf("\nenter 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';
sscanf(nextline,"%s",s1);
if(strcmp(s1,"END")==0)
break;
if(searchMOT(s1)==-1)
{
if(searchST(s1)==-1)
insertST(s1,lc,0);
tokencount=sscanf(nextline,"%s%s%s%s",label,s1,s2,s3);
tokencount--;
}
else
tokencount=sscanf(nextline,"%s%s%s",s1,s2,s3);
if(tokencount==0)
continue;
i=searchMOT(s1);
if(i==-1)
{
printf("\nwrong %s",s1);
continue;
}
switch(MOT[i].clas)
{
case 1: imperative();break;
case 2: declaration();break;
case 3: directive();break;
default: printf("\nwrong...%s",s1);break;
}
}
ltorg();
print_symbol();
getch();
print_literal();
getch();
print_pool();
getch();
print_opcode();
getch();
intermediate();
getch();
//return(0);
}
void imperative()
{
int index;
index=searchMOT(s1);
IC[iIC].type1=IC[iIC].type2=IC[iIC].type3=0;
IC[iIC].lc=lc;
IC[iIC].code1=index;
IC[iIC].type1=MOT[index].clas;
lc=lc+1;
if(tokencount>1)
{
index=searchMOT(s2);
if(index!=-1)
{
IC[iIC].code2=index;
IC[iIC].type2=MOT[index].clas;
}
else
{
index=searchST(s2);
if(index==-1)
index=insertST(s2,0,0);
IC[iIC].code2=index;
IC[iIC].type2=7;
}
}
if(tokencount>2)
{
if(isdigit(*s3))
{
LT[iLT].litrl=atoi(s3);
IC[iIC].code3=iLT;
IC[iIC].type3=8;
iLT++;
}
else
{
index=searchST(s3);
if(index==-1)
index=insertST(s3,0,0);
IC[iIC].code3=index;
IC[iIC].type3=7;
}
}
iIC++;
}

void declaration()
{
if(strcmp(s1,"DC")==0)
{
dc();
return;
}
if(strcmp(s1,"DS")==0)
ds();
}
void directive()
{
if(strcmp(s1,"START")==0)
{
start();
return;
}
if(strcmp(s1,"LTORG")==0)
ltorg();
}

void ltorg()
{
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;
IC[iIC].lc=lc;
IC[iIC].code1=index;
IC[iIC].type1=MOT[index].clas;
IC[iIC].code2=LT[i].litrl;
IC[iIC].type2=6;
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;
IC[iIC].lc=lc;
IC[iIC].code1=index;
IC[iIC].type1=MOT[index].clas;
IC[iIC].type2=6;
IC[iIC].code2=atoi(s2);
index=searchST(label);
if(index==-1)
insertST(label,0,0);
ST[index].address=lc;
ST[index].size=1;
lc=lc+1;
iIC++;
}

void ds()
{
int index;
index=searchMOT(s1);
IC[iIC].type1=IC[iIC].type2=IC[iIC].type3=0;
IC[iIC].lc=lc;
IC[iIC].code1=index;
IC[iIC].type1=MOT[index].clas;
IC[iIC].type2=6;
IC[iIC].code2=atoi(s2);
index=searchST(label);
if(index==-1)
insertST(label,0,0);
ST[index].address=lc;
ST[index].size=atoi(s2);
lc=lc+atoi(s2);
iIC++;
}

void start()
{
int index;
index=searchMOT(s1);
IC[iIC].type1=IC[iIC].type2=IC[iIC].type3=0;
IC[iIC].lc=lc;
IC[iIC].code1=index;
IC[iIC].type1=MOT[index].clas;
IC[iIC].type2=6;
IC[iIC].code2=atoi(s2);
lc=atoi(s2);
iIC++;
}

void intermediate()
{
int i;
char decode[9][3]={"","IS","DL","AD","RG","CC","C","S","L"};
printf("\n.........intermediate code:::::");
for(i=0;i<iIC;i++)
{
printf("\n%3d) (%s,%2s)",IC[i].lc,decode[IC[i].type1],MOT[IC[i].code1].op
code);
if(IC[i].type2!=0)
{
if(IC[i].type2<6)
printf(" (%s,%2s)",decode[IC[i].type2],MOT[IC[i].code2].opcode);
printf(" (%s,%2d)",decode[IC[i].type2],IC[i].code2);
}
if(IC[i].type3!=0)
printf(" (%s%2d)",decode[IC[i].type3],IC[i].code3);
}
}

void print_symbol()
{
int i;
printf("\n....symbol table....");
for(i=0;i<iST;i++)
printf("\n%10s %3d %3d",ST[i].symb,ST[i].address,ST[i].size);
}
void print_literal()
{
int i;
printf("\n....literal table....");
for(i=0;i<iLT;i++)
printf("\n %5d %5d",LT[i].litrl,LT[i].address);
}

void print_pool()
{
int i;
printf("\n....pool table....");
for(i=0;i<iPT;i++)
printf("\n%d",PT[i]);
}
void print_opcode()
{
int i;
printf("\n....opcode table....");
for(i=0;i<nMOT;i++)
if(MOT[i].clas==1)
printf("\n%6s %3s",MOT[i].mnemonic,MOT[i].opcode);
}

You might also like