annotate lab4/test/queens2.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 (* N queens with array of choices *)
mike@0 2
mike@0 3 const N = 8;
mike@0 4
mike@0 5 proc queens(k: integer; var choice: array N of integer);
mike@0 6 var y, j, q: integer; ok: boolean;
mike@0 7 begin
mike@0 8 if k = N then
mike@0 9 print(choice)
mike@0 10 else
mike@0 11 y := 0;
mike@0 12 while y < N do
mike@0 13 j := 0; ok := true;
mike@0 14 while ok and (j < k) do
mike@0 15 q := choice[j];
mike@0 16 ok := (q <> y) and (q+j <> y+k) and (q-j <> y-k);
mike@0 17 j := j+1
mike@0 18 end;
mike@0 19 if ok then
mike@0 20 choice[k] := y;
mike@0 21 queens(k+1, choice)
mike@0 22 end;
mike@0 23 y := y+1
mike@0 24 end
mike@0 25 end
mike@0 26 end;
mike@0 27
mike@0 28 proc print(var choice: array N of integer);
mike@0 29 var x: integer;
mike@0 30 begin
mike@0 31 x := 0;
mike@0 32 while x < N do
mike@0 33 print_num(choice[x]+1);
mike@0 34 x := x+1
mike@0 35 end;
mike@0 36 newline()
mike@0 37 end;
mike@0 38
mike@0 39 var choice: array N of integer;
mike@0 40
mike@0 41 begin
mike@0 42 queens(0, choice)
mike@0 43 end.
mike@0 44
mike@0 45 (*<<
mike@0 46 15863724
mike@0 47 16837425
mike@0 48 17468253
mike@0 49 17582463
mike@0 50 24683175
mike@0 51 25713864
mike@0 52 25741863
mike@0 53 26174835
mike@0 54 26831475
mike@0 55 27368514
mike@0 56 27581463
mike@0 57 28613574
mike@0 58 31758246
mike@0 59 35281746
mike@0 60 35286471
mike@0 61 35714286
mike@0 62 35841726
mike@0 63 36258174
mike@0 64 36271485
mike@0 65 36275184
mike@0 66 36418572
mike@0 67 36428571
mike@0 68 36814752
mike@0 69 36815724
mike@0 70 36824175
mike@0 71 37285146
mike@0 72 37286415
mike@0 73 38471625
mike@0 74 41582736
mike@0 75 41586372
mike@0 76 42586137
mike@0 77 42736815
mike@0 78 42736851
mike@0 79 42751863
mike@0 80 42857136
mike@0 81 42861357
mike@0 82 46152837
mike@0 83 46827135
mike@0 84 46831752
mike@0 85 47185263
mike@0 86 47382516
mike@0 87 47526138
mike@0 88 47531682
mike@0 89 48136275
mike@0 90 48157263
mike@0 91 48531726
mike@0 92 51468273
mike@0 93 51842736
mike@0 94 51863724
mike@0 95 52468317
mike@0 96 52473861
mike@0 97 52617483
mike@0 98 52814736
mike@0 99 53168247
mike@0 100 53172864
mike@0 101 53847162
mike@0 102 57138642
mike@0 103 57142863
mike@0 104 57248136
mike@0 105 57263148
mike@0 106 57263184
mike@0 107 57413862
mike@0 108 58413627
mike@0 109 58417263
mike@0 110 61528374
mike@0 111 62713584
mike@0 112 62714853
mike@0 113 63175824
mike@0 114 63184275
mike@0 115 63185247
mike@0 116 63571428
mike@0 117 63581427
mike@0 118 63724815
mike@0 119 63728514
mike@0 120 63741825
mike@0 121 64158273
mike@0 122 64285713
mike@0 123 64713528
mike@0 124 64718253
mike@0 125 68241753
mike@0 126 71386425
mike@0 127 72418536
mike@0 128 72631485
mike@0 129 73168524
mike@0 130 73825164
mike@0 131 74258136
mike@0 132 74286135
mike@0 133 75316824
mike@0 134 82417536
mike@0 135 82531746
mike@0 136 83162574
mike@0 137 84136275
mike@0 138 >>*)
mike@0 139
mike@0 140 (*[[
mike@0 141 @ picoPascal compiler output
mike@0 142 .include "fixup.s"
mike@0 143 .global pmain
mike@0 144
mike@0 145 @ proc queens(k: integer; var choice: array N of integer);
mike@0 146 .text
mike@0 147 _queens:
mike@0 148 mov ip, sp
mike@0 149 stmfd sp!, {r0-r1}
mike@0 150 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 151 mov fp, sp
mike@0 152 sub sp, sp, #8
mike@0 153 @ if k = N then
mike@0 154 ldr r0, [fp, #40]
mike@0 155 cmp r0, #8
mike@0 156 bne .L3
mike@0 157 @ print(choice)
mike@0 158 ldr r0, [fp, #44]
mike@0 159 bl _print
mike@0 160 b .L1
mike@0 161 .L3:
mike@0 162 @ y := 0;
mike@0 163 mov r4, #0
mike@0 164 .L5:
mike@0 165 @ while y < N do
mike@0 166 cmp r4, #8
mike@0 167 bge .L1
mike@0 168 @ j := 0; ok := true;
mike@0 169 mov r5, #0
mike@0 170 mov r0, #1
mike@0 171 strb r0, [fp, #-1]
mike@0 172 .L8:
mike@0 173 @ while ok and (j < k) do
mike@0 174 ldrb r0, [fp, #-1]
mike@0 175 cmp r0, #0
mike@0 176 beq .L10
mike@0 177 ldr r7, [fp, #40]
mike@0 178 cmp r5, r7
mike@0 179 bge .L10
mike@0 180 @ q := choice[j];
mike@0 181 ldr r0, [fp, #44]
mike@0 182 lsl r1, r5, #2
mike@0 183 add r0, r0, r1
mike@0 184 ldr r6, [r0]
mike@0 185 @ ok := (q <> y) and (q+j <> y+k) and (q-j <> y-k);
mike@0 186 cmp r6, r4
mike@0 187 mov r0, #0
mike@0 188 movne r0, #1
mike@0 189 add r1, r6, r5
mike@0 190 add r2, r4, r7
mike@0 191 cmp r1, r2
mike@0 192 mov r1, #0
mike@0 193 movne r1, #1
mike@0 194 and r0, r0, r1
mike@0 195 sub r1, r6, r5
mike@0 196 sub r2, r4, r7
mike@0 197 cmp r1, r2
mike@0 198 mov r1, #0
mike@0 199 movne r1, #1
mike@0 200 and r0, r0, r1
mike@0 201 strb r0, [fp, #-1]
mike@0 202 @ j := j+1
mike@0 203 add r5, r5, #1
mike@0 204 b .L8
mike@0 205 .L10:
mike@0 206 @ if ok then
mike@0 207 ldrb r0, [fp, #-1]
mike@0 208 cmp r0, #0
mike@0 209 beq .L14
mike@0 210 @ choice[k] := y;
mike@0 211 ldr r0, [fp, #44]
mike@0 212 ldr r1, [fp, #40]
mike@0 213 lsl r1, r1, #2
mike@0 214 add r0, r0, r1
mike@0 215 str r4, [r0]
mike@0 216 @ queens(k+1, choice)
mike@0 217 ldr r1, [fp, #44]
mike@0 218 ldr r0, [fp, #40]
mike@0 219 add r0, r0, #1
mike@0 220 bl _queens
mike@0 221 .L14:
mike@0 222 @ y := y+1
mike@0 223 add r4, r4, #1
mike@0 224 b .L5
mike@0 225 .L1:
mike@0 226 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 227 .ltorg
mike@0 228
mike@0 229 @ proc print(var choice: array N of integer);
mike@0 230 _print:
mike@0 231 mov ip, sp
mike@0 232 stmfd sp!, {r0-r1}
mike@0 233 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 234 mov fp, sp
mike@0 235 @ x := 0;
mike@0 236 mov r4, #0
mike@0 237 .L16:
mike@0 238 @ while x < N do
mike@0 239 cmp r4, #8
mike@0 240 bge .L18
mike@0 241 @ print_num(choice[x]+1);
mike@0 242 ldr r0, [fp, #40]
mike@0 243 lsl r1, r4, #2
mike@0 244 add r0, r0, r1
mike@0 245 ldr r0, [r0]
mike@0 246 add r0, r0, #1
mike@0 247 bl print_num
mike@0 248 @ x := x+1
mike@0 249 add r4, r4, #1
mike@0 250 b .L16
mike@0 251 .L18:
mike@0 252 @ newline()
mike@0 253 bl newline
mike@0 254 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 255 .ltorg
mike@0 256
mike@0 257 pmain:
mike@0 258 mov ip, sp
mike@0 259 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 260 mov fp, sp
mike@0 261 @ queens(0, choice)
mike@0 262 set r1, _choice
mike@0 263 mov r0, #0
mike@0 264 bl _queens
mike@0 265 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 266 .ltorg
mike@0 267
mike@0 268 .comm _choice, 32, 4
mike@0 269 @ End
mike@0 270 ]]*)