kimo12

pass 2

Apr 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.32 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 Pass2 {
  11.  
  12.     private int lineNumber = 0;
  13.     private ArrayList<String> labels = new ArrayList<>();
  14.     private ArrayList<String> operationCodes = new ArrayList<>();
  15.     private ArrayList<String> operands = new ArrayList<>();
  16.     private ArrayList<String> Addresses = new ArrayList<>();
  17.     private ArrayList<String> listingfile = new ArrayList<>();
  18.     private ArrayList<String> objectProgram = new ArrayList<>();
  19.     private static final String FILENAME2 = "C:\\Users\\karem\\Desktop\\listingfile.txt";
  20.     private static final String FILENAME3 = "C:\\Users\\karem\\Desktop\\objectprogram.txt";
  21.  
  22.     Pass2(ArrayList<String> labels,ArrayList<String> operationCodes,ArrayList<String> operands,ArrayList<String> Addresses){
  23.     this.labels = labels;
  24.     this.operationCodes = operationCodes;
  25.     this.operands = operands;
  26.     this.Addresses = Addresses;
  27.     this.lineNumber = 0;
  28.     this.run();
  29.     }
  30.  
  31.     private void run(){
  32.         for(int i = 0; i < labels.size();i++){
  33.         String objectCode = getObjectCode(operationCodes.get(i), operands.get(i));
  34.         writeListing(labels.get(i), operationCodes.get(i), operands.get(i), objectCode);
  35.         writeObject(labels.get(i), operationCodes.get(i), objectCode);
  36.         }
  37.     }
  38.    
  39.     private String getObjectCode(String operationCode, String operand) {
  40.         // search for operation Code in the mnemonic Table
  41.         boolean found = false;
  42.         String mnemonicValue = "00";
  43.         String operandAddress;
  44.         String objectCode = new String();
  45.         if (found) {
  46.             if (operand == null) {
  47.                 operandAddress = "0000";
  48.                 objectCode = mnemonicValue.concat(operandAddress);
  49.             } else {
  50.                 // search for operand in the symtab
  51.                 // if found
  52.                 // operandAddress = symbol value;
  53.                 // objectCode = mnemonicValue.concat(operandAddress);
  54.                 // if not found (Un Defined Symbol)
  55.                 // operandAddress = "0000";
  56.                 // ERROR
  57.             }
  58.         } else if (operationCode.equals("WORD")) {
  59.             String hex = Integer.toHexString(Integer.parseInt(operand));
  60.             if (hex.length() == 1) {
  61.                 String concat = "00000";
  62.                 hex = concat.concat(hex);
  63.             } else if (hex.length() == 2) {
  64.                 String concat = "0000";
  65.                 hex = concat.concat(hex);
  66.             } else if (hex.length() == 3) {
  67.                 String concat = "000";
  68.                 hex = concat.concat(hex);
  69.             } else if (hex.length() == 4) {
  70.                 String concat = "00";
  71.                 hex = concat.concat(hex);
  72.             } else if (hex.length() == 5) {
  73.                 String concat = "0";
  74.                 hex = concat.concat(hex);
  75.             }
  76.             objectCode = hex;
  77.         } else if (operationCode.equals("BYTE")) {
  78.             if (operand.charAt(0) == 'X') {
  79.                 objectCode = operand.substring(2, operationCode.length());
  80.             } else if (operand.charAt(0) == 'C') {
  81.                 for (int i = 2; i < operand.length() - 1; i++) {
  82.                     objectCode = objectCode.concat(Integer.toHexString((int) operand.charAt(i)));
  83.                 }
  84.             }
  85.         }
  86.         // if object code will not fit into the current text record
  87.         // write text record to object program
  88.         // intialize new text record
  89.         // add object code to text record
  90.         return objectCode;
  91.     }
  92.  
  93.     private void writeListing(String label, String operationCode, String operand, String objectCode) {
  94.         if (label == null) {
  95.             label = "\t";
  96.         }
  97.         if (operand == null) {
  98.             operand = "\t";
  99.         }
  100.         // while(label.length() < 9){
  101.         // label = label;
  102.         // }
  103.         //
  104.         // while(operationCode.length() < 6){
  105.         // operationCode = operationCode.concat(" ");
  106.         // }
  107.         // listing file
  108.         lineNumber++;
  109.         int line = lineNumber * 5;
  110.         String listing;
  111.         if (lineNumber == 1) {
  112.             listing = "Line" + "\t" + "LOC" + "\t\t" + "Source Statement" + "\t\t" + "Object Code" + "\n";
  113.             listingfile.add(listing);
  114.         }
  115.         if (operationCode.equals("START") || operationCode.equals("RESB") || operationCode.equals("RESW")) {
  116.             listing = String.valueOf(line) + "\t" + "LOC" + "\t" + label + "\t" + operationCode + "\t" + operand + "\n";
  117.             listingfile.add(listing);
  118.         } else if (operationCode.equals("END")) {
  119.             listing = String.valueOf(line) + "\t" + "LOC" + "\t" + label + "\t" + operationCode + "\t" + operand;
  120.             listingfile.add(listing);
  121.             writeListingFile();
  122.         } else {
  123.             listing = String.valueOf(line) + "\t" + "LOC" + "\t" + label + "\t" + operationCode + "\t" + operand + "\t"
  124.                     + objectCode + "\n";
  125.             listingfile.add(listing);
  126.         }
  127.         // objectProgram
  128.  
  129.     }
  130.  
  131.     private void writeObject(String label, String operationCode, String objectCode) {
  132.         String objectLine;
  133.         if (operationCode.equals("START")) {
  134.             while (label.length() != 6) {
  135.                 label = label.concat(" ");
  136.             }
  137.             objectLine = "H" + label + "STADDR" + "LENPRO" + "\n";
  138.             objectProgram.add(objectLine);
  139.             writeObjectProgramFile();
  140.         }
  141.        
  142.     }
  143.  
  144.     private void writeListingFile() {
  145.         // Listing File
  146.         try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME2))) {
  147.             for (int i = 0; i < listingfile.size(); i++) {
  148.                 bw.write(listingfile.get(i));
  149.             }
  150.             bw.close();
  151.         } catch (IOException e) {
  152.  
  153.             e.printStackTrace();
  154.         }
  155.  
  156.     }
  157.  
  158.     private void writeObjectProgramFile() {
  159.         // objectProgram file
  160.         try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME3))) {
  161.             for (int i = 0; i < objectProgram.size(); i++) {
  162.                 bw.write(objectProgram.get(i));
  163.             }
  164.             bw.close();
  165.         } catch (IOException e) {
  166.  
  167.             e.printStackTrace();
  168.         }
  169.     }
  170.  
  171.  
  172. }
Add Comment
Please, Sign In to add comment