comparison lab4/test/digits.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 (* The digits problem with sets represented by nested functions *)
2
3 proc search(k, n: integer; proc avail(x: integer): boolean);
4 var d, n1: integer;
5 proc avail1(x: integer): boolean;
6 begin
7 if x <> d then
8 return avail(x)
9 else
10 return false
11 end
12 end;
13 begin
14 if k = 9 then
15 print_num(n); newline()
16 else
17 d := 1;
18 while d < 10 do
19 n1 := 10 * n + d;
20 if (n1 mod (k+1) = 0) and avail(d) then
21 search(k+1, n1, avail1)
22 end;
23 d := d+1
24 end
25 end
26 end;
27
28 proc avail0(x: integer): boolean;
29 begin
30 return true
31 end;
32
33 begin
34 search(0, 0, avail0)
35 end.
36
37 (*<<
38 381654729
39 >>*)
40
41 (*[[
42 @ picoPascal compiler output
43 .include "fixup.s"
44 .global pmain
45
46 @ proc search(k, n: integer; proc avail(x: integer): boolean);
47 .text
48 _search:
49 mov ip, sp
50 stmfd sp!, {r0-r3}
51 stmfd sp!, {r4-r10, fp, ip, lr}
52 mov fp, sp
53 sub sp, sp, #8
54 @ if k = 9 then
55 ldr r0, [fp, #40]
56 cmp r0, #9
57 bne .L3
58 @ print_num(n); newline()
59 ldr r0, [fp, #44]
60 bl print_num
61 bl newline
62 b .L1
63 .L3:
64 @ d := 1;
65 mov r0, #1
66 str r0, [fp, #-4]
67 .L5:
68 @ while d < 10 do
69 ldr r5, [fp, #-4]
70 cmp r5, #10
71 bge .L1
72 @ n1 := 10 * n + d;
73 ldr r0, [fp, #44]
74 mov r1, #10
75 mul r0, r0, r1
76 add r4, r0, r5
77 @ if (n1 mod (k+1) = 0) and avail(d) then
78 ldr r0, [fp, #40]
79 add r1, r0, #1
80 mov r0, r4
81 bl int_mod
82 cmp r0, #0
83 bne .L10
84 ldr r0, [fp, #-4]
85 ldr r10, [fp, #52]
86 ldr r1, [fp, #48]
87 blx r1
88 cmp r0, #0
89 beq .L10
90 @ search(k+1, n1, avail1)
91 mov r3, fp
92 set r2, _avail1
93 mov r1, r4
94 ldr r0, [fp, #40]
95 add r0, r0, #1
96 bl _search
97 .L10:
98 @ d := d+1
99 ldr r0, [fp, #-4]
100 add r0, r0, #1
101 str r0, [fp, #-4]
102 b .L5
103 .L1:
104 ldmfd fp, {r4-r10, fp, sp, pc}
105 .ltorg
106
107 @ proc avail1(x: integer): boolean;
108 _avail1:
109 mov ip, sp
110 stmfd sp!, {r0-r1}
111 stmfd sp!, {r4-r10, fp, ip, lr}
112 mov fp, sp
113 @ if x <> d then
114 ldr r4, [fp, #40]
115 ldr r5, [fp, #24]
116 ldr r0, [r5, #-4]
117 cmp r4, r0
118 beq .L14
119 @ return avail(x)
120 mov r0, r4
121 ldr r10, [r5, #52]
122 ldr r1, [r5, #48]
123 blx r1
124 b .L12
125 .L14:
126 @ return false
127 mov r0, #0
128 .L12:
129 ldmfd fp, {r4-r10, fp, sp, pc}
130 .ltorg
131
132 @ proc avail0(x: integer): boolean;
133 _avail0:
134 mov ip, sp
135 stmfd sp!, {r0-r1}
136 stmfd sp!, {r4-r10, fp, ip, lr}
137 mov fp, sp
138 @ return true
139 mov r0, #1
140 ldmfd fp, {r4-r10, fp, sp, pc}
141 .ltorg
142
143 pmain:
144 mov ip, sp
145 stmfd sp!, {r4-r10, fp, ip, lr}
146 mov fp, sp
147 @ search(0, 0, avail0)
148 mov r3, #0
149 set r2, _avail0
150 mov r1, #0
151 mov r0, #0
152 bl _search
153 ldmfd fp, {r4-r10, fp, sp, pc}
154 .ltorg
155
156 @ End
157 ]]*)