annotate ppc/test/sumpower.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 (* sumpower.p *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 proc sum(a, b: integer; proc f(x: integer): integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 var i, s: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 i := a; s := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 while i <= b do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 s := s + f(i);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 i := i + 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 return s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 proc sum_powers(a, b, n: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 proc pow(x: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 var j, p: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 j := 0; p := 1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 while j < n do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 p := p * x;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 j := j + 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 return p
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 return sum(a, b, pow)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 print_num(sum_powers(1, 10, 3));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 end.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 3025
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 >>*)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 (*[[
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 MODULE Main 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 IMPORT Lib 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 ENDHDR
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 PROC _sum 8 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 ! i := a; s := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 STLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 STLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 ! while i <= b do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 JUMP L2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 LABEL L1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 ! s := s + f(i);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 LDLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 LDLW 28
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 LDLW 24
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 PCALLW 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 PLUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 STLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 ! i := i + 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 PLUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 STLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 LABEL L2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 LDLW 20
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 JLEQ L1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 ! return s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 LDLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 RETURNW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 PROC _sum_powers 0 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 ! return sum(a, b, pow)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 LOCAL 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 GLOBAL _pow
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 LDLW 20
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 GLOBAL _sum
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 PCALLW 4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 RETURNW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 PROC _pow 8 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 ! j := 0; p := 1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 STLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 STLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 ! while j < n do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 JUMP L5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94 LABEL L4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
95 ! p := p * x;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
96 LDLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
97 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
98 TIMES
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
99 STLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
100 ! j := j + 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
101 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
102 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
103 PLUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
104 STLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
105 LABEL L5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
106 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
107 LDLW 12
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
108 LDNW 24
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
109 JLT L4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
110 ! return p
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
111 LDLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
112 RETURNW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
113 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
114
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
115 PROC MAIN 0 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
116 ! print_num(sum_powers(1, 10, 3));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
117 CONST 3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
118 CONST 10
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
119 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
120 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
121 GLOBAL _sum_powers
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
122 PCALLW 3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
123 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
124 GLOBAL lib.print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
125 PCALL 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
126 ! newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
127 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
128 GLOBAL lib.newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
129 PCALL 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
130 RETURN
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
131 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
132
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
133 ! End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
134 ]]*)