Tue Oct 9 15:22:53 EDT 2007

COS 109 Toy Machine Simulator

(You must have Javascript enabled.) Type your program in the left window. Labels must start in the first column and operators like GET or ADD must start anywhere but the first column (i.e., there must be a space or tab before them). The simulator does not distinguish upper case from lower case, but is otherwise not robust, so be sure to spell instructions correctly and format code carefully.

Push RUN to run your program. A dialog box will appear when a GET is executed, and output from PRINT will appear in the right window. The simulator will stop if you Cancel a GET or don't enter anything.

          Accumulator:

    

Syntax reminder

  get       get a number from keyboard into accumulator
  print     print contents of accumulator
  load Val  load accumulator with Val (Val unchanged)
  store M   store contents of accumulator into memory location M (accumulator unchanged)
  add Val   add Val to contents of accumulator (Val unchanged)
  sub Val   subtract Val from contents of accumulator (Val unchanged)
  goto L    go to instruction labeled L
  ifpos L   go to instruction labeled L if accumulator is >= zero
  ifzero L  go to instruction labeled L if accumulator is zero
  stop      stop running
  Num       initialize this memory location to numeric value Num (once, before program runs)
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.

Single-stepping

You can also try a single-step version of the simulator, which does one instruction at a time so you can see what happens more easily.

If you want to see how the simulator works, View / Page Source in Firefox or View / Source in Internet Explorer.

This version has a limit of 1000 instructions by default, to make it easier to prevent infinite loops. The single-step version has no limit.