next up previous
Next: The official account of Up: Alan Turing's Manual for Previous: Programming Principles

Subsections

Programming hints

This section consists of a number of detached and rather trivial tricks and precepts, which are nevertheless considered worth while having in writing.

Manoevring space

It is seldom that one writes down a page of instructions for the first time without having forgotten a few vital instructions. It is therefore considered desirable to aim, not at pages which are chock-full, but say at ones which are about five-eighths full. The extra space is best left between sequences of consecutive instructions, so that once sequence may be extended without interfering with another. The spaces can also be filled with numbers if desired, when the discovery of mistakes calls for retrenchment. In this connection see also sandwiching. Another useful way of using reserve space is to put in a number of dummy stops or of time wasting instructions, or hoots. The latter provide `rhythm clicks' which are very informative concerning the progress of the routine.

Do programming directly in teleprint code

It is never too soon to learn the meanings of the 64 functions [i.e., the opcodes]. The way to do so is to start programming in teleprint code straight away. Keep a list of the meanings always at hand, and refer to it as much as you wish: you will find that after a week very few references are necessary. You will not yet know all the codes, but you will know a `working selection'. Likewise you will eventually get to know the teleprint equivalents (p. [*]), but this is likely to be slower, chiefly because it is less essential to know them. Although the lines are given names which are in teleprint code, and which also correspond to numbers, for many applications particularly the ones which do not use `systematic storage space', it is not necessary to know anything concerning the relation of these labellings, or even to have very much to do with the numbers at all. The names of the lines are just used as labels. Later it will be desirable to know the teleprint equivalents of the single characters by heart, but it is never necessary to know the equivalents for pairs of characters.

Counting procedure

One of the commonest operations is a sequence of instructions to be repeated a given number of times. Sometimes the sequence is in two successive parts of which the first is to be omitted on the first round

a)
Case of omission. The counting process may be done in the B tube e.g. in B7. The repetitive part should preferably be programmed as follows:

\begin{picture}(4449,1506)(1414,-3673)
\thinlines\put(1426,-2686){\vector( 1, 0)...
...tFigFont{12}{14.4}{\rmdefault}{\mddefault}{\updefault}Test (/T)}}}
\end{picture}
The arrow from the left shows how the loop is entered, by a control transfer (e.g. /P). The B line must first have been set to an appropriate value. The value chosen depends on the number of times the operation is to be repeated, and also on where the reduction of the B line is done. It is intended that only one of the reductions shown should be done, i.e. one of the bracketed lines must be struck out. In whichever place it is put the number of times the reduction occurs will be greater by one than the number set in the B line, if (as probably) the latter is less than $2^{19}$.

b)
Case of no omission. The control transfer may be omitted thus

\begin{picture}(1662,1749)(3076,-3373)
\thinlines\put(3601,-2836){\line( 1, 0){1...
...h{\SetFigFont{12}{14.4}{\rmdefault}{\mddefault}{\updefault}Test}}}
\end{picture}
The rule about the number of repetitions still applies.

It is frequently desirable to subtract something other than 1. This may be because the number of repetitions is the result of a computation, and is given with some factor applied, e.g. a power of two. Alternatively it may be desired to save a line by setting the B line with some quantity already available, e.g. to count 12 one might set GC/J into the B tube, and subtract ///E, the former being supposed an instruction which is used in any case, the latter available in PERM.

Discrimination by control transfer

When two cases have to be given quite different treatment, involving different sequences of instructions, it is natural to choose the relevant sequences by a test instruction (i.e. conditional transfer /T, /H, /M, or /O). When there is a large number the best method is to manufacture a control transfer number which will lead to the appropriate sequence. A good example of this is in the input programme where the six warning characters have to be given six different treatments, and the remaining twenty-six are given a seventh. This is dealt with as follows. If $\alpha$ is the character in question $\alpha$/// is set to B6, and the instruction //IP given. The lines // to £/ contain the seven control transfer numbers appropriate to the thirty two possible values of $\alpha$ and the sequence required is immediately entered.

The B-tube as shunting station

