comparison ppc/test/record0.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 const wordlen = 10;
2
3 type string = array wordlen of char;
4
5 type rec = record name: string; age: integer end;
6
7 var
8 db: array 20 of rec;
9 N: integer;
10
11 proc equal(x, y: string): boolean;
12 var i: integer;
13 begin
14 i := 0;
15 while i < wordlen do
16 if x[i] <> y[i] then
17 return false
18 end;
19 i := i+1
20 end;
21 return true
22 end;
23
24 proc copy(var dst: string; src: string);
25 var i: integer;
26 begin
27 i := 0;
28 while i < wordlen do
29 dst[i] := src[i]; i := i+1
30 end
31 end;
32
33 proc store(n: string; a: integer);
34 begin
35 copy(db[N].name, n);
36 db[N].age := a;
37 N := N+1
38 end;
39
40 proc recall(n: string): integer;
41 var i: integer;
42 begin
43 i := 0;
44 while i < N do
45 if equal(db[i].name, n) then
46 return db[i].age
47 end;
48 i := i+1
49 end;
50 return 999
51 end;
52
53 begin
54 N := 0;
55
56 store("bill ", 23);
57 store("george ", 34);
58
59 print_num(recall("george ")); newline();
60 print_num(recall("fred ")); newline()
61 end.
62
63 (*<<
64 34
65 999
66 >>*)
67
68 (*[[
69 MODULE Main 0 0
70 IMPORT Lib 0
71 ENDHDR
72
73 PROC _equal 4 0 0
74 ! i := 0;
75 CONST 0
76 STLW -4
77 ! while i < wordlen do
78 JUMP L6
79 LABEL L5
80 ! if x[i] <> y[i] then
81 LDLW 16
82 LDLW -4
83 LDIC
84 LDLW 20
85 LDLW -4
86 LDIC
87 JEQ L10
88 ! return false
89 CONST 0
90 RETURNW
91 LABEL L10
92 ! i := i+1
93 LDLW -4
94 CONST 1
95 PLUS
96 STLW -4
97 LABEL L6
98 LDLW -4
99 CONST 10
100 JLT L5
101 ! return true
102 CONST 1
103 RETURNW
104 END
105
106 PROC _copy 4 0 0
107 ! i := 0;
108 CONST 0
109 STLW -4
110 ! while i < wordlen do
111 JUMP L12
112 LABEL L11
113 ! dst[i] := src[i]; i := i+1
114 LDLW 20
115 LDLW -4
116 LDIC
117 LDLW 16
118 LDLW -4
119 STIC
120 LDLW -4
121 CONST 1
122 PLUS
123 STLW -4
124 LABEL L12
125 LDLW -4
126 CONST 10
127 JLT L11
128 RETURN
129 END
130
131 PROC _store 0 0 0
132 ! copy(db[N].name, n);
133 LDLW 16
134 GLOBAL _db
135 LDGW _N
136 CONST 16
137 TIMES
138 OFFSET
139 CONST 0
140 GLOBAL _copy
141 PCALL 2
142 ! db[N].age := a;
143 LDLW 20
144 GLOBAL _db
145 LDGW _N
146 CONST 16
147 TIMES
148 OFFSET
149 STNW 12
150 ! N := N+1
151 LDGW _N
152 CONST 1
153 PLUS
154 STGW _N
155 RETURN
156 END
157
158 PROC _recall 4 0 0
159 ! i := 0;
160 CONST 0
161 STLW -4
162 ! while i < N do
163 JUMP L15
164 LABEL L14
165 ! if equal(db[i].name, n) then
166 LDLW 16
167 GLOBAL _db
168 LDLW -4
169 CONST 16
170 TIMES
171 OFFSET
172 CONST 0
173 GLOBAL _equal
174 PCALLW 2
175 JNEQZ L17
176 JUMP L19
177 LABEL L17
178 ! return db[i].age
179 GLOBAL _db
180 LDLW -4
181 CONST 16
182 TIMES
183 OFFSET
184 LDNW 12
185 RETURNW
186 LABEL L19
187 ! i := i+1
188 LDLW -4
189 CONST 1
190 PLUS
191 STLW -4
192 LABEL L15
193 LDLW -4
194 LDGW _N
195 JLT L14
196 ! return 999
197 CONST 999
198 RETURNW
199 END
200
201 PROC MAIN 0 0 0
202 ! N := 0;
203 CONST 0
204 STGW _N
205 ! store("bill ", 23);
206 CONST 23
207 GLOBAL g1
208 CONST 0
209 GLOBAL _store
210 PCALL 2
211 ! store("george ", 34);
212 CONST 34
213 GLOBAL g2
214 CONST 0
215 GLOBAL _store
216 PCALL 2
217 ! print_num(recall("george ")); newline();
218 GLOBAL g3
219 CONST 0
220 GLOBAL _recall
221 PCALLW 1
222 CONST 0
223 GLOBAL lib.print_num
224 PCALL 1
225 CONST 0
226 GLOBAL lib.newline
227 PCALL 0
228 ! print_num(recall("fred ")); newline()
229 GLOBAL g4
230 CONST 0
231 GLOBAL _recall
232 PCALLW 1
233 CONST 0
234 GLOBAL lib.print_num
235 PCALL 1
236 CONST 0
237 GLOBAL lib.newline
238 PCALL 0
239 RETURN
240 END
241
242 GLOVAR _db 320
243 GLOVAR _N 4
244 ! String "bill "
245 DEFINE g1
246 STRING 62696C6C20202020202000
247
248 ! String "george "
249 DEFINE g2
250 STRING 67656F7267652020202000
251
252 ! String "george "
253 DEFINE g3
254 STRING 67656F7267652020202000
255
256 ! String "fred "
257 DEFINE g4
258 STRING 6672656420202020202000
259
260 ! End
261 ]]*)