comparison lab4/test/choices.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 with nested functions *)
2
3 const letters = "abcdef";
4
5 (* Complete suffix with k items chosen from [0..n) *)
6 proc choose(k, n: integer; proc suffix());
7 proc suffix1();
8 begin
9 print_char(letters[n-1]); suffix()
10 end;
11 begin
12 if k <= n then
13 if k = 0 then
14 suffix(); newline()
15 else
16 choose(k, n-1, suffix);
17 choose(k-1, n-1, suffix1)
18 end
19 end
20 end;
21
22 proc null(); begin end;
23
24 begin
25 choose(3, 6, null)
26 end.
27
28 (*<<
29 abc
30 abd
31 acd
32 bcd
33 abe
34 ace
35 bce
36 ade
37 bde
38 cde
39 abf
40 acf
41 bcf
42 adf
43 bdf
44 cdf
45 aef
46 bef
47 cef
48 def
49 >>*)
50
51 (*[[
52 @ picoPascal compiler output
53 .include "fixup.s"
54 .global pmain
55
56 @ proc choose(k, n: integer; proc suffix());
57 .text
58 _choose:
59 mov ip, sp
60 stmfd sp!, {r0-r3}
61 stmfd sp!, {r4-r10, fp, ip, lr}
62 mov fp, sp
63 @ if k <= n then
64 ldr r4, [fp, #40]
65 ldr r0, [fp, #44]
66 cmp r4, r0
67 bgt .L2
68 @ if k = 0 then
69 cmp r4, #0
70 bne .L7
71 @ suffix(); newline()
72 ldr r10, [fp, #52]
73 ldr r0, [fp, #48]
74 blx r0
75 bl newline
76 b .L2
77 .L7:
78 @ choose(k, n-1, suffix);
79 ldr r3, [fp, #52]
80 ldr r2, [fp, #48]
81 ldr r0, [fp, #44]
82 sub r1, r0, #1
83 ldr r0, [fp, #40]
84 bl _choose
85 @ choose(k-1, n-1, suffix1)
86 mov r3, fp
87 set r2, _suffix1
88 ldr r0, [fp, #44]
89 sub r1, r0, #1
90 ldr r0, [fp, #40]
91 sub r0, r0, #1
92 bl _choose
93 .L2:
94 ldmfd fp, {r4-r10, fp, sp, pc}
95 .ltorg
96
97 @ proc suffix1();
98 _suffix1:
99 mov ip, sp
100 stmfd sp!, {r4-r10, fp, ip, lr}
101 mov fp, sp
102 @ print_char(letters[n-1]); suffix()
103 set r0, g1
104 ldr r1, [fp, #24]
105 ldr r1, [r1, #44]
106 add r0, r0, r1
107 ldrb r0, [r0, #-1]
108 bl print_char
109 ldr r4, [fp, #24]
110 ldr r10, [r4, #52]
111 ldr r0, [r4, #48]
112 blx r0
113 ldmfd fp, {r4-r10, fp, sp, pc}
114 .ltorg
115
116 @ proc null(); begin end;
117 _null:
118 mov ip, sp
119 stmfd sp!, {r4-r10, fp, ip, lr}
120 mov fp, sp
121 ldmfd fp, {r4-r10, fp, sp, pc}
122 .ltorg
123
124 pmain:
125 mov ip, sp
126 stmfd sp!, {r4-r10, fp, ip, lr}
127 mov fp, sp
128 @ choose(3, 6, null)
129 mov r3, #0
130 set r2, _null
131 mov r1, #6
132 mov r0, #3
133 bl _choose
134 ldmfd fp, {r4-r10, fp, sp, pc}
135 .ltorg
136
137 .data
138 g1:
139 .byte 97, 98, 99, 100, 101, 102
140 .byte 0
141 @ End
142 ]]*)