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