annotate lab4/test/fac.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 (* The usual recursive factorial *)
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): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 if n = 0 then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 return 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 else
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 return n * fac(n-1)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 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 var f: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 f := fac(10);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 print_num(f);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 end.
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 3628800
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 >>*)
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 @ picoPascal compiler output
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 .include "fixup.s"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 .global pmain
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 @ proc fac(n: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 .text
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 _fac:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 stmfd sp!, {r0-r1}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 @ if n = 0 then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 cmp r0, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 bne .L3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 @ return 1
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 @ return n * fac(n-1)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 ldr r4, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 sub r0, r4, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 bl _fac
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 mul r0, r4, r0
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 pmain:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 @ f := fac(10);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 mov r0, #10
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 bl _fac
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 set r1, _f
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 str r0, [r1]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 @ print_num(f);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 @ newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 bl newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 .comm _f, 4, 4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 @ End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 ]]*)