Advertisement
kimo12

Last edition before arraylist

Apr 27th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.38 KB | None | 0 0
  1. package assembler;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9.  
  10. public class ObjectCode {
  11.     private int cursor;
  12.     private int lineNumber = 0;
  13.     private ArrayList<String> listingfile = new ArrayList<>();
  14.     private ArrayList<String> objectProgram = new ArrayList<>();
  15.     private static final String FILENAME = "C:\\Users\\karem\\Downloads\\SIC-Example.txt";
  16.     private static final String FILENAME2 = "C:\\Users\\karem\\Desktop\\listingfile.txt";
  17.     private static final String FILENAME3 = "C:\\Users\\karem\\Desktop\\objectprogram.txt";
  18.  
  19.     private void readfile() {
  20.         BufferedReader br = null;
  21.         FileReader fr = null;
  22.         try {
  23.             fr = new FileReader(FILENAME);
  24.             br = new BufferedReader(fr);
  25.             String CurrentLine;
  26.             while ((CurrentLine = br.readLine()) != null) {
  27.                 // call for a function that uses the line
  28.                 lineNumber++;
  29.                 this.line(CurrentLine);
  30.             }
  31.  
  32.         } catch (IOException e) {
  33.             e.printStackTrace();
  34.         } finally {
  35.             try {
  36.                 if (br != null)
  37.                     br.close();
  38.                 if (fr != null)
  39.                     fr.close();
  40.             } catch (IOException ex) {
  41.                 ex.printStackTrace();
  42.             }
  43.         }
  44.     }
  45.  
  46.     private void line(String line) {
  47.         // check if comment
  48.         char firstChar = line.charAt(0);
  49.         if (firstChar == '.') {
  50.             System.out.println("hello from the other side");
  51.         } else {
  52.             // checking format
  53.             this.checkFormat(line);
  54.             // get Label From Line
  55.             // if no Label in the line get Label will return null
  56.             String label = getLabel(line);
  57.             if (label != null)
  58.                 label = label.toUpperCase();
  59.             System.out.print(label);
  60.             // get OperationCode From Line
  61.             String operationCode = getOperationCode(line).toUpperCase();
  62.             System.out.print("\t" + operationCode);
  63.             // get Operand From Line
  64.             // if no operand in the line get operand will return null
  65.             String operand = getOperand(line);
  66.             if (operand != null)
  67.                 operand = operand.toUpperCase();
  68.             System.out.print("\t" + operand);
  69.             String objectCode = getObjectCode(operationCode, operand);
  70.             writeListing(label, operationCode, operand, objectCode);
  71.             writeObject(label, operationCode, objectCode);
  72.         }
  73.         System.out.println();
  74.     }
  75.  
  76.     private String getObjectCode(String operationCode, String operand) {
  77.         // search for operation Code in the mnemonic Table
  78.         boolean found = false;
  79.         String mnemonicValue = "00";
  80.         String operandAddress;
  81.         String objectCode = new String();
  82.         if (found) {
  83.             if (operand == null) {
  84.                 operandAddress = "0000";
  85.                 objectCode = mnemonicValue.concat(operandAddress);
  86.             } else {
  87.                 // search for operand in the symtab
  88.                 // if found
  89.                 // operandAddress = symbol value;
  90.                 // objectCode = mnemonicValue.concat(operandAddress);
  91.                 // if not found (Un Defined Symbol)
  92.                 // operandAddress = "0000";
  93.                 // ERROR
  94.             }
  95.         } else if (operationCode.equals("WORD")) {
  96.             String hex = Integer.toHexString(Integer.parseInt(operand));
  97.             if (hex.length() == 1) {
  98.                 String concat = "00000";
  99.                 hex = concat.concat(hex);
  100.             } else if (hex.length() == 2) {
  101.                 String concat = "0000";
  102.                 hex = concat.concat(hex);
  103.             } else if (hex.length() == 3) {
  104.                 String concat = "000";
  105.                 hex = concat.concat(hex);
  106.             } else if (hex.length() == 4) {
  107.                 String concat = "00";
  108.                 hex = concat.concat(hex);
  109.             } else if (hex.length() == 5) {
  110.                 String concat = "0";
  111.                 hex = concat.concat(hex);
  112.             }
  113.             objectCode = hex;
  114.         } else if (operationCode.equals("BYTE")) {
  115.             if (operand.charAt(0) == 'X') {
  116.                 objectCode = operand.substring(2, operationCode.length());
  117.             } else if (operand.charAt(0) == 'C') {
  118.                 for (int i = 2; i < operand.length() - 1; i++) {
  119.                     objectCode = objectCode.concat(Integer.toHexString((int) operand.charAt(i)));
  120.                 }
  121.             }
  122.         }
  123.         // if object code will not fit into the current text record
  124.         // write text record to object program
  125.         // intialize new text record
  126.         // add object code to text record
  127.         return objectCode;
  128.     }
  129.  
  130.     private void writeListing(String label, String operationCode, String operand, String objectCode) {
  131.         if (label == null) {
  132.             label = "\t";
  133.         }
  134.         if (operand == null) {
  135.             operand = "\t";
  136.         }
  137.         // while(label.length() < 9){
  138.         // label = label;
  139.         // }
  140.         //
  141.         // while(operationCode.length() < 6){
  142.         // operationCode = operationCode.concat(" ");
  143.         // }
  144.         // listing file
  145.         int line = lineNumber * 5;
  146.         String listing;
  147.         if (lineNumber == 1) {
  148.             listing = "Line" + "\t" + "LOC" + "\t\t" + "Source Statement" + "\t\t" + "Object Code" + "\n";
  149.             listingfile.add(listing);
  150.         }
  151.         if (operationCode.equals("START") || operationCode.equals("RESB") || operationCode.equals("RESW")) {
  152.             listing = String.valueOf(line) + "\t" + "LOC" + "\t" + label + "\t" + operationCode + "\t" + operand + "\n";
  153.             listingfile.add(listing);
  154.         } else if (operationCode.equals("END")) {
  155.             listing = String.valueOf(line) + "\t" + "LOC" + "\t" + label + "\t" + operationCode + "\t" + operand;
  156.             listingfile.add(listing);
  157.             writeListingFile();
  158.         } else {
  159.             listing = String.valueOf(line) + "\t" + "LOC" + "\t" + label + "\t" + operationCode + "\t" + operand + "\t"
  160.                     + objectCode + "\n";
  161.             listingfile.add(listing);
  162.         }
  163.         // objectProgram
  164.  
  165.     }
  166.  
  167.     private void writeObject(String label, String operationCode, String objectCode) {
  168.         String objectLine;
  169.         if (operationCode.equals("START")) {
  170.             while (label.length() != 6) {
  171.                 label = label.concat(" ");
  172.             }
  173.             objectLine = "H" + label + "STADDR" + "LENPRO" + "\n";
  174.             objectProgram.add(objectLine);
  175.             writeObjectProgramFile();
  176.         }
  177.        
  178.     }
  179.  
  180.     private void writeListingFile() {
  181.         // Listing File
  182.         try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME2))) {
  183.             for (int i = 0; i < listingfile.size(); i++) {
  184.                 bw.write(listingfile.get(i));
  185.             }
  186.             bw.close();
  187.         } catch (IOException e) {
  188.  
  189.             e.printStackTrace();
  190.         }
  191.  
  192.     }
  193.  
  194.     private void writeObjectProgramFile() {
  195.         // objectProgram file
  196.         try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME3))) {
  197.             for (int i = 0; i < objectProgram.size(); i++) {
  198.                 bw.write(objectProgram.get(i));
  199.             }
  200.             bw.close();
  201.         } catch (IOException e) {
  202.  
  203.             e.printStackTrace();
  204.         }
  205.     }
  206.  
  207.     private void checkFormat(String line) {
  208.         cursor = 8;
  209.         int y = 0;
  210.         if (line.charAt(0) == '\t' || line.charAt(4) == '\t') {
  211.             cursor = 5;
  212.             y = y + 4;
  213.             if (line.charAt(1) == '\t') {
  214.                 cursor = 2;
  215.                 y = y + 4;
  216.             }
  217.         } else if (line.charAt(5) == '\t') {
  218.             cursor = 6;
  219.             y = y + 3;
  220.         }
  221.         if (line.length() > cursor && line.charAt(cursor) != ' ') {
  222.             System.out.println("Error");
  223.         }
  224.         for (int i = 9; line.length() > i && i < 15; i++) {
  225.             if (line.charAt(i) == '\t') {
  226.                 y = y + 3;
  227.                 break;
  228.             }
  229.         }
  230.  
  231.         if (line.length() > 15 - y && line.charAt(15 - y) != ' ' && line.charAt(15 - y) != '\t') {
  232.             System.out.println("Error");
  233.         }
  234.         if (line.length() > 16 - y && line.charAt(16 - y) != ' ' && line.charAt(15 - y) != '\t') {
  235.             System.out.println("Error");
  236.         }
  237.  
  238.     }
  239.  
  240.     private String getLabel(String line) {
  241.         boolean flag = false;
  242.         int i = 0;
  243.         char firstChar = line.charAt(0);
  244.         if (firstChar == ' ' || firstChar == '\t') {
  245.             flag = true;
  246.         }
  247.         if (!flag) {
  248.             while (i < 8) {
  249.                 if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
  250.                 } else {
  251.                     break;
  252.                 }
  253.                 i++;
  254.             }
  255.             String label = line.substring(0, i);
  256.             return label;
  257.         } else
  258.             return null;
  259.     }
  260.  
  261.     private String getOperationCode(String line) {
  262.         int i = cursor + 1;
  263.         int end = cursor + 7;
  264.         while (i < end) {
  265.             if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
  266.             } else {
  267.                 break;
  268.             }
  269.             i++;
  270.         }
  271.         String opertionCode = line.substring(cursor + 1, i);
  272.         cursor = i;
  273.         return opertionCode;
  274.     }
  275.  
  276.     private String getOperand(String line) {
  277.         boolean flag = false;
  278.         int i = cursor + 1;
  279.         int end = cursor + 31;
  280.         while (i < end) {
  281.             if (line.length() > i && line.charAt(i) == ' ' && line.charAt(i) != '\t') {
  282.             } else {
  283.                 break;
  284.             }
  285.             i++;
  286.             cursor++;
  287.         }
  288.         while (i < end) {
  289.             if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
  290.                 flag = true;
  291.             } else {
  292.                 break;
  293.             }
  294.             i++;
  295.         }
  296.         if (flag) {
  297.             String operand = line.substring(cursor + 1, i);
  298.             return operand;
  299.         } else
  300.             return null;
  301.     }
  302.  
  303.     public static void main(String[] args) {
  304.         ObjectCode g = new ObjectCode();
  305.         g.readfile();
  306.     }
  307.  
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement