package rebelsky.pal;

/**
 * The PAL instruction to jump to a destination if a container contains 0.  
 * 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, not just registers.
 *
 * @author Samuel A. Rebelsky
 * @version 1.1 of December 2002.
 */
public class JumpZero
  implements Instruction
{
  // +--------+------------------------------------------------------------
  // | Fields |
  // +--------+

  /** The container that gives the destination. */
  Variable destination;

  /** The container that gives the value to compare to zero. */
  Variable value;


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

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


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

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

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

} // class JumpZero

