Advertisement
kimo12

Untitled

Apr 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 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 no Label in the line get Label will return null
  47.             String label = getLabel(line);
  48.             if (label != null)
  49.                 System.out.print(label);
  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.         boolean flag = false;
  97.         int i = 0;
  98.         char firstChar = line.charAt(0);
  99.         if (firstChar == ' ' || firstChar == '\t') {
  100.             flag = true;
  101.         }
  102.         if (!flag) {
  103.             while (i < 8) {
  104.                 if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
  105.                 } else {
  106.                     break;
  107.                 }
  108.                 i++;
  109.             }
  110.             String label = line.substring(0, i);
  111.             return label;
  112.         } else
  113.             return null;
  114.     }
  115.  
  116.     private String getOperationCode(String line) {
  117.         int i = cursor + 1;
  118.         int end = cursor + 7;
  119.         while (i < end) {
  120.             if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
  121.             } else {
  122.                 break;
  123.             }
  124.             i++;
  125.         }
  126.         String opertionCode = line.substring(cursor + 1, i);
  127.         cursor = i;
  128.         return opertionCode;
  129.     }
  130.  
  131.     private String getOperand(String line) {
  132.         boolean flag = false;
  133.         int i = cursor + 1;
  134.         int end = cursor + 31;
  135.         while (i < end) {
  136.             if (line.length() > i && line.charAt(i) == ' ' && line.charAt(i) != '\t') {
  137.             } else {
  138.                 break;
  139.             }
  140.             i++;
  141.             cursor++;
  142.         }
  143.         while (i < end) {
  144.             if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
  145.                 flag = true;
  146.             } else {
  147.                 break;
  148.             }
  149.             i++;
  150.         }
  151.         if (flag) {
  152.             String operand = line.substring(cursor + 1, i);
  153.             return operand;
  154.         } else
  155.             return null;
  156.     }
  157.  
  158.     public static void main(String[] args) {
  159.         ObjectCode g = new ObjectCode();
  160.         g.readfile();
  161.     }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement