comparison lab4/test/choices2.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 (* Enumerate (n choose k) choices by recursion *)
2
3 const letters = "abcdef";
4
5 var buf: array 10 of char;
6
7 (* Complete choice with k items chosen from [0..n) *)
8 proc choose(k, n: integer);
9 begin
10 if k <= n then
11 if k = 0 then
12 print_string(buf); newline()
13 else
14 choose(k, n-1);
15 buf[k-1] := letters[n-1];
16 choose(k-1, n-1);
17 end
18 end
19 end;
20
21 begin
22 choose(3, 6)
23 end.
24
25 (*<<
26 abc
27 abd
28 acd
29 bcd
30 abe
31 ace
32 bce
33 ade
34 bde
35 cde
36 abf
37 acf
38 bcf
39 adf
40 bdf
41 cdf
42 aef
43 bef
44 cef
45 def
46 >>*)
47
48 (*[[
49 @ picoPascal compiler output
50 .include "fixup.s"
51 .global pmain
52
53 @ proc choose(k, n: integer);
54 .text
55 _choose:
56 mov ip, sp
57 stmfd sp!, {r0-r1}
58 stmfd sp!, {r4-r10, fp, ip, lr}
59 mov fp, sp
60 @ if k <= n then
61 ldr r4, [fp, #40]
62 ldr r0, [fp, #44]
63 cmp r4, r0
64 bgt .L5
65 @ if k = 0 then
66 cmp r4, #0
67 bne .L7
68 @ print_string(buf); newline()
69 mov r1, #10
70 set r0, _buf
71 bl print_string
72 bl newline
73 b .L5
74 .L7:
75 @ choose(k, n-1);
76 ldr r0, [fp, #44]
77 sub r1, r0, #1
78 ldr r0, [fp, #40]
79 bl _choose
80 @ buf[k-1] := letters[n-1];
81 ldr r4, [fp, #44]
82 ldr r5, [fp, #40]
83 set r0, g1
84 add r0, r0, r4
85 ldrb r0, [r0, #-1]
86 set r1, _buf
87 add r1, r1, r5
88 strb r0, [r1, #-1]
89 @ choose(k-1, n-1);
90 sub r1, r4, #1
91 sub r0, r5, #1
92 bl _choose
93 .L5:
94 ldmfd fp, {r4-r10, fp, sp, pc}
95 .ltorg
96
97 pmain:
98 mov ip, sp
99 stmfd sp!, {r4-r10, fp, ip, lr}
100 mov fp, sp
101 @ choose(3, 6)
102 mov r1, #6
103 mov r0, #3
104 bl _choose
105 ldmfd fp, {r4-r10, fp, sp, pc}
106 .ltorg
107
108 .comm _buf, 10, 4
109 .data
110 g1:
111 .byte 97, 98, 99, 100, 101, 102
112 .byte 0
113 @ End
114 ]]*)