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