annotate lab4/test/flip.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 (* Nested functions with 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(x: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 proc flop(y: integer): 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 y = 0 then return 1 else return flip(y-1) + x end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 if x = 0 then return 1 else return 2 * flop(x-1) end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 print_num(flip(5));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 newline()
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 (* flip(5) = 2 * flop(4) = 2 * (flip(3) + 5)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 = 4 * flop(2) + 10 = 4 * (flip(1) + 3) + 10
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 = 8 * flop(0) + 22 = 30 *)
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 30
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 >>*)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 (*[[
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 @ picoPascal compiler output
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 .include "fixup.s"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 .global pmain
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 @ proc flip(x: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 .text
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 _flip:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 stmfd sp!, {r0-r1}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 @ if x = 0 then return 1 else return 2 * flop(x-1) end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 cmp r0, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 bne .L3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 mov r0, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 b .L1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 .L3:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 sub r0, r0, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 mov r10, fp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 bl _flop
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 lsl r0, r0, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 .L1:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 @ proc flop(y: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 _flop:
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!, {r0-r1}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 @ if y = 0 then return 1 else return flip(y-1) + x end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 cmp r0, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 bne .L7
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 mov r0, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 b .L5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 .L7:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 sub r0, r0, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 bl _flip
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 ldr r1, [fp, #24]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 ldr r1, [r1, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 add r0, r0, r1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 .L5:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 pmain:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 @ print_num(flip(5));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 mov r0, #5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 bl _flip
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 @ newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 bl newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 @ End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 ]]*)