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