package rebelsky.pal;

/**
 * The PAL instruction to jump to a destination if two containers 
 * contain equal values.  Currently fairly general: You can jump to 
 * an address given by any kind of container not just labels, constants, 
 * or registers.  You can also use any kind of container for the values
 * to be compared, not just registers.
 *
 * @author Samuel A. Rebelsky
 * @version 1.1 of December 2002.
 */
public class JumpEqual
  implements Instruction
{
  // +--------+------------------------------------------------------------
  // | Fields |
  // +--------+

  /** The destination of the jump. */
  Variable destination;

  /** The first value to compare. */
  Variable left;

  /** The second value to compare. */
  Variable right;


  // +--------------+------------------------------------------------------
  // | Constructors |
  // +--------------+

  /** 
   * Create a new instruction to conditionally jump to a location.
   */
  public JumpEqual(Variable left, Variable right, Variable destination)
  { 
    this.left = left;
    this.right = right;
    this.destination = destination;
  } // JumpEqual(Variable)


  // +---------+-----------------------------------------------------------
  // | Methods |
  // +---------+

  /** 
   * Execute the instruction on a machine. 
   * 
   * @exception Exception
   *   If the memory location is invalid.
   */
  public void execute(Computer hal)
    throws Exception
  {
    if (left.iget(hal) == right.iget(hal))
      hal.pc = destination.iget(hal);
  } // execute(Computer)

  /** Convert the instruction to a string (usually for printing). */
  public String toString()
  {
    return "  JEQ " 
           + left.toString() + ", " + right.toString()
           + " -> " + destination.toString();
  } // toString()

} // class JumpEqual

