; TPK algorithm
; from "Programming the Mark I, Early
; programming activity at the University 
; of Manchester", M. Campbell-Kelly, 
; Annals of the History of Computing
; vol 2, No 2, April 1980


; routine to read a vector of 11 fractions from 
; paper tape and store at addr given in index q (B6)

          a@// d@DS  ; d = const in PERM = -1
          LOOP 10n
             SUBROUTINE W ->aq ; read from tape (in form .nnnn+/-) to a[q]
             SUBROUTINE Y      ; print CR LF
             +aq SUBROUTINE Q  ; print with decimals a[q] (the read value) then CR LF
             q-d q-d           ; decrement index q with value -1 two times -> q=q+2
          REPEAT n
          SUBROUTINE Y SUBROUTINE Y ; print two blank lines
          CLOSE                ; return to caller
          WRITE 1              ; write to drum as routine 1

; auxiliary routine to calc
; y=0.1*sqrt(abs(t)) + 0.5*(t^3)

          b@VA c@MA d@GA e@OA t@/C
          FRACTIONS +49999->b +1->e ; b=0.5, e=0.1
          ->t                       ; if (t>0) acc=+t else acc=-t
          +t TESTA X                ; => acc=abs(t)
          -t
ENTRY X   SUBROUTINE B ->c          ; Square root of value held in acc
          +tt ->d +td ->d +bd ->d   ; d=b*t*t*t => d=0.5*(t^3)
          +ec +d                    ; d=0.1 * sqrt(abs(t)) + d
          CLOSE
          WRITE 4

; master routine

          a@/# b@IS c@MA d@GA e@OA f@Z: y@PA ; b = PERM const = -0.5
          FRACTIONS +00004->d +0000999991->e 
          f->q         ; set q=/#// = the addr of array a where to store input data
          SUBROUTINE 1 ; INPUT vector of signed fractions at addr given by q
          LOOP 10n
          n->c             ; copy index n to c
          -bc              ; c=-(-0.5)*c, result in AM
          SUBROUTINE Q ->c ; print AM as integer (DECOUTPUT)
                           ; we should finish the equation with a dummy assign
                           ; so compiler will load again +an into AL (instead of continue using AM)                           
          +an SUBROUTINE Q ; on subroutine Q termination, Acc is cleared, so we
                           ; can just start a new equation with +an, call 
                           ; auxiliary subroutine to perform calculation and then
                           ; store result in y
          +an SUBROUTINE 4 ->y
          +y -d TESTA X
          +y SUBROUTINE Q CONTROL Y
ENTRY X   +e SUBROUTINE Q
ENTRY Y   REPEAT n
ENTRY Z   CONTROL Z
          WRITE 5 START 5

.010-
.020-
.030-
.040+
.050+
.060+
.070-
.080-
.085+
.090+
.095+


