#9: Labels and comparisons

Labels are essentially a more flexible version of loops.

In short, labels define points in code, and by using comparisons, you can jump to that point to repeat a part of the code.

Let’s see how these features are implemented in Aglang!

Labels

Labels are defined with the Label operator (~) — not to be confused with the Label argument — and they can only be named with dots. So, for example, this line:

~..;

defines a label named .. that points at that statement.

Comparisons

In Aglang, comparisons are composed of two statements: one compares two values, and the other checks the result and jumps to a label.

The first statement is written with the Compare operator (?), and it compares the first argument to the second, and it can result either in Greater (if the first argument is greater than the second), Less (if the first argument is smaller) or Equal (if the two have the same value). As you can imagine, the two arguments need to be readable.

So, for example:

01001?101;

this line compares the number 9 to the number 5, and since that 9 is greater than 5, it will result in Greater.

Note: a comparison is valid for the next line only.

Then, the second argument can either be a Greater (^), Less (<) or Equal (=) operator, and it needs a Label argument to its right to perform a jump.

Therefore, by adding one of those to the previous example:

01001?101;
^..;

we can jump to the label .. we defined above.


And now, for this objective, rewrite the example in the last lesson that prints numbers from 1 up to 10 using labels.

Hint: you have to check if the current number is less than 11.

Hint 2: the ASCII code for the newline is 1010, remember to print it after each number.

Print numbers up to 10 continue
Loading editor...

Loading editor...

$ write your code here $

Register 00
Register 10
Stack
Instruction counter0
Expected output:
1
2
3
4
5
6
7
8
9
10