### comparison lab4/test/record0.p @ 0:bfdcc3820b32

Basis
author Mike Spivey Thu, 05 Oct 2017 08:04:15 +0100
comparison
equal inserted replaced
-1:000000000000 0:bfdcc3820b32
1 (* Phone book using record type *)
2
3 const wordlen = 10;
4
5 type string = array wordlen of char;
6
7 type rec = record name: string; age: integer end;
8
9 var
10 db: array 20 of rec;
11 N: integer;
12
13 proc equal(x, y: string): boolean;
14 var i: integer;
15 begin
16 i := 0;
17 while i < wordlen do
18 if x[i] <> y[i] then
19 return false
20 end;
21 i := i+1
22 end;
23 return true
24 end;
25
26 proc copy(var dst: string; src: string);
27 var i: integer;
28 begin
29 i := 0;
30 while i < wordlen do
31 dst[i] := src[i]; i := i+1
32 end
33 end;
34
35 proc store(n: string; a: integer);
36 begin
37 copy(db[N].name, n);
38 db[N].age := a;
39 N := N+1
40 end;
41
42 proc recall(n: string): integer;
43 var i: integer;
44 begin
45 i := 0;
46 while i < N do
47 if equal(db[i].name, n) then
48 return db[i].age
49 end;
50 i := i+1
51 end;
52 return 999
53 end;
54
55 begin
56 N := 0;
57
58 store("bill ", 23);
59 store("george ", 34);
60
61 print_num(recall("george ")); newline();
62 print_num(recall("fred ")); newline()
63 end.
64
65 (*<<
66 34
67 999
68 >>*)
69
70 (*[[
71 @ picoPascal compiler output
72 .include "fixup.s"
73 .global pmain
74
75 @ proc equal(x, y: string): boolean;
76 .text
77 _equal:
78 mov ip, sp
79 stmfd sp!, {r0-r1}
80 stmfd sp!, {r4-r10, fp, ip, lr}
81 mov fp, sp
82 @ i := 0;
83 mov r4, #0
84 .L6:
85 @ while i < wordlen do
86 cmp r4, #10
87 bge .L8
88 @ if x[i] <> y[i] then
89 ldr r0, [fp, #40]
91 ldrb r0, [r0]
92 ldr r1, [fp, #44]
94 ldrb r1, [r1]
95 cmp r0, r1
96 beq .L11
97 @ return false
98 mov r0, #0
99 b .L5
100 .L11:
101 @ i := i+1
103 b .L6
104 .L8:
105 @ return true
106 mov r0, #1
107 .L5:
108 ldmfd fp, {r4-r10, fp, sp, pc}
109 .ltorg
110
111 @ proc copy(var dst: string; src: string);
112 _copy:
113 mov ip, sp
114 stmfd sp!, {r0-r1}
115 stmfd sp!, {r4-r10, fp, ip, lr}
116 mov fp, sp
117 @ i := 0;
118 mov r4, #0
119 .L13:
120 @ while i < wordlen do
121 cmp r4, #10
122 bge .L12
123 @ dst[i] := src[i]; i := i+1
124 ldr r0, [fp, #44]
126 ldrb r0, [r0]
127 ldr r1, [fp, #40]
129 strb r0, [r1]
131 b .L13
132 .L12:
133 ldmfd fp, {r4-r10, fp, sp, pc}
134 .ltorg
135
136 @ proc store(n: string; a: integer);
137 _store:
138 mov ip, sp
139 stmfd sp!, {r0-r1}
140 stmfd sp!, {r4-r10, fp, ip, lr}
141 mov fp, sp
142 @ copy(db[N].name, n);
143 ldr r1, [fp, #40]
144 set r0, _db
145 set r2, _N
146 ldr r2, [r2]
147 lsl r2, r2, #4
149 bl _copy
150 @ db[N].age := a;
151 set r4, _N
152 ldr r0, [fp, #44]
153 set r1, _db
154 ldr r2, [r4]
155 lsl r2, r2, #4
157 str r0, [r1, #12]
158 @ N := N+1
159 ldr r0, [r4]
161 str r0, [r4]
162 ldmfd fp, {r4-r10, fp, sp, pc}
163 .ltorg
164
165 @ proc recall(n: string): integer;
166 _recall:
167 mov ip, sp
168 stmfd sp!, {r0-r1}
169 stmfd sp!, {r4-r10, fp, ip, lr}
170 mov fp, sp
171 @ i := 0;
172 mov r4, #0
173 .L18:
174 @ while i < N do
175 set r0, _N
176 ldr r0, [r0]
177 cmp r4, r0
178 bge .L20
179 @ if equal(db[i].name, n) then
180 ldr r1, [fp, #40]
181 set r0, _db
182 lsl r2, r4, #4
184 bl _equal
185 cmp r0, #0
186 beq .L23
187 @ return db[i].age
188 set r0, _db
189 lsl r1, r4, #4
191 ldr r0, [r0, #12]
192 b .L17
193 .L23:
194 @ i := i+1
196 b .L18
197 .L20:
198 @ return 999
199 set r0, #999
200 .L17:
201 ldmfd fp, {r4-r10, fp, sp, pc}
202 .ltorg
203
204 pmain:
205 mov ip, sp
206 stmfd sp!, {r4-r10, fp, ip, lr}
207 mov fp, sp
208 @ N := 0;
209 mov r0, #0
210 set r1, _N
211 str r0, [r1]
212 @ store("bill ", 23);
213 mov r1, #23
214 set r0, g1
215 bl _store
216 @ store("george ", 34);
217 mov r1, #34
218 set r0, g2
219 bl _store
220 @ print_num(recall("george ")); newline();
221 set r0, g3
222 bl _recall
223 bl print_num
224 bl newline
225 @ print_num(recall("fred ")); newline()
226 set r0, g4
227 bl _recall
228 bl print_num
229 bl newline
230 ldmfd fp, {r4-r10, fp, sp, pc}
231 .ltorg
232
233 .comm _db, 320, 4
234 .comm _N, 4, 4
235 .data
236 g1:
237 .byte 98, 105, 108, 108, 32, 32, 32, 32, 32, 32
238 .byte 0
239 g2:
240 .byte 103, 101, 111, 114, 103, 101, 32, 32, 32, 32
241 .byte 0
242 g3:
243 .byte 103, 101, 111, 114, 103, 101, 32, 32, 32, 32
244 .byte 0
245 g4:
246 .byte 102, 114, 101, 100, 32, 32, 32, 32, 32, 32
247 .byte 0
248 @ End
249 ]]*)