annotate ppc/test/sumarray.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 type vector = array 10 of integer;
mike@0 2
mike@0 3 proc dovec(proc f(x: integer); var v: vector);
mike@0 4 var i: integer;
mike@0 5 begin
mike@0 6 i := 0;
mike@0 7 while i < 10 do
mike@0 8 f(v[i]); i := i+1
mike@0 9 end
mike@0 10 end;
mike@0 11
mike@0 12 proc sum(var v: vector): integer;
mike@0 13 var s: integer;
mike@0 14
mike@0 15 proc add(x: integer);
mike@0 16 begin
mike@0 17 s := s + x
mike@0 18 end;
mike@0 19
mike@0 20 begin
mike@0 21 s := 0;
mike@0 22 dovec(add, v);
mike@0 23 return s
mike@0 24 end;
mike@0 25
mike@0 26 var a: vector; i: integer;
mike@0 27
mike@0 28 begin
mike@0 29 i := 0;
mike@0 30 while i < 10 do
mike@0 31 a[i] := (i+1)*(i+1);
mike@0 32 i := i+1
mike@0 33 end;
mike@0 34 print_num(sum(a));
mike@0 35 newline()
mike@0 36 end.
mike@0 37
mike@0 38 (*<<
mike@0 39 385
mike@0 40 >>*)
mike@0 41
mike@0 42 (*[[
mike@0 43 MODULE Main 0 0
mike@0 44 IMPORT Lib 0
mike@0 45 ENDHDR
mike@0 46
mike@0 47 PROC _dovec 4 0 0
mike@0 48 ! i := 0;
mike@0 49 CONST 0
mike@0 50 STLW -4
mike@0 51 ! while i < 10 do
mike@0 52 JUMP L2
mike@0 53 LABEL L1
mike@0 54 ! f(v[i]); i := i+1
mike@0 55 LDLW 24
mike@0 56 LDLW -4
mike@0 57 LDIW
mike@0 58 LDLW 20
mike@0 59 LDLW 16
mike@0 60 PCALL 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 CONST 10
mike@0 68 JLT L1
mike@0 69 RETURN
mike@0 70 END
mike@0 71
mike@0 72 PROC _sum 4 0 0
mike@0 73 ! s := 0;
mike@0 74 CONST 0
mike@0 75 STLW -4
mike@0 76 ! dovec(add, v);
mike@0 77 LDLW 16
mike@0 78 LOCAL 0
mike@0 79 GLOBAL _add
mike@0 80 CONST 0
mike@0 81 GLOBAL _dovec
mike@0 82 PCALL 3
mike@0 83 ! return s
mike@0 84 LDLW -4
mike@0 85 RETURNW
mike@0 86 END
mike@0 87
mike@0 88 PROC _add 0 0 0
mike@0 89 ! s := s + x
mike@0 90 LDLW 12
mike@0 91 LDNW -4
mike@0 92 LDLW 16
mike@0 93 PLUS
mike@0 94 LDLW 12
mike@0 95 STNW -4
mike@0 96 RETURN
mike@0 97 END
mike@0 98
mike@0 99 PROC MAIN 0 0 0
mike@0 100 ! i := 0;
mike@0 101 CONST 0
mike@0 102 STGW _i
mike@0 103 ! while i < 10 do
mike@0 104 JUMP L5
mike@0 105 LABEL L4
mike@0 106 ! a[i] := (i+1)*(i+1);
mike@0 107 LDGW _i
mike@0 108 CONST 1
mike@0 109 PLUS
mike@0 110 LDGW _i
mike@0 111 CONST 1
mike@0 112 PLUS
mike@0 113 TIMES
mike@0 114 GLOBAL _a
mike@0 115 LDGW _i
mike@0 116 STIW
mike@0 117 ! i := i+1
mike@0 118 LDGW _i
mike@0 119 CONST 1
mike@0 120 PLUS
mike@0 121 STGW _i
mike@0 122 LABEL L5
mike@0 123 LDGW _i
mike@0 124 CONST 10
mike@0 125 JLT L4
mike@0 126 ! print_num(sum(a));
mike@0 127 GLOBAL _a
mike@0 128 CONST 0
mike@0 129 GLOBAL _sum
mike@0 130 PCALLW 1
mike@0 131 CONST 0
mike@0 132 GLOBAL lib.print_num
mike@0 133 PCALL 1
mike@0 134 ! newline()
mike@0 135 CONST 0
mike@0 136 GLOBAL lib.newline
mike@0 137 PCALL 0
mike@0 138 RETURN
mike@0 139 END
mike@0 140
mike@0 141 GLOVAR _a 40
mike@0 142 GLOVAR _i 4
mike@0 143 ! End
mike@0 144 ]]*)