You are on page 1of 6

A2 : Assembler Pass 2

Code :
SymbolTable.java:
package pass2;

public class SymbolTable {


String symbol_no;
String symbol_name;
String symbol_address;
public SymbolTable() {}
public SymbolTable(String symbol_no,String symbol_name,String symbol_address)
{
this.symbol_no=symbol_no;
this.symbol_name=symbol_name;
this.symbol_address=symbol_address;
}
}

LiteralTable.java:
package pass2;

public class LiteralTable {


String lit_no;
String lit_name;
String lit_address;
public LiteralTable() {}
public LiteralTable(String lit_no,String lit_name,String lit_address)
{
this.lit_no=lit_no;
this.lit_name=lit_name;
this.lit_address=lit_address;
}
}

Pass2.java :
package pass2;

import java.io.*;
import java.lang.*;
import java.util.LinkedList;

public class Pass2 {

public static void main(String[] args) throws ArrayIndexOutOfBoundsException {


// TODO Auto-generated method stub
try {
BufferedReader in = new BufferedReader(new
FileReader("C:\\Users\\lenovo\\eclipse-
workspace\\Assembler2\\src\\pass2\\IntermediateCode"));
BufferedReader in1 = new BufferedReader(new
FileReader("C:\\Users\\lenovo\\eclipse-workspace\\Assembler2\\src\\pass2\\Symbol"));
BufferedReader in2 = new BufferedReader(new
FileReader("C:\\Users\\lenovo\\eclipse-workspace\\Assembler2\\src\\pass2\\Literal"));
LinkedList<SymbolTable> l1=new LinkedList<SymbolTable>();
LinkedList<LiteralTable> l2=new LinkedList<LiteralTable>();

String sym_read;
while((sym_read = in1.readLine())!=null) //reading symbol
table from file
{
String[] ss=sym_read.split(" ");
SymbolTable sst=new SymbolTable(ss[0],ss[1],ss[2]);
l1.add(sst);
}

String lit_read;
while((lit_read = in2.readLine())!=null) //reading
literal table from file
{
String[] spp=lit_read.split(" ");
LiteralTable lt=new LiteralTable(spp[0],spp[1],spp[2]);
l2.add(lt);
}

String sentence;

String str;
String str1;
String str2;
PrintWriter out = new PrintWriter(new
FileWriter("C:\\Users\\lenovo\\eclipse-
workspace\\Assembler2\\src\\pass2\\Machine_Level_Code"));

while ((str = in.readLine())!=null) { //reading intermediate


code from file

String[] words = str.split(" "); //splitting


loc_counter,type,opcode,register_no,symbol,symbol_no
//System.out.print(words[0]);
sentence = words[0];
String str11=words[1];
String[] words1=str11.split(","); //splitting
"(IS" and "opcode)"
int last_index=words1[1].indexOf(')');
//System.out.print("
+"+words1[1].substring(0,last_index)+" "); //printing +opcode
sentence = sentence+"
+"+words1[1].substring(0,last_index)+" ";

if(words[2].equals("(1)")||words[2].equals("(2)")||words[2].equals("(3)"))
//checking if register_no is present
{
if(words[2].equals("(1)"))
{
//System.out.print(" 1 ");
sentence = sentence+" 1 ";
}

else if(words[2].equals("(2)"))
{
//System.out.print(" 2 ");
sentence = sentence+" 2 ";
}

else if(words[2].equals("(3)"))
{
//System.out.print(" 3 ");
sentence = sentence+" 3 ";
}

if(words[3].charAt(1)=='S') //If its a


symbol_no, replace by its address
{
int lindex=words[3].indexOf(')');

for(SymbolTable s:l1)
{
String a=words[3].substring(3,lindex);
if(s.symbol_no.equals(a))
{
//System.out.println("
"+s.symbol_address);
sentence = sentence+"
"+s.symbol_address;
break;
}
else
continue;
}

}
else
{
if(words[3].charAt(1)=='L') //If its a
literal_no, replace by its address
{
int lindex=words[3].indexOf(')');

for(LiteralTable s:l2)
{
String
a=words[3].substring(3,lindex);
if(s.lit_no.equals(a))
{
//System.out.println("
"+s.lit_address);
sentence = sentence+"
"+s.lit_address;
break;
}
else
continue;
}

else {
//System.out.println(words[3]);
sentence = sentence+words[3];
}
}

}
else //if register_no is absent
{
//System.out.print(" 0 ");
sentence = sentence+" 0 ";
if(words[2].charAt(1)=='S')
{
int lindex=words[2].indexOf(')');
for(SymbolTable w:l1)
{
String r=words[2].substring(3,lindex);
if(w.symbol_no.equals(r))
{
//System.out.println("
"+w.symbol_address);
sentence = sentence+"
"+w.symbol_address;
break;
}
else
continue;
}
}
else
{
if(words[2].charAt(1)=='L')
{
int lindex=words[2].indexOf(')');
for(LiteralTable w:l2)
{
String
r=words[2].substring(3,lindex);
if(w.lit_no.equals(r))
{
//System.out.println("
"+w.lit_address);
sentence = sentence+"
"+w.lit_address;
break;
}
else
continue;
}
}
else {
//System.out.println(words[2]);
sentence = sentence+words[2];
}
}
}
System.out.println(sentence);
out.print(sentence+"\n"); //Writing machine
code to file
}
out.close();
}

catch(Exception e)
{
System.out.println(e);
}
}

Input Files :

Intermediate Code :
300) (IS,5) (1) (S,1)
301) (IS,4) (2) (S,2)
302) (IS,1) (2) (L,1)
303) (IS,5) (1) (L,2)
304) (IS,2) (2) (L,3)
302) (IS,1) (2) (L,4)
309) (IS,5) (1) (S,5)
310) (IS,3) (2) (L,5)
311) (IS,4) (1) (S,6)
312) (IS,3) (1) (S,5)
314) (IS,5) (3) (L,6)
315) (IS,10) (S,1)
316) (IS,10) (S,2)
Symbol Table :
1 A 317
2 C 318
3 AGAIN 303
4 LOOP 309
5 D 319
6 TEMP 331
7 NEXT 312
8 BACK 309

Literal Table :
1 ='1' 305
2 ='5' 306
3 ='3' 307
4 ='2' 308
5 ='3' 313
6 ='3' 333

Output :

Machine Level Code :


300) +5 1 317
301) +4 2 318
302) +1 2 305
303) +5 1 306
304) +2 2 307
302) +1 2 308
309) +5 1 319
310) +3 2 313
311) +4 1 331
312) +3 1 319
314) +5 3 333
315) +10 0 317
316) +10 0 318

You might also like