Implementation of the Pass-II of the assembler


Problem Statement: Implementation of the Pass-II of the assembler produces a target program which is in the machine language of the target computer.
Theory: Important data structures used by Pass-II are
  1. SYMTAB
  2. LITTAB &
  3. POOLTAB
LC : Location counter
littab_ptr : Points to an entry in LITTAB
pooltab_ptr : Points to an entry in POOLTAB
machine_code_buffer : Area for constructing code for one statement
code_area : Area for assembling the target program
code_area_address : Contains address of code_area


Input program: An Assembly Language Program


START 501
NEXT: MOVER AREG,A
PRINT ONE
MOVER AREG,='5'
ADD CREG,='1'
LTORG
ADD DREG,='9'
ONE DC '1'
A DS 3
F EQU 200
END


Output of PASS-I for the above assembly language program will generate following tables:


SYMTAB:
                        NEXT 501 1
A 519 3
ONE 518 1
F 200 1
LITTAB:
                        ='5' 513
='1' 514
='9' 522
POOLTAB:
                        0
2
3
IC (Intermediate Code):
(AD,01) (C,501)
(IS,04) 1 (S,02)
(IS,09) (S,03)
(IS,04) 1 (L,01)
(IS,01) 3 (L,02)
(AD,05)
(IS,01) 4 (L,03)
(DL,01) (C,1)
(DL,02) (C,3)
(AD,04) (C,200)
(AD,02)


Now this output of PASS-I is given as input to the Pass-II. PASS-II will perform all the required operations to produce target program which is in machine understandable format.
The output of PASS-II is shown here.


OUTPUT of PASS-II:


1. What is the role of PASS-II in assembler?

2. What is the input & output of PASS-II.

3. Explain the processing steps of PASS-II in short.

4. Explain the output of PASS-II in detail.

MAIN PROGRAM:


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
# define MAX 80
# define MAX1 40
int get_symbol(int);
int get_lit(int);
void main()
{
  int pool_tab[MAX1],ptr=0,sr_no,addr,i,next_srno;
  fstream f,fp;
  char str[MAX1],str1[MAX],str2[MAX],str3[MAX],mem_buf[MAX1],a[20];
  clrscr();
  f.open("sp\\pt.txt",ios::beg|ios::in);
  while(f.getline(str,MAX))
  {
     pool_tab[ptr]=atoi(str);
     ptr++;
     if(f.eof())
     break;
  }
   ptr=0;
   f.close();
  f.open("sp\\ic.txt",ios::beg|ios::in);
  while(f.getline(str,MAX))
  {
      strcpy(str1,strtok(str," "));
      strcpy(str2,strtok(NULL," "));
      strcpy(str3,strtok(NULL," "));
      strcpy(str1,strtok(str1,"("));
      strcpy(str1,strtok(str1,","));
      if(strcmp(str1,"AD")==0)
      {
       strcpy(str1,strtok(NULL,","));
       strcpy(str1,strtok(str1,")"));
       if(strcmp(str1,"05")==0)
       {
       fp.open("sp\\lt.txt",ios::beg|ios::in);
     sr_no=pool_tab[ptr];
     ptr++;
     next_srno=pool_tab[ptr];
      for(i=0;i<sr_no;i++)
     {
       fp.getline(str,MAX);
     }
      for(i=sr_no;i<next_srno;i++)
      {
        fp.getline(str,MAX);
        strcpy(str1,strtok(str," "));
        strcpy(str1,strtok(str1,"'"));
        strcpy(str1,strtok(NULL,"'"));
        strcpy(mem_buf,"+00 0 ");
        strcat(mem_buf,str1);
        cout<<mem_buf<<endl;
       }
    fp.close();
    }
     if(strcmp(str1,"02")==0)
       {
     sr_no=pool_tab[ptr];
     fp.open("sp\\lt.txt",ios::beg|ios::in);
     for(i=0;i<sr_no;i++)
     {
       fp.getline(str,MAX);
     }
    while(fp.getline(str,MAX))
    {
        strcpy(str1,strtok(str," "));
        strcpy(str1,strtok(str1,"'"));
        strcpy(str1,strtok(NULL,"'"));
        strcpy(mem_buf,"+00 0 ");
        strcat(mem_buf,str1);
        cout<<mem_buf<<endl;
       if(fp.eof())
       break;
       }
    }
}
if(strcmp(str1,"IS")==0)
      {
    strcpy(str1,strtok(NULL,","));
       strcpy(str1,strtok(str1,")"));
       strcpy(mem_buf,"+");
       strcat(mem_buf,str1);
       strcat(mem_buf," ");
       if(!strstr(str2,"("))
       {
      strcat(mem_buf,str2);
      strcat(mem_buf," ");
       }
       else
       {
     strcpy(str3,str2);
     strcat(mem_buf,"0");
     strcat(mem_buf," ");
       }
       strcpy(str1,strtok(str3,"("));
       strcpy(str1,strtok(str1,","));
       if(strcmp(str1,"S")==0)
       {
     strcpy(str1,strtok(NULL,","));
       strcpy(str1,strtok(str1,")"));
       sr_no=atoi(str1);
       addr=get_symbol(sr_no);
       itoa(addr,a,10);
       strcat(mem_buf,a);
       }
       if(strcmp(str1,"L")==0)
       {
     strcpy(str1,strtok(NULL,","));
       strcpy(str1,strtok(str1,")"));
       sr_no=atoi(str1);
       addr=get_lit(sr_no);
       itoa(addr,a,10);
       strcat(mem_buf,a);
       }
       cout<<mem_buf<<endl;
  }
    if(strcmp(str1,"DL")==0)
    {
     strcpy(str1,strtok(NULL,","));
     strcpy(str1,strtok(str1,")"));
     if(strcmp(str1,"01")==0)      //For  DC  only
     {
     strcpy(str1,strtok(str2,"("));
     strcpy(str1,strtok(str1,","));
     if(strcmp(str1,"C")==0)
     {
       strcpy(str1,strtok(NULL,","));
       strcpy(str1,strtok(str1,")"));
      strcpy(mem_buf,"+00 0 ");
      strcat(mem_buf,str1);
      cout<<mem_buf<<endl;
       }
     }

}
}
f.close();
fp.close();
getch();
}
int get_symbol( int sr_no)
{
 fstream f;
 char str[MAX1],str1[MAX1];
 f.open("sp\\st.txt",ios::beg|ios::in);
 for(int i=0;i<sr_no;i++)
 {
   f.getline(str,MAX);
 }
strcpy(str1,strtok(str," "));
strcpy(str1,strtok(NULL," "));
int addr=atoi(str1);
f.close();
return addr;
}
int get_lit(int sr_no)
{
 fstream f;
 char str[MAX1],str1[MAX1];
 f.open("sp\\lt.txt",ios::beg|ios::in);
 for(int i=0;i<sr_no;i++)
 {
   f.getline(str,MAX);
 }
 strcpy(str1,strtok(str," "));
strcpy(str1,strtok(NULL," "));
int addr=atoi(str1);
f.close();
return addr;
}

Comments