Question
public void nextInstruction() Loads the next instruction from memory, parses it for the opcode and operand, and executes the instruction. The opcode is the high-order
public void nextInstruction()
Loads the next instruction from memory, parses it for the opcode and operand, and executes the instruction. The opcode is the high-order two digits of the instruction; the operand is the low-order two digits. Except in case of a jump, the instruction counter is incremented by one following execution of the instruction.
Invalid opcodes crash the machine.
/** * Opcode for the read instruction. */ public static final int READ = 10; /** * Opcode for the write instruction. */ public static final int WRITE = 20; /** * Opcode for the load instruction. */ public static final int LOAD = 30; /** * Opcode for the store instruction. */ public static final int STORE = 40; /** * Opcode for the add instruction. */ public static final int ADD = 50; /** * Opcode for the sub instruction. */ public static final int SUB = 51; /** * Opcode for the div instruction. */ public static final int DIV = 52; /** * Opcode for the mod instruction. */ public static final int MOD = 53; /** * Opcode for the mul instruction. */ public static final int MUL = 54; /** * Opcode for the jump instruction. */ public static final int JUMP = 60; /** * Opcode for the jumpn (jump if negative) instruction. */ public static final int JUMPN = 61; /** * Opcode for the jumpz (jump if zero) instruction. */ public static final int JUMPZ = 63; /** * Opcode for the halt instruction. */ public static final int HALT = 80;
Descriptions of all instructions follow:
- READ: Executes the read instruction. Reads a decimal word from the terminal into the address referenced by operand and updates the instruction counter. Valid words are in the range [-9999,9999]. Out of range words are truncated on the right until within range before being stored; the truncated portion is discarded. For example, -723471 will be truncated to -7234.
- WRITE: Displays the value stored in memory at the address referenced by the operand as a signed, four digit, zero padded decimal integer and updates the instruction counter.
- LOAD: Loads the value stored in memory at the address referenced by operand into the accumulator and updates the instruction counter.
- STORE: Stores the value in the accumulator into memory at the address referenced by the operand and updates the instruction counter.
- ADD: Adds the value stored in memory at the address referenced by operand to the accumulator, accounting for overflow, and updates the instruction counter.
- SUB: Subtracts the value stored in memory at the address referenced by operand from the accumulator, accounting for overflow, and updates the instruction counter.
- DIV: Divides the accumulator by the value stored in memory at the address referenced by operand, accounting for overflow, and updates the instruction counter. All division is integer division. Division by zero crashes the machine.
- MOD: Calculates the remainder when dividing the accumulator by the value stored in memory at the address referenced by operand, accounting for overflow, stores the result in the accumulator, and updates the instruction counter. All division is integer division. Division by zero crashes the machine.
- MUL: Multiplies the accumulator by the value stored in memory at the address referenced by operand, accounting for overflow, and updates the instruction counter.
- JUMP: Changes the instruction counter to operand.
- JUMPN: If the accumulator is negative, changes the instruction counter to operand, otherwise updates the instruction counter normally.
- JUMPZ: If the accumulator is zero, changes the instruction counter to operand, otherwise updates the instruction counter normally.
- HALT: Displays the message "*** Program terminated normally ***", halts the machine, and dumps core.
Arithmetic overflow occurs when the accumulator acquires a value outside of the range [-9999,9999]. It is handled by truncating the value of the accumulator to the low order four digits.
Instruction counter overflow occurs when when the value of the instruction counter matches or exceeds the memory size. It is handled by setting the instruction counter to zero.
All crashes dump core.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started