Advertisement
Guest User

a

a guest
Apr 21st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.32 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package sicxe;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileReader;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.Enumeration;
  14. import java.util.Hashtable;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17.  
  18. /**
  19.  *
  20.  * @author BCC
  21.  */
  22. public class SICXE {
  23.    static ArrayList strs1 = new ArrayList();
  24.    static ArrayList labels = new ArrayList();
  25.    static ArrayList insts = new ArrayList();
  26.    static ArrayList op = new ArrayList();
  27.    static ArrayList com = new ArrayList();
  28.    static ArrayList pc = new ArrayList();
  29.    static Hashtable<String, String> symboltable = new Hashtable<String, String>();
  30.    static Hashtable<String, String> instructionset = new Hashtable<String, String>();
  31.    static Hashtable<String, String> instructionsize = new Hashtable<String, String>();
  32.    static int LocationCounter;
  33.    static int CurrentLocation;
  34.    static int StartAdress = 0;
  35.    static int Base;
  36.    static String value;
  37.    static boolean vflag = false;
  38.    static boolean pflag = true;
  39.    static boolean bflag = false;
  40.    
  41.    
  42.    public static void getInstructions() throws IOException
  43.    {
  44.        try {
  45.            BufferedReader mr1 = new BufferedReader(new FileReader("C:\\Users\\BCC\\Desktop\\IS.txt"));
  46.            BufferedReader mr = new BufferedReader(new FileReader("C:\\Users\\BCC\\Desktop\\IS.txt"));
  47.  
  48.             while(mr1.readLine()!=null)             //Adding the instruction set to a hash map
  49.         {
  50.             String temp = mr.readLine();
  51.             String[] tempo = temp.split("\\t");
  52.            
  53.             instructionset.put(tempo[0], tempo[1]);
  54.             instructionsize.put(tempo[0], tempo[2]);  
  55.         }
  56.        } catch (FileNotFoundException ex) {
  57.            System.out.println("Error");
  58.        }  
  59.    }
  60.    
  61.    public static void readprogram()
  62.    {
  63.           try
  64.         {
  65.        FileReader fr = new FileReader("test1.txt");
  66.        BufferedReader br = new BufferedReader(fr);
  67.        ArrayList strs = new ArrayList();
  68.        String str = null;
  69.        
  70.        while((str=br.readLine()) != null)
  71.        {
  72.            strs.add(str);
  73.        }
  74.        strs1=strs;
  75.      
  76.         }
  77.         catch (IOException e)
  78.         {
  79.             System.out.println("file not found");
  80.         }
  81.            
  82.          
  83.           for (int i = 0; i<strs1.size(); i++)
  84.           {
  85.               String line = (String) strs1.get(i);
  86.              
  87.               char[] label = new char[8];
  88.               char[] inst = new char[6];
  89.               char[] operand = new char[18];
  90.               char[] comment = new char[31];
  91.               int x = 0;
  92.               int y = 0;
  93.               int z = 0;
  94.               int c = 0;
  95.              
  96.               if (line.charAt(0) != 46)
  97.               {
  98.                  for (int j = 0; j<line.length(); j++)
  99.                 {
  100.                   if (j<8)
  101.                   {
  102.                       label[x]=line.charAt(j);
  103.                       x++;
  104.                   }
  105.                  
  106.                   if (j>8 && j<15)
  107.                   {
  108.                       inst[y] = line.charAt(j);
  109.                       y++;
  110.                   }
  111.                  
  112.                   if (j>16 && j<35)
  113.                   {
  114.                       operand[z] = line.charAt(j);
  115.                       z++;
  116.                   }
  117.                  
  118.                   if (j>35 && j<66)
  119.                   {
  120.                       comment[c] = line.charAt(j);
  121.                       c++;
  122.                   }
  123.                  
  124.                 }
  125.              
  126.                    if (line.length()<35)
  127.                 {
  128.                      comment[c] = 32;
  129.                 }
  130.              
  131.              
  132.                    String temp1 = new StringBuilder().append(label).toString();
  133.                    labels.add(temp1);
  134.                    String temp2 = new StringBuilder().append(inst).toString();
  135.                    insts.add(temp2);
  136.                    String temp3 = new StringBuilder().append(operand).toString();
  137.                    op.add(temp3);
  138.                    String temp4 = new StringBuilder().append(comment).toString();
  139.                    com.add(temp4);
  140.                    
  141.              
  142.                 if ("START".equalsIgnoreCase(temp2.trim()))
  143.                 {
  144.                     LocationCounter = Integer.parseInt(temp3.trim(), 16);
  145.                     StartAdress = LocationCounter;
  146.                     pc.add(LocationCounter);
  147.                     CurrentLocation = LocationCounter;
  148.                 }
  149.                
  150.                 else if ("RESW".equalsIgnoreCase(temp2.trim()))
  151.                 {
  152.                     pc.add(LocationCounter);
  153.                     int jump = Integer.parseInt(temp3.trim());
  154.                     jump = jump * 3;
  155.                     CurrentLocation = LocationCounter;
  156.                     LocationCounter = LocationCounter + jump;    
  157.                 }
  158.                
  159.                 else if ("RESB".equalsIgnoreCase(temp2.trim()))
  160.                 {
  161.                     pc.add(LocationCounter);
  162.                     CurrentLocation = LocationCounter;
  163.                     int size = Integer.parseInt(temp3.trim());
  164.                     LocationCounter = LocationCounter + size;
  165.                 }
  166.                
  167.                 else if ("BYTE".equalsIgnoreCase(temp2.trim()))
  168.                 {
  169.                     pc.add(LocationCounter);
  170.                     CurrentLocation = LocationCounter;
  171.                     int size;
  172.                     char t = temp3.charAt(0);
  173.                        
  174.                     if (t == 67 || t == 99)
  175.                     {
  176.                         String a = temp3.trim();
  177.                         String b = a.substring(2, a.length()-1);
  178.                         value = b;
  179.                         size = b.length();
  180.                         LocationCounter = LocationCounter + size;
  181.                         vflag = true;
  182.                     }
  183.                    
  184.                     else if (t == 88 || t == 120)
  185.                     {
  186.                         String a = temp3.trim();
  187.                         String b = a.substring(2, a.length()-1);
  188.                         value = b;
  189.                         size = b.length();
  190.                         if (size%2 == 1)
  191.                         {
  192.                             size = (size + 1)/2;
  193.                         }
  194.                         else
  195.                             size = size/2;
  196.                        
  197.                         LocationCounter = LocationCounter + size;
  198.                         vflag = true;
  199.                     }
  200.                    
  201.                     else
  202.                     {
  203.                         System.out.println("Error in byte");
  204.                     }
  205.                    
  206.                 }
  207.                
  208.                 else if ("WORD".equalsIgnoreCase(temp2.trim()))
  209.                 {
  210.                     pc.add(LocationCounter);
  211.                     CurrentLocation = LocationCounter;
  212.                     int size;
  213.                     char t = temp3.charAt(0);
  214.                        
  215.                     if (t == 67 || t == 99)
  216.                     {
  217.                         String a = temp3.trim();
  218.                         String b = a.substring(2, a.length()-1);
  219.                         value = b;
  220.                         double f = b.length()/3.0;
  221.                         size =(int) Math.ceil(f);
  222.                         size = size*3;
  223.                         LocationCounter = LocationCounter + size;
  224.                         vflag = true;
  225.                     }
  226.                    
  227.                     else if (t == 88 || t == 120)
  228.                     {
  229.                         String a = temp3.trim();
  230.                         String b = a.substring(2, a.length()-1);
  231.                         value = b;
  232.                         int h = b.length();
  233.                         if (h%2 == 1)
  234.                         {
  235.                             h = (h + 1)/2;
  236.                         }
  237.                         else
  238.                             h = h/2;
  239.                        
  240.                         double f = h/3.0;
  241.                         size = (int) Math.ceil(f);
  242.                         size = size * 3;
  243.                         LocationCounter = LocationCounter + size;
  244.                         vflag = true;
  245.                     }
  246.                    
  247.                     else
  248.                     {
  249.                         System.out.println("Error in word");
  250.                     }
  251.                 }
  252.                
  253.                 else if (temp2.charAt(0) == 43)
  254.                 {
  255.                     String s = temp2.substring(1);
  256.                     s = s.trim();
  257.                     if (Integer.parseInt(instructionsize.get(s)) == 3)
  258.                     {
  259.                     pc.add(LocationCounter);
  260.                     CurrentLocation = LocationCounter;
  261.                     LocationCounter = LocationCounter + 4;
  262.                     }
  263.                     else
  264.                     {
  265.                         System.out.println("Error ... " + temp2 + " is not supported");
  266.                         break;
  267.                     }
  268.                 }
  269.                
  270.                 else if ("BASE".equalsIgnoreCase(temp2.trim()))
  271.                 {
  272.                     pc.add(LocationCounter);
  273.                     CurrentLocation = LocationCounter;
  274.                     Base = Integer.parseInt(temp3.trim(), 16);
  275.                 }
  276.                
  277.                
  278.                 else
  279.                 {
  280.                     if (instructionsize.get(temp2.trim()) != null)
  281.                     {
  282.                         pc.add(LocationCounter);
  283.                         CurrentLocation = LocationCounter;
  284.                         int size = Integer.parseInt(instructionsize.get(temp2.trim()));
  285.                         LocationCounter = LocationCounter + size;
  286.                     }
  287.                     else
  288.                     {
  289.                         System.out.println(temp2.trim() + " is not supported");
  290.                     }
  291.                 }
  292.                
  293.                 if (temp1.charAt(0) != 32)
  294.                 {
  295.                     if (symboltable.get(temp1) == null)
  296.                     {
  297.                         if (vflag)
  298.                         {
  299.                             symboltable.put(temp1, value);
  300.                         }
  301.                         else
  302.                         {
  303.                             symboltable.put(temp1, Integer.toHexString(CurrentLocation));
  304.                         }
  305.                         vflag = false;
  306.                     }
  307.                     else
  308.                     {
  309.                         System.out.println("*********Label " + temp1 + " has already been defined");
  310.                     }
  311.                 }
  312.              
  313.                  
  314.               }
  315.              
  316.               else
  317.               {
  318.                   com.add(line);
  319.                   labels.add(null);
  320.                   insts.add(null);
  321.                   op.add(null);
  322.                   pc.add(null);
  323.               }
  324.            
  325.               String s = (String) com.get(i);
  326.                if (s.charAt(0) != 46)
  327.                {
  328.                    String e = "0";
  329.                    String g = Integer.toHexString((int) pc.get(i));
  330.                    int d = g.length();
  331.                    for (int q = d; q<5; q++)
  332.                    {
  333.                       e = e.concat("0");
  334.                    }
  335.                    
  336.                    e = e.concat(g);
  337.                    
  338.                    System.out.println(e.toUpperCase() + "\t" + labels.get(i) + "\t" + insts.get(i) + "\t" + op.get(i) + "\t" + com.get(i));
  339.                }
  340.                else
  341.                    System.out.println(s);
  342.              
  343.              
  344.           }
  345.              
  346.           }
  347.  
  348.    
  349.  
  350.  
  351.     public static void main(String[] args) throws IOException {
  352.        
  353.         int flag = 0;
  354.         getInstructions();
  355.         readprogram();
  356.        
  357.        /*for (int i = 0; i<insts.size(); i++)
  358.        {
  359.            try
  360.            {
  361.                String s = (String) com.get(i);
  362.                if (s.charAt(0) != 46)
  363.                {
  364.                    System.out.println(Integer.toHexString((int) pc.get(i)).toUpperCase() + "\t" + labels.get(i) + "\t" + insts.get(i) + "\t" + op.get(i) + "\t" + com.get(i));
  365.                }
  366.                else
  367.                    System.out.println(s);
  368.            }
  369.            catch(Exception ex)
  370.                    {
  371.                        System.out.println("Error occured");
  372.                        flag = 1;
  373.                    }
  374.        }*/
  375.        
  376.        if (flag == 0)
  377.        {
  378.            Enumeration <String>keys = symboltable.keys();
  379.        
  380.            System.out.println("\n\n");
  381.        while (keys.hasMoreElements())
  382.        {
  383.            String key = keys.nextElement();
  384.            System.out.println(""+key+" : " + symboltable.get(key).toUpperCase());
  385.        }
  386.        
  387.        }
  388.      
  389.        int ProgramSize = LocationCounter - StartAdress;
  390.        System.out.println(Integer.toHexString(ProgramSize));
  391.     }
  392.  
  393.    
  394.    
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement