Z-Machine
From charlesreid1
The Z Machine Problem:
This is a computer science challenge related to Turing Machines. This programming problem is from the Oxford Department of Computer Science's "interviews" (final exam? entrance exam? not clear) via John Graham-Cumming (link).
The Z Machine
Here is the problem:
Suppose you have a computer with a very simple memory layout. The memory consists of a series of numbered locations, each of which can store numbers. These numbers may be integers or real numbers, positive or negative. Here is an illustration of an example of this memory layout:
The machine can only perform three instructions: Zero (Z), Increment (I), and Jump (J).
The Z operator zeros out a location in memory. The operation specifies which index should be zeroed out. For example, Z4 will zero out index 4 (which is the 5th item in memory, since indexing starts at 0).
The I operator increments the value at a location in memory by 1. The operation specifies which index should be incremented. For example, I6 will increment index 6 (the 7th item in memory) by 1.
The J operator compares two locations in memory. If the values are different, the jump operator will branch (that is, jump to a different location in the code). The two locations are specified when calling the operator, and an arrow (or operation number) indicates where the operator should branch TO if the values are not the same. If the values are the same, the code continues.
The program stops when it reaches the end of the instruction list.
Example
Here is an example of a loop program. This program sets a location in memory to 0, and increments it until it equals another location in memory:
001 Z4 002 I4 003 J4,20 --> 002