04 How is a Cow program structured?

Cow consists of a continuous sequence of moo instructions, which are stored in a program memory and are processed continuously starting with the first instruction.

Data is stored in a data memory and processed with a register.


0mooThis command is connected to the MOO command. When encountered during normal execution, it searches the program code in reverse looking for a matching MOO command and begins executing again starting from the found MOO command. When searching, it skips the instruction that is immediately before it (see MOO).
1mOoMoves current memory position one block back.
2moOMoves current memory position one block forward.
3mOOExecute value in current memory block as if it were an instruction. The command executed is based on the instruction code value (for example, if the current memory block contains a 2, then the moO command is executed). An invalid command exits the running program. Value 3 is invalid as it would cause an infinite loop.
4MooIf current memory block has a 0 in it, read a single ASCII character from STDIN and store it in the current memory block. If the current memory block is not 0, then print the ASCII character that corresponds to the value in the current memory block to STDOUT.
5MOoDecrement current memory block value by 1.
6MoOIncrement current memory block value by 1.
7MOOIf current memory block value is 0, skip next command and resume execution after the next matching moo command. If current memory block value is not 0, then continue with next command. Note that the fact that it skips the command immediately following it has interesting ramifications for where the matching moo command really is. For example, the following will match the second and not the first moo: OOO MOO moo moo
8OOOSet current memory block value to 0.
9MMMIf no current value in register, copy current memory block value. If there is a value in the register, then paste that value into the current memory block and clear the register.
10OOMPrint value of current memory block to STDOUT as an integer.
11oomRead an integer from STDIN and put it into the current memory block.