package rebelsky.pal;

/**
 * The PAL instruction to jump to a destination if one container contains
 * a value strictly less than another container's value.  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 JumpLessThan
  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 JumpLessThan(Variable left, Variable right, Variable destination)
  { 
    this.left = left;
    this.right = right;
    this.destination = destination;
  } // JumpLessThan(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 "  JLT " 
           + left.toString() + ", " + right.toString()
           + " -> " + destination.toString();
  } // toString()

} // class JumpLessThan

