comparison lab4/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 (* Another version of phone book using a record type *)
2
3 type string = array 10 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 < 10 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 store(n: string; a: integer);
25 begin
26 db[N].name := n;
27 db[N].age := a;
28 N := N+1
29 end;
30
31 proc recall(n: string): integer;
32 var i: integer;
33 begin
34 i := 0;
35 while i < N do
36 if equal(db[i].name, n) then
37 return db[i].age
38 end;
39 i := i+1
40 end;
41 return 999
42 end;
43
44 begin
45 N := 0;
46
47 store("bill ", 23);
48 store("george ", 34);
49
50 print_num(recall("george ")); newline();
51 print_num(recall("fred ")); newline()
52 end.
53
54 (*<<
55 34
56 999
57 >>*)
58
59 (*[[
60 @ picoPascal compiler output
61 .include "fixup.s"
62 .global pmain
63
64 @ proc equal(x, y: string): boolean;
65 .text
66 _equal:
67 mov ip, sp
68 stmfd sp!, {r0-r1}
69 stmfd sp!, {r4-r10, fp, ip, lr}
70 mov fp, sp
71 @ i := 0;
72 mov r4, #0
73 .L6:
74 @ while i < 10 do
75 cmp r4, #10
76 bge .L8
77 @ if x[i] <> y[i] then
78 ldr r0, [fp, #40]
79 add r0, r0, r4
80 ldrb r0, [r0]
81 ldr r1, [fp, #44]
82 add r1, r1, r4
83 ldrb r1, [r1]
84 cmp r0, r1
85 beq .L11
86 @ return false
87 mov r0, #0
88 b .L5
89 .L11:
90 @ i := i+1
91 add r4, r4, #1
92 b .L6
93 .L8:
94 @ return true
95 mov r0, #1
96 .L5:
97 ldmfd fp, {r4-r10, fp, sp, pc}
98 .ltorg
99
100 @ proc store(n: string; a: integer);
101 _store:
102 mov ip, sp
103 stmfd sp!, {r0-r1}
104 stmfd sp!, {r4-r10, fp, ip, lr}
105 mov fp, sp
106 @ db[N].name := n;
107 mov r2, #10
108 ldr r1, [fp, #40]
109 set r0, _db
110 set r3, _N
111 ldr r3, [r3]
112 lsl r3, r3, #4
113 add r0, r0, r3
114 bl memcpy
115 @ db[N].age := a;
116 set r4, _N
117 ldr r0, [fp, #44]
118 set r1, _db
119 ldr r2, [r4]
120 lsl r2, r2, #4
121 add r1, r1, r2
122 str r0, [r1, #12]
123 @ N := N+1
124 ldr r0, [r4]
125 add r0, r0, #1
126 str r0, [r4]
127 ldmfd fp, {r4-r10, fp, sp, pc}
128 .ltorg
129
130 @ proc recall(n: string): integer;
131 _recall:
132 mov ip, sp
133 stmfd sp!, {r0-r1}
134 stmfd sp!, {r4-r10, fp, ip, lr}
135 mov fp, sp
136 @ i := 0;
137 mov r4, #0
138 .L14:
139 @ while i < N do
140 set r0, _N
141 ldr r0, [r0]
142 cmp r4, r0
143 bge .L16
144 @ if equal(db[i].name, n) then
145 ldr r1, [fp, #40]
146 set r0, _db
147 lsl r2, r4, #4
148 add r0, r0, r2
149 bl _equal
150 cmp r0, #0
151 beq .L19
152 @ return db[i].age
153 set r0, _db
154 lsl r1, r4, #4
155 add r0, r0, r1
156 ldr r0, [r0, #12]
157 b .L13
158 .L19:
159 @ i := i+1
160 add r4, r4, #1
161 b .L14
162 .L16:
163 @ return 999
164 set r0, #999
165 .L13:
166 ldmfd fp, {r4-r10, fp, sp, pc}
167 .ltorg
168
169 pmain:
170 mov ip, sp
171 stmfd sp!, {r4-r10, fp, ip, lr}
172 mov fp, sp
173 @ N := 0;
174 mov r0, #0
175 set r1, _N
176 str r0, [r1]
177 @ store("bill ", 23);
178 mov r1, #23
179 set r0, g1
180 bl _store
181 @ store("george ", 34);
182 mov r1, #34
183 set r0, g2
184 bl _store
185 @ print_num(recall("george ")); newline();
186 set r0, g3
187 bl _recall
188 bl print_num
189 bl newline
190 @ print_num(recall("fred ")); newline()
191 set r0, g4
192 bl _recall
193 bl print_num
194 bl newline
195 ldmfd fp, {r4-r10, fp, sp, pc}
196 .ltorg
197
198 .comm _db, 320, 4
199 .comm _N, 4, 4
200 .data
201 g1:
202 .byte 98, 105, 108, 108, 32, 32, 32, 32, 32, 32
203 .byte 0
204 g2:
205 .byte 103, 101, 111, 114, 103, 101, 32, 32, 32, 32
206 .byte 0
207 g3:
208 .byte 103, 101, 111, 114, 103, 101, 32, 32, 32, 32
209 .byte 0
210 g4:
211 .byte 102, 114, 101, 100, 32, 32, 32, 32, 32, 32
212 .byte 0
213 @ End
214 ]]*)