package rebelsky.pal; /** * The PAL instruction to pop a value. * * @author Samuel A. Rebelsky * @version 1.1 of December 2002. */ public class Pop implements Instruction { // +--------+------------------------------------------------------------ // | Fields | // +--------+ /** The container to hold the popped value. */ Variable location; // +--------------+------------------------------------------------------ // | Constructors | // +--------------+ /** * Create a new instruction to pop a value into a location. */ public Pop(Variable location) { this.location = location; } // Pop(Variable) // +---------+----------------------------------------------------------- // | Methods | // +---------+ /** * Execute the instruction on a machine. * * @exception Exception * If the memory location is invalid. * If the stack is full or empty. */ public void execute(Computer hal) throws Exception { if ((hal.sp < 0) || (hal.sp >= hal.codesize)) throw new Exception("Invalid stack pointer: " + hal.sp); location.iset(hal, hal.memory[hal.sp++]); } // execute(Computer) /** Convert the instruction to a string (usually for printing). */ public String toString() { return " POP -> " + location.toString(); } // toString() } // class Pop