kimo12

pass 1

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