Advertisement
kimo12

Untitled

Apr 24th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. package assembler;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6.  
  7. public class ObjectCode {
  8.     private int cursor;
  9.     private static final String FILENAME = "C:\\Users\\karem\\Downloads\\SIC-Example.txt";
  10.  
  11.     private void readfile() {
  12.         BufferedReader br = null;
  13.         FileReader fr = null;
  14.         try {
  15.             fr = new FileReader(FILENAME);
  16.             br = new BufferedReader(fr);
  17.             String CurrentLine;
  18.             while ((CurrentLine = br.readLine()) != null) {
  19.                 // call for a function that uses the line
  20.                 this.line(CurrentLine);
  21.             }
  22.  
  23.         } catch (IOException e) {
  24.             e.printStackTrace();
  25.         } finally {
  26.             try {
  27.                 if (br != null)
  28.                     br.close();
  29.                 if (fr != null)
  30.                     fr.close();
  31.             } catch (IOException ex) {
  32.                 ex.printStackTrace();
  33.             }
  34.         }
  35.     }
  36.  
  37.     private void line(String line) {
  38.         // check if comment
  39.         char firstChar = line.charAt(0);
  40.         if (firstChar == '.') {
  41.             System.out.println("hello from the other side");
  42.         } else {
  43.             // checking format
  44.             this.checkFormat(line);
  45.             // get Label From Line
  46.             if (firstChar != ' ' && line.indexOf("\t") != 0) {
  47.                 String label = getLabel(line);
  48.                 System.out.print(label);
  49.             }
  50.             // get OperationCode From Line
  51.             String operationCode = getOperationCode(line);
  52.             System.out.print("\t" + operationCode);
  53.             // get Operand From Line
  54.             // if no operand in the line get operand will return null
  55.             String operand = getOperand(line);
  56.             if (operand != null)
  57.                 System.out.print("\t" + operand);
  58.         }
  59.         System.out.println();
  60.     }
  61.  
  62.     private void checkFormat(String line) {
  63.         cursor = 8;
  64.         int y = 0;
  65.         if (line.charAt(0) == '\t' || line.charAt(4) == '\t') {
  66.             cursor = 5;
  67.             y = y + 4;
  68.             if (line.charAt(1) == '\t') {
  69.                 cursor = 2;
  70.                 y = y + 4;
  71.             }
  72.         } else if (line.charAt(5) == '\t') {
  73.             cursor = 6;
  74.             y = y + 3;
  75.         }
  76.         if (line.length() > cursor && line.charAt(cursor) != ' ') {
  77.             System.out.println("Error");
  78.         }
  79.         for (int i = 9; line.length() > i && i < 15; i++) {
  80.             if (line.charAt(i) == '\t') {
  81.                 y = y + 3;
  82.                 break;
  83.             }
  84.         }
  85.  
  86.         if (line.length() > 15 - y && line.charAt(15 - y) != ' ' && line.charAt(15 - y) != '\t') {
  87.             System.out.println("Error");
  88.         }
  89.         if (line.length() > 16 - y && line.charAt(16 - y) != ' ' && line.charAt(15 - y) != '\t') {
  90.             System.out.println("Error");
  91.         }
  92.  
  93.     }
  94.  
  95.     private String getLabel(String line) {
  96.         int i = 0;
  97.         while (i < 8) {
  98.             if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
  99.             } else {
  100.                 break;
  101.             }
  102.             i++;
  103.         }
  104.         String label = line.substring(0, i);
  105.         return label;
  106.     }
  107.  
  108.     private String getOperationCode(String line) {
  109.         int i = cursor + 1;
  110.         int end = cursor + 7;
  111.         while (i < end) {
  112.             if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
  113.             } else {
  114.                 break;
  115.             }
  116.             i++;
  117.         }
  118.         String opertionCode = line.substring(cursor + 1, i);
  119.         cursor = i;
  120.         return opertionCode;
  121.     }
  122.  
  123.     private String getOperand(String line) {
  124.         boolean flag = false;
  125.         int i = cursor + 1;
  126.         int end = cursor + 31;
  127.         while (i < end) {
  128.             if (line.length() > i && line.charAt(i) == ' ' && line.charAt(i) != '\t') {
  129.             } else {
  130.                 break;
  131.             }
  132.             i++;
  133.             cursor++;
  134.         }
  135.         while (i < end) {
  136.             if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
  137.                 flag = true;
  138.             } else {
  139.                 break;
  140.             }
  141.             i++;
  142.         }
  143.         if (flag) {
  144.             String operand = line.substring(cursor + 1, i);
  145.             return operand;
  146.         } else
  147.             return null;
  148.     }
  149.  
  150.     public static void main(String[] args) {
  151.         ObjectCode g = new ObjectCode();
  152.         g.readfile();
  153.     }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement