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