annotate lab4/test/record0.p @ 1:b5139af1a420 tip basis

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