
Surprisingly, no one wrote anything about the toy
“TIS-100” , which recently appeared on Steam (costs only 150 rubles, already 460 positive reviews against 6 negative).
I’ll just say that I don’t have a relationship with the authors of the game, but this game itself is a great tool for all programmers who want to fight each other in optimizing the code on a fictional cunning assembler.
')
So what is the game about?
The bottom line is that you are given the task to complete. For example, “read the number from IN.A, compare with the number from IN.B and if IN.A> IN.B, write to the output IN.A-IN.B, otherwise IN.B-IN.A.
In fact, it is very simple and everyone can master the local assembler. He has two chips.
1. He is terribly minimalistic and from that - uncomfortable
2. It is multi-threaded. Here, those blocks that you see on the screen - they are all executed simultaneously.
In order to understand what is here and how - here is the very first level:

The task is simple. Read from the entrance, double up, write to the exit.
Here is my (most straightforward) solution:

Code in the first block:
MOV UP, ACC
Next is just a number of exchanges between exits.
We start, the program passes the control test, everything is OK. And we see the result:

The left shows what your decision is regarding other players. We see that by the number of nodes and instructions used, we are the most optimal. But someone solved this problem in a smaller number of cycles. How? Here the second stage of interest is already included - not just to solve the problem, but also to solve it optimally for each of the 3 parameters.
A few words about the assembler.
First, the operands. Actually, everything is simple. These are expected LEFT, RIGHT, DOWN, UP, ACC. As well as "ANY" (which reads from any port) and "LAST" (reads from the last port). There is also NIL to refer to the garbage port.
Also, besides the battery, there is still a backup (BAK). You cannot work with him directly, only through swaps (see below).
Next - a list of commands.
- NOP - does nothing
- MOV [1], [2] - record from [1] to [2]
- ADD [1] and SUB [1] - add / subtract to the battery [1]
- NEG - invert value in battery
- SWP - exchange battery and BAK values
- SAV - save battery in BAK
- JMP - unconditional jump to a label (tags are denoted as “LABEL:“)
- JEZ (equal zero), JNZ (not zero), JGZ (greater zero), JLZ (less zero) - conditional transitions (the argument of comparison is the battery)
- JRO [1] - relative transition (forward / backward to [1] instructions)
Actually, that's all. Those. in fact, you have only 1 register (plus 1 spare, which is not so easy to get to) and several commands.
The most inconvenient thing in this assembler is that the comparison for the conditions of the transitions is made on the battery. In "real" assemblers (which I have seen), the condition is checked against a flag register, which, in turn, is set only by a special comparison command, or after an arithmetic operation. Those. You can write this:
CMP 2 MOV 1, ACC
As a result, we will switch to LABEL only if the parameter was 2 at the input, and in the meantime, we put a one in the battery (since the operation “MOV” does not change the flag register).
The TIS-100 is not like that. To do something like this, you have to do this:
SWP
And this despite the fact that we have spoiled the original value of the battery. If you then have to compare it with another number, we will experience problems.
Actually, this is the whole game. There are tasks, there is just a sandbox.
I think the programmer should be interesting. Especially bored of hardcore times.