private void assemble() {
    if (sourceFile == null) {
      printError("-i : No source file specified");
    }

    if (assembler == null) {
      printError("-f : No format specified");
    }

    print("...Assembling " + sourceFile.getName());

    assembler.assemble(sourceFile);

    print("..." + assembler.getErrors().length + " error(s) found");

    try {
      PrintWriter output = null;

      if (!assembler.hasErrors()) {
        if (!machineCode) {
          print("...saving .pco file");

          output = new PrintWriter(new FileWriter(baseFileName + ".pco"));

          for (Instruction instruction : assembler.getInstructions()) {
            output.print(instruction.getInstruction());
            output.println(" ;" + instruction.getSourceCode());
          }
        } else {
          print("Machine code file varified");
        }
      } else {
        print("...saving .err file");

        output = new PrintWriter(new FileWriter(baseFileName + ".err"));
        for (AssemblyError error : assembler.getErrors()) {
          output.println(error.toString());
        }
      }

      if (output != null) {
        output.close();
      }
    } catch (IOException e) {
      printError(e.getMessage());
    }
  }
  private void doInput(String[] arguments) {
    if (arguments.length < 2) {
      printError("-i : No filename specified");
    }

    if (arguments[1].endsWith(".pco")) {
      machineCode = true;
      assembler = new MachineCodeAssembler();
    } else {
      if (!new File(arguments[1]).exists()) {
        print("...no file extension specified.  Guessing .pca");

        arguments[1] = arguments[1] + ".pca";
      }
    }

    try {
      sourceFile = SourceFile.load(arguments[1]);
      baseFileName = arguments[1].substring(0, arguments[1].lastIndexOf("."));
    } catch (IOException e) {
      printError(e.getMessage());
    }
  }
  private void addTraceListeners() {
    try {
      final PrintWriter memoryTrace = new PrintWriter(new FileWriter(baseFileName + ".mem"));
      final PrintWriter registerTrace = new PrintWriter(new FileWriter(baseFileName + ".reg"));
      final PrintWriter decoderTrace = new PrintWriter(new FileWriter(baseFileName + ".dec"));

      computer
          .getMemory()
          .addMemoryListener(
              new MemoryListener() {
                public void memoryReadFrom(Memory memory, int address, String value) {
                  memoryTrace.println("READ," + address + "," + value);
                }

                public void memoryWroteTo(Memory memory, int address, String value) {
                  memoryTrace.println("WRITE," + address + "," + value);
                }

                public void memoryError(Memory memory, Throwable error) {
                  memoryTrace.println("ERROR: " + error.getMessage());

                  printError(error.getMessage());
                }
              });

      computer
          .getRegisterStore()
          .addRegisterListener(
              new RegisterListener() {
                public void registerReadFrom(
                    RegisterStore registers, Register register, String value) {
                  registerTrace.println("READ," + register.getMnemonic() + "," + value);
                }

                public void registerWroteTo(
                    RegisterStore registers, Register register, String value) {
                  registerTrace.println("WRITE," + register.getMnemonic() + "," + value);
                }
              });

      computer
          .getInstructionDecoder()
          .addDecoderListener(
              new InstructionDecoderListener() {
                public void instructionDecoded(InstructionDecoder decoder, String instruction) {
                  decoderTrace.println(instruction);
                }

                public void decodeError(
                    InstructionDecoder decoder, String instruction, Throwable error) {
                  decoderTrace.println("Decoding '" + instruction + "'...");
                  decoderTrace.print("ERROR: " + error.getMessage());

                  printError(error.getMessage());
                }
              });

      computer.addComputerListener(
          new ComputerAdapter() {
            public void computerStopped(Computer computer) {
              // Close file output streams when execution is finished
              memoryTrace.close();
              registerTrace.close();
              decoderTrace.close();
            }

            public void computerError(Computer computer, Throwable error) {
              printError(error.getMessage());
            }
          });

    } catch (IOException e) {
      printError(e.getMessage());
    }
  }