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