Experiment no 3
Problem Statement: Implementation of the Pass-I of the assembler
to generate SYMTAB from assembly
language program.
Theory: Tasks
performed by the Pass-I are
- Separate the symbol table, mnemonic table, and operand fields.
- Build the symbol table.
- Perform LC Processing.
- Construct Intermediate Representation (IR).
Pass-I performs the analysis of the Source
Program and synthesize the IR.
Pass-I uses the following data structures:
i)
OPTAB= A table of mnemonic opcodes and related
information.
ii)
SYMTAB= Table of Symbols used in the program.
iii)
LITTAB= A table of Literals used in the program.
iv)
POOLTAB= Table containing the information about the
LITTAB.
In this experiment we will generate only the
SYMTAB for the assembly language program.
SYMTAB:
- An entry in the SYMTAB contains the fields symbol, address & length.
- While processing an assembly statement if the symbol is found, the symbol & address contained in location counter LC is copied in to a new entry of SYMTAB.
- In the start of the assembly program an address is specified, this address is assigned to LC to keep track of each and every instruction.
Input: An
assembly Language Program:
START 501
NEXT: MOVER
AREG,A
PRINT ONE
A DS
3
ONE DC
'1'
F EQU
200
END
The assembly language program given above contains four symbols as NEXT,
A, ONE, F.
Input file:MEM_TAB.txt
MOVER IS (04,3)
ADD IS (01,3)
PRINT IS (10,3)
READ IS (09,3)
Input program: T.txt
START 501
NEXT:MOVER AREG,A
PRINT ONE
A DS 3
ONE DC '1'
F EQU 200
END
and final CPP program:
/* Symbol tabel genration*/
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
# define MAX 80
# define SI 30
class sym
{
char name[SI];
int add,len;
public:
void set(char [],int ,int);
void set_symbol(char []);
int msearch(char []);
int search(char []);
int get_addr(char []);
void modify(char [],int ,int);
void display();
}s;
void sym::display()
{
fstream f;
f.open("s.txt",ios::in|ios::beg);
cout<<"Symbol Tabel:-"<<endl;
while(f.read((char *)&(*this),sizeof(*this)))
{
cout<<this->name<<" "<<this->add<<" "<<this->len<<endl;
}
f.close();
}
void sym::modify(char a[],int b,int c)
{
fstream f;
int pos,cnt=0;
f.open("s.txt",ios::out|ios::in);
while(f.read((char *)&(*this),sizeof(*this)))
{
if(strcmp(this->name,a)==0)
{
pos=cnt*sizeof(*this);
strcpy(this->name,a);
this->add=b;
this->len=c;
f.seekp(pos,ios::beg);
f.write((char *)&(*this),sizeof(*this));
}
else
cnt++;
}
f.close();
}
int sym::get_addr(char a[])
{
fstream f;
f.open("s.txt",ios::in|ios::beg);
while(f.read((char *)&(*this),sizeof(*this)))
{
if(strcmp(this->name,a)==0)
{
f.close();
return (this->add);
}
}
f.close();
return 0;
}
int sym::search(char a[])
{
fstream f;
f.open("s.txt",ios::in|ios::beg);
while(f.read((char *)&(*this),sizeof(*this)))
{
if(strcmp(this->name,a)==0)
{
f.close();
return 0;
}
}
f.close();
return 1;
}
int sym::msearch(char a[])
{
fstream f;
char str[SI],str1[SI],str2[SI],strc[SI];
f.open("mem_tab.txt",ios::in|ios::beg);
while(f.getline(str,MAX))
{
strcpy(strc,str);
strcpy(str1,strtok(str," "));
if(strcmp(str1,a)==0)
{
strcpy(str,strc);
strcpy(str1,strtok(str,","));
strcpy(str1,strtok(NULL,","));
strcpy(str1,strtok(str1,")"));
f.close();
return atoi(str1);
}
}
f.close();
return 0;
}
void sym::set(char a[],int b,int c)
{
fstream f;
f.open("s.txt",ios::out|ios::app);
strcpy(this->name,a);
this->add=b;
this->len=c;
f.write((char *)&(*this),sizeof(*this));
f.close();
}
void sym::set_symbol(char a[])
{
fstream f;
f.open("s.txt",ios::out|ios::app);
strcpy(this->name,a);
this->add=-1;
this->len=-1;
f.write((char *)&(*this),sizeof(*this));
f.close();
}
void main()
{
char str[SI],str1[SI],str2[SI],strc[SI],strm[SI],strv[SI];
fstream f;
int lc,flag=0,c,flag1,size,flag2;
clrscr();
f.open("t.txt",ios::beg|ios::in);
while(f.getline(str,MAX))
{
strcpy(strc,str);
if(flag==0)
{
strcpy(str1,strtok(str," "));
if(strcmp(str1,"START")==0)
{
strcpy(str1,strtok(NULL," "));
lc=atoi(str1);
flag=1;
}
}
else
{
strcpy(str,strc);
if(strstr(str,":"))
{
strcpy(str1,strtok(str,":"));
s.set(str1,lc,1);
strcpy(strc,strtok(NULL,":"));
}
strcpy(str,strc);
if(strstr(str,","))
{
strcpy(strm,strtok(str," "));
strcpy(str1,strtok(NULL," "));
strcpy(str1,strtok(str1,","));
strcpy(str1,strtok(NULL,","));
if(!strstr(str1,"="))
{
s.set_symbol(str1);
}
c=s.msearch(strm);
lc=lc+c;
}
else
{
strcpy(str,strc);
strcpy(strm,strtok(str," "));
if(strcmp(strm,"PRINT")==0||strcmp(strm,"READ")==0)
{
strcpy(str1,strtok(NULL," "));
s.set_symbol(str1);
c=s.msearch(strm);
lc=lc+c;
}
else if(strcmp(strm,"ORIGIN")==0)
{
strcpy(str1,strtok(NULL," "));
flag1=s.search(str1);
if(flag1==1)
{
lc=atoi(str1);
}
else
{
lc=s.get_addr(str1);
}
}
strcpy(str,strc);
strcpy(strv,strtok(str," "));
strcpy(str1,strtok(NULL," "));
if(strcmp(str1,"DS")==0)
{
strcpy(str1,strtok(NULL," "));
size=atoi(str1);
flag1=s.search(strv);
if(flag1==1)
{
s.set(strv,lc,size);
}
else
s.modify(strv,lc,size);
lc=lc+size;
}
if(strcmp(str1,"DC")==0)
{
strcpy(str,strc);
strcpy(str1,strtok(str,"'"));
strcpy(str1,strtok(NULL,"'"));
strcpy(str1,strtok(str1,"'"));
size=atoi(str1);
flag1=s.search(strv);
if(flag1==1)
{
s.set(strv,lc,size);
}
else
s.modify(strv,lc,size);
lc++;
}
if(strcmp(str1,"EQU")==0)
{
strcpy(str1,strtok(NULL," "));
flag2=s.search(str1);
if(flag2==1)
{
size=atoi(str1);
flag1=s.search(strv);
if(flag1==1)
{
s.set(strv,size,1);
}
else
s.modify(strv,size,1);
}
else
{
size=s.get_addr(str1);
flag1=s.search(strv);
if(flag1==1)
{
s.set(strv,size,1);
}
else
s.modify(strv,size,1);
}
}
}
}//else complete
if(strcmp(str,"END")==0)
break;
}//while complete
f.close();
s.display();
getch();
}
Input file:MEM_TAB.txt
MOVER IS (04,3)
ADD IS (01,3)
PRINT IS (10,3)
READ IS (09,3)
Input program: T.txt
START 501
NEXT:MOVER AREG,A
PRINT ONE
A DS 3
ONE DC '1'
F EQU 200
END
and final CPP program:
/* Symbol tabel genration*/
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
# define MAX 80
# define SI 30
class sym
{
char name[SI];
int add,len;
public:
void set(char [],int ,int);
void set_symbol(char []);
int msearch(char []);
int search(char []);
int get_addr(char []);
void modify(char [],int ,int);
void display();
}s;
void sym::display()
{
fstream f;
f.open("s.txt",ios::in|ios::beg);
cout<<"Symbol Tabel:-"<<endl;
while(f.read((char *)&(*this),sizeof(*this)))
{
cout<<this->name<<" "<<this->add<<" "<<this->len<<endl;
}
f.close();
}
void sym::modify(char a[],int b,int c)
{
fstream f;
int pos,cnt=0;
f.open("s.txt",ios::out|ios::in);
while(f.read((char *)&(*this),sizeof(*this)))
{
if(strcmp(this->name,a)==0)
{
pos=cnt*sizeof(*this);
strcpy(this->name,a);
this->add=b;
this->len=c;
f.seekp(pos,ios::beg);
f.write((char *)&(*this),sizeof(*this));
}
else
cnt++;
}
f.close();
}
int sym::get_addr(char a[])
{
fstream f;
f.open("s.txt",ios::in|ios::beg);
while(f.read((char *)&(*this),sizeof(*this)))
{
if(strcmp(this->name,a)==0)
{
f.close();
return (this->add);
}
}
f.close();
return 0;
}
int sym::search(char a[])
{
fstream f;
f.open("s.txt",ios::in|ios::beg);
while(f.read((char *)&(*this),sizeof(*this)))
{
if(strcmp(this->name,a)==0)
{
f.close();
return 0;
}
}
f.close();
return 1;
}
int sym::msearch(char a[])
{
fstream f;
char str[SI],str1[SI],str2[SI],strc[SI];
f.open("mem_tab.txt",ios::in|ios::beg);
while(f.getline(str,MAX))
{
strcpy(strc,str);
strcpy(str1,strtok(str," "));
if(strcmp(str1,a)==0)
{
strcpy(str,strc);
strcpy(str1,strtok(str,","));
strcpy(str1,strtok(NULL,","));
strcpy(str1,strtok(str1,")"));
f.close();
return atoi(str1);
}
}
f.close();
return 0;
}
void sym::set(char a[],int b,int c)
{
fstream f;
f.open("s.txt",ios::out|ios::app);
strcpy(this->name,a);
this->add=b;
this->len=c;
f.write((char *)&(*this),sizeof(*this));
f.close();
}
void sym::set_symbol(char a[])
{
fstream f;
f.open("s.txt",ios::out|ios::app);
strcpy(this->name,a);
this->add=-1;
this->len=-1;
f.write((char *)&(*this),sizeof(*this));
f.close();
}
void main()
{
char str[SI],str1[SI],str2[SI],strc[SI],strm[SI],strv[SI];
fstream f;
int lc,flag=0,c,flag1,size,flag2;
clrscr();
f.open("t.txt",ios::beg|ios::in);
while(f.getline(str,MAX))
{
strcpy(strc,str);
if(flag==0)
{
strcpy(str1,strtok(str," "));
if(strcmp(str1,"START")==0)
{
strcpy(str1,strtok(NULL," "));
lc=atoi(str1);
flag=1;
}
}
else
{
strcpy(str,strc);
if(strstr(str,":"))
{
strcpy(str1,strtok(str,":"));
s.set(str1,lc,1);
strcpy(strc,strtok(NULL,":"));
}
strcpy(str,strc);
if(strstr(str,","))
{
strcpy(strm,strtok(str," "));
strcpy(str1,strtok(NULL," "));
strcpy(str1,strtok(str1,","));
strcpy(str1,strtok(NULL,","));
if(!strstr(str1,"="))
{
s.set_symbol(str1);
}
c=s.msearch(strm);
lc=lc+c;
}
else
{
strcpy(str,strc);
strcpy(strm,strtok(str," "));
if(strcmp(strm,"PRINT")==0||strcmp(strm,"READ")==0)
{
strcpy(str1,strtok(NULL," "));
s.set_symbol(str1);
c=s.msearch(strm);
lc=lc+c;
}
else if(strcmp(strm,"ORIGIN")==0)
{
strcpy(str1,strtok(NULL," "));
flag1=s.search(str1);
if(flag1==1)
{
lc=atoi(str1);
}
else
{
lc=s.get_addr(str1);
}
}
strcpy(str,strc);
strcpy(strv,strtok(str," "));
strcpy(str1,strtok(NULL," "));
if(strcmp(str1,"DS")==0)
{
strcpy(str1,strtok(NULL," "));
size=atoi(str1);
flag1=s.search(strv);
if(flag1==1)
{
s.set(strv,lc,size);
}
else
s.modify(strv,lc,size);
lc=lc+size;
}
if(strcmp(str1,"DC")==0)
{
strcpy(str,strc);
strcpy(str1,strtok(str,"'"));
strcpy(str1,strtok(NULL,"'"));
strcpy(str1,strtok(str1,"'"));
size=atoi(str1);
flag1=s.search(strv);
if(flag1==1)
{
s.set(strv,lc,size);
}
else
s.modify(strv,lc,size);
lc++;
}
if(strcmp(str1,"EQU")==0)
{
strcpy(str1,strtok(NULL," "));
flag2=s.search(str1);
if(flag2==1)
{
size=atoi(str1);
flag1=s.search(strv);
if(flag1==1)
{
s.set(strv,size,1);
}
else
s.modify(strv,size,1);
}
else
{
size=s.get_addr(str1);
flag1=s.search(strv);
if(flag1==1)
{
s.set(strv,size,1);
}
else
s.modify(strv,size,1);
}
}
}
}//else complete
if(strcmp(str,"END")==0)
break;
}//while complete
f.close();
s.display();
getch();
}
Comments
Post a Comment