Thursday, 23 October 2014

JAVA IMPLEMENTATION OF PASS1 of TWO PASS Assembler

An Assembler is a system software which converts an Assembly Language(ALP) into object code.A two pass assembler converts the assembly language into computer language (object code) which then passes through the linkers and loaders and then gets converted as an application file.
In the pass 1 of two pass assembler the ALP is coverted to an intermediate file which again gets converted to an object code in the pass2.
PASS1 OF A TWO PASS ASSEMBLER :


algorithm :

begin
            if starting address is given
                        LOCCTR = starting address;
            else
                        LOCCTR = 0;
            while OPCODE != END do                 ;; or EOF
                        begin
                        read a line from the code
                        if there is a label
                                    if this label is in SYMTAB, then error
                                    else insert (label, LOCCTR) into SYMTAB
                        search OPTAB for the op code
                        if found
                                    LOCCTR += N           ;; N is the length of this instruction (4 for MIPS)
                        else if this is an assembly directive
                                    update LOCCTR as directed
                        else error
                        write line to intermediate file
                        end
            PROGRAM size =  LOCCTR - starting address;
end

JAVA CODE FOR PASS 1 OF A TWO PASS ASSEMBLER:

import java.io.*;
import java.util.*;
class pass1
{
public static void main(String args[])throws Exception
{
int loc=0;
int j=0,k;
FileReader f1=new FileReader("input.txt");
BufferedReader b1=new BufferedReader(f1);
FileReader f2=new FileReader("optab.txt");
BufferedReader b2=new BufferedReader(f2);
FileWriter f3=new FileWriter("symtab.txt");
BufferedWriter b3=new BufferedWriter(f3);
FileWriter f4=new FileWriter("intermediate.txt");
BufferedWriter  b4=new BufferedWriter(f4);
StringBuffer sb = new StringBuffer();
String s,s3,s4,tmp,sd;
String s1[]=new String[3];
String s2[]=new String[3];
while((s=b1.readLine())!=null)
{
int m=0;
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens())
{
s1[m]=st.nextToken();
m++;
}
if(s1[1].equals("START"))
{
b4.write("   ");
for(int h=0;h<3;h++)
{
b4.write("  ");
b4.write("  "+s1[h]);
}
loc=Integer.parseInt(s1[2]);
b4.newLine();
}
else 
{
b4.write(loc+"  ");
if(s1[0].equals("-")!=true)
{
b3.write(" "+s1[0]);
b3.write(loc+" ");
}
while((sl=b2.readLine())!=null)
{
j=0;
StringTokenizer se=new StringTokenizer(sl);
while(se.hasMoreTokens())
{
s2[j]=se.nextToken();
j++;
}
if(s1[1].equals(s2[0])==true)
loc=loc+3;
break;
}
    if(s1[1].equals("BYTE"))
loc=loc+1;
else if(s1[1].equals("RESB"))
loc=loc+(1*Integer.parseInt(s1[2]));
else if(s1[1].equals("WORD"))
loc=loc+3;
else if(s1[1].equals("RESW"))
loc=loc+(3*Integer.parseInt(s1[2]));
for(int a=0;a<3;a++)
{
b4.write("  ");
b4.write("   "+s1[a]);
}
b4.newLine();
b3.newLine();
}
}
b4.close();
b3.close();
}
}

16 comments:

  1. PASS 2 will soon be published

    ReplyDelete
    Replies
    1. hi ! could you please publish pass 2 soon !

      Delete
    2. Will publish it soon!!

      Delete
    3. while((sl=b2.readLine())!=null) ///
      {
      j=0;
      StringTokenizer se=new StringTokenizer(sl);/


      there is error for sl

      Delete
  2. Where is PASS 2 algorithm and program ????

    ReplyDelete
  3. can i get this file intermediate.txt

    ReplyDelete
  4. please #Anantha Raman could you post pass2's code
    or let me know any other useful resource as soon as possibele...

    ReplyDelete
  5. Hi Abrham,
    Am very sorry that I won't be able to post the pass2 assemble right now, but I can share you some links of blogs which do have the idea on this.

    http://www.pracspedia.com/SPCC/2passjava.html

    http://codearea.in/program-for-implementing-pass-2-of-assembler/

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. wrong program!!!!!!!!!!!!!!!!!!!!!111

    ReplyDelete
  8. assemblerr.java:76: error: incompatible types: String cannot be converted to String[]
    while((s1=b2.readLine())!=null)
    ^
    assemblerr.java:79: error: incompatible types: String[] cannot be converted to String
    StringTokenizer se=new StringTokenizer(s1);
    ^
    Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
    2 errors
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    ReplyDelete