package assembler;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class PassOne {
int lc=0;
int libtab_ptr=0,pooltab_ptr=0;
int symIndex=0,litIndex=0;
LinkedHashMap<String, TableRow> SYMTAB;
ArrayList<TableRow> LITTAB;
ArrayList<Integer> POOLTAB;
private BufferedReader br;
public PassOne()
{
SYMTAB =new LinkedHashMap<>();
LITTAB=new ArrayList<>();
POOLTAB=new ArrayList<>();
lc=0;
[Link](0);
}
public static void main(String[] args) {
PassOne one=new PassOne();
try
{
[Link]();
}
catch (Exception e) {
[Link]("Error: "+e);// TODO: handle exception
}
}
public void parseFile() throws Exception
{String prev="";
String line,code;
br = new BufferedReader(new FileReader("C:\\Eclipse\\LabOne\\src\\
assembler\\[Link]"));
BufferedWriter bw=new BufferedWriter(new FileWriter("[Link]"));
INSTtable lookup=new INSTtable();
while((line=[Link]())!=null)
{
String parts[]=[Link]("\\s+");
if(!parts[0].isEmpty()) //processing of label
{
if([Link](parts[0]))
[Link](parts[0], new TableRow(parts[0], lc,
[Link](parts[0]).getIndex()));
else
[Link](parts[0],new TableRow(parts[0], lc, ++symIndex));
}
if(parts[1].equals("LTORG"))
{
int ptr=[Link](pooltab_ptr);
for(int j=ptr;j<libtab_ptr;j++)
{
lc++;
[Link](j, new TableRow([Link](j).getSymbol(),lc));
code="(DL,01)\t(C,"+[Link](j).symbol+")";
[Link](code+"\n");
}
pooltab_ptr++;
[Link](libtab_ptr);
}
if(parts[1].equals("START"))
{
lc=expr(parts[2]);
code="(AD,01)\t(C,"+lc+")";
[Link](code+"\n");
prev="START";
}
else if(parts[1].equals("ORIGIN"))
{
lc=expr(parts[2]);
String splits[]=parts[2].split("\\+"); //Same for - SYMBOL //Add
code
code="(AD,03)\t(S,"+[Link](splits[0]).getIndex()+")
+"+[Link](splits[1]);
[Link](code+"\n");
}
//Now for EQU
if(parts[1].equals("EQU"))
{
int loc=expr(parts[2]);
//below If conditions are optional as no IC is generated for them
if(parts[2].contains("+"))
{
String splits[]=parts[2].split("\\+");
code="(AD,04)\t(S,"+[Link](splits[0]).getIndex()+")
+"+[Link](splits[1]);
}
else if(parts[2].contains("-"))
{
String splits[]=parts[2].split("\\-");
code="(AD,04)\t(S,"+[Link](splits[0]).getIndex()
+")-"+[Link](splits[1]);
}
else
{
code="(AD,04)\t(C,"+[Link](parts[2]+")");
}
[Link](code+"\n");
if([Link](parts[0]))
[Link](parts[0], new
TableRow(parts[0],loc,[Link](parts[0]).getIndex())) ;
else
[Link](parts[0], new TableRow(parts[0],loc,++symIndex));
if(parts[1].equals("DC"))
{
lc++;
int constant=[Link](parts[2].replace("'",""));
code="(DL,01)\t(C,"+constant+")";
[Link](code+"\n");
}
else if(parts[1].equals("DS"))
{
int size=[Link](parts[2].replace("'", ""));
code="(DL,02)\t(C,"+size+")";
[Link](code+"\n");
/*if([Link]("START"))
{
lc=lc+size-1;//[Link]("here");
}
else
*/ lc=lc+size;
prev="";
}
if([Link](parts[1]).equals("IS"))
{
code="(IS,0"+[Link](parts[1])+")\t";
int j=2;
String code2="";
while(j<[Link])
{
parts[j]=parts[j].replace(",", "");
if([Link](parts[j]).equals("RG"))
{
code2+=[Link](parts[j])+"\t";
}
else
{
if(parts[j].contains("="))
{
parts[j]=parts[j].replace("=", "").replace("'", "");
[Link](new TableRow(parts[j], -1,++litIndex));
libtab_ptr++;
code2+="(L,"+(litIndex)+")";
}
else if([Link](parts[j]))
{
int ind=[Link](parts[j]).getIndex();
code2+= "(S,0"+ind+")";
}
else
{
[Link](parts[j], new TableRow(parts[j],-1,+
+symIndex));
int ind=[Link](parts[j]).getIndex();
code2+= "(S,0"+ind+")";
}
}
j++;
}
lc++;
code=code+code2;
[Link](code+"\n");
}
if(parts[1].equals("END"))
{
int ptr=[Link](pooltab_ptr);
for(int j=ptr;j<libtab_ptr;j++)
{
lc++;
[Link](j, new TableRow([Link](j).getSymbol(),lc));
code="(DL,01)\t(C,"+[Link](j).symbol+")";
[Link](code+"\n");
}
pooltab_ptr++;
[Link](libtab_ptr);
code="(AD,02)";
[Link](code+"\n");
}
}
[Link]();
printSYMTAB();
//Printing Literal table
PrintLITTAB();
printPOOLTAB();
}
void PrintLITTAB() throws IOException
{
BufferedWriter bw=new BufferedWriter(new FileWriter("[Link]"));
[Link]("\nLiteral Table\n");
//Processing LITTAB
for(int i=0;i<[Link]();i++)
{
TableRow row=[Link](i);
[Link](i+"\t"+[Link]()+"\t"+[Link]());
[Link]((i+1)+"\t"+[Link]()+"\t"+[Link]()+"\n");
}
[Link]();
}
void printPOOLTAB() throws IOException
{
BufferedWriter bw=new BufferedWriter(new FileWriter("[Link]"));
[Link]("\nPOOLTAB");
[Link]("Index\t#first");
for (int i = 0; i < [Link](); i++) {
[Link](i+"\t"+[Link](i));
[Link]((i+1)+"\t"+[Link](i)+"\n");
}
[Link]();
}
void printSYMTAB() throws IOException
{
BufferedWriter bw=new BufferedWriter(new FileWriter("[Link]"));
//Printing Symbol Table
[Link]<String> iterator = [Link]().iterator();
[Link]("SYMBOL TABLE");
while ([Link]()) {
String key = [Link]().toString();
TableRow value = [Link](key);
[Link]([Link]()+"\t" + [Link]()+"\
t"+[Link]());
[Link]([Link]()+"\t" + [Link]()+"\
t"+[Link]()+"\n");
}
[Link]();
}
public int expr(String str)
{
int temp=0;
if([Link]("+"))
{
String splits[]=[Link]("\\+");
temp=[Link](splits[0]).getAddress()+[Link](splits[1]);
}
else if([Link]("-"))
{
String splits[]=[Link]("\\-");
temp=[Link](splits[0]).getAddress()-
([Link](splits[1]));
}
else
{
temp=[Link](str);
}
return temp;
}
}