annotate lab4/test/prob3-3.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 (* From a problem sheet *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 proc double(x: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 return x + x
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 proc apply3(proc f(x:integer): integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 return f(3)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 print_num(apply3(double));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 end.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 >>*)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 (*[[
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 @ picoPascal compiler output
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 .include "fixup.s"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 .global pmain
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 @ proc double(x: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 .text
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 _double:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 stmfd sp!, {r0-r1}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 @ return x + x
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 ldr r4, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 add r0, r4, r4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 @ proc apply3(proc f(x:integer): integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 _apply3:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 stmfd sp!, {r0-r1}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 @ return f(3)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 mov r0, #3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 ldr r10, [fp, #44]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 ldr r1, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 blx r1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 pmain:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 @ print_num(apply3(double));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 mov r1, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 set r0, _double
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 bl _apply3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 @ newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 bl newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 @ End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 ]]*)