bunny007

Code 2

Nov 20th, 2022
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.util.ArrayList;
  6.  
  7. public class Pass2 {
  8.     ArrayList<TableRow> SYMTAB,LITTAB;
  9.  
  10.     public Pass2()
  11.     {
  12.         SYMTAB=new ArrayList<>();
  13.         LITTAB=new ArrayList<>();
  14.     }
  15.     public static void main(String[] args) {
  16.         Pass2 pass2=new Pass2();
  17.        
  18.         try {
  19.             pass2.generateCode("IC.txt");
  20.         } catch (Exception e) {
  21.             // TODO Auto-generated catch block
  22.             e.printStackTrace();
  23.         }
  24.     }
  25.     public void readtables()
  26.     {
  27.         BufferedReader br;
  28.         String line;
  29.         try
  30.         {
  31.             br=new BufferedReader(new FileReader("SYMTAB.txt"));
  32.             while((line=br.readLine())!=null)
  33.             {
  34.                 String parts[]=line.split("\\s+");
  35.                 SYMTAB.add(new TableRow(parts[1], Integer.parseInt(parts[2]),Integer.parseInt(parts[0]) ));
  36.             }
  37.             br.close();
  38.             br=new BufferedReader(new FileReader("LITTAB.txt"));
  39.             while((line=br.readLine())!=null)
  40.             {
  41.                 String parts[]=line.split("\\s+");
  42.                 LITTAB.add(new TableRow(parts[1], Integer.parseInt(parts[2]),Integer.parseInt(parts[0])));
  43.             }
  44.             br.close();
  45.         }
  46.         catch (Exception e) {
  47.             System.out.println(e.getMessage());
  48.         }
  49.     }
  50.  
  51.     public void generateCode(String filename) throws Exception
  52.     {
  53.         readtables();
  54.         BufferedReader br=new BufferedReader(new FileReader(filename));
  55.  
  56.         BufferedWriter bw=new BufferedWriter(new FileWriter("PASS2.txt"));
  57.         String line,code;
  58.         while((line=br.readLine())!=null)
  59.         {
  60.             String parts[]=line.split("\\s+");
  61.             if(parts[0].contains("AD")||parts[0].contains("DL,02"))
  62.             {
  63.                 bw.write("\n");
  64.                 continue;
  65.             }
  66.             else if(parts.length==2)
  67.             {
  68.                 if(parts[0].contains("DL")) //DC INSTR
  69.                 {
  70.                     parts[0]=parts[0].replaceAll("[^0-9]", "");
  71.                     if(Integer.parseInt(parts[0])==1)
  72.                     {
  73.                         int constant=Integer.parseInt(parts[1].replaceAll("[^0-9]", ""));
  74.                         code="00\t0\t"+String.format("%03d", constant)+"\n";
  75.                         bw.write(code);
  76.                        
  77.                        
  78.                     }
  79.                 }
  80.                 else if(parts[0].contains("IS"))
  81.                 {
  82.                     int opcode=Integer.parseInt(parts[0].replaceAll("[^0-9]", ""));
  83.                     if(opcode==10)
  84.                     {
  85.                         if(parts[1].contains("S"))
  86.                         {
  87.                             int symIndex=Integer.parseInt(parts[1].replaceAll("[^0-9]", ""));
  88.                             code=String.format("%02d", opcode)+"\t0\t"+String.format("%03d", SYMTAB.get(symIndex-1).getAddress())+"\n";
  89.                             bw.write(code);
  90.                         }
  91.                         else if(parts[1].contains("L"))
  92.                         {
  93.                             int symIndex=Integer.parseInt(parts[1].replaceAll("[^0-9]", ""));
  94.                             code=String.format("%02d", opcode)+"\t0\t"+String.format("%03d", LITTAB.get(symIndex-1).getAddress())+"\n";
  95.                             bw.write(code);
  96.                         }
  97.                        
  98.                     }
  99.                 }
  100.             }
  101.             else if(parts.length==1 && parts[0].contains("IS"))
  102.             {
  103.                 int opcode=Integer.parseInt(parts[0].replaceAll("[^0-9]", ""));
  104.                 code=String.format("%02d", opcode)+"\t0\t"+String.format("%03d", 0)+"\n";
  105.                 bw.write(code);
  106.             }
  107.             else if(parts[0].contains("IS") && parts.length==3) //All OTHER IS INSTR
  108.             {
  109.             int opcode= Integer.parseInt(parts[0].replaceAll("[^0-9]", ""));
  110.            
  111.             int regcode=Integer.parseInt(parts[1]);
  112.            
  113.             if(parts[2].contains("S"))
  114.             {
  115.                 int symIndex=Integer.parseInt(parts[2].replaceAll("[^0-9]", ""));
  116.                 code=String.format("%02d", opcode)+"\t"+regcode+"\t"+String.format("%03d", SYMTAB.get(symIndex-1).getAddress())+"\n";
  117.                 bw.write(code);
  118.             }
  119.             else if(parts[2].contains("L"))
  120.             {
  121.                 int symIndex=Integer.parseInt(parts[2].replaceAll("[^0-9]", ""));
  122.                 code=String.format("%02d", opcode)+"\t"+regcode+"\t"+String.format("%03d", LITTAB.get(symIndex-1).getAddress())+"\n";
  123.                 bw.write(code);
  124.             }      
  125.             }  
  126.         }
  127.         bw.close();
  128.         br.close();
  129.     }
  130. }
Add Comment
Please, Sign In to add comment