Spos Assignment Pass 1
Spos Assignment Pass 1
import java.io.*;
import java.util.*;
class Mnemonic
{
String mname;
String stmnt;
String opcode;
String length;
}
class t_data //to pass the complete data via object to the file
{
int sno;
String value;
int addr;
t_data()
{
addr = 0;
}
}
public class Pass1
{
public static void main(String[] args)throws Exception
{
String st;
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
System.out.println("Tokens:");
while(itr.hasNext())
{
System.out.print(itr.next()+ "\n");
}
br.close();
String st1;
//System.out.println(str.nextToken());
}
}
br1.close();
for(int i=0;i<m.size();i++)
{
System.out.println(m.get(i).mname);
System.out.println(m.get(i).stmnt);
System.out.println(m.get(i).opcode);
System.out.println(m.get(i).length);
} */
int check=0;
int flag = 0;
int f = 0;
int check_lit_after_ltorg = 0;
int cnt_pool = 0;
//---------------------------------------------------------------------------
-----------------------
if(str.equals("START"))
{
//System.out.println("Value of i in START"+i);
flag = 1;
lc=Integer.parseInt(tokens.get(i+1).toString()); //storing
the location counter value in the instruction start
i++; //to skip the address
//System.out.println("Value of i in after START"+i);
}
else if(str.contains("="))
{
if(cnt_pool == 0) //first literal and hence first entry in
pool table
{
//System.out.println("RED");
fr3.write("# 1");
cnt_pool++;
}
String temp = str.substring(2,3);
int tem = 0;
for(int y = 0;y < array_lit.size();y++)
{
if(array_lit.get(y).value.equals(temp))
{
tem = 1;
break;
}
}
if(check_lit_after_ltorg > 0) //to check if the
LTORG instruction is executed
{
t_data a=new t_data();
cntlit++;
a.sno=cntlit;
a.value=new String(temp);
array_lit.add(a);
fr1.write(" ,"+"("+"L,"+cntlit+")"); //to
make entry in intermediate code
else if((str.equals("AREG"))||(str.equals("BREG"))||
(str.equals("CREG"))||(str.equals("DREG")))
{
fr1.write(" "+str); //write to intermediate code file
//System.out.println("Value of i in reg"+i);
}
else if((str.equals("MOVER"))||(str.equals("MOVEM"))||
(str.equals("ADD"))||(str.equals("SUB"))||(str.equals("MULT"))||
(str.equals("STOP"))||(str.equals("BC")))
{
for(int y=0;y<array_sym.size();y++)
{
if(array_sym.get(y).value.equals(var)) //to
retrieve the address of LOOP from the symbol table
{
p=array_sym.get(y).addr;
break;
}
}
lc=p+a;
i++; //incrementing the token arraylist index
}
if(str.equals("EQU"))
{
fr1.write("\n"+lc+" (AD,04)
"+tokens.get(i+1).toString()); //write to intermediate code file
int address=0;
//System.out.println("Value of i in ltorg"+i);
//fr1.write("\n"+" "+"(AD,05)
"+tokens.get(i+1).toString()); //write to intermediate code file
i++;
for(int k=0;k<array_sym.size();k++)
{
if(array_sym.get(k).value.equals(str))
{
//System.out.println(tokens.get(i));
//System.out.println("yellow");
//System.out.println("Value of i in ="+i);
if(tokens.get(i+1).equals(":"))
{
array_sym.get(k).addr=lc;
i++; //to skip :
// System.out.println("Value of i in
="+i);
}
else if((tokens.get(i+1).equals("DS")) ||
(tokens.get(i+1).equals("EQU")) )
{
// System.out.println(i);
if(tokens.get(i+1).equals("DS"))
{
for(int y=0;y<array_sym.size();y+
+)
{
if(array_sym.get(y).value.equals(str))
{
array_sym.get(y).addr =
lc;
}
}
// System.out.println(i);
}
if(tokens.get(i+1).equals("EQU"))
{
f = 1; //inorder to skip the
if(f==0) loop
break;
}
}
else
{
fr1.write(" , "+str);
//System.out.println("Value of i in
after second a"+i);
}
f=1;
break;
}
}
if(f==0)
{
t_data b=new t_data();
cntsym++;
b.sno=cntsym;
b.value=new String(str);
//---------------------------------------------------------------------
--------------------------------------
fr3.close();
FileWriter fw= new FileWriter(lit);
for(int y=0;y<array_lit.size();y++)
{
fw.write(array_lit.get(y).sno+" "+array_lit.get(y).value+"
"+array_lit.get(y).addr+"\n");
}
fw.close();
OUTPUT:
SOURCE CODE:
Contents of file:
START 200
MOVER AREG,='5'
MOVEM AREG,A
LOOP : MOVER AREG,A
MOVER CREG,B
ADD CREG,='1'
BC CREG,NEXT
LTORG
='5'
='1'
NEXT : SUB AREG,='1'
BC AREG,BACK
LAST : STOP
ORIGIN LOOP+2
MULT CREG,B
ORIGIN LAST+1
A DS 1
BACK EQU LOOP
B DS 1
END
='1'
INTERMEDIATE CODE:
200 (IS,04), AREG ,(L,1)
203 (IS,05), AREG , A
206 (IS,04), AREG , A
209 (IS,04), CREG , B
212 (IS,01), CREG ,(L,2)
215 (IS,07), CREG , NEXT
218 (AD,05) 5
219 (AD,05) 1
220 (IS,02), AREG ,(L,3)
223 (IS,07), AREG , BACK
226 (IS,00),
227 (AD,03) LOOP+2
208 (IS,03), CREG , B
211 (AD,03) LAST+1
227 (DL,02) 1
228 (AD,04) LOOP
228 (DL,02) 1
229 (AD,02)
1
LITERAL_TABLE:
1 5 218
2 1 219
3 1 229
MNEUMONICS_TABLE
STOP IS 00 1
ADD IS 01 3
SUB IS 02 3
MULT IS 03 3
MOVER IS 04 3
MOVEM IS 05 3
BC IS 07 3
POOL TABLE:
# 1
# 3
SYMBOL TABLE:
1 A 227
2 LOOP 206
3 B 228
4 NEXT 220
5 BACK 206
6 LAST 226