|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcomputer.ToyComputer
public class ToyComputer
A simulator for a toy computer with a few instructions. The computer uses a stack for all computations rather than registers.
Field Summary | |
---|---|
private static int |
add
|
private Instruction |
decoder
|
private static int |
dupl
|
private static int |
halt
|
private static int |
inc
|
private static int |
inp
|
private static int |
jmp
|
private static int |
jnn
|
private static int |
ldd
|
private int |
loadAddress
|
private Memory |
memory
|
private static int |
outp
|
private int |
pc
|
private static int |
pop
|
private static int |
push
|
private boolean |
running
|
private Stack<java.lang.Double> |
stack
|
private static int |
subtr
|
private static int |
toss
|
private static int |
zer
|
Constructor Summary | |
---|---|
ToyComputer()
|
Method Summary | |
---|---|
void |
add()
Pop the two top elements from the stack, add them and push the result |
private void |
decodeDo()
Decode the instruction in the decoder (break it into parts) and execute it |
void |
dupl()
Duplicate the top of the stack. |
private void |
fetch()
Fetch an instruction using the program counter (pc). |
void |
halt()
Terminate execution of the running program |
void |
inc()
Add 1.0 to the top of the stack. |
private void |
increment()
Increment the pc |
void |
inp()
Retrieve a double from standard input and push the result |
void |
jmp(int where)
Unconditionally jump to an instruction at the specified address |
void |
jnn(int where)
Pop the stack. |
void |
ldd()
The top of the stack is an address. |
void |
loadData(double data,
int location)
Load a data cell at an arbitrary memory address |
void |
loadInstruction(int opcode)
Load an instruction into the next available memory location starting from zero. |
void |
loadInstruction(int opcode,
int operand)
Load an instruction into the next available memory location starting from zero |
static void |
main(java.lang.String[] args)
|
void |
outp()
Pop the stack and print the result on standard output |
private void |
perform(int op,
int opnd)
The internal map between opcode numbers and the operational methods |
void |
pop(int mem)
Pop the stack and put the result into memory |
void |
push(int mem)
Copies a memory cell and pushes it onto the stack |
void |
run()
Execute the program currently stored in the memory. |
void |
subtr()
Pop the two top elements from the stack and subtract them. |
void |
toss()
Discard the top of the stack. |
void |
zer()
Push a zero onto the top of the stack. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private Memory memory
private Stack<java.lang.Double> stack
private int loadAddress
private boolean running
private int pc
private Instruction decoder
private static final int push
private static final int pop
private static final int toss
private static final int outp
private static final int dupl
private static final int add
private static final int subtr
private static final int halt
private static final int inp
private static final int jmp
private static final int jnn
private static final int ldd
private static final int zer
private static final int inc
Constructor Detail |
---|
public ToyComputer()
Method Detail |
---|
public void push(int mem)
mem
- the address of the memory cell desiredpublic void pop(int mem)
mem
- the memory cell address into which to put the
stack toppublic void toss()
public void outp()
public void dupl()
public void add()
public void subtr()
public void jmp(int where)
where
- the address of the next instruction to be executedpublic void zer()
public void inc()
public void ldd()
public void jnn(int where)
where
- the address of the next instruction of the
stack top was non-negativepublic void halt()
public void inp()
java.lang.NumberFormatException
- if the input can't be
interpreted as a doublepublic void loadInstruction(int opcode, int operand)
opcode
- the opcode of the instructionoperand
- the operand (memory address) that
the opcode will operate onpublic void loadInstruction(int opcode)
opcode
- the opcode of the instructionpublic void loadData(double data, int location)
data
- the double value to be stored (a Data cell
will be created)location
- the memory address into which to put the new cellpublic void run()
private void fetch()
private void increment()
private void decodeDo()
private void perform(int op, int opnd)
op
- an opcodeopnd
- the (optionsl) oparand for this opcodepublic static void main(java.lang.String[] args)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |