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