annotate lab4/test/sumpower.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 (* Higher-order functions *)
mike@0 2
mike@0 3 proc sum(a, b: integer; proc f(x: integer): integer): integer;
mike@0 4 var i, s: integer;
mike@0 5 begin
mike@0 6 i := a; s := 0;
mike@0 7 while i <= b do
mike@0 8 s := s + f(i);
mike@0 9 i := i + 1
mike@0 10 end;
mike@0 11 return s
mike@0 12 end;
mike@0 13
mike@0 14 proc sum_powers(a, b, n: integer): integer;
mike@0 15 proc pow(x: integer): integer;
mike@0 16 var j, p: integer;
mike@0 17 begin
mike@0 18 j := 0; p := 1;
mike@0 19 while j < n do
mike@0 20 p := p * x;
mike@0 21 j := j + 1
mike@0 22 end;
mike@0 23 return p
mike@0 24 end;
mike@0 25 begin
mike@0 26 return sum(a, b, pow)
mike@0 27 end;
mike@0 28
mike@0 29 begin
mike@0 30 print_num(sum_powers(1, 10, 3));
mike@0 31 newline()
mike@0 32 end.
mike@0 33
mike@0 34 (*<<
mike@0 35 3025
mike@0 36 >>*)
mike@0 37
mike@0 38 (*[[
mike@0 39 @ picoPascal compiler output
mike@0 40 .include "fixup.s"
mike@0 41 .global pmain
mike@0 42
mike@0 43 @ proc sum(a, b: integer; proc f(x: integer): integer): integer;
mike@0 44 .text
mike@0 45 _sum:
mike@0 46 mov ip, sp
mike@0 47 stmfd sp!, {r0-r3}
mike@0 48 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 49 mov fp, sp
mike@0 50 @ i := a; s := 0;
mike@0 51 ldr r4, [fp, #40]
mike@0 52 mov r5, #0
mike@0 53 .L2:
mike@0 54 @ while i <= b do
mike@0 55 ldr r0, [fp, #44]
mike@0 56 cmp r4, r0
mike@0 57 bgt .L4
mike@0 58 @ s := s + f(i);
mike@0 59 mov r0, r4
mike@0 60 ldr r10, [fp, #52]
mike@0 61 ldr r1, [fp, #48]
mike@0 62 blx r1
mike@0 63 add r5, r5, r0
mike@0 64 @ i := i + 1
mike@0 65 add r4, r4, #1
mike@0 66 b .L2
mike@0 67 .L4:
mike@0 68 @ return s
mike@0 69 mov r0, r5
mike@0 70 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 71 .ltorg
mike@0 72
mike@0 73 @ proc sum_powers(a, b, n: integer): integer;
mike@0 74 _sum_powers:
mike@0 75 mov ip, sp
mike@0 76 stmfd sp!, {r0-r3}
mike@0 77 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 78 mov fp, sp
mike@0 79 @ return sum(a, b, pow)
mike@0 80 mov r3, fp
mike@0 81 set r2, _pow
mike@0 82 ldr r1, [fp, #44]
mike@0 83 ldr r0, [fp, #40]
mike@0 84 bl _sum
mike@0 85 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 86 .ltorg
mike@0 87
mike@0 88 @ proc pow(x: integer): integer;
mike@0 89 _pow:
mike@0 90 mov ip, sp
mike@0 91 stmfd sp!, {r0-r1}
mike@0 92 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 93 mov fp, sp
mike@0 94 @ j := 0; p := 1;
mike@0 95 mov r4, #0
mike@0 96 mov r5, #1
mike@0 97 .L7:
mike@0 98 @ while j < n do
mike@0 99 ldr r0, [fp, #24]
mike@0 100 ldr r0, [r0, #48]
mike@0 101 cmp r4, r0
mike@0 102 bge .L9
mike@0 103 @ p := p * x;
mike@0 104 ldr r0, [fp, #40]
mike@0 105 mul r5, r5, r0
mike@0 106 @ j := j + 1
mike@0 107 add r4, r4, #1
mike@0 108 b .L7
mike@0 109 .L9:
mike@0 110 @ return p
mike@0 111 mov r0, r5
mike@0 112 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 113 .ltorg
mike@0 114
mike@0 115 pmain:
mike@0 116 mov ip, sp
mike@0 117 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 118 mov fp, sp
mike@0 119 @ print_num(sum_powers(1, 10, 3));
mike@0 120 mov r2, #3
mike@0 121 mov r1, #10
mike@0 122 mov r0, #1
mike@0 123 bl _sum_powers
mike@0 124 bl print_num
mike@0 125 @ newline()
mike@0 126 bl newline
mike@0 127 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 128 .ltorg
mike@0 129
mike@0 130 @ End
mike@0 131 ]]*)