annotate ppc/test/fac.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 (* fac.p *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 proc fac(n: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 if n = 0 then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 return 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 else
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 return n * fac(n-1)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 var f: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 f := fac(10);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 print_num(f);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 end.
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 3628800
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 >>*)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 (*[[
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 MODULE Main 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 IMPORT Lib 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 ENDHDR
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 PROC _fac 0 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 ! if n = 0 then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 JEQZ L1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 JUMP L2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 LABEL L1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 ! return 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 RETURNW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 LABEL L2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 ! return n * fac(n-1)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 MINUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 GLOBAL _fac
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 PCALLW 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 TIMES
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 RETURNW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 PROC MAIN 0 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 ! f := fac(10);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 CONST 10
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 GLOBAL _fac
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 PCALLW 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 STGW _f
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 ! print_num(f);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 LDGW _f
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 GLOBAL lib.print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 PCALL 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 ! newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 GLOBAL lib.newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 PCALL 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 RETURN
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 GLOVAR _f 4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 ! End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 ]]*)