Advertisement
kimo12

Untitled

Apr 24th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.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.     private void readfile() {
  11.         BufferedReader br = null;
  12.         FileReader fr = null;
  13.         try {
  14.             fr = new FileReader(FILENAME);
  15.             br = new BufferedReader(fr);
  16.             String CurrentLine;
  17.             while ((CurrentLine = br.readLine()) != null) {
  18.                 // call for a function that uses the line
  19.                 this.line(CurrentLine);
  20.             }
  21.  
  22.         } catch (IOException e) {
  23.             e.printStackTrace();
  24.         } finally {
  25.             try {
  26.                 if (br != null)
  27.                     br.close();
  28.                 if (fr != null)
  29.                     fr.close();
  30.             } catch (IOException ex) {
  31.                 ex.printStackTrace();
  32.             }
  33.         }
  34.     }
  35.  
  36.     private void line(String line) {
  37.         // check if comment
  38.         char firstChar = line.charAt(0);
  39.         if (firstChar == '.') {
  40.             System.out.println("hello from the other side");
  41.         } else {
  42.             // checking format
  43.             this.checkFormat(line);
  44.              //get Label From Line
  45.              if (firstChar != ' ' && line.indexOf("\t")!=0) {
  46.              String label = getLabel(line);
  47.              System.out.print(label);
  48.              }
  49.              //get Operand From Line
  50.              String operand = getOperationCode(line);
  51.              System.out.println("\t"+operand);
  52.         }
  53.     }
  54.  
  55.     private void checkFormat(String line) {
  56.         cursor = 8;
  57.         int y = 0;
  58.         if (line.charAt(0) == '\t' || line.charAt(4) == '\t') {
  59.             cursor = 5;
  60.             y = y + 4;
  61.             if (line.charAt(1) == '\t') {
  62.                 cursor = 2;
  63.                 y = y + 4;
  64.             }
  65.         } else if (line.charAt(5) == '\t') {
  66.             cursor = 6;
  67.             y = y + 3;
  68.         }
  69.         if (line.length() > cursor && line.charAt(cursor) != ' ') {
  70.             System.out.println("Error");
  71.         }
  72.         for (int i = 9; line.length() > i && i < 15; i++) {
  73.             if (line.charAt(i) == '\t') {
  74.                 y = y + 3;
  75.                 break;
  76.             }
  77.         }
  78.  
  79.         if (line.length() > 15 - y && line.charAt(15 - y) != ' ' && line.charAt(15 - y) != '\t') {
  80.             System.out.println("Error");
  81.         }
  82.         if (line.length() > 16 - y && line.charAt(16 - y) != ' ' && line.charAt(15 - y) != '\t') {
  83.             System.out.println("Error");
  84.         }
  85.  
  86.     }
  87.  
  88.     private String getLabel(String line) {
  89.         int i = 0;
  90.         while (i < 8) {
  91.             if (line.length() > i && line.charAt(i) != ' '&&line.charAt(i) != '\t') {
  92.             } else {
  93.                 break;
  94.             }
  95.             i++;
  96.         }
  97.         String label = line.substring(0, i);
  98.         return label;
  99.     }
  100.  
  101.     private String getOperationCode(String line) {
  102.         int i = cursor + 1;
  103.         int end = cursor +7;
  104.         while (i < end) {
  105.             if (line.length() > i && line.charAt(i) != ' '&&line.charAt(i) != '\t') {
  106.             } else {
  107.                 break;
  108.             }
  109.             i++;
  110.         }
  111.         String operand = line.substring(cursor+1, i);
  112.         return operand;
  113.     }
  114.  
  115.     public static void main(String[] args) {
  116.         ObjectCode g = new ObjectCode();
  117.         g.readfile();
  118.     }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement