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