Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void readFile(String fileName) {
- FileInputStream fileIS;
- BufferedInputStream bufferedIS;
- DataInputStream dataIS;
- try {
- fileIS = new FileInputStream(fileName);
- bufferedIS = new BufferedInputStream(fileIS);
- dataIS = new DataInputStream(bufferedIS);
- System.out.println("Reading file: " + fileName);
- int shift = 0;
- int counter = 3;
- int mbyte = 0;
- try {
- while(true) {
- /*
- * reads one input byte, zero-extends it to type int, and
- * returns the result, which is therefore in the range 0
- * through 255.
- */
- if(counter == -1)
- {
- program.add(shift);
- counter = 3;
- shift = 0;
- }
- mbyte = dataIS.readUnsignedByte();
- shift += (mbyte << counter*BITS_IN_BYTE);
- counter--;
- }
- }
- catch (EOFException eof) {
- System.out.println( "Finished reading file" );
- }
- dataIS.close();
- }
- catch (FileNotFoundException e) {
- System.out.println("Error: File not found");
- }
- catch (IOException e) {
- System.out.println("IO error: " + e );
- }
- }
- /**
- * uses a mutable sequence of characters (sb) since this is the easiest way of
- * collecting the output produced from the formatter. Once the entire
- * output is formed, sb should be printed. Note that printing sb does not clear
- * its content. Thus, to print line by line, sb must be reset.
- *
- * @complexity best and worst case: O(F + A + P)
- * Where F is the complexity of {@link Formatter#format(String, Object...)}
- * A is the complexity of {@link StringBuilder#append(String)}
- * P is the complexity of System.out.println()
- * *Note: you should update this complexity as you change the method
- */
- public void disassembleQ2(String fileName) {
- /*
- * sb is a mutable sequence of characters. Principal operations
- * are append and insert. The formatter object will append any
- * returned string to it.
- */
- StringBuilder sb = new StringBuilder();
- Formatter formatter = new Formatter(sb);
- int i=0;
- readFile(fileName);
- /*
- * the following three lines
- * (a) generate a string containing the hexadecimal values of 174 and 257,
- * padded with 0s to the left up to 8 characters
- * (b) append "\n" to the end of the string, and
- * (c) print sb. Note that printing sb does not clear it, thus, you only
- * need to print sb once after all code has been disassembled.
- */
- while(!program.isEmpty())
- {
- formatter.format("[0x%08x] 0x%08x ", PROGRAM_COUNTER_START + i, program.removeFirst());
- i += BYTES_IN_WORD;
- sb.append("\n");
- }
- System.out.println(sb);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement