Yet another Forth VM
It seems that every year ends with me toying around with the smallest set of instructions needed for a Forth VM. Rogozhin's 4 state universal Turing machine has 22 instructions. Stack machines cheat by combining some operations into a single instruction, so they need Stack (push data stack, pop data stack, push return stack, pop return stack), Arithmetic (add, subtract), and Control (unconditional and conditional jumps). Add instructions to read and write memory, and you have 10 instructions (12 if you want input and output).
Not an efficient VM, but it's complete, right?
- ACC - Accumulator. Memory flows through this register.
- DSP - Data stack pointer. Points to value on the top of the data stack.
- RSP - Return stack pointer. Points to value on the top of the return stack.
- PC - Program counter. Points to next instruction to execute.
- JMP - Perform an unconditional jump. Pop S1 from data stack. Set PC to S1.
- JEQ - Perform a conditional jump. Pop S1 from data stack. If ACC is ZERO, set PC to S1.
- POP - Pop S1 from data stack. Set ACC to S1.
- PUSH - Push ACC on to data stack.
- RPOP - Pop S1 from return stack. Set ACC to S1.
- RPUSH - Push ACC on to return stack.
- ADD - Pop S1 from data stack. Pop S2 from data stack. Push S2 + S1 onto data stack.
- SUB - Pop S1 from data stack. Pop S2 from data stack. Push S2 - S1 onto data stack.
- LOAD - Load a value from memory into the accumulator Pop S1 from data stack. Copy ACC to MEMORY[S1].
- STOR - Store the accumulator into memory Pop S1 from data stack. Copy MEMORY[S1] to ACC.
- NOOP Do nothing (used for aligning values).
- HALT Stop the VM.
With hints from
http://homepage.cs.uiowa.edu/~jones/arch/cisc/
http://en.wikipedia.org/wiki/Random-access_stored-program_machine
http://git.annexia.org/?p=jonesforth.git;a=blob;f=jonesforth.S