comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 (* The usual recursive factorial *)
2
3 proc fac(n: integer): integer;
4 begin
5 if n = 0 then
6 return 1
7 else
8 return n * fac(n-1)
9 end
10 end;
11
12 var f: integer;
13
14 begin
15 f := fac(10);
16 print_num(f);
17 newline()
18 end.
19
20 (*<<
21 3628800
22 >>*)
23
24 (*[[
25 @ picoPascal compiler output
26 .include "fixup.s"
27 .global pmain
28
29 @ proc fac(n: integer): integer;
30 .text
31 _fac:
32 mov ip, sp
33 stmfd sp!, {r0-r1}
34 stmfd sp!, {r4-r10, fp, ip, lr}
35 mov fp, sp
36 @ if n = 0 then
37 ldr r0, [fp, #40]
38 cmp r0, #0
39 bne .L3
40 @ return 1
41 mov r0, #1
42 b .L1
43 .L3:
44 @ return n * fac(n-1)
45 ldr r4, [fp, #40]
46 sub r0, r4, #1
47 bl _fac
48 mul r0, r4, r0
49 .L1:
50 ldmfd fp, {r4-r10, fp, sp, pc}
51 .ltorg
52
53 pmain:
54 mov ip, sp
55 stmfd sp!, {r4-r10, fp, ip, lr}
56 mov fp, sp
57 @ f := fac(10);
58 mov r0, #10
59 bl _fac
60 set r1, _f
61 str r0, [r1]
62 @ print_num(f);
63 bl print_num
64 @ newline()
65 bl newline
66 ldmfd fp, {r4-r10, fp, sp, pc}
67 .ltorg
68
69 .comm _f, 4, 4
70 @ End
71 ]]*)