Original program, by Brian W. Kernighan, can be found at http://kernighan.com/toysim.html. This program represents a modification of the original.
Your program goes in the window on the left; output will appear in the right window when you run your program.
Note that lines beginning with opcodes (get, print, load, etc.) must be indented by one or more spaces or tabs. Lines beginning with memory labels or comment lines (marked with "#") must not be indented.
opcode (instruction) (must be indented) | what it does |
---|---|
get | get a number from keyboard into accumulator |
print value stored in accumulator | |
load Val | load accumulator with value Val |
load Loc | copy value stored at memory location Loc into accumulator |
store Loc | copy value stored in accumulator to memory location Loc (value stored in accumulator unchanged) |
add Val | add Val to value stored in accumulator (Val unchanged) |
sub Val | subtract Val from value stored in accumulator (Val unchanged) |
goto Loc | go to instruction at memory location labeled Loc |
ifpos Loc | go to instruction at memory location labeled Loc if value stored in accumulator is >= zero |
ifzero Loc | go to instruction at memory location labeled Loc if value stored in accumulator is zero |
stop | stop running |
Memory location labels (must not be indented) |
|
Loc | Loc must start with a letter and contain no spaces; denotes a labeled memory location |
Loc Val | load memory location labeled Loc with numeric value Val (once, before program runs) |
# Comment | ignore this line; it's just a comment. (Comment may be any text.) |
If Val is a name like Sum, it refers to a labeled memory location. If Val is a number like 17, that value is used directly. So “add Sum” means to add the contents of the memory location named Sum to the accumulator value (leaving Sum's contents unchanged), while “add 1” means to add 1 to the accumulator value.
NOTE: This version of the TOY simulator incorporates a couple of minor bug fixes and some aesthetic modifications by R. Linley and M. Nicholson.
This version has an execution limit of instructions by default, to make it easier to prevent infinite loops.