Skip to content

Commit

Permalink
Everything except Format 4 and M record
Browse files Browse the repository at this point in the history
  • Loading branch information
quazi-irfan committed Nov 14, 2016
1 parent 6ac2364 commit f0a2437
Show file tree
Hide file tree
Showing 10 changed files with 546 additions and 138 deletions.
97 changes: 66 additions & 31 deletions src/Assembler/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Assembler;

import OperandPkg.Literal;
import OperandPkg.OperandUtility;
import SymbolPkg.*;

import java.io.*;
Expand All @@ -11,12 +12,12 @@
// ASSIGNMENT : 3
// DUE DATE : 11/4/16
// INSTRUCTOR : Dr. Hamer
// DESCRIPTION : Assignment 3
// DESCRIPTION : Assignment 4

/**
* Assembler.Main is the Entry point of Assignment 3 : Pass 1
* Assembler.Main is the Entry point of Assignment 4 : Pass 2
*
* Thic class checks if the SIC Assembly source was supplied via command line.
* 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.
* We process every instruction, and print output of Pass 1 in that file.
Expand All @@ -25,44 +26,78 @@
public class Main {
public static void main(String[] args) throws IOException{
SymbolTable symbolTable = new SymbolTable();
LinkedList<Literal> literalLnkdLst = 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 file : ");
inputFile = reader.readLine();
} else {
inputFile = args[0];
}


// Pass1Utility.populateIntermediateFile(inputFile, symbolTable, literalLnkdLst);
Pass1Utility.populateIntermediateFile("A3_1.txt", symbolTable, literalLnkdLst);
System.out.println("************************************************************");
Pass1Utility.populateIntermediateFile("A3_2.txt", symbolTable, literalLnkdLst);
System.out.println("************************************************************");
Pass1Utility.populateIntermediateFile("A3_3.txt", symbolTable, literalLnkdLst);
System.out.println("************************************************************");
Pass1Utility.populateIntermediateFile("A3_4.txt", symbolTable, literalLnkdLst);
LinkedList<Literal> literalTable = new LinkedList<>();

// // Populate Symbol Table
// SymbolTableUtility.populateSymbolTable(symbolTable, "A2_labels.txt");
// 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");
// OperandUtility.evaluateOperand(symbolTable, literalLnkdLst, "A2_operands.txt");
//
// 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();
// }

String inputFile = "CS_Func.txt";
Pass1Utility.populateTableGenerateInt(inputFile, symbolTable, literalTable);

// print the symbol table and literal table
System.out.println("> Symbol Value\trflag\tiflag\tmflag");

// TODO are the relocaability of registers are false? Does it matter?
symbolTable.addLine("A 0 false");
symbolTable.addLine("X 1 false");
symbolTable.addLine("L 2 false");
symbolTable.addLine("B 3 false");
symbolTable.addLine("S 4 false");
symbolTable.addLine("T 5 false");
symbolTable.addLine("F 6 false");
symbolTable.addLine("PC 8 false");
symbolTable.addLine("SW 9 false");

symbolTable.view();
System.out.println("\n> literal\tValue\tlength\taddress");
for(Literal l : literalTable)
System.out.println(l);

System.out.println("\n> Generated Object code");
Pass2Utility.generateObj(inputFile, symbolTable, literalTable);

// Pass1Utility.populateTableGenerateInt("A3_1.txt", symbolTable, literalTable);
// System.out.println("************************************************************");
// Pass1Utility.populateTableGenerateInt("A3_2.txt", symbolTable, literalTable);
// System.out.println("************************************************************");
// Pass1Utility.populateTableGenerateInt("A3_3.txt", symbolTable, literalTable);
// System.out.println("************************************************************");
// Pass1Utility.populateTableGenerateInt("A3_4.txt", 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 : literalLnkdLst){
// 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];
// }
}
}
16 changes: 15 additions & 1 deletion src/Assembler/OpcodeUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ public class OpcodeUtility {
* @return Returns opcode if found from instructions.txt, or returns -1
*/
public static int getFormat(String opcode){
boolean format4 = false;
if(opcode.contains("+")) {
format4 = true;
opcode = opcode.substring(1);
}

try(BufferedReader inputReader = new BufferedReader(new FileReader("instructions.txt"))){
String instruction = inputReader.readLine();
StringTokenizer tokenizer;
while(instruction != null){
tokenizer = new StringTokenizer(instruction, " ");
if(tokenizer.hasMoreTokens()){
if(tokenizer.nextToken().equals(opcode)){
return Integer.parseInt(tokenizer.nextToken());
// if opcode found fetch the second column or return 4 if format4 is true
if(format4)
return 4;
else
return Integer.parseInt(tokenizer.nextToken());
}
}
instruction = inputReader.readLine();
Expand All @@ -42,13 +52,17 @@ public static int getFormat(String opcode){
* @return Returns Hex code as String if found from instructions.txt, or returns null
*/
public static String getHexCode(String opcode){
if(opcode.contains("+"))
opcode = opcode.substring(1);

try(BufferedReader inputReader = new BufferedReader(new FileReader("instructions.txt"))){
String instruction = inputReader.readLine();
StringTokenizer tokenizer;
while(instruction != null){
tokenizer = new StringTokenizer(instruction, " ");
if(tokenizer.hasMoreTokens()){
if(tokenizer.nextToken().equals(opcode)){
// if opcode found fetch the third column
tokenizer.nextToken();
return tokenizer.nextToken();
}
Expand Down
93 changes: 37 additions & 56 deletions src/Assembler/Pass1Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
public class Pass1Utility {
public static int startAddress = 0;
public static int LineCounter = startAddress; // both startAddress and LineCounter will change if START directive is found
public static int programLength = 0;

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

// set the output file name, and set a output writer to write on that file
String intermediateFileName = assemblyFileName.substring(0, assemblyFileName.indexOf('.')).concat(".int");
PrintWriter outputWriter = new PrintWriter(intermediateFileName, "UTF-8");
PrintWriter writer = new PrintWriter(intermediateFileName, "UTF-8");

// Take input and process SIC Instruction
BufferedReader inputReader = new BufferedReader(new FileReader(assemblyFileName));
String instruction = inputReader.readLine();
BufferedReader reader = new BufferedReader(new FileReader(assemblyFileName));
String instruction = reader.readLine();

String label, opcode, operand;
int format = 0;
Expand All @@ -50,14 +51,12 @@ public static void populateIntermediateFile(
// After comment removal if there is no token, read the next line
StringTokenizer tokenizer = new StringTokenizer(instruction);
if(tokenizer.countTokens() == 0){
instruction = inputReader.readLine();
instruction = reader.readLine();
continue;
}


// Make everything upper case in input instruction


// read label
Node symbol = null;
// if first character is a white space then there is no label
Expand All @@ -79,14 +78,8 @@ public static void populateIntermediateFile(
symbol.rflag = false;

// format 3 or format 4 opcode
if(opcode.charAt(0) == '+'){
format = 4;
LineCounter += format;
} else {
format = OpcodeUtility.getFormat(opcode);
LineCounter += format;
}

format = OpcodeUtility.getFormat(opcode);
LineCounter += format;
}

// read operand
Expand All @@ -104,11 +97,11 @@ public static void populateIntermediateFile(
symbol.rflag = true;
} else {
// Evaluate the operand
OperandUtility.evaluateOperand(symbolTable, literalLnkdLst, operand);
OperandUtility.evaluateOperand(symbolTable, literalTable, operand);
}
}

// Handles BYTE/WORD operand
// Handles BYTE operand
if(opcode.equals("BYTE")){
String temp = operand.substring(operand.indexOf('\'')+1, operand.lastIndexOf('\''));
if(operand.contains("C"))
Expand All @@ -119,6 +112,12 @@ public static void populateIntermediateFile(
LineCounter += format;
}

// Handles WORD operand
if(opcode.equals("WORD")){
format = 3;
LineCounter += format;
}

// Handles RESW operand
if(opcode.equals("RESW")){
format = 3 * Integer.parseInt(operand);
Expand All @@ -132,66 +131,48 @@ public static void populateIntermediateFile(
}

// generate the intermediate instruction
String intermediateInstruction = String.format("%-10s %-10s %-10s %-10s",
padHexString(LineCounter-format),
String intermediateInstruction = String.format("%-15s%-15s%-15s%-15s",
Utility.pad(LineCounter-format, 5),
((label == null) ? " " : label ),
((opcode == null) ? " " : opcode ),
((operand == null) ? " " : operand));

// print the intermediate instruction to file
outputWriter.println(intermediateInstruction);

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


// print the intermediate instruction to terminal
instruction = inputReader.readLine();
instruction = reader.readLine();
}

// add the literal lists at the end of the program and update the address of the literals
for(Literal literal : literalLnkdLst){
String intermediateInstruction = String.format("%-10s * %-10s", padHexString(LineCounter-format), literal.name);
for(Literal literal : literalTable){
String intermediateInstruction = String.format("%-15s* %-15s", Utility.pad(LineCounter-format, 5), literal.name);

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

literal.address = LineCounter-format;
LineCounter += literal.length;
}

// Print the program langth; LineCounter is always updated
System.out.println("Program Length : " + Integer.toHexString(LineCounter).toUpperCase());
// System.out.println("Program Length : " + Integer.toHexString(LineCounter).toUpperCase());
programLength = LineCounter;

// close the intermediate file
outputWriter.close();
writer.close();

// print the populated symbol table
System.out.println("\n *** Symbol Table : (Symbol Value Rflag Iflag Mflag)");
symbolTable.view();
// 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 : literalLnkdLst){
System.out.println(o);
}
// System.out.println("\n *** Literal Table : (Literal HexValue Length Address)");
// for(Object o : literalTable){
// System.out.println(o);
// }
}

private static String padHexString(int lineCounter){
int padCount;

if(lineCounter >= 16 & lineCounter < 255) // More than F, Pad '00010'
padCount = 3;
else if(lineCounter >= 255 & lineCounter < 4095) // More than FF, Pad '00100'
padCount = 2;
else if(lineCounter >= 4095) // More than FFF, Pad '01000'
padCount = 1;
else
padCount = 4; // Single character, Pad '00001'

// add zero's
StringBuilder sb = new StringBuilder();
for(int i=0; i<padCount; i++)
sb.append('0');

// add the hex value after zero
sb.append(Integer.toHexString(lineCounter));

return sb.toString().toUpperCase();
}
}
Loading

0 comments on commit f0a2437

Please sign in to comment.