Skip to content

@counter: a jump as a value

A jump is not only jump. The number of the next line sits in a variable, and it can be calculated

@counter is the number of the line that will run next. In «How the processor works» it was read: it shows where the program is now. This lesson is about writing to it.

Press “step” three times.

tick 0
Set0
=
Set1
=
Set2
=
Set3
=
End4
@counter0number
targetnullnull
skippednullnull
reachednullnull

The second line puts a three into @counter — and the third line never runs at all: skipped stays empty and reached becomes one. A jump with its arrow on the fourth line would do exactly the same.

There is one difference, and it is the decisive one: a number can be computed, an arrow cannot.

The counter already points at the next line

Section titled “The counter already points at the next line”

Before you can count, you have to know what you are counting from. By the time a line runs, the counter has already been increased and points at the next line.

tick 0
Set0
=
Operation1
=
Set2
=
Set3
=
Set4
=
@counter0number
stepnullnull
firstOnenullnull
secondOnenullnull
thirdOnenullnull

op add @counter @counter 2 in this example skips two lines, not one: firstOne and secondOne stay empty and the one goes to thirdOne. Adding zero would mean “the next line”, that is, change nothing.

Hence the rule for calculations: adding n skips n lines.

Three cases this exists for at all:

  • A choice among many. Not five jumps in a row but one line: the branch number is computed in advance and added to the counter. That is how jump tables are made.
  • A number out of memory. A cell holds which subprogram to run; the program reads the number and jumps by it. The result is something like calling by name.
  • Returning. Before jumping “into a subprogram” the current @counter is saved into a variable and restored at the end — the closest thing to a real call that mlog has at all.

Fractions are truncated. Write 2.9 and line number two runs, not three.

A number outside the program returns to the start. Negative or beyond the last line — the counter is cleared and the program goes from the first line. That is exactly how end works: it puts the program’s line count into the counter, and that is certainly past the edge.

Unofficial fan project, not affiliated with Anuke. Mindustry sprites, fonts and translations © Anuke, used under GPL-3.0; Fira Code under OFL-1.1. Site code is GPL-3.0.