# Looping with TIX

#
Lbl
Op
ATD

### General Memory

#### Help

Instruction to be executed next is highlighted in blue.

Target address of instruction to be executed next is highlighted in pink.

Mouse over words (the binary numbers) to see how they could be interpreted.

### Other Registers:

Instruction Location Counter

Instruction Register

Storage Register

Accumulator

Multiplier-Quotient Register:

Index Register A

Index Register B

Index Register C

In programming, we often use something called a loop in order to do a set of instructions multiple times.  In Python, this program might look like this:

`x = 1`

`for a in range(5):`

`x = x + x`

And the instruction `x = x + x` will magically be executed 5 times.  But this abstracts away a lot of what's happening.  In a sort-of psuedocode Fortran, this is how you might write something closer to the program that's on the simulator right now:

`x = 1`

`a = 5`

`LOOP:`

`x = x + x`

`IF ``(a > 1) THEN`

`a = a - 1`

`GOTO LOOP`

`a` starts out as 5 as specified by `a = 5`, decreases by one at the end of each loop as described by `a = a-1`, and stops when the condition, `a > 1`, is no longer true.

Now, in assembly language, we can't use a while-loop structure, because each instruction has to correspond to a specific instruction.  However, we can still take advantage of looping to do things multiple times. `PZE `is a pseudoinstruction similar to `DEC`, except essentially it can only store fixed point numbers and can only store values up to 8191.  So you can imagine our instructions are set-up in memory like this:

`0: LXA EXPONENT, 1`

`1: CLA 7`

`LOOP: ADD 7`

`3: STO 7`

`4: TIX LOOP, 1, 1`

`EXPONENT: 5`

`7: 1`

`LXA EXPONENT, 1` still targets the register with address 5.  What the second number in an instruction (called the tag) refers to is the targeted index register, in this case Index Register A.  (B corresponds to 2, and C corresponds to 4).  So what will happen is that index register A will gain the value of register 5, which is 5.  This is analogous to `a = 5`.

The next three steps, `CLA 7``ADD 7`, and `STO 7`, essentially amount to `x = x + x`, except performed with the accumulator as we are required to.

The last part, `TIX LOOP, 1, 1`, is the trickiest to understand.  `TIX` stands for Transfer on Index.  Again, first number is the targeted address in general memory (register 1), and the second number is the targeted index register (Index Register A).  The third number is called the decrement.  What will happen is that the computer will compare the values in the specified index register with the decrement.  If the index register is greater than the decrement (in this case, if `a > 1`), then the index register will be reduced by the decrement (```a = a - 1```), and the program will go to to the address specified by the target address, which is 1.  You'll note that this is `CLA 7`, the beginning of our `x = x + x` analogue.

So `x = x + x` will occur 5 times, until, the index register A is no longer greater than 1.  Then, when the computer reaches `TIX`, it will simply continue on to the next instruction (which in this case halts the computer).

This might not be the best way to calculate 25, but it's how you do loops in assembly.  The modern, RISC-V version isn't too much different.  You might appreciate better now the ability to write loops in higher level language.