Advertisement
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 ObjectCode {
- private int cursor;
- private int lineNumber = 0;
- private ArrayList<String> listingfile = new ArrayList<>();
- private ArrayList<String> objectProgram = new ArrayList<>();
- private static final String FILENAME = "C:\\Users\\karem\\Downloads\\SIC-Example.txt";
- private static final String FILENAME2 = "C:\\Users\\karem\\Desktop\\listingfile.txt";
- private static final String FILENAME3 = "C:\\Users\\karem\\Desktop\\objectprogram.txt";
- private void readfile() {
- BufferedReader br = null;
- FileReader fr = null;
- try {
- fr = new FileReader(FILENAME);
- br = new BufferedReader(fr);
- String CurrentLine;
- while ((CurrentLine = br.readLine()) != null) {
- // call for a function that uses the line
- lineNumber++;
- this.line(CurrentLine);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (br != null)
- br.close();
- if (fr != null)
- fr.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- private void line(String line) {
- // check if comment
- char firstChar = line.charAt(0);
- if (firstChar == '.') {
- System.out.println("hello from the other side");
- } else {
- // checking format
- this.checkFormat(line);
- // get Label From Line
- // if no Label in the line get Label will return null
- String label = getLabel(line);
- if (label != null)
- label = label.toUpperCase();
- System.out.print(label);
- // get OperationCode From Line
- String operationCode = getOperationCode(line).toUpperCase();
- System.out.print("\t" + operationCode);
- // get Operand From Line
- // if no operand in the line get operand will return null
- String operand = getOperand(line);
- if (operand != null)
- operand = operand.toUpperCase();
- System.out.print("\t" + operand);
- String objectCode = getObjectCode(operationCode, operand);
- writeListing(label, operationCode, operand, objectCode);
- writeObject(label, operationCode, objectCode);
- }
- System.out.println();
- }
- 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
- 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();
- }
- }
- private void checkFormat(String line) {
- cursor = 8;
- int y = 0;
- if (line.charAt(0) == '\t' || line.charAt(4) == '\t') {
- cursor = 5;
- y = y + 4;
- if (line.charAt(1) == '\t') {
- cursor = 2;
- y = y + 4;
- }
- } else if (line.charAt(5) == '\t') {
- cursor = 6;
- y = y + 3;
- }
- if (line.length() > cursor && line.charAt(cursor) != ' ') {
- System.out.println("Error");
- }
- for (int i = 9; line.length() > i && i < 15; i++) {
- if (line.charAt(i) == '\t') {
- y = y + 3;
- break;
- }
- }
- if (line.length() > 15 - y && line.charAt(15 - y) != ' ' && line.charAt(15 - y) != '\t') {
- System.out.println("Error");
- }
- if (line.length() > 16 - y && line.charAt(16 - y) != ' ' && line.charAt(15 - y) != '\t') {
- System.out.println("Error");
- }
- }
- private String getLabel(String line) {
- boolean flag = false;
- int i = 0;
- char firstChar = line.charAt(0);
- if (firstChar == ' ' || firstChar == '\t') {
- flag = true;
- }
- if (!flag) {
- while (i < 8) {
- if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
- } else {
- break;
- }
- i++;
- }
- String label = line.substring(0, i);
- return label;
- } else
- return null;
- }
- private String getOperationCode(String line) {
- int i = cursor + 1;
- int end = cursor + 7;
- while (i < end) {
- if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
- } else {
- break;
- }
- i++;
- }
- String opertionCode = line.substring(cursor + 1, i);
- cursor = i;
- return opertionCode;
- }
- private String getOperand(String line) {
- boolean flag = false;
- int i = cursor + 1;
- int end = cursor + 31;
- while (i < end) {
- if (line.length() > i && line.charAt(i) == ' ' && line.charAt(i) != '\t') {
- } else {
- break;
- }
- i++;
- cursor++;
- }
- while (i < end) {
- if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
- flag = true;
- } else {
- break;
- }
- i++;
- }
- if (flag) {
- String operand = line.substring(cursor + 1, i);
- return operand;
- } else
- return null;
- }
- public static void main(String[] args) {
- ObjectCode g = new ObjectCode();
- g.readfile();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement