Home
 Gallery
 Mad Tech
 Cars
 KDF9
 Project Details
 Testing
 Links

 Vamoosh
 Denham Broadband
 Locostbuilders
 Speedcentral

 About Me
 Contact Info

KDF9 Sample Programs

Presented here is some sample code that I have written for the KDF9 simulator. It is written in original (as per the original Programming Manual) KDF9 User Code, and as such the programs would run on an original KDF9 machine (after having been transcribed to paper tape).


Fibonacci Number Calculator
This program calculates the first few Fibonacci numbers and shows how the stack can be used carefully to avoid the use of the main store. It also shows how one of the specialised store instructions works with the Q-stores to make working with loops very straightforward:
SET +10;=C1; (Number of Values to calculate)
SET +1; (Starting value)
DUP;=M1; (Set memory location counter)
DUP;=I1; (Set memory increment)
DUP;
30; (Start Loop)
DUPD;+; (Duplicate and add)
DUP;=Y20M1Q; (Store result)
CAB;ERASE; (Set up next addition)
J30C1NZ; (Jump for the loop)


Factorial Calculator
This program calculates the factorials of the integers from 1 to 10:
SET +1;=M1; (Set memory location counter)
SET +1;=I1; (Set memory increment)
SET +10;=C1; (Number of values to calculate)

30; (Start outer loop)
M1;DUP;=C2; (Call down number to calc. from)

40; (Start inner loop)
C2;
SET 1;
-;
DUP;
J50=Z; (End loop if countdown reached zero)

DUP;=C2;
xD;CONT; (Workaround for fixed length mult.)
J40; (End first loop)

50;ERASE;
=Y21M1Q; (Store result and update counters)
J30C1NZ; (End Outer Loop)


Euclids Algorithm
This piece of code implements Euclid’s algorithm for finding the highest common factor of a pair of integers. KDF9 had no manual inputs, and this is reflected in this code – the numbers the calculation is to be performed on are given in code in the first two lines. On the KDF9 itself, these could have been read from punched cards if necessary. This code is an example of an algorithm that can be coded entirely on the main stack, and as such it would have (in relation to other machines at the time) run very quickly.
SET +1115; (First number a)
SET +35; (Second number b)

20;
DUP;
PERM;
/I; (Perform integer division with remainder)
PERM;
ERASE;
REV;DUP; (Rearrange stack so loop restarts ok)
J20!=Z; (Jump until remainder is zero)
ERASE; (Clear unused number)
(Result left in top of stack)