comparison ppc/test/fib.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 (* fib.p *)
2
3 (* fib -- fibonacci numbers *)
4 proc fib(n: integer): integer;
5 begin
6 if n <= 1 then
7 return 1
8 else
9 return fib(n-1) + fib(n-2)
10 end
11 end;
12
13 begin
14 print_num(fib(6)); newline()
15 end.
16
17 (*<<
18 13
19 >>*)
20
21 (*[[
22 MODULE Main 0 0
23 IMPORT Lib 0
24 ENDHDR
25
26 PROC _fib 0 0 0
27 ! if n <= 1 then
28 LDLW 16
29 CONST 1
30 JGT L2
31 ! return 1
32 CONST 1
33 RETURNW
34 LABEL L2
35 ! return fib(n-1) + fib(n-2)
36 LDLW 16
37 CONST 1
38 MINUS
39 CONST 0
40 GLOBAL _fib
41 PCALLW 1
42 LDLW 16
43 CONST 2
44 MINUS
45 CONST 0
46 GLOBAL _fib
47 PCALLW 1
48 PLUS
49 RETURNW
50 END
51
52 PROC MAIN 0 0 0
53 ! print_num(fib(6)); newline()
54 CONST 6
55 CONST 0
56 GLOBAL _fib
57 PCALLW 1
58 CONST 0
59 GLOBAL lib.print_num
60 PCALL 1
61 CONST 0
62 GLOBAL lib.newline
63 PCALL 0
64 RETURN
65 END
66
67 ! End
68 ]]*)