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