Contents | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Previous | Next

5. Making Choices and Looping

How does the Transputer make choices in the "if ... then ... else ..." style? The answer is by testing and by jumping.

Testing Top

Testing means calculating logical expressions. The Transputer treats this as just another kind of arithmetic, using 0 to mean false, and 1 to mean true. Load the test1 program into the simulator to see the first example:

         43    ldc 3
         c4    eqc 4
         45    ldc 5
         c5    eqc 5

The first instruction ldc loads the constant 3 into Areg. The second instruction eqc (equals constant) tests whether the number in Areg is equal to the constant 4. It isn't, so the number 0 (false) is put into Areg.

The next pair of instructions do the same again, but this time comparing 5 with 5. The answer is true, so the number 1 ends up in Areg.

The other main comparison instruction is gt (greater than) which compares the two numbers in Areg and Breg and puts the result of the comparison (0 or 1) in Areg. Other comparisons can be carried out with various combinations of instructions. Testing for equality (where neither number is a constant) can be done using sub, testing for "less than" can be carried out by changing the two numbers round and using gt, testing for "greater than or equal to" can be carried out by testing for "less than" and negating the answer, and so on.

To combine boolean results, you can use and, or and not. The and and or instructions work on the numbers in Areg and Breg and put the answer in Areg. Actually, they work on all the bits of the arguments at once, so each word can hold 32 separate boolean values. If the arguments are both 0 or 1, you get the expected answer.

The not instruction takes Areg as its argument and puts its result in Areg. It too works on all the bits at once. This means that if the argument is 0 or 1, it does not give the expected value. It can be used in some situations, but for a negation operation which works exactly as expected on 0 and 1, the instruction eqc 0 works fine, so no separate instruction is needed.

Jumping Top

Suppose you want to calculate 5+4+3+2+1 using a loop. In C, you might write:

         n = 5;
         t = 0;
         while (n > 0)
         {  t = t + n;
            n = n - 1;

If you load the test2 program into the simulator, you can see the transputer equivalent:

         ajw -1     (initialisation)
         ldc 5
         stl 0
         ldc 0

         ldl 0      (the loop)
         jz 8
         ldl 0
         ldl 0
         adc -1
         stl 0
         j -10

The first instruction ajw reserves a single word of workspace for n, the counter variable. The next instructions ldc and stl initialise n to 5. The following ldc instruction loads zero into Areg, to initialise t, the running total variable. The remaining instructions form the loop. The first instruction ldl loads the counter n into Areg, pushing the running total t down into Breg.

The instruction jz 8 is a test-and-jump instruction. It means test the value in Areg, and jump if it is zero. As well as testing Areg, the instruction pops its value off the register stack, so that Breg and Creg are restored into Areg and Breg. In this program, that means that the running total t is restored in Areg.

If Areg is not zero when jz 8 is executed, then the instruction does nothing, and execution continues with the following instruction, as normal. If Areg is zero, then the jz 8 instruction skips the next 8 bytes of instructions, which takes us just beyond the loop. The argument to jz is the number of bytes by which to jump, relative to the start of the next instruction (so that 0 would mean continue as normal).

The next two instructions, ldl and add, add the value of the counter n into the running total t in Areg. The three instructions after that, ldl, adc and stl, subtract one from the counter n.

The loop ends with the instruction j -10, which is an unconditional jump. It jumps by the specified number of bytes, without testing or affecting the registers. As with jz, the j instruction jumps by a number of bytes, relative to the start of the following instruction, so j -10 takes us back to the start of the loop.

Contents | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Previous | Next

Copyright 1998 University of Bristol. All rights reserved.
Author: Ian Holyer
Last modified: 14 Sep 1998 11:31
Authored in CALnet