When the B tube is used for the transfer of 20 digits from one short line to another, and both locations are fixed and known to the programmer, there is no difficulty. The difficulty arises when one of the short lines is in an indeterminate position, i.e. one computed by the machine, for it is usual to keep such indeterminate positions in a B-line, and the instructions involved will therefore presumably have to refer to two B-lines, one being the shunting station, the other describing the indeterminate position. To be more specific suppose that a quantity $\alpha\beta$ can easily be determined and that it is desired to set $[\alpha\beta + \tp{GI}]_s'=[\tp{OK}]_s$. How should this be done? The instruction OKQO will effect ${\tt\bf B}7' = [\tp{OK}]_s$. We need to follow this with $[\alpha\beta + \tp{GI}]_s'={\tt\bf B}7$, which can be achieved either by the instruction GIPZ when ${\tt\bf B}6=\alpha\beta\tp{E/}$ or by GIIZ when ${\tt\bf B}6=\alpha\beta\tp{Z/}$. There is a school of thought which maintains that a difference between presumptive and actual instructions in respect of the function number is to be deplored, and that the former instruction is therefore to be preferred to the latter. The instructions TT, TZ, TL were introduced in deference to this view; they do not improve speed or reduce space, but they may save a little programmer's time. When doing the reverse process, e.g. setting $[\tp{OK}]_s'=[\alpha\beta + \tp{GI}]_s$ the situation is usually rather simpler. One can set ${\tt\bf B}7'=\alpha\beta\tp{//}$ and follow this with GIQT and OKQB.

Note: If $[\alpha\beta]_+ + [\gamma\delta]_+ > 2^{10}$, $[\alpha\beta + \gamma\delta]_s' = {\tt\bf B}7$ is achieved with ${\tt\bf B}6 = \alpha\beta\tp{//}$ and presumptive instruction $\gamma\delta\tp{PZ}$.

Omission of counting

If the operation to be repeated contains rather few instructions e.g. three, and is crucial for the speed of the whole process it may be best to omit the instructions concerned with counting and to repeat the instructions concerned with the process in question the requisite number of times. Sometimes the number of repetitions may be the result of calculation, but even then the omission of counting method may still be applied, the number of repetitions being controlled through a control transfer entering the sequence of repeated operations at the appropriate point.

Alternative entry

It is often necessary to have a number of routines differing in certain minor particulars. One would like to use essentially the same instructions for all of them. The most convenient method seems to be to use one assembly of instructions, with various points of entry. The cues of these routines will then differ only in their first ten digits.

Changing sign in the accumulator

The instruction DSTJ has the effect ${\tt\bf A}'=\{1-{\tt\bf A}_\pm\}_0^{79}$ which for most purposes is as good as ${\tt\bf A}'=\{-{\tt\bf A}_\pm\}_0^{79}$ which can only be achieved in two instructions, or more if 80 digits are required.

Twenty-digit numbers

It is often sufficient to specify tabular numbers to twenty digits only. One might for instance wish to have values of $2^{38} \log n$ for $n$ from 1 to 32 with an error of not more than $2^{20}$. This can be achieved by putting e.g. $[\tp{/+}+n]_s=\{\log n\}_{-19}^1$. Then $[\tp{/+}+n-1]_+ = 2^{38}\log n + \Theta(2^{20})$.

Clearing the accumulator

The beginner is liable either to leave things in the accumulator to get mixed up with the next calculation or else to put in accumulator clearing instructions which could easily have been avoided. In fact it is very seldom necessary to give a special instruction for clearing the accumulator, if the points below are held in mind.

(a)
Instruction TA clears the accumulator as well as transferring L [i.e., the lower half of the accumulator] to store. If both halves of the accumulator are required to be stored one can use /U twice and the accumulator will be cleared.

(b)
If an expression of form $a+bc$ is required and the accumulator is not clear the term $a$ should be put into the accumulator first. This applies if the final value required will be in L.

(c)
When doing multiplications with results taken from M [i.e., the upper half of the accumulator] it is not necessary to clear the whole of the accumulator in advance, but only M. The maximum error will be 1 in either case: the mean sequare error will be one third with clearing but only one sixth without. If the results are taken out with /A then M remains clear for another multiplication.

Electronic space economy measures

We have explained that the economising of instructions in order to reduce the space occupied in the magnetic store is seldom worth while. There are however occasions when it is worth while to economise them to save space in the electronic store. This is nearly always in order to get instructions either into one page or into two pages. To do so makes the routine tidier, and usually has time-economy effects. For instance a one-page routine may be combined with another one-page (master) routine which uses it again and again without losing time over magnetic transfers. In these cases the two routines are able to be in the electronic store together, one on one page and one on the other. This effect can also be achieved by the disagreeable device of borrowing part of the systematic working space (if available) for part of the master routine. If a routine occupies more than two pages then (unless the same objectionable shift is relied on) it must involve magnetic transfers, and consequent loss of time. To some extent then the considerations mentioned under `manoevring space' may be overruled, though they generally apply for routines of three or more pages. Some possible economy measures are described below.

Of course an economy measure which simply reduces the number of instructions in a straight sequence will normally be a time economy as well. We have mentioned two or three devices for keeping the number of instructions down, but this will mostly be learnt by experience.

Duplication of use of lines

The chief space economy measure available other than reducing the number of instructions is the use of a line for more than one purpose. One or two forms of this have already been mentioned. It is usual for instance to use addressless instructions (p. [*]) also as control transfer numbers. Another case was mentioned under the head of counting. No attempt can be made to list all such devices, but there are a number associated with control transfer numbers. These are sufficiently numerous that it should nearly always be possible to avoid using any lines specially for control transfer numbers. To make a point of doing so in cases where it is not strictly necessary is however strongly to be discouraged, as liable to lead to a most wasteful use of programmers' time. The methods already known to be available are mentioned below.

Sandwiching

If a (short) line is sandwiched between two sequences of instructions, the instruction which uses that short line can also be used as a control transfer number for the beginning of the later sequence of instructions. A long line can only be used in this way if it ends with a pair of characters representing a harmless instruction. Lines ending with , , ..., ££ are almost the only suitable ones.

Positioning of dummy stops

If a dummy stop or other addressless instruction be placed immediately before a `junction', (i.e. an instruction to which a control transfer is made, but which is not the beginning of a straight sequence) then this addressless instruction may be used as a control transfer number in the usual way, and the control transfer instruction may also be used as a control transfer number for the transfer to the junction. This position will not however always be suitable from other points of view.

Relative control transfers

Relative transfers (e.g. /Q) are more troublesome to use than absolute ones, but provide a second string. The necessary relative transfer number may sometimes be found in PERM, or if part of the routine itself is being used as special working space, it may be found in the routine itself.

Inaccurate numbers

If a line pair is required to be known to more than twenty but less than thirty binary digits, the first two characters of the line pair used may be changed to a control transfer number. Likewise if a number is required to less than ten digits a control transfer number may also be concealed in it.

Changeling instructions

This is another space economy measure but not concerned with control transfer numbers. Suppose that the same sequence of instructions is to be used twice in different connections, but with one instruction changed. It may be worth while to use the same actual sequence of instructions. The troublesome instruction may be altered with a B line e.g. if the two effects required are IKT¼ and IKTF they may be achieved with the instruction IEZ¼, B1 having as content either /C// or /C/S.

Wholesale reciprocals

A few devices which are almost purely computational, having little connection with the machine, may also be mentioned. One of these is a method of obtaining reciprocals if a number are required at once. The simplest case is where two are required, e.g. $a^{-1}$ and $b^{-1}$. One can get them with only one use of the reciprocals routine, using it only to obtain $(ab)^{-1}$ and multiplying by $b$ or by $a$ to obtain $a^{-1}$ or $b^{-1}$. Any number of reciprocals may be obtained in a similar way with only one use of the reciprocals routine, but all the numbers to be inverted must be known before any reciprocal can be obtained. Best accuracy is retained if the numbers are brought to `standard form' i.e. to between $1 \over 2$ and 1 before the process is applied.

Tchebysheff polynomials

The number of terms to be taken in a power series may often be reduced by the use of Tchebysheff polynomials. The Tchebysheff polynomial $T(x)$ of degree $n$ is given by

\begin{eqnarray*}
T_0(\cos \theta) & = & 1 \\
T_n(\cos \theta) & = & 2^{1-n}\cos n\theta    (n \ge 0)
\end{eqnarray*}



It has the property that the coefficient of $x^n$ is 1 and the other coefficients are chosen so that the maximum modulus of the polynomial over the interval (-1,1) shall be minimised. The resulting minimum-maximum has the value $2^{1-n}$. The application of these polynomials to the calculation of power series is that one may replace a term $ax^n (-1 \le x \le 1)$ by $a(x^n - T_n(x))$ which is of degree $n-1$, with an error of at most $2^{1-n}a$, as compared with an error of $a$, which would occur if the term were merely dropped. In the case that the relevant range is $0 \le x \le 1$ one replaces $ax^n$ by $a(x^n - T_n({x + 1 \over 2}))$, with an error at most $2^{1-2n}a$. [The ms. contains a crossed-out note that the $0 \le x \le 1$ condition arises ``by a substitution when odd or even functions are to be calculated''.]

The coefficients of the polynomials may be calculated by using the recurrence relations [as documented in the ms., not yet checked]

\begin{eqnarray*}
T_0(x) & = & 1 \\
T_1(x) & = & x \\
T_{n+1}(x) & = & x T_n(x) - {1 \over 4}T_{n-1}(x)    (n > 1) \\
\end{eqnarray*}



and if we put $V_n(x)=T_n({x + 1 \over 2})$ its coefficients may be calculated from

\begin{eqnarray*}
V_0(x) & = & 1 \\
V_1(x) & = & {x + 1\over 2}\\
V_{n+1}(x) & = & {x+1\over 2}V_n(x) - {1 \over 4}V_{n-1}(x)    (n > 1) \\
\end{eqnarray*}



A number of the polynomials are given below.

\begin{eqnarray*}
T_0(x) & = & 1 \\
T_1(x) & = & x \\
T_2(x) & = & {1 \over 2}...
...) \\
T_9(x) & = & {1 \over 256}(256x^9-576x^7+432x^5-120x^2+9x)
\end{eqnarray*}




next up previous
Next: The official account of Up: Alan Turing's Manual for Previous: Programming Principles
Robert S. Thau 2000-02-13