Skip to content

Commit

Permalink
Beautify code
Browse files Browse the repository at this point in the history
  • Loading branch information
quazi-irfan committed Nov 15, 2016
1 parent 6d7fd52 commit be7c832
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/Assembler/Pass2Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public static void generateObj(String inputFile, SymbolTable symbolTable, Linked
String objectCode = "";
String[] fields = getFields(instruction);

// ********************************************************

// if field[1] starts with * we have rached the literal dump section
if(fields[1] != null && fields[1].equals("*")){
instruction = reader.readLine();
Expand Down Expand Up @@ -126,18 +128,20 @@ else if(fields[2].equals("WORD")){
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
OperandUtility.evaluateOperand(symbolTable, literalTable, fields[3]); // Doesn't handle old style literal operand
OperandUtility.evaluateOperand(symbolTable, literalTable, fields[3]);
int opcodeNI = Integer.parseInt(OpcodeUtility.getHexCode(fields[2]), 16)
+ getAddressingMode(OperandUtility.operand); // opcode hex + addressing mode
objectCode = objectCode.concat(Utility.pad(opcodeNI, 2));

// *** Part 2 :: XBPEDisplacement/Address

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

Expand All @@ -146,7 +150,8 @@ else if(fields[2].equals("WORD")){
continue;
}

// format 2
// format 2 ********************************************

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

Expand Down Expand Up @@ -174,7 +179,8 @@ else if(fields[2].equals("WORD")){
XBPE += 8;
}

// format 3 & BPEdisplacement
// format 3 & BPEdisplacement *********************************

if(OpcodeUtility.getFormat(fields[2]) == 3){

// if there is no operand in a format 3 instruction
Expand All @@ -184,10 +190,12 @@ else if(fields[2].equals("WORD")){
System.out.println(instruction + " " + objectCode);
instruction = reader.readLine();
continue;
} else {
OperandUtility.evaluateOperand(symbolTable, literalTable, fields[3]);
}

// else {
// OperandUtility.evaluateOperand(symbolTable, literalTable, fields[3]);
// }

// if there is #1000 or #ARRAY as operand in format 3 instruction
// 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
Expand Down Expand Up @@ -227,7 +235,7 @@ else if(fields[2].equals("WORD")){
objectCode = objectCode.concat(Utility.pad(XBPE, 1)).concat(Utility.pad(targetAddress, 3));
System.out.println(instruction + " " + objectCode + " (Using Base Relative addressing)"); // printing objectcode
} else {
System.out.println(instruction + " Error : Address out of range " + Utility.pad(targetAddress, 5));
System.out.println(instruction + " Error : Address out of range " + Utility.pad(targetAddress, 5)); // printing objectcode
}
}
// Use of Base register isn't set
Expand All @@ -251,8 +259,8 @@ else if(OperandUtility.operand.value < Integer.parseInt(fields[0], 16)){
}
}

// if there is +LDA
// format 4 & Address
// format 4 & Address ******************************

else if(OpcodeUtility.getFormat(fields[2]) == 4) {
XBPE += 1;
OperandUtility.evaluateOperand(symbolTable, literalTable, fields[3]);
Expand All @@ -268,7 +276,7 @@ else if(OpcodeUtility.getFormat(fields[2]) == 4) {
continue;
}

// END directive has been reached
// END directive has been reached *****************
else {
// END directive with operand
if(fields[3] != null) {
Expand All @@ -283,6 +291,7 @@ else if(OpcodeUtility.getFormat(fields[2]) == 4) {
}
}

// END directive has been reached *****************
for(String mrecord : MRecordLists)
System.out.println(mrecord);
}
Expand Down Expand Up @@ -312,7 +321,7 @@ private static ArrayList<String> generateMRecord(String[] fields, SymbolTable sy
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
char ch = getSignOfSymbol(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);
}
Expand All @@ -325,7 +334,7 @@ private static ArrayList<String> generateMRecord(String[] fields, SymbolTable sy
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
char ch = getSignOfSymbol(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;
Expand Down Expand Up @@ -405,10 +414,11 @@ private static int findLiteralAddress(LinkedList<Literal> literalTable, String l
* @return
*/
private static int getNextLineCounter(String[] fields){
// current list counter
int currentLineCounter = Integer.parseInt(fields[0], 16);
int format = OpcodeUtility.getFormat(fields[2]);

// handle LDA STA
// handle format 1/2/3/4 opcode
int format = OpcodeUtility.getFormat(fields[2]);
if(format != 0){
return currentLineCounter + format;
}
Expand All @@ -423,6 +433,10 @@ private static int getNextLineCounter(String[] fields){
return currentLineCounter + temp.length() / 2;
}

else if(fields[2].equals("WORD")){
return currentLineCounter + 3;
}

else if(fields[2].equals("RESW")){
return currentLineCounter + 3 * Integer.parseInt(fields[3]);
}
Expand All @@ -441,7 +455,7 @@ else if(fields[2].equals("RESB")){
* @param indexOfSymbol
* @return
*/
private static char getSign(String operand, int indexOfSymbol){
private static char getSignOfSymbol(String operand, int indexOfSymbol){
try {
if (operand.charAt(indexOfSymbol - 1) == '-') {
return '-';
Expand Down

0 comments on commit be7c832

Please sign in to comment.