annotate ppc/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 (* sumpower.p *)
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 MODULE Main 0 0
mike@0 40 IMPORT Lib 0
mike@0 41 ENDHDR
mike@0 42
mike@0 43 PROC _sum 8 0 0
mike@0 44 ! i := a; s := 0;
mike@0 45 LDLW 16
mike@0 46 STLW -4
mike@0 47 CONST 0
mike@0 48 STLW -8
mike@0 49 ! while i <= b do
mike@0 50 JUMP L2
mike@0 51 LABEL L1
mike@0 52 ! s := s + f(i);
mike@0 53 LDLW -8
mike@0 54 LDLW -4
mike@0 55 LDLW 28
mike@0 56 LDLW 24
mike@0 57 PCALLW 1
mike@0 58 PLUS
mike@0 59 STLW -8
mike@0 60 ! i := i + 1
mike@0 61 LDLW -4
mike@0 62 CONST 1
mike@0 63 PLUS
mike@0 64 STLW -4
mike@0 65 LABEL L2
mike@0 66 LDLW -4
mike@0 67 LDLW 20
mike@0 68 JLEQ L1
mike@0 69 ! return s
mike@0 70 LDLW -8
mike@0 71 RETURNW
mike@0 72 END
mike@0 73
mike@0 74 PROC _sum_powers 0 0 0
mike@0 75 ! return sum(a, b, pow)
mike@0 76 LOCAL 0
mike@0 77 GLOBAL _pow
mike@0 78 LDLW 20
mike@0 79 LDLW 16
mike@0 80 CONST 0
mike@0 81 GLOBAL _sum
mike@0 82 PCALLW 4
mike@0 83 RETURNW
mike@0 84 END
mike@0 85
mike@0 86 PROC _pow 8 0 0
mike@0 87 ! j := 0; p := 1;
mike@0 88 CONST 0
mike@0 89 STLW -4
mike@0 90 CONST 1
mike@0 91 STLW -8
mike@0 92 ! while j < n do
mike@0 93 JUMP L5
mike@0 94 LABEL L4
mike@0 95 ! p := p * x;
mike@0 96 LDLW -8
mike@0 97 LDLW 16
mike@0 98 TIMES
mike@0 99 STLW -8
mike@0 100 ! j := j + 1
mike@0 101 LDLW -4
mike@0 102 CONST 1
mike@0 103 PLUS
mike@0 104 STLW -4
mike@0 105 LABEL L5
mike@0 106 LDLW -4
mike@0 107 LDLW 12
mike@0 108 LDNW 24
mike@0 109 JLT L4
mike@0 110 ! return p
mike@0 111 LDLW -8
mike@0 112 RETURNW
mike@0 113 END
mike@0 114
mike@0 115 PROC MAIN 0 0 0
mike@0 116 ! print_num(sum_powers(1, 10, 3));
mike@0 117 CONST 3
mike@0 118 CONST 10
mike@0 119 CONST 1
mike@0 120 CONST 0
mike@0 121 GLOBAL _sum_powers
mike@0 122 PCALLW 3
mike@0 123 CONST 0
mike@0 124 GLOBAL lib.print_num
mike@0 125 PCALL 1
mike@0 126 ! newline()
mike@0 127 CONST 0
mike@0 128 GLOBAL lib.newline
mike@0 129 PCALL 0
mike@0 130 RETURN
mike@0 131 END
mike@0 132
mike@0 133 ! End
mike@0 134 ]]*)