I picked up a programming game, TIS-100. Programming manual can be found on Steam as well, but I have described the relevant syntax in my question.
Basically, you're dealing with some old machine that uses its own variant of assembly. It's a gamification of learning assembly, almost.
The machine consists of nodes. Each node has pipes (UP LEFT DOWN RIGHT) that it can write and read from. When a pipe is read from, the value in the pipe disappears. It also has two registers, ACC
and BAK
. BAK
can only be accessed with the SAV
and SWP
opcodes. Each node has its own code that it executes. This code is limited to 15 lines of 18 characters per node.
I've already solved this level. The goal is counting sequences:
- SEQUENCE COUNTER -
> SEQUENCES ARE ZERO-TERMINATED
> READ A SEQUENCE FROM IN
> WRITE THE SUM TO OUT.S
> WRITE THE LENGTH TO OUT.L
Short syntax run down (based on commands I used):
MOV <src> <dest> //moves from source to destination. Blocks if source is a pipe and doesn't have a value available. Blocks if dest is a pipe and already has a value.
ADD value //adds to ACC
<label>: //defines a label (for jumps)
JMP <label> //jumps execution to label
JEZ <label> //jumps to label if ACC = 0. JumpifEqualsZero
//there's also JumpifNotZero (JNZ), JumpifGreaterZero (JGZ), JumpifLesserZero (JLZ).
SAV //MOV ACC BAK
SWP //switches values of ACC and BAK
The game describes it like this:
My program is a bit unwieldy to post in full, so I'll limit it to the three main nodes.
This is the node responsible for counting the sequence length:
S: MOV LEFT ACC
JEZ E
SWP
ADD 1
SAV
JMP S
E: SWP
MOV ACC DOWN
MOV 0 ACC
SAV
This is the node responsible for summing sequences:
S: MOV UP ACC
JEZ E
MOV ACC LEFT
SWP
ADD LEFT
SAV
JMP S
E: SWP
MOV ACC DOWN
MOV 0 ACC
And this is the node I abuse as temporary storage:
MOV RIGHT ACC
MOV ACC RIGHT
What I don't like about my code is that it doesn't read very well.
The sequence counter goes like so:
START
read value to ACC
if ACC is 0, then GOTO END
swap ACC and BAK
add 1 to ACC
write ACC to BAK
GOTO START
END
swap ACC and BAK
write ACC as output
set ACC to 0
write ACC to BAK
There's duplication in here, where no matter if you are in the then
or the (implicit) else
case of the JEZ
, you first swap ACC
and BAK
. Additionally, I'm abusing "swap set write" for altering BAK
, but maybe that's the shortest way there is.
For the sequence summer, I don't like how I'm abusing a separate node just for temporary storage.
It works without errors.
For reference, these are histograms showing the scores of people on levels, with mine highlighted via arrow: