The virtual machine executes a single program and terminates, either by executing an '!' instruction, or because an exception was thrown during execution. A program is represented by a 2D array of single-character instructions. The virtual machine starts with the first instruction, executes it, and moves on to the next instruction, etc... An underlying 2D array of cells holds memory which can be accessed through various instructions. These are also treated like a stack for many instructions. The (2D) index of the current instruction is called the program counter (PC). All instructions move the PC one step in the current direction. Execution starts at the '%' instruction or at the upper-left. The PC is initially moving right. The memory and code arrays are limited by default to (1024 x 128). Each item in memory is a signed integer. For implementation reasons, those integers are currently limited to 32 bits, but do not count on it, they could be large in future implementations. The number of threads you create is limited by default to 32. The default number of cycles you may run for is 10000. Note that all threads execute one instruction in each cycle. When measuring "code size", we look at the area of the bounding rectangle of your code.


In the following descriptions, S<n> is the (n+1)th value from the top of the stack (S0 is at the top, then S1, S2, etc.) The 'stack' is defined as the current memory location for a thread in the X-dimension. I.e., to pop a value, the x-coordinate decrements by one.
'%'Begin execution here
'p'Print S0 interpreted as an integer
'P'Print S0 interpreted as an ASCII character (only the least significant 7 bits of the value are used)
'0'Push the value 0
'1'Push the value 1
'2'Push the value 2
'3'Push the value 3
'4'Push the value 4
'5'Push the value 5
'6'Push the value 6
'7'Push the value 7
'8'Push the value 8
'9'Push the value 9
'+'Push S1+S0
'-'Push S1-S0
'*'Push S1*S0
'd'Push S1/S0
','Read one value from input
's'Skip one instruction
'\'If going left, go up up, go left right, go down down, go right
'/'If going right, go up up, go right left, go down down, go left
':'If S0 > S1, turn left. If S0 < S1, turn right.
'?'Skip if S0 is 0
'@'Push the current direction and PC location on the call-stack
'$'Pop direction and PC location off call-stack and advance PC one step
'<'Push the value of memory cell (S0, S1)
'>'Store S2 into (S0, S1)
'['Move memory pointer to the left by S0 places
']'Move memory pointer to the right by S0 places
'{'Move memory pointer up one place
'}'Move memory pointer down one place
'x'Push a copy of S0
'^'Push a copy of S<S0+1> (ex: 0^ duplicates S0)
'v'Remove S<S0+1> from the stack and push it on top (ex: 1v swaps S0 and S1)
'g'Get code value at (S0, S1)
'w'Write code value S2 at (S0, S1)
'&'Create new thread, advance current thread one step. New threads inherit the PC and memory pointer of the parent, but have an empty call stack.
'!'Terminate the program
...All other characters are no-ops


  p  1
  x  |
Output: 876543210
Output: @@@@@

  s  /\ @
 /\  || @
 p7  || @
 \/  || |
Output: 7777777777777777

Try it!

This page embeds a JavaScript implementation [NOTE: the JavaScript version is just for casual experimentation on this page, it may not always behave exactly the same as the implementation used to judge the challenge submissions).

Enter input values. The format is: value-0, value-1, ...

Enter your code here

Show Trace