Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. public void readFile(String fileName) {
  2. FileInputStream fileIS;
  3. BufferedInputStream bufferedIS;
  4. DataInputStream dataIS;
  5.  
  6. try {
  7. fileIS = new FileInputStream(fileName);
  8. bufferedIS = new BufferedInputStream(fileIS);
  9. dataIS = new DataInputStream(bufferedIS);
  10. System.out.println("Reading file: " + fileName);
  11. int shift = 0;
  12. int counter = 3;
  13. int mbyte = 0;
  14.  
  15. try {
  16. while(true) {
  17. /*
  18. * reads one input byte, zero-extends it to type int, and
  19. * returns the result, which is therefore in the range 0
  20. * through 255.
  21. */
  22.  
  23. if(counter == -1)
  24. {
  25. program.add(shift);
  26.  
  27. counter = 3;
  28. shift = 0;
  29. }
  30.  
  31. mbyte = dataIS.readUnsignedByte();
  32.  
  33. shift += (mbyte << counter*BITS_IN_BYTE);
  34. counter--;
  35. }
  36. }
  37. catch (EOFException eof) {
  38. System.out.println( "Finished reading file" );
  39. }
  40. dataIS.close();
  41. }
  42. catch (FileNotFoundException e) {
  43. System.out.println("Error: File not found");
  44. }
  45. catch (IOException e) {
  46. System.out.println("IO error: " + e );
  47. }
  48. }
  49.  
  50. /**
  51. * uses a mutable sequence of characters (sb) since this is the easiest way of
  52. * collecting the output produced from the formatter. Once the entire
  53. * output is formed, sb should be printed. Note that printing sb does not clear
  54. * its content. Thus, to print line by line, sb must be reset.
  55. *
  56. * @complexity best and worst case: O(F + A + P)
  57. * Where F is the complexity of {@link Formatter#format(String, Object...)}
  58. * A is the complexity of {@link StringBuilder#append(String)}
  59. * P is the complexity of System.out.println()
  60. * *Note: you should update this complexity as you change the method
  61. */
  62. public void disassembleQ2(String fileName) {
  63. /*
  64. * sb is a mutable sequence of characters. Principal operations
  65. * are append and insert. The formatter object will append any
  66. * returned string to it.
  67. */
  68. StringBuilder sb = new StringBuilder();
  69. Formatter formatter = new Formatter(sb);
  70. int i=0;
  71.  
  72. readFile(fileName);
  73.  
  74. /*
  75. * the following three lines
  76. * (a) generate a string containing the hexadecimal values of 174 and 257,
  77. * padded with 0s to the left up to 8 characters
  78. * (b) append "\n" to the end of the string, and
  79. * (c) print sb. Note that printing sb does not clear it, thus, you only
  80. * need to print sb once after all code has been disassembled.
  81. */
  82.  
  83. while(!program.isEmpty())
  84. {
  85. formatter.format("[0x%08x] 0x%08x ", PROGRAM_COUNTER_START + i, program.removeFirst());
  86. i += BYTES_IN_WORD;
  87. sb.append("\n");
  88. }
  89.  
  90. System.out.println(sb);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement