comparison ppc/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 proc choose(n, k: integer): integer;
2 begin
3 if n = 0 then
4 if k = 0 then
5 return 1
6 else
7 return 0
8 end
9 else
10 return choose(n-1, k-1) + choose(n-1, k)
11 end
12 end;
13
14 begin
15 print_num(choose(6,4));
16 newline()
17 end.
18
19 (*<<
20 15
21 >>*)
22
23 (*[[
24 MODULE Main 0 0
25 IMPORT Lib 0
26 ENDHDR
27
28 PROC _choose 0 0 0
29 ! if n = 0 then
30 LDLW 16
31 JEQZ L1
32 JUMP L2
33 LABEL L1
34 ! if k = 0 then
35 LDLW 20
36 JEQZ L4
37 JUMP L5
38 LABEL L4
39 ! return 1
40 CONST 1
41 RETURNW
42 LABEL L5
43 ! return 0
44 CONST 0
45 RETURNW
46 LABEL L2
47 ! return choose(n-1, k-1) + choose(n-1, k)
48 LDLW 20
49 CONST 1
50 MINUS
51 LDLW 16
52 CONST 1
53 MINUS
54 CONST 0
55 GLOBAL _choose
56 PCALLW 2
57 LDLW 20
58 LDLW 16
59 CONST 1
60 MINUS
61 CONST 0
62 GLOBAL _choose
63 PCALLW 2
64 PLUS
65 RETURNW
66 END
67
68 PROC MAIN 0 0 0
69 ! print_num(choose(6,4));
70 CONST 4
71 CONST 6
72 CONST 0
73 GLOBAL _choose
74 PCALLW 2
75 CONST 0
76 GLOBAL lib.print_num
77 PCALL 1
78 ! newline()
79 CONST 0
80 GLOBAL lib.newline
81 PCALL 0
82 RETURN
83 END
84
85 ! End
86 ]]*)