annotate lab4/test/search.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
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 (* String searching *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 const target = "abracadabra";
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 var i: integer; found: boolean;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 i := 0; found := false;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 while not found do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 found := target[i] = 'd';
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 i := i + 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 print_num(i);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 end.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 7
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 >>*)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 (*[[
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 @ picoPascal compiler output
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 .include "fixup.s"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 .global pmain
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 .text
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 pmain:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 @ i := 0; found := false;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 mov r0, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 set r1, _i
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 str r0, [r1]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 mov r0, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 set r1, _found
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 strb r0, [r1]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 .L3:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 @ while not found do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 set r4, _found
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 ldrb r0, [r4]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 cmp r0, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 bne .L5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 @ found := target[i] = 'd';
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 set r5, _i
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 ldr r6, [r5]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 set r0, g1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 add r0, r0, r6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 ldrb r0, [r0]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 cmp r0, #100
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 mov r0, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 moveq r0, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 strb r0, [r4]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 @ i := i + 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 add r0, r6, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 str r0, [r5]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 b .L3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 .L5:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 @ print_num(i);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 set r0, _i
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 ldr r0, [r0]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 @ newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 bl newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 .comm _i, 4, 4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 .comm _found, 1, 4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 .data
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 g1:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 .byte 97, 98, 114, 97, 99, 97, 100, 97, 98, 114
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 .byte 97
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 .byte 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 @ End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 ]]*)