annotate ppc/test/fib.p @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 (* fib.p *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 (* fib -- fibonacci numbers *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 proc fib(n: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 if n <= 1 then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 return 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 else
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 return fib(n-1) + fib(n-2)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 print_num(fib(6)); newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 end.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 13
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 >>*)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 (*[[
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 MODULE Main 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 IMPORT Lib 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 ENDHDR
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 PROC _fib 0 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 ! if n <= 1 then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 JGT L2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 ! return 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 RETURNW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 LABEL L2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 ! return fib(n-1) + fib(n-2)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 MINUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 GLOBAL _fib
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 PCALLW 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 CONST 2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 MINUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 GLOBAL _fib
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 PCALLW 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 PLUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 RETURNW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 PROC MAIN 0 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 ! print_num(fib(6)); newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 CONST 6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 GLOBAL _fib
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 PCALLW 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 GLOBAL lib.print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 PCALL 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 GLOBAL lib.newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 PCALL 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 RETURN
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 ! End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 ]]*)