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