annotate lab4/test/choose.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 (* Compute (n choose k) by recursion *)
mike@0 2
mike@0 3 proc choose(n, k: integer): integer;
mike@0 4 begin
mike@0 5 if n = 0 then
mike@0 6 if k = 0 then
mike@0 7 return 1
mike@0 8 else
mike@0 9 return 0
mike@0 10 end
mike@0 11 else
mike@0 12 return choose(n-1, k-1) + choose(n-1, k)
mike@0 13 end
mike@0 14 end;
mike@0 15
mike@0 16 begin
mike@0 17 print_num(choose(6,4));
mike@0 18 newline()
mike@0 19 end.
mike@0 20
mike@0 21 (*<<
mike@0 22 15
mike@0 23 >>*)
mike@0 24
mike@0 25 (*[[
mike@0 26 @ picoPascal compiler output
mike@0 27 .include "fixup.s"
mike@0 28 .global pmain
mike@0 29
mike@0 30 @ proc choose(n, k: integer): integer;
mike@0 31 .text
mike@0 32 _choose:
mike@0 33 mov ip, sp
mike@0 34 stmfd sp!, {r0-r1}
mike@0 35 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 36 mov fp, sp
mike@0 37 @ if n = 0 then
mike@0 38 ldr r0, [fp, #40]
mike@0 39 cmp r0, #0
mike@0 40 bne .L3
mike@0 41 @ if k = 0 then
mike@0 42 ldr r0, [fp, #44]
mike@0 43 cmp r0, #0
mike@0 44 bne .L6
mike@0 45 @ return 1
mike@0 46 mov r0, #1
mike@0 47 b .L1
mike@0 48 .L6:
mike@0 49 @ return 0
mike@0 50 mov r0, #0
mike@0 51 b .L1
mike@0 52 .L3:
mike@0 53 @ return choose(n-1, k-1) + choose(n-1, k)
mike@0 54 ldr r0, [fp, #44]
mike@0 55 sub r1, r0, #1
mike@0 56 ldr r0, [fp, #40]
mike@0 57 sub r0, r0, #1
mike@0 58 bl _choose
mike@0 59 ldr r1, [fp, #44]
mike@0 60 mov r4, r0
mike@0 61 ldr r0, [fp, #40]
mike@0 62 sub r0, r0, #1
mike@0 63 bl _choose
mike@0 64 add r0, r4, r0
mike@0 65 .L1:
mike@0 66 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 67 .ltorg
mike@0 68
mike@0 69 pmain:
mike@0 70 mov ip, sp
mike@0 71 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 72 mov fp, sp
mike@0 73 @ print_num(choose(6,4));
mike@0 74 mov r1, #4
mike@0 75 mov r0, #6
mike@0 76 bl _choose
mike@0 77 bl print_num
mike@0 78 @ newline()
mike@0 79 bl newline
mike@0 80 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 81 .ltorg
mike@0 82
mike@0 83 @ End
mike@0 84 ]]*)