### comparison lab4/test/mutual.p @ 0:bfdcc3820b32

Basis
author Mike Spivey Thu, 05 Oct 2017 08:04:15 +0100
comparison
equal inserted replaced
-1:000000000000 0:bfdcc3820b32
1 (* Mutual recursion *)
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 @ picoPascal compiler output
50 .include "fixup.s"
51 .global pmain
52
53 @ proc flip(i: integer): integer;
54 .text
55 _flip:
56 mov ip, sp
57 stmfd sp!, {r0-r1}
58 stmfd sp!, {r4-r10, fp, ip, lr}
59 mov fp, sp
60 @ if i = 0 then
61 ldr r0, [fp, #40]
62 cmp r0, #0
63 bne .L7
64 @ r := 1
65 mov r4, #1
66 b .L8
67 .L7:
68 @ r := 2 * flop(i-1)
69 ldr r0, [fp, #40]
70 sub r0, r0, #1
71 bl _flop
72 lsl r4, r0, #1
73 .L8:
74 @ print_string("flip("); print_num(i);
75 mov r1, #5
76 set r0, g1
77 bl print_string
78 ldr r0, [fp, #40]
79 bl print_num
80 @ print_string(") = "); print_num(r);
81 mov r1, #4
82 set r0, g2
83 bl print_string
84 mov r0, r4
85 bl print_num
86 @ newline();
87 bl newline
88 @ return r
89 mov r0, r4
90 ldmfd fp, {r4-r10, fp, sp, pc}
91 .ltorg
92
93 @ proc flop(i: integer): integer;
94 _flop:
95 mov ip, sp
96 stmfd sp!, {r0-r1}
97 stmfd sp!, {r4-r10, fp, ip, lr}
98 mov fp, sp
99 @ if i = 0 then
100 ldr r0, [fp, #40]
101 cmp r0, #0
102 bne .L11
103 @ r := 1
104 mov r4, #1
105 b .L12
106 .L11:
107 @ r := flip(i-1) + k
108 ldr r0, [fp, #40]
109 sub r0, r0, #1
110 bl _flip
112 .L12:
113 @ print_string("flop("); print_num(i);
114 mov r1, #5
115 set r0, g3
116 bl print_string
117 ldr r0, [fp, #40]
118 bl print_num
119 @ print_string(") = "); print_num(r);
120 mov r1, #4
121 set r0, g4
122 bl print_string
123 mov r0, r4
124 bl print_num
125 @ newline();
126 bl newline
127 @ return r
128 mov r0, r4
129 ldmfd fp, {r4-r10, fp, sp, pc}
130 .ltorg
131
132 pmain:
133 mov ip, sp
134 stmfd sp!, {r4-r10, fp, ip, lr}
135 mov fp, sp
136 @ print_num(flip(5));
137 mov r0, #5
138 bl _flip
139 bl print_num
140 @ newline()
141 bl newline
142 ldmfd fp, {r4-r10, fp, sp, pc}
143 .ltorg
144
145 .data
146 g1:
147 .byte 102, 108, 105, 112, 40
148 .byte 0
149 g2:
150 .byte 41, 32, 61, 32
151 .byte 0
152 g3:
153 .byte 102, 108, 111, 112, 40
154 .byte 0
155 g4:
156 .byte 41, 32, 61, 32
157 .byte 0
158 @ End
159 ]]*)