comparison ppc/test/mutual.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 (* mutual.p *)
2
3 proc flip(i: integer): integer;
4 var r: integer;
5 begin
6 if i = 0 then
7 r := 1
8 else
9 r := 2 * flop(i-1)
10 end;
11 print_string("flip("); print_num(i);
12 print_string(") = "); print_num(r);
13 newline();
14 return r
15 end;
16
17 proc flop(i: integer): integer;
18 var r: integer;
19 begin
20 if i = 0 then
21 r := 1
22 else
23 r := flip(i-1) + k
24 end;
25 print_string("flop("); print_num(i);
26 print_string(") = "); print_num(r);
27 newline();
28 return r
29 end;
30
31 const k = 5;
32
33 begin
34 print_num(flip(5));
35 newline()
36 end.
37
38 (*<<
39 flop(0) = 1
40 flip(1) = 2
41 flop(2) = 7
42 flip(3) = 14
43 flop(4) = 19
44 flip(5) = 38
45 38
46 >>*)
47
48 (*[[
49 MODULE Main 0 0
50 IMPORT Lib 0
51 ENDHDR
52
53 PROC _flip 4 0 0
54 ! if i = 0 then
55 LDLW 16
56 JEQZ L5
57 JUMP L6
58 LABEL L5
59 ! r := 1
60 CONST 1
61 STLW -4
62 JUMP L7
63 LABEL L6
64 ! r := 2 * flop(i-1)
65 CONST 2
66 LDLW 16
67 CONST 1
68 MINUS
69 CONST 0
70 GLOBAL _flop
71 PCALLW 1
72 TIMES
73 STLW -4
74 LABEL L7
75 ! print_string("flip("); print_num(i);
76 CONST 5
77 GLOBAL g1
78 CONST 0
79 GLOBAL lib.print_string
80 PCALL 2
81 LDLW 16
82 CONST 0
83 GLOBAL lib.print_num
84 PCALL 1
85 ! print_string(") = "); print_num(r);
86 CONST 4
87 GLOBAL g2
88 CONST 0
89 GLOBAL lib.print_string
90 PCALL 2
91 LDLW -4
92 CONST 0
93 GLOBAL lib.print_num
94 PCALL 1
95 ! newline();
96 CONST 0
97 GLOBAL lib.newline
98 PCALL 0
99 ! return r
100 LDLW -4
101 RETURNW
102 END
103
104 PROC _flop 4 0 0
105 ! if i = 0 then
106 LDLW 16
107 JEQZ L8
108 JUMP L9
109 LABEL L8
110 ! r := 1
111 CONST 1
112 STLW -4
113 JUMP L10
114 LABEL L9
115 ! r := flip(i-1) + k
116 LDLW 16
117 CONST 1
118 MINUS
119 CONST 0
120 GLOBAL _flip
121 PCALLW 1
122 CONST 5
123 PLUS
124 STLW -4
125 LABEL L10
126 ! print_string("flop("); print_num(i);
127 CONST 5
128 GLOBAL g3
129 CONST 0
130 GLOBAL lib.print_string
131 PCALL 2
132 LDLW 16
133 CONST 0
134 GLOBAL lib.print_num
135 PCALL 1
136 ! print_string(") = "); print_num(r);
137 CONST 4
138 GLOBAL g4
139 CONST 0
140 GLOBAL lib.print_string
141 PCALL 2
142 LDLW -4
143 CONST 0
144 GLOBAL lib.print_num
145 PCALL 1
146 ! newline();
147 CONST 0
148 GLOBAL lib.newline
149 PCALL 0
150 ! return r
151 LDLW -4
152 RETURNW
153 END
154
155 PROC MAIN 0 0 0
156 ! print_num(flip(5));
157 CONST 5
158 CONST 0
159 GLOBAL _flip
160 PCALLW 1
161 CONST 0
162 GLOBAL lib.print_num
163 PCALL 1
164 ! newline()
165 CONST 0
166 GLOBAL lib.newline
167 PCALL 0
168 RETURN
169 END
170
171 ! String "flip("
172 DEFINE g1
173 STRING 666C69702800
174
175 ! String ") = "
176 DEFINE g2
177 STRING 29203D2000
178
179 ! String "flop("
180 DEFINE g3
181 STRING 666C6F702800
182
183 ! String ") = "
184 DEFINE g4
185 STRING 29203D2000
186
187 ! End
188 ]]*)