Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ============================================================================
- Name : Assembler.c
- Author : RB
- Version :
- Copyright : Your copyright notice
- Description : Hello World in C, Ansi-style
- ============================================================================
- */
- //IS-Imperative,DL-Declarative,AD-Assembler Directive
- #include<iostream>
- #include<string>
- #include<fstream>
- #include<stdlib.h>
- using namespace std;
- typedef struct abcd
- {
- string operationname;
- string operator_class;
- int opcode;
- int length;
- }opcodetable;
- typedef struct aghcd
- {
- string symbolname;
- int address;
- int length;
- }symtab;
- typedef struct abdwcd
- {
- string literalname;
- int address;
- }littab;
- typedef struct dsdsdds
- {
- int first;
- int number_of_literals;
- }pooltab;
- typedef struct aascd
- {
- string regname;
- int address;
- int length;
- }regtab;
- int main(void)
- {
- opcodetable optable[20];
- regtab registers_table[16];
- symtab symbols_table[30];
- littab literals[20];
- pooltab literal_pools[20];
- int temp,operatorcount=0,symbolcount=0,instruction_addresses[40],num_addresses=0,literalcount=0,curpool=0,lc=0;
- string opnametemp,opclassname;
- /*Configuring system registers*/
- for(int i=0;i<10;i++)
- {
- char ch[2];
- ch[0]=65+i;
- ch[1]='\0';
- string abc=ch;
- registers_table[i].regname=abc+"REG";
- }
- registers_table[10].regname="LT";
- registers_table[11].regname="LE";
- registers_table[12].regname="EQ";
- registers_table[13].regname="GT";
- registers_table[14].regname="GE";
- registers_table[15].regname="ANY";
- /*
- cout<<"System registers are : "<<endl;
- for(int i=0;i<16;i++)
- {
- cout<<registers_table[i].regname<<endl;
- }
- */
- //Reading the operators from file.
- ifstream inputfile;
- inputfile.open("opinput.txt");
- while(1)
- {
- if(inputfile>>opnametemp)
- optable[operatorcount].operationname=opnametemp;
- else
- break;
- if(inputfile>>temp)
- optable[operatorcount].opcode=temp;
- else
- break;
- if(inputfile>>opclassname)
- optable[operatorcount].operator_class=opclassname;
- else
- break;
- if(inputfile>>temp)
- optable[operatorcount].length=temp;
- else
- break;
- operatorcount++;
- }
- inputfile.close();
- /*
- cout<<endl<<"Operations are : "<<endl;
- for(int i=0;i<operatorcount;i++)
- {
- cout<<optable[i].operationname<<"\t"<<optable[i].opcode<<"\t"<<optable[i].operator_class<<"\t"<<optable[i].length<<endl;
- }
- */
- /*PASS 1*/
- int isnewline=0;
- char peeking;
- string symtemp;
- literal_pools[0].first=0;
- literal_pools[0].number_of_literals=0;
- string filetobeused;
- cout<<"\nEnter the name of the file : ";
- cin>>filetobeused;
- inputfile.open(filetobeused);
- inputfile>>symtemp;
- inputfile>>temp;
- isnewline=1;
- string pooltable[50];
- instruction_addresses[num_addresses]=temp;
- num_addresses++;
- int isnotsymbol=1;
- int inslength=1; //account for this later
- int poofilled=0;
- int getliterals=0;
- int nomemtostatement=0;
- temp=1;
- cout<<"\nThe intermediate code after pass 1 is : \n";
- cout<<"\t(AD,1)"<<"(C,"<<temp<<")"<<endl;
- int icopindex=-1,icpoolindex=-1;
- int iclitindex=-1;
- string icsysreg=" ",icsymbol=" ";
- while(inputfile>>symtemp)
- {
- isnotsymbol=0;
- if(symtemp!="END")
- {
- if(getliterals==1)
- {
- if(symtemp.at(0)=='=')
- {
- lc=instruction_addresses[num_addresses-1]+1;
- literals[literalcount].literalname=symtemp;
- literals[literalcount].address=lc;
- iclitindex=literalcount;
- literalcount++;
- }
- else
- {
- getliterals=0;
- curpool++;
- literal_pools[curpool].first=literal_pools[curpool-1].number_of_literals+literal_pools[curpool-1].first;
- literal_pools[curpool].number_of_literals=0;
- }
- }
- if(getliterals==0)
- {
- for(int i=0;i<operatorcount;i++)
- {
- if(symtemp==optable[i].operationname)
- {
- icopindex=i;
- isnotsymbol=1;
- isnewline=0;
- temp=optable[i].length;
- if(num_addresses==1)
- {
- temp=0;
- }
- lc=instruction_addresses[num_addresses-1]+temp;
- if(optable[i].opcode==-60)
- {
- icopindex=-1;
- getliterals=1;
- nomemtostatement=1;
- }
- break;
- }
- }
- if(isnotsymbol==0)
- {
- for(int i=0;i<16;i++)
- {
- if(symtemp==registers_table[i].regname)
- {
- icsysreg=symtemp;
- isnotsymbol=1;
- break;
- }
- }
- }
- if(isnotsymbol==0)
- {
- if(symtemp.at(0)=='=')
- {
- int present=0;
- int pool_size=literal_pools[curpool].number_of_literals;
- for(int i=0;i<pool_size;i++)
- {
- if(symtemp==pooltable[i])
- {
- present=1;
- icpoolindex=i;
- break;
- }
- }
- if(present==0)
- {
- pooltable[pool_size]=symtemp;
- icpoolindex=pool_size;
- literal_pools[curpool].number_of_literals++;
- }
- }
- else
- {
- if(isnewline==1)
- {
- symbols_table[symbolcount].symbolname=symtemp;
- inputfile>>symtemp;
- lc=instruction_addresses[num_addresses-1]+temp;
- for(int i=0;i<operatorcount;i++)
- {
- if(symtemp==optable[i].operationname)
- {
- isnewline=0;
- if(optable[i].opcode==-50)
- {
- inputfile>>temp;
- }
- else if(optable[i].opcode==-70)
- {
- nomemtostatement=1;
- inputfile>>symtemp;
- for(int h=0;h<symbolcount;h++)
- {
- if(symtemp==symbols_table[h].symbolname)
- {
- lc=symbols_table[h].address;
- break;
- }
- }
- }
- else
- {
- temp=optable[i].length;
- }
- icopindex=i;
- }
- }
- symbols_table[symbolcount].address=lc;
- symbolcount++;
- }
- else
- {
- icsymbol=symtemp;
- }
- }
- }
- }
- peeking=inputfile.peek();
- if(peeking=='\n')
- {
- isnewline=1;
- // cout<<instruction_addresses[num_addresses-1]<<endl;
- if(nomemtostatement==0)
- {
- //This is our pass1 output
- if(icpoolindex!=-1)
- {
- if(icopindex!=-1)
- {
- int printicopcode=optable[icopindex].opcode;
- if(printicopcode==-50)
- {
- printicopcode=1;
- string newbuffer;
- char buffer[50];
- itoa(temp,buffer,10);
- newbuffer=buffer;
- icsymbol="(C,"+newbuffer+")";
- }
- cout<<lc<<"("<<optable[icopindex].operator_class<<","<<printicopcode<<") "<<icsysreg<<" "<<"(L,"<<literal_pools[curpool].first+icpoolindex<<")"<<endl;
- }
- }
- else
- {
- if(icopindex!=-1)
- {
- int printicopcode=optable[icopindex].opcode;
- if(printicopcode==-50)
- {
- printicopcode=1;
- string newbuffer;
- char buffer[50];
- itoa(temp,buffer,10);
- newbuffer=buffer;
- icsymbol="(C,"+newbuffer+")";
- }
- cout<<lc<<"("<<optable[icopindex].operator_class<<","<<printicopcode<<") "<<icsysreg<<" "<<icsymbol<<endl;
- }
- else if(iclitindex!=-1)
- {
- cout<<lc<<" "<<literals[iclitindex].literalname<<endl;
- }
- }
- icsysreg=" ";
- icsymbol=" ";
- icopindex=-1;
- icpoolindex=-1;
- iclitindex=-1;
- //
- instruction_addresses[num_addresses]=lc;
- num_addresses++;
- }
- else
- {
- nomemtostatement=0;
- }
- }
- else
- isnewline=0;
- }
- }
- cout<<"\t(AD,0)"<<endl;
- inputfile.close();
- /*
- cout<<"\nThe addresses are : ";
- cout<<num_addresses<<endl;
- for(int i=1;i<num_addresses;i++)
- {
- cout<<instruction_addresses[i]<<endl;
- }
- cout<<endl<<"The symbols are : "<<endl;
- for(int i=0;i<symbolcount;i++)
- {
- cout<<symbols_table[i].symbolname<<"\t"<<symbols_table[i].address<<endl;
- }
- cout<<endl;
- cout<<endl<<"The literals are : "<<endl;
- for(int i=0;i<literalcount;i++)
- {
- cout<<literals[i].literalname<<"\t"<<literals[i].address<<endl;
- }
- cout<<endl;
- cout<<endl<<"The literal pool table is : "<<endl;
- for(int i=0;i<=curpool;i++)
- {
- cout<<literal_pools[i].first<<"\t"<<literal_pools[i].number_of_literals<<endl;
- }
- cout<<endl;
- */
- /*PASS 2*/
- int tempsysaddress=0,tempopcode=0,tempsymaddress=0;
- inputfile.open(filetobeused);
- inputfile>>symtemp;
- inputfile>>temp;
- int insno=1;
- int isfound=0;
- string leftover;
- char nextchar;
- curpool=0;
- int ltorgon=0;
- cout<<"\nMachine code produced after pass 2 is as follows : ";
- while(inputfile>>symtemp)
- {
- if(ltorgon==1)
- {
- if(symtemp.at(0)=='=')
- {
- tempopcode=0;
- tempsysaddress=0;
- leftover=symtemp.substr(1);
- }
- else
- {
- ltorgon=0;
- curpool++;
- }
- }
- if(ltorgon==0)
- {
- leftover="00";
- if(symtemp=="END")
- {
- break;
- }
- isfound=0;
- for(int i=0;i<operatorcount;i++)
- {
- if(symtemp==optable[i].operationname)
- {
- tempopcode=optable[i].opcode;
- if(tempopcode==-60)
- {
- ltorgon=1;
- }
- isfound=1;
- break;
- }
- }
- if(isfound==0)
- {
- for(int i=0;i<16;i++)
- {
- if(symtemp==registers_table[i].regname)
- {
- tempsysaddress=i+1;
- isfound=1;
- break;
- }
- }
- }
- if(isfound==0)
- {
- for(int i=0;i<symbolcount;i++)
- {
- if(symtemp==symbols_table[i].symbolname)
- {
- tempsymaddress=symbols_table[i].address;
- isfound=1;
- break;
- }
- }
- }
- if(isfound==0)
- {
- int starting=literal_pools[curpool].first;
- int ending=literal_pools[curpool].first+literal_pools[curpool].number_of_literals;
- for(int i=starting;i<ending;i++)
- {
- if(symtemp==literals[i].literalname)
- {
- tempsymaddress=literals[i].address;
- isfound=1;
- break;
- }
- }
- }
- if(isfound==0)
- {
- leftover=symtemp;
- }
- }
- peeking=inputfile.peek();
- if(peeking=='\n')
- {
- if(tempopcode!=-60 && tempopcode!=-70)
- {
- cout<<endl<<instruction_addresses[insno]<<") ";
- if(tempopcode>0)
- {
- cout<<tempopcode<<" "<<tempsysaddress%10<<" "<<tempsymaddress;
- }
- if(tempopcode==0)
- {
- cout<<tempopcode<<" "<<tempsysaddress%10<<" "<<leftover;
- }
- tempsysaddress=0;
- tempopcode=0;
- tempsymaddress=0;
- insno++;
- }
- }
- }
- inputfile.close();
- cout<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment