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