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