Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package sicxe;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author BCC
- */
- public class SICXE {
- static ArrayList strs1 = new ArrayList();
- static ArrayList labels = new ArrayList();
- static ArrayList insts = new ArrayList();
- static ArrayList op = new ArrayList();
- static ArrayList com = new ArrayList();
- static ArrayList pc = new ArrayList();
- static Hashtable<String, String> symboltable = new Hashtable<String, String>();
- static Hashtable<String, String> instructionset = new Hashtable<String, String>();
- static Hashtable<String, String> instructionsize = new Hashtable<String, String>();
- static int LocationCounter;
- static int CurrentLocation;
- static int StartAdress = 0;
- static int Base;
- static String value;
- static boolean vflag = false;
- static boolean pflag = true;
- static boolean bflag = false;
- public static void getInstructions() throws IOException
- {
- try {
- BufferedReader mr1 = new BufferedReader(new FileReader("C:\\Users\\BCC\\Desktop\\IS.txt"));
- BufferedReader mr = new BufferedReader(new FileReader("C:\\Users\\BCC\\Desktop\\IS.txt"));
- while(mr1.readLine()!=null) //Adding the instruction set to a hash map
- {
- String temp = mr.readLine();
- String[] tempo = temp.split("\\t");
- instructionset.put(tempo[0], tempo[1]);
- instructionsize.put(tempo[0], tempo[2]);
- }
- } catch (FileNotFoundException ex) {
- System.out.println("Error");
- }
- }
- public static void readprogram()
- {
- try
- {
- FileReader fr = new FileReader("test1.txt");
- BufferedReader br = new BufferedReader(fr);
- ArrayList strs = new ArrayList();
- String str = null;
- while((str=br.readLine()) != null)
- {
- strs.add(str);
- }
- strs1=strs;
- }
- catch (IOException e)
- {
- System.out.println("file not found");
- }
- for (int i = 0; i<strs1.size(); i++)
- {
- String line = (String) strs1.get(i);
- char[] label = new char[8];
- char[] inst = new char[6];
- char[] operand = new char[18];
- char[] comment = new char[31];
- int x = 0;
- int y = 0;
- int z = 0;
- int c = 0;
- if (line.charAt(0) != 46)
- {
- for (int j = 0; j<line.length(); j++)
- {
- if (j<8)
- {
- label[x]=line.charAt(j);
- x++;
- }
- if (j>8 && j<15)
- {
- inst[y] = line.charAt(j);
- y++;
- }
- if (j>16 && j<35)
- {
- operand[z] = line.charAt(j);
- z++;
- }
- if (j>35 && j<66)
- {
- comment[c] = line.charAt(j);
- c++;
- }
- }
- if (line.length()<35)
- {
- comment[c] = 32;
- }
- String temp1 = new StringBuilder().append(label).toString();
- labels.add(temp1);
- String temp2 = new StringBuilder().append(inst).toString();
- insts.add(temp2);
- String temp3 = new StringBuilder().append(operand).toString();
- op.add(temp3);
- String temp4 = new StringBuilder().append(comment).toString();
- com.add(temp4);
- if ("START".equalsIgnoreCase(temp2.trim()))
- {
- LocationCounter = Integer.parseInt(temp3.trim(), 16);
- StartAdress = LocationCounter;
- pc.add(LocationCounter);
- CurrentLocation = LocationCounter;
- }
- else if ("RESW".equalsIgnoreCase(temp2.trim()))
- {
- pc.add(LocationCounter);
- int jump = Integer.parseInt(temp3.trim());
- jump = jump * 3;
- CurrentLocation = LocationCounter;
- LocationCounter = LocationCounter + jump;
- }
- else if ("RESB".equalsIgnoreCase(temp2.trim()))
- {
- pc.add(LocationCounter);
- CurrentLocation = LocationCounter;
- int size = Integer.parseInt(temp3.trim());
- LocationCounter = LocationCounter + size;
- }
- else if ("BYTE".equalsIgnoreCase(temp2.trim()))
- {
- pc.add(LocationCounter);
- CurrentLocation = LocationCounter;
- int size;
- char t = temp3.charAt(0);
- if (t == 67 || t == 99)
- {
- String a = temp3.trim();
- String b = a.substring(2, a.length()-1);
- value = b;
- size = b.length();
- LocationCounter = LocationCounter + size;
- vflag = true;
- }
- else if (t == 88 || t == 120)
- {
- String a = temp3.trim();
- String b = a.substring(2, a.length()-1);
- value = b;
- size = b.length();
- if (size%2 == 1)
- {
- size = (size + 1)/2;
- }
- else
- size = size/2;
- LocationCounter = LocationCounter + size;
- vflag = true;
- }
- else
- {
- System.out.println("Error in byte");
- }
- }
- else if ("WORD".equalsIgnoreCase(temp2.trim()))
- {
- pc.add(LocationCounter);
- CurrentLocation = LocationCounter;
- int size;
- char t = temp3.charAt(0);
- if (t == 67 || t == 99)
- {
- String a = temp3.trim();
- String b = a.substring(2, a.length()-1);
- value = b;
- double f = b.length()/3.0;
- size =(int) Math.ceil(f);
- size = size*3;
- LocationCounter = LocationCounter + size;
- vflag = true;
- }
- else if (t == 88 || t == 120)
- {
- String a = temp3.trim();
- String b = a.substring(2, a.length()-1);
- value = b;
- int h = b.length();
- if (h%2 == 1)
- {
- h = (h + 1)/2;
- }
- else
- h = h/2;
- double f = h/3.0;
- size = (int) Math.ceil(f);
- size = size * 3;
- LocationCounter = LocationCounter + size;
- vflag = true;
- }
- else
- {
- System.out.println("Error in word");
- }
- }
- else if (temp2.charAt(0) == 43)
- {
- String s = temp2.substring(1);
- s = s.trim();
- if (Integer.parseInt(instructionsize.get(s)) == 3)
- {
- pc.add(LocationCounter);
- CurrentLocation = LocationCounter;
- LocationCounter = LocationCounter + 4;
- }
- else
- {
- System.out.println("Error ... " + temp2 + " is not supported");
- break;
- }
- }
- else if ("BASE".equalsIgnoreCase(temp2.trim()))
- {
- pc.add(LocationCounter);
- CurrentLocation = LocationCounter;
- Base = Integer.parseInt(temp3.trim(), 16);
- }
- else
- {
- if (instructionsize.get(temp2.trim()) != null)
- {
- pc.add(LocationCounter);
- CurrentLocation = LocationCounter;
- int size = Integer.parseInt(instructionsize.get(temp2.trim()));
- LocationCounter = LocationCounter + size;
- }
- else
- {
- System.out.println(temp2.trim() + " is not supported");
- }
- }
- if (temp1.charAt(0) != 32)
- {
- if (symboltable.get(temp1) == null)
- {
- if (vflag)
- {
- symboltable.put(temp1, value);
- }
- else
- {
- symboltable.put(temp1, Integer.toHexString(CurrentLocation));
- }
- vflag = false;
- }
- else
- {
- System.out.println("*********Label " + temp1 + " has already been defined");
- }
- }
- }
- else
- {
- com.add(line);
- labels.add(null);
- insts.add(null);
- op.add(null);
- pc.add(null);
- }
- String s = (String) com.get(i);
- if (s.charAt(0) != 46)
- {
- String e = "0";
- String g = Integer.toHexString((int) pc.get(i));
- int d = g.length();
- for (int q = d; q<5; q++)
- {
- e = e.concat("0");
- }
- e = e.concat(g);
- System.out.println(e.toUpperCase() + "\t" + labels.get(i) + "\t" + insts.get(i) + "\t" + op.get(i) + "\t" + com.get(i));
- }
- else
- System.out.println(s);
- }
- }
- public static void main(String[] args) throws IOException {
- int flag = 0;
- getInstructions();
- readprogram();
- /*for (int i = 0; i<insts.size(); i++)
- {
- try
- {
- String s = (String) com.get(i);
- if (s.charAt(0) != 46)
- {
- 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));
- }
- else
- System.out.println(s);
- }
- catch(Exception ex)
- {
- System.out.println("Error occured");
- flag = 1;
- }
- }*/
- if (flag == 0)
- {
- Enumeration <String>keys = symboltable.keys();
- System.out.println("\n\n");
- while (keys.hasMoreElements())
- {
- String key = keys.nextElement();
- System.out.println(""+key+" : " + symboltable.get(key).toUpperCase());
- }
- }
- int ProgramSize = LocationCounter - StartAdress;
- System.out.println(Integer.toHexString(ProgramSize));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement