Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assembler;
- import java.io.BufferedWriter;
- 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 = " ";
- }
- if (operand == null) {
- operand = " ";
- }
- lineNumber++;
- int line = lineNumber * 5;
- String listing;
- if (lineNumber == 1) {
- listing = "Line";
- listing = addSpaces(listing, 8);
- listing = listing.concat("Loc");
- listing = addSpaces(listing, 19);
- listing = listing.concat("Source statement");
- listing = addSpaces(listing, 54);
- listing = listing.concat("Object Code");
- listing = listing.concat("\n");
- listingfile.add(listing);
- }
- if (operationCode.equals("START") || operationCode.equals("RESB") || operationCode.equals("RESW")) {
- listing = String.valueOf(line);
- listing = addSpaces(listing, 8);
- listing = listing.concat("LOC");
- listing = addSpaces(listing, 16);
- listing = listing.concat(label);
- listing = addSpaces(listing, 26);
- listing = listing.concat(operationCode);
- listing = addSpaces(listing, 34);
- listing = listing.concat(operand);
- listing = listing.concat("\n");
- listingfile.add(listing);
- } else if (operationCode.equals("END")) {
- listing = String.valueOf(line);
- listing = addSpaces(listing, 8);
- listing = listing.concat("LOC");
- listing = addSpaces(listing, 16);
- listing = listing.concat(label);
- listing = addSpaces(listing, 26);
- listing = listing.concat(operationCode);
- listing = addSpaces(listing, 34);
- listing = listing.concat(operand);
- listing = listing.concat("\n");
- listingfile.add(listing);
- writeListingFile();
- } else {
- listing = String.valueOf(line);
- listing = addSpaces(listing, 8);
- listing = listing.concat("LOC");
- listing = addSpaces(listing, 16);
- listing = listing.concat(label);
- listing = addSpaces(listing, 26);
- listing = listing.concat(operationCode);
- listing = addSpaces(listing, 34);
- listing = listing.concat(operand);
- listing = addSpaces(listing, 54);
- listing = listing.concat(objectCode);
- listing = listing.concat("\n");
- listingfile.add(listing);
- }
- }
- private String addSpaces(String STR, int length) {
- while (STR.length() < length) {
- STR = STR.concat(" ");
- }
- return STR;
- }
- 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