annotate lab4/test/digits.p @ 1:b5139af1a420 tip basis

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