Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.jamessmith;
- import java.io.*;
- class ErrorTable {
- public static String report(int status){
- String s = "ERROR: ";
- switch(status){
- case 100:
- s += "Invalid Mneumonic";
- break;
- case 200:
- s += "Invalid Instruction Format";
- break;
- default:
- s += "Some error occured";
- break;
- }
- return s;
- }
- }
- class Program {
- private LinkedList instructions;
- private PrintWriter writer;
- /**
- * Constructor for program
- * @param f - the input file containing source code
- * @throws IOException
- */
- public Program(File f) throws IOException{
- instructions = new LinkedList();
- readInput(f);
- instructions.calculateAddresses();
- createLST();
- createOBJ();
- }
- /**
- * Reads the files for finding and deleting
- * @throws IOException
- */
- public void readInput(File f) throws IOException{
- FileReader reader;
- BufferedReader bReader;
- String input;
- String content;
- reader = new FileReader(f);
- bReader = new BufferedReader(reader);
- input = "";
- content = "";
- while((input = bReader.readLine()) != null){
- if(input.split("\\s+").length > 0){
- instructions.insert(input);
- }//checks if there is an empty line
- }
- bReader.close();
- reader.close();
- }//end readInput
- public void display(){
- instructions.display();
- }
- public void createLST() throws FileNotFoundException, UnsupportedEncodingException{
- writer = new PrintWriter("pgm.lst","UTF-8");
- writer.println(instructions.toLST());
- writer.close();
- }
- public void createOBJ() throws FileNotFoundException, UnsupportedEncodingException{
- writer = new PrintWriter("pgm.obj","UTF-8");
- writer.println(instructions.toOBJ());
- writer.close();
- }
- }//end program
- class SymbolTable {
- public static HashTable table;
- //Variables for hashing
- private static String[] inputArr; //The string array of the inputs
- private static int size; //Size of the HashTables
- public static void setupTable() throws IOException{
- table = new HashTable(size);
- createTable();
- }
- /**
- * Reads the files for finding and deleting
- * @throws IOException
- */
- public static String readInput() throws IOException{
- FileReader reader;
- BufferedReader bReader;
- String input;
- String content;
- reader = new FileReader(new File("SICOPS.txt"));
- bReader = new BufferedReader(reader);
- input = "";
- content = "";
- while((input = bReader.readLine()) != null){
- content+=input+";";
- }
- return content;
- }
- /**
- * Inserts the data into the tables
- * @throws IOException
- */
- public static void createTable() throws IOException{
- String s = readInput();//read input into string
- inputArr = s.split(";"); //Split string into array
- size=(2*inputArr.length); //Create odd size array
- int arrSize = findNextPrime(size);
- table = new HashTable(arrSize);
- //For each element inert into the table
- for(String element : inputArr){
- table.insert(new DataItem(element,false));
- }
- }//end insertions
- public static void insertWord(String data){
- table.insert(new DataItem(data,true));
- }
- /**
- * Finds the next prime greater than a min value
- * @param min - the value to find the next prime
- * @return - the next prime number
- */
- public static int findNextPrime(int min){
- boolean isPrime = true;
- boolean isFound = false;
- int num = min+1;
- while(!isFound){
- isPrime = true;
- for(int x=2;x<Math.sqrt(num) && isPrime;x++){
- if(num%x==0){
- isPrime = false;
- }//if the number is divisible by x
- }//for every number up to the square root(test it's primeness)
- if(!isPrime){
- num+=1;
- }else{
- isFound = true;
- }
- }//while the number isn't prime
- return num;
- }//end findNextPrime
- public static HashTable getTable(){
- return table;
- }
- }
- class DataItem {
- private String instruction;//Used for any type of data
- private boolean format4;
- private boolean sic;
- private String optCode;
- private int bytes;
- private boolean word;
- public DataItem(String s,boolean isWord){
- assignValues(s,isWord);
- }
- public void assignValues(String s,boolean isWord){
- word = isWord;
- String[] temp = s.split("\\s+");
- instruction = temp[0];
- optCode = temp[1];
- bytes = Integer.parseInt(temp[2]);
- if(temp[0].charAt(0) == '+'){
- format4 = true;
- }else if(temp[0].charAt(0) == '*'){
- sic = true;
- }else{
- sic=false;
- format4=false;
- }
- }
- //gets the content of the item
- public String getContent(){
- return String.format("%8s %8d",instruction,bytes);
- }
- public String getOptCode(){
- return optCode;
- }
- public boolean isWordStorage(){
- return word;
- }
- public int getBytes(){
- return bytes;
- }
- public String getInstruction(){
- return instruction;
- }
- public boolean compareTo(String s){
- return (s == instruction);
- }
- }//end DataItem
- class HashTable {
- private DataItem[] arr; // the array of items
- private int size; //The size of the array
- private int maxSearch;
- /**
- * Constructor for the HashTable
- * @param size - the size of the table
- */
- public HashTable(int size){
- this.size = size;
- arr = new DataItem[size];
- maxSearch = 1;
- }//end HashTable
- public DataItem find(String key){
- int walker;//walker to walk on array
- int count = 1;//counter for probing
- int newHash;
- boolean found = false;
- int hash = hash(key);
- if(arr[hash] != null && arr[hash].getInstruction().equals(key)){
- return arr[hash];
- }
- while(!found){
- walker = hash;
- newHash = quadP(count++);
- for(int x=newHash;x>0;x--){
- if(++walker == arr.length){
- walker = 0;
- }
- }
- if(arr[walker] != null && arr[walker].getInstruction().equals(key)){
- return arr[walker];
- }//if the array item is null or
- if(count>maxSearch){
- break;
- }
- }
- return null;
- }
- /**
- * Hashes the given string
- * @param s - the string to hash
- * @return - the value of the hashed string
- */
- public int hash(String s){
- int hash = 0;
- char[] arr = s.toCharArray();
- hash=arr[0];
- for(int x=1;x<arr.length;x++){
- hash=(hash*26+arr[x])%size;
- }//hash the string
- hash%=size;//Just in case its one character(will not affect if more)
- return hash;
- }//end hash
- /**
- * Displays the insertions of the data
- * @return - the formated string of info
- */
- public String displayTable(){
- String s ="";
- for(int x=0;x<arr.length;x++){
- if(arr[x] != null){
- s+=(String.format("Memory Locatiom:%-10d%-10s\n",x,arr[x].getContent()));
- }
- }
- return s;
- }//end displayInsertions
- public String displayWordStorage(){
- String s = String.format("\n%-10s%8s%8s\n","Table Location","Label","Address");
- for(int x=0;x<arr.length;x++){
- if(arr[x] != null && arr[x].isWordStorage()){
- s+=(String.format("%-18d%-8s%-10s\n",x,arr[x].getInstruction(),Integer.toHexString(Integer.parseInt(arr[x].getOptCode())).toUpperCase()));
- }
- }
- return s;
- }
- /**
- * Insertion method for hash table
- * @param item - the item to be inserted
- */
- public void insert(DataItem item){
- //Hash
- String s = item.getInstruction();
- int hash = hash(s);
- //insert
- if(arr[hash] == null){
- arr[hash] = item;
- }else{//if the index is null or there is a deleted item
- probeInsert(hash,item);
- }//else probe until valid insertion
- }//end insert
- /**
- * probeInsertion
- * @param hash - the hash value of the string
- * @param item - the item to be inserted
- */
- public void probeInsert(int hash,DataItem item){
- boolean isFound = false;//boolean to know when found
- int walker = hash;//walker to walk on array
- int count = 1;//counter for probing
- int newHash = hash;//New hash value
- while(!isFound){
- walker = hash;
- newHash = quadP(count++);
- for(int x=newHash;x>0;x--){
- if(++walker == arr.length){
- walker = 0;
- }
- }
- if(arr[walker]== null){
- arr[walker] = item;
- isFound = true;
- }//if the array item is null or
- }//while the item position isn't found
- if(count>maxSearch){
- maxSearch = count;
- }
- }//end probeInsert
- //Used if using quadratic probing
- public int quadP(int x){
- return x*x;
- }//end quadP
- }//end HashTable
- class LinkedList{
- private Link first;
- private Link last;
- private int baseAdd;
- public LinkedList(){
- first = null;
- last = null;
- }
- public String delete(){
- String data = first.getDisplayFormat();
- first = first.next;
- return data;
- }
- public void reset(){
- first = null;
- last = null;
- }
- public String getPeek(){
- return first.getDisplayFormat();
- }
- public boolean isEmpty(){
- return (first == null);
- }
- public void insert(String data){
- if(first == null){
- first = new Link(data,-1);
- last = first;
- }else{
- Link newLink = new Link(data,last.nextAddress());
- last.next = newLink;
- last = newLink;
- }
- }
- public void display(){
- Link temp = first;
- System.out.println("****************************\nKyle Gray: SIC/XE Assembler\nVersion Date 04/14/17\n****************************");
- System.out.println(String.format("ASSEMBLER REPORT\n-----------------\n%8s %13s %15s","Loc","Object Code","Source Code"));
- System.out.println(String.format("%8s %13s %16s","---","-----------","-----------"));
- int line = 1;
- while(temp != null){
- System.out.println(String.format("%03d- %s",line++,temp.getDisplayFormat()));
- temp = temp.next;
- }
- }
- public String toLST(){
- Link temp = first;
- String output = "";
- output+="****************************\nKyle Gray: SIC/XE Assembler\nVersion Date 04/14/17\n****************************\n";
- output+=String.format("ASSEMBLER REPORT\n-----------------\n%8s %13s %15s\n","Loc","Object Code","Source Code");
- output+=String.format("%8s %13s %16s\n","---","-----------","-----------");
- int line = 1;
- while(temp != null){
- output+=String.format("%03d- %s\n",line++,temp.getDisplayFormat());
- temp = temp.next;
- }
- return output;
- }
- public String toOBJ(){
- Link temp = first;
- String output = "";
- while(temp != null){
- output+=String.format("%s\n",temp.getObjectCode().toString());
- temp = temp.next;
- }
- return output+"!";
- }
- public void findBase(){
- //Base Find
- Link temp = first;
- while(temp.next != null){
- if(temp.getMneumonic().equals("BASE")){
- break;
- }
- temp = temp.next;
- }
- String base = temp.getOperand();
- temp = temp.next;
- while(temp.next != null){
- if(temp.getOperand() == base){
- baseAdd = temp.getAddress();
- break;
- }
- temp = temp.next;
- }
- }//end findBase
- public void calculateAddresses(){
- findBase();
- Link temp = first;
- while(temp.next != null){
- switch(temp.getMneumonic()){
- case "START":
- case "END":
- case "RESW":
- case "WORD":
- case "BASE":
- break;
- default:
- boolean address = false;
- if(temp.getObjectCode().isExtended()){//if extended
- String key = temp.getOperand();
- if(key.charAt(0) == '#' || key.charAt(0) == '@'){
- key = key.substring(1);
- }
- Link link = first;
- while(link.next != null){
- if(link.getLabel().equals(key) && !link.compareTo(temp)){
- temp.getObjectCode().setAddress(link.getAddress());
- break;
- }else{
- link = link.next;
- }
- }
- }else if(SymbolTable.table.find(temp.getMneumonic()).getBytes() == 2){//if Resistor operation
- String[] registers = temp.getOperand().split(",");
- for(int i=0;i<2;i++){
- switch(registers[i]){
- case "A":
- registers[i] = "0";
- break;
- case "T":
- registers[i] = "5";
- break;
- }
- }//end for
- temp.getObjectCode().setAddress(Integer.parseInt((registers[0]+registers[1]),16));
- }else{//if its a 6 byte instruction
- String key = temp.getOperand();
- if(key.charAt(0) == '#' || key.charAt(0) == '@'){
- key = key.substring(1);
- }
- Link link = first;
- while(link.next != null){
- if(link.getLabel().equals(key) && !link.compareTo(temp)){//if we found the label
- if(link.getAddress() - temp.next.getAddress() > 2048){
- //Base
- temp.getObjectCode().setAddress(link.getAddress()-baseAdd);
- temp.getObjectCode().modifyPC();
- }else{
- temp.getObjectCode().setAddress(link.getAddress()-temp.nextAddress());
- break;
- }
- break;
- }else{
- link = link.next;
- }
- }
- }//end if
- break;
- }//end swtich
- temp = temp.next;
- }
- }
- }//end List
- class Link{
- public ObjectCode objectCode;//ObjectCode String
- public Link next;
- private String mneumonic;
- private String operand;
- private int address;
- private String label;
- private String comment;
- private int status;
- public Link(String data,int add){
- objectCode = new ObjectCode();
- status = -1;
- if(add == -1){
- this.address = (int)(Long.parseLong(data.substring(19,28).trim(),16));
- }else{
- this.address = add;
- }
- //Checks label
- label = data.substring(0,7).trim();
- //Checks format
- if(data.charAt(9) == '+'){
- objectCode.setExtended(true);
- }
- //Instruction
- mneumonic = data.substring(10,16).trim();
- try{
- if(SymbolTable.table.find(this.mneumonic).getBytes() == 2){
- objectCode.setResistor(true);
- }
- }catch(NullPointerException e){
- }
- //Operand
- try{
- operand = data.substring(18,28).trim();
- }catch(StringIndexOutOfBoundsException e){
- operand = data.substring(18).trim();
- }
- //Comments
- try{
- comment = data.substring(31);
- }catch(StringIndexOutOfBoundsException e){
- comment = "";
- }
- if(mneumonic.equals("RESW") && !label.isEmpty()){
- SymbolTable.insertWord(String.format("%s %s %d",label,address,Integer.parseInt(operand)*3));
- }else if(mneumonic.equals("WORD") && !label.isEmpty()){
- SymbolTable.insertWord(String.format("%s %s %d",label,address,3));
- }
- //Append the first part of object code
- switch(this.mneumonic){
- case "START":
- case "BASE":
- case "END":
- break;
- case "WORD":
- objectCode.setAddress(Integer.parseInt(operand,16));
- break;
- case "RESW":
- objectCode.setAddress(Integer.parseInt("FFFFFF",16));
- break;
- default:
- try{
- objectCode.setOptCode(Integer.parseInt(SymbolTable.table.find(this.mneumonic).getOptCode(),16));
- switch(data.charAt(18)){
- case '#':
- try{
- if(Integer.parseInt(operand) == Integer.parseInt(operand,10)){
- objectCode.calcNI(false,true);
- }
- }catch(NumberFormatException e){
- objectCode.calcNI(true, true);
- }
- break;
- case '@':
- objectCode.calcNI(true, false);
- break;
- default:
- if(objectCode.isResistor()){
- objectCode.calcNI(false, false);
- }else{
- objectCode.calcNI(true, true);
- }
- break;
- }
- }catch(NullPointerException e){
- }
- //Calculate Flags
- int temp = 0;
- if(objectCode.isExtended()){
- temp+=1;
- }else if(!objectCode.isImmediate() && SymbolTable.table.find(this.mneumonic).getBytes() != 2){
- temp+=2;
- }
- if(operand.contains(",X") && operand.length() != 3){
- temp+=8;
- }
- objectCode.setFlags(temp);
- }//end switch
- }//end Constructor
- public ObjectCode getObjectCode(){
- return objectCode;
- }
- public int nextAddress(){
- int inc;
- if(mneumonic.equals("RESW")){
- inc = 3*Integer.parseInt(operand);
- }else if(this.mneumonic == null || this.mneumonic.equals("")){
- status = 200;
- inc = 0;
- }else{
- inc = SymbolTable.getTable().find(this.mneumonic).getBytes();
- }
- if(inc == -1){
- status = 100;
- inc = 0;
- }
- return incrementAddress(inc);
- }
- public int incrementAddress(int count){
- return (int)(Long.parseLong(Integer.toString(address))+Long.parseLong(Integer.toString(count)));
- }
- public String getMneumonic(){
- return mneumonic;
- }
- public String getOperand(){
- return operand;
- }
- public int getAddress(){
- return address;
- }
- public String getLabel(){
- return label;
- }
- public String getDisplayFormat(){
- if(status == -1){
- String addy = Integer.toHexString(address);
- while(addy.length() != 5){
- addy=("0"+addy);
- }
- String mnu = mneumonic;
- if(objectCode.isExtended()){
- mnu = "+"+mnu;
- }
- return String.format("%-6s%-15s%-8s%-7s%-7s%-10s",addy.toUpperCase(),objectCode.toString().toUpperCase(),label,mnu,operand,comment);
- }else{
- return ErrorTable.report(status);
- }
- }
- public boolean compareTo(Link link){
- return (this.address == link.address);
- }
- }
- class ObjectCode {
- private int optCode;
- private int flags;
- private int address;
- private boolean extended;
- private boolean immediate;
- private boolean pc;
- private boolean resistor;
- public ObjectCode(){
- extended = false;
- immediate = false;
- pc = false;
- resistor = false;
- optCode=0;
- flags=0;
- }
- public String toString(){
- if(optCode == flags){
- String temp = Integer.toHexString(address);
- while(temp.length()!=6){
- temp = ("0"+temp);
- }
- return temp;
- }else if(resistor){
- return(Integer.toHexString(optCode)+Integer.toHexString(address));
- }else{
- String tempObj = Integer.toHexString(optCode);
- while(tempObj.length() != 2){
- tempObj = ("0"+tempObj);
- }
- String tempAddy;
- if(address < 0){
- tempAddy = Integer.toHexString(address).substring(Integer.toHexString(address).length()-3).toUpperCase();
- }else{
- tempAddy = Integer.toHexString(address);
- }
- if(extended){
- while(tempAddy.length() != 5){
- tempAddy = "0"+tempAddy;
- }
- }else if(!resistor){
- while(tempAddy.length() != 3){
- tempAddy = "0"+tempAddy;
- }
- }
- return (tempObj+Integer.toHexString(flags)+tempAddy);
- }
- }
- public void setOptCode(int optCode){
- this.optCode = optCode;
- }
- public void calcNI(boolean indirect,boolean immediate){
- if(indirect && immediate){
- optCode+= 3;
- }else if(indirect && !immediate){
- optCode+= 2;
- }else if(immediate && !indirect){
- this.immediate = true;
- optCode+= 1;
- }
- }
- public void setFlags(int num){
- flags = num;
- }
- public void setAddress(int address){
- this.address = address;
- }
- public boolean isImmediate(){
- return immediate;
- }
- public void setExtended(boolean e){
- extended = e;
- }
- public boolean isExtended(){
- return extended;
- }
- public void setPc(boolean p){
- pc = p;
- }
- public void modifyPC(){
- flags= flags+2;
- }
- public boolean isResistor(){
- return resistor;
- }
- public void setResistor(boolean is){
- resistor = is;
- }
- public boolean isPC(){
- return pc;
- }
- }
- public class Main {
- private Program program;
- public void readProgram(String filename) throws IOException {
- try {
- program = new Program(new File(filename));
- program.display();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //Main
- public static void main(String[] args) throws IOException{
- //Create instance of the mainClass
- Main main = new Main();
- SymbolTable.createTable();
- main.readProgram(args[0]);
- //Display the insertions
- //System.out.println(SymbolTable.table.displayWordStorage());
- }//end main
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement