Skip to content

Commit

Permalink
Remaining T record listing and Base register addressing
Browse files Browse the repository at this point in the history
  • Loading branch information
quazi-irfan committed Nov 14, 2016
1 parent f0a2437 commit ca8f746
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 49 deletions.
40 changes: 14 additions & 26 deletions src/Assembler/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package Assembler;

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

import java.io.*;
Expand All @@ -28,6 +27,20 @@ public static void main(String[] args) throws IOException{
SymbolTable symbolTable = new SymbolTable();
LinkedList<Literal> literalTable = new LinkedList<>();

String inputFile = "CS_Prog.txt";

System.out.println("> Generated Intermediate File");
Pass1Utility.populateTableGenerateInt(inputFile, symbolTable, literalTable);

System.out.println("\n> Symbol Value\trflag\tiflag\tmflag");
symbolTable.view();

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

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

// // 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 @@ -47,31 +60,6 @@ public static void main(String[] args) throws IOException{
// 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);
Expand Down
2 changes: 2 additions & 0 deletions src/Assembler/Pass1Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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 String controlSectionName = "unnamed";

public static void populateTableGenerateInt(
String assemblyFileName, SymbolTable symbolTable, LinkedList<Literal> literalTable)
Expand Down Expand Up @@ -89,6 +90,7 @@ public static void populateTableGenerateInt(
if(opcode.equals("START") && Utility.isInteger(operand)) {
startAddress = Integer.parseInt(operand);
LineCounter = startAddress;
controlSectionName = label;
}

if(operand.equals("*") && symbol != null) {
Expand Down
150 changes: 127 additions & 23 deletions src/Assembler/Pass2Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.StringTokenizer;

/**
* Pass2 class of SIC Assembler
* Pass2 class of SICXE Assembler
*/
public class Pass2Utility {
public static ArrayList<String> MRecordLists = new ArrayList<>();

public static void generateObj(String inputFile, SymbolTable symbolTable, LinkedList<Literal> literalTable) throws IOException{
inputFile = inputFile.substring(0, inputFile.indexOf('.')).concat(".int");

Expand All @@ -39,7 +42,6 @@ public static void generateObj(String inputFile, SymbolTable symbolTable, Linked
}

if(fields[2].equals("BASE") | fields[2].equals("EQU") | fields[2].equals("RESB") | fields[2].equals("RESW")){
// terminate and dump pending t records, and start a new t record
System.out.println(instruction + " -"); // printing objectcode
instruction = reader.readLine();
continue;
Expand All @@ -54,6 +56,7 @@ public static void generateObj(String inputFile, SymbolTable symbolTable, Linked
int symbolValue = symbolTable.search(symbolName).getValue();
objectCode = objectCode.concat("^").concat(Utility.pad(symbolName)).concat("^").concat(Utility.pad(symbolValue, 6));
}

System.out.println(objectCode); // printing objectcode
instruction = reader.readLine();
continue;
Expand All @@ -72,15 +75,16 @@ public static void generateObj(String inputFile, SymbolTable symbolTable, Linked

objectCode = objectCode.concat("^").concat(Utility.pad(symbolName));
}

System.out.println(objectCode); // printing objectcode
instruction = reader.readLine();
continue;
}

// handle BYTE C'abc'
if(fields[2].equals("BYTE")){
// handle BYTE C'AB'
if(fields[3].contains("C'")){
// generate the object code
fields[3] = fields[3].substring(fields[3].indexOf("'")+1, fields[3].lastIndexOf("'"));
String charHexValue = "";
for(int i = 0; i<fields[3].length(); i++) {
Expand All @@ -96,21 +100,26 @@ public static void generateObj(String inputFile, SymbolTable symbolTable, Linked
else {
fields[3] = fields[3].substring(fields[3].indexOf("'")+1, fields[3].lastIndexOf("'"));
objectCode = fields[3];

System.out.println(instruction + " " + objectCode.toUpperCase()); // printing objectcode
MRecordLists.addAll(generateMRecord(fields, symbolTable));
instruction = reader.readLine();
continue;
}

}

// handle WORD 97
// handle WORD 97 or WORD ONE-TWO
else if(fields[2].equals("WORD")){
objectCode = Utility.pad(Integer.parseInt(fields[3]), 5);
System.out.println(instruction + " " + objectCode.toUpperCase()); // printing objectcode
OperandUtility.evaluateOperand(symbolTable, literalTable, fields[3]);
objectCode = Utility.pad(OperandUtility.operand.value, 6);

System.out.println(instruction + " " + objectCode); // printing objectcode
MRecordLists.addAll(generateMRecord(fields, symbolTable));
instruction = reader.readLine();
continue;
}

// All the remaining Opcode
// lineCounter-field[0] label-field[1] opcode-field[2] operand-field[3]
if(!fields[2].equals("END")){
// *** Part 1 :: OpcodeNI
Expand All @@ -123,27 +132,27 @@ else if(fields[2].equals("WORD")){

// format 1
if(OpcodeUtility.getFormat(fields[2]) == 1) {
objectCode = objectCode.concat("0000");
// objectCode = objectCode.concat("0000");

System.out.println(instruction + " " + objectCode); // printing objectcode
instruction = reader.readLine();
continue;
}

// format 2
else if(OpcodeUtility.getFormat(fields[2]) == 2){
if(OpcodeUtility.getFormat(fields[2]) == 2){
StringTokenizer tokenizer = new StringTokenizer(fields[3], ",");

while(tokenizer.hasMoreTokens()){
String symbolName = tokenizer.nextToken();
Node tempSymbol = symbolTable.search(symbolName);
if(tempSymbol != null){
objectCode = objectCode.concat(Integer.toString(tempSymbol.value));
String registerName = tokenizer.nextToken();
if(registerName != null){
objectCode = objectCode.concat(Integer.toString(Utility.getRegisterValue(registerName)));
} else {
objectCode = objectCode.concat("0");
}
}

while (objectCode.length()<4){
while (objectCode.length()<4) {
objectCode = objectCode.concat("0");
}

Expand All @@ -154,7 +163,7 @@ else if(OpcodeUtility.getFormat(fields[2]) == 2){

// X bit
int XBPE = 0;
if(OperandUtility.operand.Xbit) {
if(OperandUtility.operand.Xbit){
XBPE += 8;
}

Expand All @@ -164,6 +173,7 @@ else if(OpcodeUtility.getFormat(fields[2]) == 2){
// if there is no operand in a format 3 instruction
if(fields[3] == null){
objectCode = objectCode.concat("0000");

System.out.println(instruction + " " + objectCode);
instruction = reader.readLine();
continue;
Expand All @@ -173,6 +183,7 @@ else if(OpcodeUtility.getFormat(fields[2]) == 2){
// for non-relocatable operand, appended the value at the end of object code
if(!OperandUtility.operand.relocability & fields[3].charAt(0) != '='){ // if rflag = false == true AND there is no literal
objectCode = objectCode.concat(Utility.pad(OperandUtility.operand.value, 4)); // #ARRAY OR 5 or 5+7

System.out.println(instruction + " " + objectCode); // printing objectcode
}

Expand Down Expand Up @@ -202,17 +213,22 @@ else if(OpcodeUtility.getFormat(fields[2]) == 2){
// before using Base relative addressing

XBPE += 3;
// TODO handle base register addressing mode
System.out.println("USE BASE RELATIVE ADDRESSING!");
}
}
}

// if there is +LDA
// format 4 & Address
else if(OpcodeUtility.getFormat(fields[2]) == 4){
else if(OpcodeUtility.getFormat(fields[2]) == 4) {
XBPE += 1;
objectCode = objectCode.concat(Utility.pad(XBPE,1)).concat(Utility.pad(OperandUtility.operand.value, 5));
OperandUtility.evaluateOperand(symbolTable, literalTable, fields[3]);
objectCode = objectCode.concat(Utility.pad(XBPE, 1)).concat(Utility.pad(OperandUtility.operand.value, 5));
System.out.println(instruction + " " + objectCode); // printing objectcode

MRecordLists.addAll(generateMRecord(fields, symbolTable));

}

// after processing format 3 and format 4 instruction, go to next line
Expand All @@ -225,14 +241,67 @@ else if(OpcodeUtility.getFormat(fields[2]) == 4){
// END directive with operand
if(fields[3] != null) {
OperandUtility.evaluateOperand(symbolTable, literalTable, fields[3]);
System.out.println(instruction + " " + "E^" + Utility.pad(OperandUtility.operand.value, 6));
objectCode = objectCode.concat("E^").concat(Utility.pad(OperandUtility.operand.value, 6));
} else {
System.out.println(instruction + " " + "E^");
objectCode = objectCode.concat("E^");
}
System.out.println(instruction + " " + objectCode); // printint object code
instruction = reader.readLine();
continue;
}
}

for(String mrecord : MRecordLists)
System.out.println(mrecord);
}

/**
* Generate M records given the Symbol Table, and the full instruction as an array of string.
* @param fields Given instruction split into array of strings
* @param symbolTable symbol table to check for the rflag of the symbol found in the operand
* @return Returns the list of generated M records.
*/
private static ArrayList<String> generateMRecord(String[] fields, SymbolTable symbolTable) {
ArrayList<String> MRecordList = new ArrayList<>();

int offset = 0;
String nibbles;
if(fields[2].equals("WORD") | fields[2].equals("BYTE")){
nibbles = "06";
offset = 0;
}
else {
nibbles = "05";
offset = 1;
}

// always M record for external symbol
for (Node symbol : symbolTable.getAllExternal()) {
int index = fields[3].indexOf(symbol.getKey()); // find if the symbol exists in the operand

if (index != -1) {
char ch = getSign(fields[3], index); // check for sign
String genMRec = "M^" + Utility.pad(Integer.parseInt(fields[0], 16) + offset, 6) + "^" + nibbles + "^" + ch + Utility.pad(symbol.getKey());
MRecordList.add(genMRec);
}
}

// if the operand is relocatable, M record for all relocatable symbols
if(OperandUtility.operand.relocability) {
for (Node symbol : symbolTable.getAll()) {
int index = fields[3].indexOf(symbol.getKey()); // find if the symbol exists in the operand
String controlSection = (symbol.getIflag() ? Pass1Utility.controlSectionName : symbol.getKey()); // identify control section

if (index != -1 && symbol.rflag) {
char ch = getSign(fields[3], index); // check for sign
String genMRec = "M^" + Utility.pad(Integer.parseInt(fields[0], 16) + offset, 6) + "^" + nibbles + "^" + ch + Utility.pad(controlSection);
MRecordList.add(genMRec);
break;
}
}
}

return MRecordList;
}

/**
Expand Down Expand Up @@ -261,21 +330,31 @@ private static String[] getFields(String instruction) {

// get operand if exists
if(tokenizer.hasMoreTokens())
// if(!(instruction.charAt(45) <= 32))
fields[3] = tokenizer.nextToken();

return fields;
}

public static int getAddressingMode(Operand o){
if(!o.Nbit && o.Ibit)
/**
*
* @param operand
* @return
*/
public static int getAddressingMode(Operand operand){
if(!operand.Nbit && operand.Ibit)
return 1;
else if(o.Nbit && !o.Ibit)
else if(operand.Nbit && !operand.Ibit)
return 2;
else
return 3;
}

/**
*
* @param literalTable
* @param literalExpression
* @return
*/
private static int findLiteralAddress(LinkedList<Literal> literalTable, String literalExpression){
literalExpression = literalExpression.substring(1);

Expand All @@ -287,6 +366,13 @@ private static int findLiteralAddress(LinkedList<Literal> literalTable, String l
return -1;
}

/**
*
* @param currentLineCounter
* @param opcode
* @param operand
* @return
*/
private static int getNextLineCounter(int currentLineCounter, String opcode, String operand){
int opcodeFormat = OpcodeUtility.getFormat(opcode);

Expand Down Expand Up @@ -316,4 +402,22 @@ else if(opcode.equals("RESB")){

return currentLineCounter;
}

/**
*
* @param operand
* @param indexOfSymbol
* @return
*/
private static char getSign(String operand, int indexOfSymbol){
try {
if (operand.charAt(indexOfSymbol - 1) == '-') {
return '-';
}
} catch(StringIndexOutOfBoundsException e) {
return '+';
}

return '+';
}
}
Loading

0 comments on commit ca8f746

Please sign in to comment.