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