kimo12

Untitled

Apr 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. package assembler;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7.  
  8. public class Pass2 {
  9.  
  10. private int lineNumber = 0;
  11. private ArrayList<String> labels = new ArrayList<>();
  12. private ArrayList<String> operationCodes = new ArrayList<>();
  13. private ArrayList<String> operands = new ArrayList<>();
  14. private ArrayList<String> Addresses = new ArrayList<>();
  15. private ArrayList<String> listingfile = new ArrayList<>();
  16. private ArrayList<String> objectProgram = new ArrayList<>();
  17. private static final String FILENAME2 = "C:\\Users\\karem\\Desktop\\listingfile.txt";
  18. private static final String FILENAME3 = "C:\\Users\\karem\\Desktop\\objectprogram.txt";
  19.  
  20. Pass2(ArrayList<String> labels, ArrayList<String> operationCodes, ArrayList<String> operands,
  21. ArrayList<String> Addresses) {
  22. this.labels = labels;
  23. this.operationCodes = operationCodes;
  24. this.operands = operands;
  25. this.Addresses = Addresses;
  26. this.lineNumber = 0;
  27. this.run();
  28. }
  29.  
  30. private void run() {
  31. for (int i = 0; i < labels.size(); i++) {
  32. String objectCode = getObjectCode(operationCodes.get(i), operands.get(i));
  33. writeListing(labels.get(i), operationCodes.get(i), operands.get(i), objectCode);
  34. writeObject(labels.get(i), operationCodes.get(i), objectCode);
  35. }
  36. }
  37.  
  38. private String getObjectCode(String operationCode, String operand) {
  39. // search for operation Code in the mnemonic Table
  40. boolean found = false;
  41. String mnemonicValue = "00";
  42. String operandAddress;
  43. String objectCode = new String();
  44. if (found) {
  45. if (operand == null) {
  46. operandAddress = "0000";
  47. objectCode = mnemonicValue.concat(operandAddress);
  48. } else {
  49. // search for operand in the symtab
  50. // if found
  51. // operandAddress = symbol value;
  52. // objectCode = mnemonicValue.concat(operandAddress);
  53. // if not found (Un Defined Symbol)
  54. // operandAddress = "0000";
  55. // ERROR
  56. }
  57. } else if (operationCode.equals("WORD")) {
  58. String hex = Integer.toHexString(Integer.parseInt(operand));
  59. if (hex.length() == 1) {
  60. String concat = "00000";
  61. hex = concat.concat(hex);
  62. } else if (hex.length() == 2) {
  63. String concat = "0000";
  64. hex = concat.concat(hex);
  65. } else if (hex.length() == 3) {
  66. String concat = "000";
  67. hex = concat.concat(hex);
  68. } else if (hex.length() == 4) {
  69. String concat = "00";
  70. hex = concat.concat(hex);
  71. } else if (hex.length() == 5) {
  72. String concat = "0";
  73. hex = concat.concat(hex);
  74. }
  75. objectCode = hex;
  76. } else if (operationCode.equals("BYTE")) {
  77. if (operand.charAt(0) == 'X') {
  78. objectCode = operand.substring(2, operationCode.length());
  79. } else if (operand.charAt(0) == 'C') {
  80. for (int i = 2; i < operand.length() - 1; i++) {
  81. objectCode = objectCode.concat(Integer.toHexString((int) operand.charAt(i)));
  82. }
  83. }
  84. }
  85. // if object code will not fit into the current text record
  86. // write text record to object program
  87. // intialize new text record
  88. // add object code to text record
  89. return objectCode;
  90. }
  91.  
  92. private void writeListing(String label, String operationCode, String operand, String objectCode) {
  93. if (label == null) {
  94. label = " ";
  95. }
  96. if (operand == null) {
  97. operand = " ";
  98. }
  99. lineNumber++;
  100. int line = lineNumber * 5;
  101. String listing;
  102. if (lineNumber == 1) {
  103. listing = "Line";
  104. listing = addSpaces(listing, 8);
  105. listing = listing.concat("Loc");
  106. listing = addSpaces(listing, 19);
  107. listing = listing.concat("Source statement");
  108. listing = addSpaces(listing, 54);
  109. listing = listing.concat("Object Code");
  110. listing = listing.concat("\n");
  111. listingfile.add(listing);
  112. }
  113. if (operationCode.equals("START") || operationCode.equals("RESB") || operationCode.equals("RESW")) {
  114. listing = String.valueOf(line);
  115. listing = addSpaces(listing, 8);
  116. listing = listing.concat("LOC");
  117. listing = addSpaces(listing, 16);
  118. listing = listing.concat(label);
  119. listing = addSpaces(listing, 26);
  120. listing = listing.concat(operationCode);
  121. listing = addSpaces(listing, 34);
  122. listing = listing.concat(operand);
  123. listing = listing.concat("\n");
  124. listingfile.add(listing);
  125. } else if (operationCode.equals("END")) {
  126. listing = String.valueOf(line);
  127. listing = addSpaces(listing, 8);
  128. listing = listing.concat("LOC");
  129. listing = addSpaces(listing, 16);
  130. listing = listing.concat(label);
  131. listing = addSpaces(listing, 26);
  132. listing = listing.concat(operationCode);
  133. listing = addSpaces(listing, 34);
  134. listing = listing.concat(operand);
  135. listing = listing.concat("\n");
  136. listingfile.add(listing);
  137. writeListingFile();
  138. } else {
  139. listing = String.valueOf(line);
  140. listing = addSpaces(listing, 8);
  141. listing = listing.concat("LOC");
  142. listing = addSpaces(listing, 16);
  143. listing = listing.concat(label);
  144. listing = addSpaces(listing, 26);
  145. listing = listing.concat(operationCode);
  146. listing = addSpaces(listing, 34);
  147. listing = listing.concat(operand);
  148. listing = addSpaces(listing, 54);
  149. listing = listing.concat(objectCode);
  150. listing = listing.concat("\n");
  151. listingfile.add(listing);
  152. }
  153. }
  154.  
  155. private String addSpaces(String STR, int length) {
  156. while (STR.length() < length) {
  157. STR = STR.concat(" ");
  158. }
  159. return STR;
  160. }
  161.  
  162. private void writeObject(String label, String operationCode, String objectCode) {
  163. String objectLine;
  164. if (operationCode.equals("START")) {
  165. while (label.length() != 6) {
  166. label = label.concat(" ");
  167. }
  168. objectLine = "H" + label + "STADDR" + "LENPRO" + "\n";
  169. objectProgram.add(objectLine);
  170. writeObjectProgramFile();
  171. }
  172.  
  173. }
  174.  
  175. private void writeListingFile() {
  176. // Listing File
  177. try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME2))) {
  178. for (int i = 0; i < listingfile.size(); i++) {
  179. bw.write(listingfile.get(i));
  180. }
  181. bw.close();
  182. } catch (IOException e) {
  183.  
  184. e.printStackTrace();
  185. }
  186.  
  187. }
  188.  
  189. private void writeObjectProgramFile() {
  190. // objectProgram file
  191. try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME3))) {
  192. for (int i = 0; i < objectProgram.size(); i++) {
  193. bw.write(objectProgram.get(i));
  194. }
  195. bw.close();
  196. } catch (IOException e) {
  197.  
  198. e.printStackTrace();
  199. }
  200. }
  201.  
  202. }
Add Comment
Please, Sign In to add comment