Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
quazi-irfan committed Nov 16, 2016
1 parent 7ebb6e0 commit 7cf7443
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 38 deletions.
56 changes: 46 additions & 10 deletions src/Assembler/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,56 @@ public static void main(String[] args) throws IOException{
// String inputFile = "SICXE Program 4.asm";
// String inputFile = "CS_Func.asm";
String inputFile = "A3_4.asm";
System.out.println("Reading from : " + inputFile );
System.out.println("Reading from File : " + inputFile );

// This function creates an intermediate file in .inc extension, and populates symbol and literal table
Pass1Utility.generateIntermediate(inputFile, symbolTable, literalTable);

// print the .inc file
System.out.println("\n*********** PASS 1 ***********");
System.out.println("\n> Generated Intermediate File");
Pass1Utility.populateTableGenerateInt(inputFile, symbolTable, literalTable);
String incFileNmae = inputFile.substring(0, inputFile.indexOf('.')).concat(".int");
Utility.printFile(incFileNmae);

// print the symbol table
System.out.println("\n> Symbol Value\trflag\tiflag\tmflag");
symbolTable.view();
int symbolTableSize = symbolTable.size();
if(symbolTableSize != 0)
symbolTable.view();
else
System.out.println("Empty Symbol Table");

System.out.println("\n> literal\tValue\tlength\taddress");
for(Literal literal : literalTable) System.out.println(literal);
// print the literal table
System.out.println("\n> literal\tValue\t\tlength\taddress");
if(literalTable.size() != 0) {
for (Literal literal : literalTable)
System.out.println(literal);
} else {
System.out.println("Empty Literal Table");
}

System.out.println("\n> Generated Object code");
// This function creates an updates intermediate file in .txt extension, and object file in .o extension
Pass2Utility.generateObj(inputFile, symbolTable, literalTable);

// print the updated intermediate code
System.out.println("\n\n*********** PASS 2 ***********");
System.out.println("\n> Updated Intermediate Code");
String txtfileName = inputFile.substring(0, inputFile.indexOf('.')).concat(".txt");
Utility.printFile(txtfileName);

// print the symbol table
if(symbolTable.size() != symbolTableSize) {
System.out.println("\nUpdated Symbol Table\n> Symbol Value\trflag\tiflag\tmflag");
symbolTable.view();
}
else
System.out.println("\nNo change in Symbol table during Pass 2.");

// print the generated object code
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"));
Expand All @@ -63,13 +99,13 @@ public static void main(String[] args) throws IOException{
// operandLine = operandReader.readLine();
// }

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


// // Print contents of the literal table
Expand Down
14 changes: 1 addition & 13 deletions src/Assembler/Pass1Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Pass1Utility {
public static int programLength = 0;
public static String controlSectionName = "unnamed";

public static void populateTableGenerateInt(
public static void generateIntermediate(
String assemblyFileName, SymbolTable symbolTable, LinkedList<Literal> literalTable)
throws IOException {

Expand Down Expand Up @@ -146,10 +146,8 @@ public static void populateTableGenerateInt(
((operand == null) ? " " : operand));

// print the intermediate instruction to terminal and file
System.out.println(intermediateInstruction);
incWriter.println(intermediateInstruction);


// print the intermediate instruction to terminal
instruction = asmReader.readLine();
}
Expand All @@ -158,7 +156,6 @@ public static void populateTableGenerateInt(
for(Literal literal : literalTable){
String intermediateInstruction = String.format("%-15s* %-15s", Utility.pad(LineCounter-format, 5), literal.name);

System.out.println(intermediateInstruction);
incWriter.println(intermediateInstruction);

literal.address = LineCounter-format;
Expand All @@ -172,15 +169,6 @@ public static void populateTableGenerateInt(
// close the intermediate file
incWriter.close();

// print the populated symbol table
// System.out.println("\n *** Symbol Table : (Symbol Value Rflag Iflag Mflag)");
// symbolTable.view();

// print the populated literal tbale
// System.out.println("\n *** Literal Table : (Literal HexValue Length Address)");
// for(Object o : literalTable){
// System.out.println(o);
// }
}

}
30 changes: 15 additions & 15 deletions src/Assembler/TRecordList.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ class TRecordList {
public ArrayList<String > allrecords = new ArrayList<>();
private String record = "";

public void terminateTRecord(){
if(length > 1) {
allrecords.add(insertLengthInfo(record));
count = 0;
record = "";
}
}

public void add(String objCode, String[] fields){
// first time
if (count == 0){
Expand Down Expand Up @@ -50,6 +42,15 @@ record = record.concat("^").concat(objCode);
}
}

public String insertLengthInfo(String record){
StringBuilder sb = new StringBuilder(record);

sb.insert(9, Utility.pad(length, 2).concat("^"));
length = 0;

return sb.toString();
}

public ArrayList<String> getAllTRecords(){
// append all the pending records
if(length > 1)
Expand All @@ -59,13 +60,12 @@ public ArrayList<String> getAllTRecords(){

}

public String insertLengthInfo(String record){
StringBuilder sb = new StringBuilder(record);

sb.insert(9, Utility.pad(length, 2).concat("^"));
length = 0;

return sb.toString();
public void terminateTRecord(){
if(length > 1) {
allrecords.add(insertLengthInfo(record));
count = 0;
record = "";
}
}

}
14 changes: 14 additions & 0 deletions src/Assembler/Utility.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Assembler;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
Expand Down Expand Up @@ -145,4 +146,17 @@ public static int getRegisterValue(String s){

return registerTable.get(s);
}

public static void printFile(String fileName) throws IOException{
try(BufferedReader reader = new BufferedReader(new FileReader(fileName))){
String line = reader.readLine();
while (line != null){
System.out.println(line);
line = reader.readLine();
}
} catch (IOException e){
System.out.println("Problem opening file : " + fileName);
}

}
}
4 changes: 4 additions & 0 deletions src/SymbolPkg/SymbolTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,8 @@ public ArrayList<Node> getAllInternal(){

return allInternal;
}

public int size(){
return getAll().size();
}
}

0 comments on commit 7cf7443

Please sign in to comment.