Loops

Now we want to print the numbers from 1 to 100. This would be a way to do it:

    push_i   1, L0;

    print_s  "1";
    print_n  L0;

    print_s  "2";
    print_n  L0;

    print_s  "3";
    print_n  L0;

    ...
    
We would need a lot time to type this.
We need something to repeat the print part, a "loop".
Here's the example:

        loop.na

     1| push_i  1, L0;
     2| push_i  0, L1;
     3| push_i  99, L2;
     4|
     5| lab loop;
     6|    inc_l    L1;
     7|
     8|    print_l  L1;
     9|    print_n  L0;
    10|
    11|    lseq_l   L1, L2, L3;
    12|    jmp_l    L3, loop;
    13|
    14| push_i  0, L4;
    15| exit    L4;
    
Line 5 is named as "loop".

Line 6 increases "L1" by one.

Line 8 and 9 do the printing.

Line 11 and 12: the "lseq_l" opcode compares "L1" with "L2".
If "L1" is less or equal "L2", then "L3" is set to true (1).
If not, "L3" is set to false (0).
The "jmp_l" opcode jumps to the label "loop", if "L3" is true.

The program stays in the loop, until the counter register "L1" reaches "100".


Prev: Console Output | Next: Jumps