Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assembler;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- public class ObjectCode {
- private int cursor;
- private static final String FILENAME = "C:\\Users\\karem\\Downloads\\SIC-Example.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
- 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)
- System.out.print(label);
- // get OperationCode From Line
- String operationCode = getOperationCode(line);
- 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)
- System.out.print("\t" + operand);
- }
- System.out.println();
- }
- 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