annotate lab4/test/fib.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 (* Fibonacci numbers by the usual recursion *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 (* fib -- fibonacci numbers *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 proc fib(n: 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 n <= 1 then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 return 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 else
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 return fib(n-1) + fib(n-2)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 end
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(fib(6)); 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 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 13
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 >>*)
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 @ picoPascal compiler output
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 .include "fixup.s"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 .global pmain
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 @ proc fib(n: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 .text
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 _fib:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 stmfd sp!, {r0-r1}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 @ if n <= 1 then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 cmp r0, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 bgt .L3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 @ return 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 mov r0, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 b .L1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 .L3:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 @ return fib(n-1) + fib(n-2)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 sub r0, r0, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 bl _fib
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 mov r4, r0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 sub r0, r0, #2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 bl _fib
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 add r0, r4, r0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 .L1:
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(fib(6)); newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 mov r0, #6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 bl _fib
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 bl newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 @ End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 ]]*)