Toy Computer Simulator

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.


               Accumulator:

    

Syntax reminder

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
    getget a number from keyboard into accumulator
    printprint value stored in accumulator
    load Valload accumulator with value Val
    load Loccopy value stored at memory location Loc into accumulator
    store Loccopy value stored in accumulator to memory location Loc (value stored in accumulator unchanged)
    add Valadd Val to value stored in accumulator (Val unchanged)
    sub Valsubtract Val from value stored in accumulator (Val unchanged)
    goto Locgo to instruction at memory location labeled Loc
    ifpos Locgo to instruction at memory location labeled Loc if value stored in accumulator is >= zero
    ifzero Locgo to instruction at memory location labeled Loc if value stored in accumulator is zero
    stopstop running
Memory location labels
(must not be indented)
LocLoc must start with a letter and contain no spaces; denotes a labeled memory location
Loc Valload memory location labeled Loc with numeric value Val (once, before program runs)
# Commentignore 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.