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