Skip to content

Commit

Permalink
Complete without Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
quazi-irfan committed Nov 16, 2016
1 parent ba36864 commit 7ebb6e0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Assembler/Pass1Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static void populateTableGenerateInt(
((operand == null) ? " " : operand));

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


Expand Down
35 changes: 29 additions & 6 deletions src/Assembler/Pass2Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static void generateObj(String inputFile, SymbolTable symbolTable, Linked
useBase = true;

txtWriter.println(instruction);
TRecordLists.terminateTRecord();
instruction = reader.readLine();
continue;
}
Expand All @@ -77,6 +78,7 @@ public static void generateObj(String inputFile, SymbolTable symbolTable, Linked
if(fields[2].equals("EQU") | fields[2].equals("RESB") | fields[2].equals("RESW")){

txtWriter.println(instruction);
TRecordLists.terminateTRecord();
instruction = reader.readLine();
continue;
}
Expand All @@ -94,6 +96,7 @@ public static void generateObj(String inputFile, SymbolTable symbolTable, Linked

txtWriter.println(instruction);
DRecordLists.add(objectCode);
TRecordLists.terminateTRecord();
instruction = reader.readLine();
continue;
}
Expand All @@ -118,6 +121,7 @@ public static void generateObj(String inputFile, SymbolTable symbolTable, Linked

txtWriter.println(instruction);
RRecordLists.add(objectCode);
TRecordLists.terminateTRecord();
instruction = reader.readLine();
continue;
}
Expand All @@ -136,6 +140,7 @@ public static void generateObj(String inputFile, SymbolTable symbolTable, Linked

objectCode = hexValue.toUpperCase();
txtWriter.printf("%-60s%s\n",instruction, objectCode);
TRecordLists.add(objectCode, fields);
instruction = reader.readLine();
continue;
}
Expand All @@ -147,6 +152,7 @@ else if(fields[3].contains("X'")){

objectCode = hexValue.toUpperCase();
txtWriter.printf("%-60s%s\n",instruction, objectCode);
TRecordLists.add(objectCode, fields);
instruction = reader.readLine();
continue;
}
Expand All @@ -159,6 +165,7 @@ else if(fields[3].contains("X'")){
objectCode = Utility.pad(operand.value, 6);

txtWriter.printf("%-60s%s\n",instruction, objectCode);
TRecordLists.add(objectCode, fields);
MRecordLists.addAll(generateMRecord(fields, symbolTable));
instruction = reader.readLine();
continue;
Expand All @@ -172,6 +179,7 @@ else if(fields[3].contains("X'")){
objectCode = findLiteralValue(literalTable, fields[2]);

txtWriter.printf("%-60s%s\n",instruction, objectCode);
TRecordLists.add(objectCode, fields);
instruction = reader.readLine();
continue;
}
Expand All @@ -187,6 +195,7 @@ else if(fields[3].contains("X'")){
// format 1 ********************************************
if(OpcodeUtility.getFormat(fields[2]) == 1) {
txtWriter.printf("%-60s%s\n",instruction, objectCode);
TRecordLists.add(objectCode, fields);
instruction = reader.readLine();
continue;
}
Expand All @@ -208,6 +217,7 @@ else if(fields[3].contains("X'")){
}

txtWriter.printf("%-60s%s\n",instruction, objectCode);
TRecordLists.add(objectCode, fields);
instruction = reader.readLine();
continue;
}
Expand All @@ -226,6 +236,7 @@ else if(fields[3].contains("X'")){
objectCode = objectCode.concat("0000");

txtWriter.printf("%-60s%s\n",instruction, objectCode);
TRecordLists.add(objectCode, fields);
instruction = reader.readLine();
continue;
}
Expand All @@ -238,6 +249,7 @@ else if(fields[3].contains("X'")){
objectCode = objectCode.concat(Utility.pad(XBPE, 1)).concat(Utility.pad(operand.value, 3));

txtWriter.printf("%-60s%s\n",instruction, objectCode);
TRecordLists.add(objectCode, fields);
}

// LDA ONE-2
Expand All @@ -263,6 +275,7 @@ else if(fields[3].contains("X'")){
objectCode = objectCode.concat(Utility.pad(XBPE, 1)).concat(Utility.pad(targetAddress, 3));

txtWriter.printf("%-60s%s\n",instruction, objectCode);
TRecordLists.add(objectCode, fields);
// txtWriter.println(instruction + " " + objectCode + " $Positive address within range of P");
instruction = reader.readLine();
continue;
Expand All @@ -280,20 +293,21 @@ else if(fields[3].contains("X'")){
if(targetAddress >= 0 && targetAddress <= 4095) { // 2^12 - 1 = 4096 - 1 = 4095
objectCode = objectCode.concat(Utility.pad(XBPE, 1)).concat(Utility.pad(targetAddress, 3));
txtWriter.printf("%-60s%s\n",instruction, objectCode);
TRecordLists.add(objectCode, fields);
// txtWriter.println(instruction + " " + objectCode + " $Using Base Relative addressing"); // printing objectcode
}

// out of range for Base register
else {
objectCode = "Error : Positive address out of range of B";
objectCode = "$Error: Base relative out of range.";
txtWriter.printf("%-60s%s\n",instruction, objectCode);
// txtWriter.println(instruction + " $Error : Positive address out of range of B : " + Utility.pad(targetAddress, 5)); // printing objectcode
}
}

// Can not use Base regsiter becasue it's not in use
// Can not use Base register because it's not in use
else {
objectCode = "Error : Address out of range, Try Base addressing.";
objectCode = "$Error: (+)PC relative out of range.";
txtWriter.printf("%-60s%s\n",instruction, objectCode);
// txtWriter.println(instruction + " $Error : Positive address out of range of P, try Base addressing. " + Utility.pad(targetAddress, 5)); // printing objectcode
}
Expand All @@ -309,12 +323,13 @@ else if(operandValue < Integer.parseInt(fields[0], 16)){
objectCode = objectCode.concat(Utility.pad(XBPE, 1)).concat(Utility.pad(targetAddress, 3));

txtWriter.printf("%-60s%s\n",instruction, objectCode);
TRecordLists.add(objectCode, fields);
// txtWriter.println(instruction + " " + objectCode + " $Negative displacement within range of P"); // printing objectcode
}

// Out or negative P range
else {
objectCode = "Error : Negative displacement out of range";
objectCode = "$Error: (-)PC relative out of range.";
txtWriter.printf("%-60s%s\n",instruction, objectCode);
// txtWriter.println(instruction + " $Error : Negative displacement out of range of P :" + Utility.pad(targetAddress, 5)); // printing objectcode
}
Expand All @@ -329,7 +344,7 @@ else if(OpcodeUtility.getFormat(fields[2]) == 4) {
objectCode = objectCode.concat(Utility.pad(XBPE, 1)).concat(Utility.pad(operand.value, 5));

txtWriter.printf("%-60s%s\n",instruction, objectCode);
// TRecordList.add(objectCode, 4);
TRecordLists.add(objectCode, fields);
MRecordLists.addAll(generateMRecord(fields, symbolTable));
}

Expand All @@ -348,6 +363,7 @@ else if(OpcodeUtility.getFormat(fields[2]) == 4) {
}

txtWriter.println(instruction);
TRecordLists.terminateTRecord();
ERecord = objectCode;
instruction = reader.readLine();
continue;
Expand All @@ -358,14 +374,21 @@ else if(OpcodeUtility.getFormat(fields[2]) == 4) {

// Write the Object File
objectWriter.println(HRecord);

for(String d : DRecordLists)
objectWriter.println(d);

for(String r : RRecordLists)
objectWriter.println(r);
// print the T records

for(String t : TRecordLists.getAllTRecords())
objectWriter.println(t);

for(String m : MRecordLists)
objectWriter.println(m);

objectWriter.println(ERecord);

objectWriter.close();

}
Expand Down
59 changes: 48 additions & 11 deletions src/Assembler/TRecordList.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,65 @@
*/
class TRecordList {
private int count = 0;
private int length = 0;
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){
record = "T^" + Utility.pad(Integer.parseInt(fields[0], 16), 6);
count++;
}
record = "T^" + Utility.pad(Integer.parseInt(fields[0], 16), 6) + "^" + objCode;

// start a new t record
if(count % 10 == 0){
// update address and add them to the list
allrecords.add(record);
record = "T^" + Utility.pad(Integer.parseInt(fields[0], 16), 6);
length += objCode.length() / 2;
count++;
}

// we already have a pending t record
else {
record = record.concat("^").concat(objCode);
count++;
// start a new t record
if(count % 10 == 0){
// update address and add them to the list

allrecords.add(insertLengthInfo(record));
record = "T^" + Utility.pad(Integer.parseInt(fields[0], 16), 6) + "^" + objCode;

length += objCode.length() / 2;
count++;
}

// we already have a pending t record
else {
record = record.concat("^").concat(objCode);

length += objCode.length() / 2;
count++;
}
}
}

public ArrayList<String> getAllTRecords(){
// append all the pending records
if(length > 1)
allrecords.add(insertLengthInfo(record));

return allrecords;

}

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

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

return sb.toString();
}

}

0 comments on commit 7ebb6e0

Please sign in to comment.