annotate lab4/test/fib.p @ 1:b5139af1a420 tip basis

Fixed permissions on compile scripts
author Mike Spivey <mike@cs.ox.ac.uk>
date Fri, 13 Oct 2017 17:27:58 +0100
parents bfdcc3820b32
children
rev   line source
mike@0 1 (* Fibonacci numbers by the usual recursion *)
mike@0 2
mike@0 3 (* fib -- fibonacci numbers *)
mike@0 4 proc fib(n: integer): integer;
mike@0 5 begin
mike@0 6 if n <= 1 then
mike@0 7 return 1
mike@0 8 else
mike@0 9 return fib(n-1) + fib(n-2)
mike@0 10 end
mike@0 11 end;
mike@0 12
mike@0 13 begin
mike@0 14 print_num(fib(6)); newline()
mike@0 15 end.
mike@0 16
mike@0 17 (*<<
mike@0 18 13
mike@0 19 >>*)
mike@0 20
mike@0 21 (*[[
mike@0 22 @ picoPascal compiler output
mike@0 23 .include "fixup.s"
mike@0 24 .global pmain
mike@0 25
mike@0 26 @ proc fib(n: integer): integer;
mike@0 27 .text
mike@0 28 _fib:
mike@0 29 mov ip, sp
mike@0 30 stmfd sp!, {r0-r1}
mike@0 31 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 32 mov fp, sp
mike@0 33 @ if n <= 1 then
mike@0 34 ldr r0, [fp, #40]
mike@0 35 cmp r0, #1
mike@0 36 bgt .L3
mike@0 37 @ return 1
mike@0 38 mov r0, #1
mike@0 39 b .L1
mike@0 40 .L3:
mike@0 41 @ return fib(n-1) + fib(n-2)
mike@0 42 ldr r0, [fp, #40]
mike@0 43 sub r0, r0, #1
mike@0 44 bl _fib
mike@0 45 mov r4, r0
mike@0 46 ldr r0, [fp, #40]
mike@0 47 sub r0, r0, #2
mike@0 48 bl _fib
mike@0 49 add r0, r4, r0
mike@0 50 .L1:
mike@0 51 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 52 .ltorg
mike@0 53
mike@0 54 pmain:
mike@0 55 mov ip, sp
mike@0 56 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 57 mov fp, sp
mike@0 58 @ print_num(fib(6)); newline()
mike@0 59 mov r0, #6
mike@0 60 bl _fib
mike@0 61 bl print_num
mike@0 62 bl newline
mike@0 63 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 64 .ltorg
mike@0 65
mike@0 66 @ End
mike@0 67 ]]*)