/** write */ public void write(SequentialInstruction currentInstruction) { switch (currentInstruction.getAddressingMode()) { case Globals.IMMEDIATE_ADDRESSING: outputTape.addElement(currentInstruction.getAddress()); break; case Globals.INDIRECT_ADDRESSING: outputTape.addElement( memoryRegister.getRegister( memoryRegister.getRegister(currentInstruction.getAddress()))); break; case Globals.REFERENCE_ADDRESSING: outputTape.addElement(memoryRegister.getRegister(currentInstruction.getAddress())); break; default: break; } currentInstructionIndex++; }
/** jgtz */ public void jgtz(JumpInstruction currentInstruction) { if (memoryRegister.getAccumulator() > 0) { if (program.checkExistLabel(currentInstruction.getLabel())) setCurrentInstructionIndex(program.getLabelIndex(currentInstruction.getLabel())); else { halt(); System.out.println( "Error. Line: " + (currentInstructionIndex + 1) + " Invalid label: " + currentInstruction.getName() + " " + currentInstruction.getLabel()); } } else currentInstructionIndex++; }
/** div */ public void div(SequentialInstruction currentInstruction) { switch (currentInstruction.getAddressingMode()) { case Globals.IMMEDIATE_ADDRESSING: memoryRegister.setAccumulator( memoryRegister.getAccumulator() / currentInstruction.getAddress()); break; case Globals.REFERENCE_ADDRESSING: memoryRegister.setAccumulator( memoryRegister.getAccumulator() / memoryRegister.getRegister(currentInstruction.getAddress())); break; case Globals.INDIRECT_ADDRESSING: memoryRegister.setAccumulator( memoryRegister.getRegister(currentInstruction.getAddress()) / memoryRegister.getRegister( memoryRegister.getAccumulator())); // asi o al contrario? break; default: break; } currentInstructionIndex++; }
/** read */ public void read(SequentialInstruction currentInstruction) { switch (currentInstruction.getAddressingMode()) { case Globals.IMMEDIATE_ADDRESSING: System.out.println( "Error. Line: " + (currentInstructionIndex + 1) + "Invalid addressing: " + currentInstruction.getName() + " =" + currentInstruction.getAddress()); break; case Globals.INDIRECT_ADDRESSING: int aux = memoryRegister.getRegister(currentInstruction.getAddress()) + 1 - memoryRegister.getSize(); if (checkMemoryRegisterOverflow( memoryRegister.getRegister(currentInstruction.getAddress()))) { for (int i = 0; i < aux; i++) memoryRegister.addRegister(0); } memoryRegister.setRegister( memoryRegister.getRegister(currentInstruction.getAddress()), inputTape.getCurrentPointerElement()); inputTape.incrementPointer(); break; case Globals.REFERENCE_ADDRESSING: int aux2 = currentInstruction.getAddress() + 1 - memoryRegister.getSize(); if (checkMemoryRegisterOverflow(currentInstruction.getAddress())) { for (int i = 0; i < aux2; i++) memoryRegister.addRegister(0); } memoryRegister.setRegister( currentInstruction.getAddress(), inputTape.getCurrentPointerElement()); inputTape.incrementPointer(); break; default: break; } currentInstructionIndex++; }
/** debugProgram */ public void debugProgram(InputTape inputTape, Program program, OutputTape outputTape) { setInputTape(inputTape); setOutputTape(outputTape); loadProgram(program); this.outputTape.clear(); currentInstructionIndex = 0; Scanner next = new Scanner(System.in); System.out.println( "- Pulse 'e' para parar la ejecución o cualquier otra tecla para continuar la traza."); while (!isStateHalt()) { showCurrentInstructionInformation(); runInstruction(); memoryRegister.showMemoryRegister(); System.out.println("---------------------"); String userOption = next.next(); if (userOption.equals("e")) { halt(); System.out.println("\nEjecucion interrumpida porel usuario."); } else continue; } setStateHalt(false); }
/** checkMemoryRegisterOverflow */ public boolean checkMemoryRegisterOverflow(int addressDirection) { if (addressDirection + 1 - memoryRegister.getSize() > 0) return true; else return false; }