Skip to content

Commit

Permalink
Added comment
Browse files Browse the repository at this point in the history
  • Loading branch information
quazi-irfan committed Nov 16, 2016
1 parent 7cf7443 commit 6bd8004
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 66 deletions.
77 changes: 24 additions & 53 deletions src/Assembler/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@

// NAME : Quazi Irfan
// CLASS : CSc 354
// ASSIGNMENT : 3
// DUE DATE : 11/4/16
// ASSIGNMENT : 4
// DUE DATE : 11/16/16
// INSTRUCTOR : Dr. Hamer
// DESCRIPTION : Assignment 4
// DESCRIPTION : Assignment 4 : Pass 2

/**
* Assembler.Main is the Entry point of Assignment 4 : Pass 2
*
* Input : Intermediate file(file format ending with .int) created from assignment 1
* If not then the program requests a file name. If file not found, the program terminates.
* If found, a static method from Pass1Utility class is called. The last parameter of this method is the output file name.
* Input : A SICXE assembly source.
* Output : The program will generate 3 files.
* During Pass 1 : One intermediate file ending in .int with line counter added to the left of every line.
* During Pass 2 : One update intermediate file ending in .txt extension with object code added to the right of every instruction.
* And one object file ending in .o extension.
*
* If no file is provides as command line argument, the program will ask for a source file.
* We process every instruction, and print output of Pass 1 in that file.
*
*/
Expand All @@ -27,9 +31,23 @@ public static void main(String[] args) throws IOException{
SymbolTable symbolTable = new SymbolTable();
LinkedList<Literal> literalTable = new LinkedList<>();

// // set input files
// String inputFile;
// if(args.length < 1){
// System.out.println("Missing command like argument.");
//
// // request the intput SIC/XE Assembly file
// BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// System.out.print("Enter SIC Assembly Source file : ");
// inputFile = reader.readLine();
// } else {
// inputFile = args[0];
// }

// String inputFile = "SICXE Program 4.asm";
// String inputFile = "CS_Func.asm";
String inputFile = "A3_4.asm";

System.out.println("Reading from File : " + inputFile );

// This function creates an intermediate file in .inc extension, and populates symbol and literal table
Expand Down Expand Up @@ -79,52 +97,5 @@ public static void main(String[] args) throws IOException{
System.out.println("\n> Generated Object Code");
String ofileName = inputFile.substring(0, inputFile.indexOf('.')).concat(".o");
Utility.printFile(ofileName);

// // Populate Symbol Table
// System.out.println("*** SYMBOL TABLE ***\nSymbol\t Value\t rflag\t iflag\t mflag\t");
// BufferedReader symbolReader = new BufferedReader(new FileReader("OldTestFiles/A2_labels.txt"));
// String symbolLine = symbolReader.readLine();
// while(symbolLine != null){
// symbolTable.addLine(symbolLine);
// symbolLine = symbolReader.readLine();
// }
// symbolTable.view();
//
// // Evaluate and print Expressions
// System.out.println("\n*** EXPRESSIONS ***\nExpresion\t\t Value\t Relocatable n i x");
// BufferedReader operandReader = new BufferedReader(new FileReader("OldTestFiles/A2_operands.txt"));
// String operandLine = operandReader.readLine();
// while(operandLine != null) {
// OperandUtility.evaluateOperand(symbolTable, literalTable, operandLine);
// operandLine = operandReader.readLine();
// }

// Pass1Utility.generateIntermediate("A3_1.asm", symbolTable, literalTable);
// System.out.println("************************************************************");
// Pass1Utility.generateIntermediate("A3_2.asm", symbolTable, literalTable);
// System.out.println("************************************************************");
// Pass1Utility.generateIntermediate("A3_3.asm", symbolTable, literalTable);
// System.out.println("************************************************************");
// Pass1Utility.generateIntermediate("A3_4.asm", symbolTable, literalTable);


// // Print contents of the literal table
// System.out.println("\n*** LITERAL TABLE ***\nName\t\t Value\t\t Size\t Address");
// for(Object o : literalTable){
// System.out.println(o);
// }

// // set input files
// String inputFile;
// if(args.length < 1){
// System.out.println("Missing command like argument.");
//
// // request the intput SIC/XE Assembly file
// BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// System.out.print("Enter SIC Assembly Intermediate file : ");
// inputFile = reader.readLine();
// } else {
// inputFile = args[0];
// }
}
}
47 changes: 34 additions & 13 deletions src/Assembler/Pass2Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public class Pass2Utility {

public static PrintWriter objectWriter, txtWriter;

/**
* Main loop of Pass 2 that loop over all instruction generated by Pass 1.
* @param inputFile Input file name. i.e. Program.asm; internally this function looks for Program.int.
* @param symbolTable Symbol table generated from Pass 1.
* @param literalTable Literal table generated from Pass 1.
* @throws IOException Exception is thrown is file operation isn't successful.
*/

public static void generateObj(String inputFile, SymbolTable symbolTable, LinkedList<Literal> literalTable) throws IOException{
String txtFile = inputFile.substring(0, inputFile.indexOf('.')).concat(".txt");
String objFile = inputFile.substring(0, inputFile.indexOf('.')).concat(".o");
Expand Down Expand Up @@ -371,6 +379,8 @@ else if(OpcodeUtility.getFormat(fields[2]) == 4) {
}

txtWriter.close();
// *****************************************************


// Write the Object File
objectWriter.println(HRecord);
Expand All @@ -389,6 +399,7 @@ else if(OpcodeUtility.getFormat(fields[2]) == 4) {

objectWriter.println(ERecord);

// close the object file
objectWriter.close();

}
Expand Down Expand Up @@ -474,9 +485,9 @@ private static String[] getFields(String instruction) {
}

/**
*
* @param operand
* @return
* Given an operand, return the addressing mode.
* @param operand Pass an operand to evaluate it's N and I bit.
* @return This function return 1 for Intermediate, 2 for Indirect and 3 for Simple/Direct addressing.
*/
public static int getAddressingMode(Operand operand){
if(!operand.Nbit && operand.Ibit)
Expand All @@ -488,10 +499,10 @@ else if(operand.Nbit && !operand.Ibit)
}

/**
*
* @param literalTable
* @param literalExpression
* @return
* Given the value of a literal, find it's address in the literal table.
* @param literalTable Provide the literal table generated during Pass 1.
* @param literalExpression Provide the string value, include C'', to perform lookup.
* @return return the address of the literal.
*/
private static int findLiteralAddress(LinkedList<Literal> literalTable, String literalExpression){
// remove the '=' character since none of the literal on literal table has that character
Expand All @@ -505,6 +516,12 @@ private static int findLiteralAddress(LinkedList<Literal> literalTable, String l
return -1;
}

/**
* Given the value of a literal, find it's address in the literal table.
* @param literalTable Provide the literal table generated during Pass 1.
* @param literalExpression Provide the string value, include C'', to perform lookup.
* @return return the hex of the literal.
*/
private static String findLiteralValue(LinkedList<Literal> literalTable, String literalExpression){
for(Literal literal : literalTable){
if(literal.name.equals(literalExpression))
Expand All @@ -515,9 +532,9 @@ private static String findLiteralValue(LinkedList<Literal> literalTable, String
}

/**
*
* @param fields
* @return
* Calcualte the value of the next Line Counter.
* @param fields Pass the instruction as an array of String.
* @return Returns the value of the next Line Counter.
*/
private static int getNextLineCounter(String[] fields){
// current list counter
Expand Down Expand Up @@ -556,10 +573,14 @@ else if(fields[2].equals("RESB")){
}

/**
* // TODO ANTI PATTERN; DON'T USE EXCEPTION TO CONTROL PROGRAM FLOW
* Return the sign of a symbol.
* It looks into the location right before the symbol, and if it finds a sign character, the sign is returns.
* Else + sign is returns.
*
* @param operand
* @param indexOfSymbol
* @return
* @param operand Provide the entire operand to search from.
* @param indexOfSymbol Provide the index of the symbol in query.
* @return returns + or -.
*/
private static char getSignOfSymbol(String operand, int indexOfSymbol){
try {
Expand Down

0 comments on commit 6bd8004

Please sign in to comment.