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.
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
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 :
beginif 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;
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();
}
}
PASS 2 will soon be published
ReplyDeletehi ! could you please publish pass 2 soon !
DeleteWill publish it soon!!
Deletewhile((sl=b2.readLine())!=null) ///
Delete{
j=0;
StringTokenizer se=new StringTokenizer(sl);/
there is error for sl
getting the same error
DeleteGreat work !!
ReplyDeletethank you mam!!
ReplyDeleteWhere is PASS 2 algorithm and program ????
ReplyDeletecan i get this file intermediate.txt
ReplyDeleteand all text file
Deleteplease #Anantha Raman could you post pass2's code
ReplyDeleteor let me know any other useful resource as soon as possibele...
Hi Abrham,
ReplyDeleteAm 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/
thanx for uploading pass2!
ReplyDeleteThis comment has been removed by the author.
ReplyDeletewrong program!!!!!!!!!!!!!!!!!!!!!111
ReplyDeleteassemblerr.java:76: error: incompatible types: String cannot be converted to String[]
ReplyDeletewhile((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
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!