annotate lab4/test/cpsfac.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 (* Compute factorials using CPS *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 proc fac(n: integer; proc k(r: integer): integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 proc k1(r: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 var r1: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 r1 := n * r;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 print_num(n); print_string(" * "); print_num(r);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 print_string(" = "); print_num(r1); newline();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 return k(r1)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 if n = 0 then return k(1) else return fac(n-1, k1) end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 proc id(r: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 return r
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 print_num(fac(10, id));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 end.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 1 * 1 = 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 2 * 1 = 2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 3 * 2 = 6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 4 * 6 = 24
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 5 * 24 = 120
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 6 * 120 = 720
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 7 * 720 = 5040
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 8 * 5040 = 40320
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 9 * 40320 = 362880
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 10 * 362880 = 3628800
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 3628800
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 >>*)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 (*[[
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 @ picoPascal compiler output
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 .include "fixup.s"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 .global pmain
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 @ proc fac(n: integer; proc k(r: integer): integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 .text
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 _fac:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 stmfd sp!, {r0-r3}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 @ if n = 0 then return k(1) else return fac(n-1, k1) end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 cmp r0, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 bne .L5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 mov r0, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 ldr r10, [fp, #48]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 ldr r1, [fp, #44]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 blx r1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 b .L3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 .L5:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 mov r2, fp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 set r1, _k1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 sub r0, r0, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 bl _fac
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 .L3:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 @ proc k1(r: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 _k1:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 stmfd sp!, {r0-r1}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 @ r1 := n * r;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 ldr r0, [fp, #24]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 ldr r5, [r0, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 mul r4, r5, r0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 @ print_num(n); print_string(" * "); print_num(r);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 mov r0, r5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 mov r1, #3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 set r0, g1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 bl print_string
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 @ print_string(" = "); print_num(r1); newline();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 mov r1, #3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 set r0, g2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 bl print_string
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94 mov r0, r4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
95 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
96 bl newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
97 @ return k(r1)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
98 ldr r5, [fp, #24]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
99 mov r0, r4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
100 ldr r10, [r5, #48]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
101 ldr r1, [r5, #44]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
102 blx r1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
103 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
104 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
105
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
106 @ proc id(r: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
107 _id:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
108 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
109 stmfd sp!, {r0-r1}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
110 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
111 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
112 @ return r
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
113 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
114 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
115 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
116
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
117 pmain:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
118 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
119 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
120 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
121 @ print_num(fac(10, id));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
122 mov r2, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
123 set r1, _id
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
124 mov r0, #10
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
125 bl _fac
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
126 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
127 @ newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
128 bl newline
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 .data
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
133 g1:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
134 .byte 32, 42, 32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
135 .byte 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
136 g2:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
137 .byte 32, 61, 32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
138 .byte 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
139 @ End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
140 ]]*)