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