annotate lab4/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
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 (* Mutual recursion *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 proc flip(i: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 var r: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 if i = 0 then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 r := 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 else
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 r := 2 * flop(i-1)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 print_string("flip("); print_num(i);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 print_string(") = "); print_num(r);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 newline();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 return r
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 proc flop(i: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 var r: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 if i = 0 then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 r := 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 else
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 r := flip(i-1) + k
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 print_string("flop("); print_num(i);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 print_string(") = "); print_num(r);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 newline();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 return r
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 const k = 5;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 print_num(flip(5));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 end.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 flop(0) = 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 flip(1) = 2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 flop(2) = 7
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 flip(3) = 14
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 flop(4) = 19
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 flip(5) = 38
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 38
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 >>*)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 (*[[
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 @ picoPascal compiler output
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 .include "fixup.s"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 .global pmain
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 @ proc flip(i: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 .text
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 _flip:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 stmfd sp!, {r0-r1}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 @ if i = 0 then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 cmp r0, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 bne .L7
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 @ r := 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 mov r4, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 b .L8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 .L7:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 @ r := 2 * flop(i-1)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 sub r0, r0, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 bl _flop
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 lsl r4, r0, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 .L8:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 @ print_string("flip("); print_num(i);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 mov r1, #5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 set r0, g1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 bl print_string
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 @ print_string(") = "); print_num(r);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 mov r1, #4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 set r0, g2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 bl print_string
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 mov r0, r4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 @ newline();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 bl newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 @ return r
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 mov r0, r4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 @ proc flop(i: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94 _flop:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
95 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
96 stmfd sp!, {r0-r1}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
97 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
98 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
99 @ if i = 0 then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
100 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
101 cmp r0, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
102 bne .L11
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
103 @ r := 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
104 mov r4, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
105 b .L12
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
106 .L11:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
107 @ r := flip(i-1) + k
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
108 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
109 sub r0, r0, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
110 bl _flip
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
111 add r4, r0, #5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
112 .L12:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
113 @ print_string("flop("); print_num(i);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
114 mov r1, #5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
115 set r0, g3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
116 bl print_string
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
117 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
118 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
119 @ print_string(") = "); print_num(r);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
120 mov r1, #4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
121 set r0, g4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
122 bl print_string
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
123 mov r0, r4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
124 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
125 @ newline();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
126 bl newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
127 @ return r
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
128 mov r0, r4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
129 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
130 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
131
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
132 pmain:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
133 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
134 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
135 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
136 @ print_num(flip(5));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
137 mov r0, #5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
138 bl _flip
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
139 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
140 @ newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
141 bl newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
142 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
143 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
144
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
145 .data
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
146 g1:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
147 .byte 102, 108, 105, 112, 40
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
148 .byte 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
149 g2:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
150 .byte 41, 32, 61, 32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
151 .byte 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
152 g3:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
153 .byte 102, 108, 111, 112, 40
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
154 .byte 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
155 g4:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
156 .byte 41, 32, 61, 32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
157 .byte 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
158 @ End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
159 ]]*)