comparison ppc/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 proc search(k, n: integer; proc avail(x: integer): boolean);
2 var d, n1: integer;
3 proc avail1(x: integer): boolean;
4 begin
5 return (x <> d) and avail(x)
6 end;
7 begin
8 if k = 9 then
9 print_num(n); newline()
10 else
11 d := 1;
12 while d < 10 do
13 n1 := 10 * n + d;
14 if (n1 mod (k+1) = 0) and avail(d) then
15 search(k+1, n1, avail1)
16 end;
17 d := d+1
18 end
19 end
20 end;
21
22 proc avail0(x: integer): boolean;
23 begin
24 return true
25 end;
26
27 begin
28 search(0, 0, avail0)
29 end.
30
31 (*<<
32 381654729
33 >>*)
34
35 (*[[
36 MODULE Main 0 0
37 IMPORT Lib 0
38 ENDHDR
39
40 PROC _search 8 0 0
41 ! if k = 9 then
42 LDLW 16
43 CONST 9
44 JNEQ L2
45 ! print_num(n); newline()
46 LDLW 20
47 CONST 0
48 GLOBAL lib.print_num
49 PCALL 1
50 CONST 0
51 GLOBAL lib.newline
52 PCALL 0
53 JUMP L3
54 LABEL L2
55 ! d := 1;
56 CONST 1
57 STLW -4
58 ! while d < 10 do
59 JUMP L5
60 LABEL L4
61 ! n1 := 10 * n + d;
62 CONST 10
63 LDLW 20
64 TIMES
65 LDLW -4
66 PLUS
67 STLW -8
68 ! if (n1 mod (k+1) = 0) and avail(d) then
69 LDLW -8
70 LDLW 16
71 CONST 1
72 PLUS
73 MOD
74 JEQZ L10
75 JUMP L9
76 LABEL L10
77 LDLW -4
78 LDLW 28
79 LDLW 24
80 PCALLW 1
81 JNEQZ L7
82 JUMP L9
83 LABEL L7
84 ! search(k+1, n1, avail1)
85 LOCAL 0
86 GLOBAL _avail1
87 LDLW -8
88 LDLW 16
89 CONST 1
90 PLUS
91 CONST 0
92 GLOBAL _search
93 PCALL 4
94 LABEL L9
95 ! d := d+1
96 LDLW -4
97 CONST 1
98 PLUS
99 STLW -4
100 LABEL L5
101 LDLW -4
102 CONST 10
103 JLT L4
104 LABEL L3
105 RETURN
106 END
107
108 PROC _avail1 0 0 0
109 ! return (x <> d) and avail(x)
110 LDLW 16
111 LDLW 12
112 LDNW -4
113 JEQ L12
114 LDLW 16
115 LDLW 12
116 LDNW 28
117 LDLW 12
118 LDNW 24
119 PCALLW 1
120 JNEQZ L11
121 JUMP L12
122 LABEL L11
123 CONST 1
124 JUMP L13
125 LABEL L12
126 CONST 0
127 LABEL L13
128 RETURNW
129 END
130
131 PROC _avail0 0 0 0
132 ! return true
133 CONST 1
134 RETURNW
135 END
136
137 PROC MAIN 0 0 0
138 ! search(0, 0, avail0)
139 CONST 0
140 GLOBAL _avail0
141 CONST 0
142 CONST 0
143 CONST 0
144 GLOBAL _search
145 PCALL 4
146 RETURN
147 END
148
149 ! End
150 ]]*)