Experiment No. 4


Problem Statement: Implementation of the Pass-I of the assembler to generate LITTAB and POOLTAB from assembly language program.
Theory: Tasks performed by the Pass-I are
  1. Separate the symbol table, mnemonic table, and operand fields.
  2. Build the symbol table.
  3. Perform LC Processing.
  4. Construct Intermediate Representation (IR).
Pass-I performs the analysis of the Source Program and synthesize the IR.
Pass-I uses the following data structures:
  1. OPTAB= A table of mnemonic opcodes and related information.
  2. SYMTAB= Table of Symbols used in the program.
  3. LITTAB= A table of Literals used in the program.
  4. POOLTAB= Table containing the information about the LITTAB.
In this experiment we will generate only the SYMTAB for the assembly language program.


LITTAB and POOLTAB:
  • Literals are entered in the LITTAB in the sequential manner, along with two fields first one is literal and second is address of that literal.
  • An entry in the POOLTAB indicates the number of pools of literals present in the input assembly language program.
  • It contains only one field i.e. literal number. Literal number indicates which entry in LITTAB contains the first literal of the pool.
  • In the assembly language program given below, there are two pools, the first one is before the LTORG statement and second is after LTORG statement to END statement.
  • Pool one is having two literals =’5’ & =’2’ and again pool two is having two literals as =’9’ and =’1’. Hence LITTAB will have total four entries.
  • Here both pools contains 2 literals, and hence POOLTAB will have the entries as 0,2 and 4.


Let’s see an example:


Input: An assembly Language Program:
START 501
MOVER AREG, ='5'
ADD AREG,='2'
LTORG
MOVER AREG,='9'
ADD AREG,='1'
END
Output: LITTAB and POOLTAB
1. Explain the process of entering the literals in the LITTTAB

2. What is the pool? Explain in detail.

3. Explain the POOLTAB contents.

4. Explain the essence of the POOLTAB.


CODE:

INPUT FILES::

 L1.txt

START 501
MOVER AREG,='5'
ADD AREG,='2'
LTORG
MOVER AREG,='9'
ADD AREG,='1'
END

MEM_TAB.txt

MOVER IS (04,3)
ADD IS (01,3)
PRINT IS (10,3)
READ IS (09,3)

MAIN PROGRAM::


//creation of Literal Tabel
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
# define MAX 80
# define SI 30
class lit
{
  char name[SI];
  int add;
  public:
  void set_lit(char []);
  int msearch(char[]);
int modify(int ,int);
void display();
}l;
void lit::display()
{
 fstream f;
 cout<<"\n Content Of Literal Tabel:-";
  f.open("litp.txt",ios::beg|ios::in);
  while(f.read((char *)&(*this),sizeof(*this)))
  {
    cout<<this->name<<" "<<this->add<<endl;
  }
}

int lit::modify(int sr_no,int lc)
{
  fstream f;
  f.open("litp.txt",ios::out|ios::in);
  int pos,cnt=0;
  pos=sr_no*sizeof(*this);
  f.seekp(pos,ios::beg);
  while(f.read((char *)&(*this),sizeof(*this)))
  {
    pos=sr_no*sizeof(*this);
    f.seekp(pos,ios::beg);
    this->add=lc;
    f.write((char *)&(*this),sizeof(*this));
    sr_no++;
    cnt++;
    lc++;
  }
f.close();
return cnt;
}
int lit::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 lit::set_lit(char a[])
{
  fstream f;
  f.open("litp.txt",ios::out|ios::app);
  strcpy(this->name,a);
  this->add=-1;
  f.write((char *)&(*this),sizeof(*this));
  f.close();
}
void main()
{
char str[SI],str1[SI],str2[SI],str3[SI],strc[SI],strm[SI];
int lc,flag=0,c,pool_tab[SI],ptr=0,sr_no,cnt;
fstream f;
clrscr();
pool_tab[ptr]=0;
ptr++;
f.open("l1.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(strm,strtok(str," "));
  strcpy(str1,strtok(NULL," "));
  strcpy(str1,strtok(str1,","));
  strcpy(str1,strtok(NULL,","));
  if(strstr(str1,"="))
  {
    l.set_lit(str1);
  }
  c=l.msearch(strm);
  lc=lc+c;
  }
  if(strcmp(str,"LTORG")==0)
  {
     sr_no=pool_tab[ptr-1];
     cnt=l.modify(sr_no,lc);
     pool_tab[ptr]=cnt+sr_no;
     ptr++;
     lc=lc+cnt;
 }
 if(strcmp(str,"END")==0)
 {
     sr_no=pool_tab[ptr-1];
     cnt=l.modify(sr_no,lc);
     pool_tab[ptr]=cnt+sr_no;
     ptr++;
     lc=lc+cnt;
     break;
 }
}
}
f.close();
l.display();
cout<<"\n Conent Of Pool TAb:-";
for(int i=0;i<ptr;i++)
{
 cout<<pool_tab[i]<<endl;
}
getch();
}

















Comments