Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assembler;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- public class Pass2 {
- private int lineNumber = 0;
- private ArrayList<String> labels = new ArrayList<>();
- private ArrayList<String> operationCodes = new ArrayList<>();
- private ArrayList<String> operands = new ArrayList<>();
- private ArrayList<String> Addresses = new ArrayList<>();
- private ArrayList<String> listingfile = new ArrayList<>();
- private ArrayList<String> objectProgram = new ArrayList<>();
- private static final String FILENAME2 = "C:\\Users\\karem\\Desktop\\listingfile.txt";
- private static final String FILENAME3 = "C:\\Users\\karem\\Desktop\\objectprogram.txt";
- Pass2(ArrayList<String> labels,ArrayList<String> operationCodes,ArrayList<String> operands,ArrayList<String> Addresses){
- this.labels = labels;
- this.operationCodes = operationCodes;
- this.operands = operands;
- this.Addresses = Addresses;
- this.lineNumber = 0;
- this.run();
- }
- private void run(){
- for(int i = 0; i < labels.size();i++){
- String objectCode = getObjectCode(operationCodes.get(i), operands.get(i));
- writeListing(labels.get(i), operationCodes.get(i), operands.get(i), objectCode);
- writeObject(labels.get(i), operationCodes.get(i), objectCode);
- }
- }
- private String getObjectCode(String operationCode, String operand) {
- // search for operation Code in the mnemonic Table
- boolean found = false;
- String mnemonicValue = "00";
- String operandAddress;
- String objectCode = new String();
- if (found) {
- if (operand == null) {
- operandAddress = "0000";
- objectCode = mnemonicValue.concat(operandAddress);
- } else {
- // search for operand in the symtab
- // if found
- // operandAddress = symbol value;
- // objectCode = mnemonicValue.concat(operandAddress);
- // if not found (Un Defined Symbol)
- // operandAddress = "0000";
- // ERROR
- }
- } else if (operationCode.equals("WORD")) {
- String hex = Integer.toHexString(Integer.parseInt(operand));
- if (hex.length() == 1) {
- String concat = "00000";
- hex = concat.concat(hex);
- } else if (hex.length() == 2) {
- String concat = "0000";
- hex = concat.concat(hex);
- } else if (hex.length() == 3) {
- String concat = "000";
- hex = concat.concat(hex);
- } else if (hex.length() == 4) {
- String concat = "00";
- hex = concat.concat(hex);
- } else if (hex.length() == 5) {
- String concat = "0";
- hex = concat.concat(hex);
- }
- objectCode = hex;
- } else if (operationCode.equals("BYTE")) {
- if (operand.charAt(0) == 'X') {
- objectCode = operand.substring(2, operationCode.length());
- } else if (operand.charAt(0) == 'C') {
- for (int i = 2; i < operand.length() - 1; i++) {
- objectCode = objectCode.concat(Integer.toHexString((int) operand.charAt(i)));
- }
- }
- }
- // if object code will not fit into the current text record
- // write text record to object program
- // intialize new text record
- // add object code to text record
- return objectCode;
- }
- private void writeListing(String label, String operationCode, String operand, String objectCode) {
- if (label == null) {
- label = "\t";
- }
- if (operand == null) {
- operand = "\t";
- }
- // while(label.length() < 9){
- // label = label;
- // }
- //
- // while(operationCode.length() < 6){
- // operationCode = operationCode.concat(" ");
- // }
- // listing file
- lineNumber++;
- int line = lineNumber * 5;
- String listing;
- if (lineNumber == 1) {
- listing = "Line" + "\t" + "LOC" + "\t\t" + "Source Statement" + "\t\t" + "Object Code" + "\n";
- listingfile.add(listing);
- }
- if (operationCode.equals("START") || operationCode.equals("RESB") || operationCode.equals("RESW")) {
- listing = String.valueOf(line) + "\t" + "LOC" + "\t" + label + "\t" + operationCode + "\t" + operand + "\n";
- listingfile.add(listing);
- } else if (operationCode.equals("END")) {
- listing = String.valueOf(line) + "\t" + "LOC" + "\t" + label + "\t" + operationCode + "\t" + operand;
- listingfile.add(listing);
- writeListingFile();
- } else {
- listing = String.valueOf(line) + "\t" + "LOC" + "\t" + label + "\t" + operationCode + "\t" + operand + "\t"
- + objectCode + "\n";
- listingfile.add(listing);
- }
- // objectProgram
- }
- private void writeObject(String label, String operationCode, String objectCode) {
- String objectLine;
- if (operationCode.equals("START")) {
- while (label.length() != 6) {
- label = label.concat(" ");
- }
- objectLine = "H" + label + "STADDR" + "LENPRO" + "\n";
- objectProgram.add(objectLine);
- writeObjectProgramFile();
- }
- }
- private void writeListingFile() {
- // Listing File
- try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME2))) {
- for (int i = 0; i < listingfile.size(); i++) {
- bw.write(listingfile.get(i));
- }
- bw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void writeObjectProgramFile() {
- // objectProgram file
- try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME3))) {
- for (int i = 0; i < objectProgram.size(); i++) {
- bw.write(objectProgram.get(i));
- }
- bw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment