annotate ppc/test/pascal.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 (* Pascal's triangle *)
mike@0 2
mike@0 3 const n = 10;
mike@0 4
mike@0 5 proc pascal2();
mike@0 6 var i, j: integer;
mike@0 7 var a: array n of array n+1 of integer;
mike@0 8 begin
mike@0 9 i := 0;
mike@0 10 while i < n do
mike@0 11 a[i][0] := 1; j := 1;
mike@0 12 print_num(a[i][0]);
mike@0 13 while j <= i do
mike@0 14 a[i][j] := a[i-1][j-1] + a[i-1][j];
mike@0 15 print_char(' '); print_num(a[i][j]);
mike@0 16 j := j+1
mike@0 17 end;
mike@0 18 a[i][i+1] := 0;
mike@0 19 newline();
mike@0 20 i := i+1
mike@0 21 end
mike@0 22 end;
mike@0 23
mike@0 24 begin
mike@0 25 pascal2()
mike@0 26 end.
mike@0 27
mike@0 28 (*<<
mike@0 29 1
mike@0 30 1 1
mike@0 31 1 2 1
mike@0 32 1 3 3 1
mike@0 33 1 4 6 4 1
mike@0 34 1 5 10 10 5 1
mike@0 35 1 6 15 20 15 6 1
mike@0 36 1 7 21 35 35 21 7 1
mike@0 37 1 8 28 56 70 56 28 8 1
mike@0 38 1 9 36 84 126 126 84 36 9 1
mike@0 39 >>*)
mike@0 40
mike@0 41 (*[[
mike@0 42 MODULE Main 0 0
mike@0 43 IMPORT Lib 0
mike@0 44 ENDHDR
mike@0 45
mike@0 46 PROC _pascal2 448 0 0
mike@0 47 ! i := 0;
mike@0 48 CONST 0
mike@0 49 STLW -4
mike@0 50 ! while i < n do
mike@0 51 JUMP L2
mike@0 52 LABEL L1
mike@0 53 ! a[i][0] := 1; j := 1;
mike@0 54 CONST 1
mike@0 55 LOCAL -448
mike@0 56 LDLW -4
mike@0 57 CONST 44
mike@0 58 TIMES
mike@0 59 OFFSET
mike@0 60 STOREW
mike@0 61 CONST 1
mike@0 62 STLW -8
mike@0 63 ! print_num(a[i][0]);
mike@0 64 LOCAL -448
mike@0 65 LDLW -4
mike@0 66 CONST 44
mike@0 67 TIMES
mike@0 68 OFFSET
mike@0 69 LOADW
mike@0 70 CONST 0
mike@0 71 GLOBAL lib.print_num
mike@0 72 PCALL 1
mike@0 73 ! while j <= i do
mike@0 74 JUMP L5
mike@0 75 LABEL L4
mike@0 76 ! a[i][j] := a[i-1][j-1] + a[i-1][j];
mike@0 77 LOCAL -448
mike@0 78 LDLW -4
mike@0 79 CONST 1
mike@0 80 MINUS
mike@0 81 CONST 44
mike@0 82 TIMES
mike@0 83 OFFSET
mike@0 84 LDLW -8
mike@0 85 CONST 1
mike@0 86 MINUS
mike@0 87 LDIW
mike@0 88 LOCAL -448
mike@0 89 LDLW -4
mike@0 90 CONST 1
mike@0 91 MINUS
mike@0 92 CONST 44
mike@0 93 TIMES
mike@0 94 OFFSET
mike@0 95 LDLW -8
mike@0 96 LDIW
mike@0 97 PLUS
mike@0 98 LOCAL -448
mike@0 99 LDLW -4
mike@0 100 CONST 44
mike@0 101 TIMES
mike@0 102 OFFSET
mike@0 103 LDLW -8
mike@0 104 STIW
mike@0 105 ! print_char(' '); print_num(a[i][j]);
mike@0 106 CONST 32
mike@0 107 CONST 0
mike@0 108 GLOBAL lib.print_char
mike@0 109 PCALL 1
mike@0 110 LOCAL -448
mike@0 111 LDLW -4
mike@0 112 CONST 44
mike@0 113 TIMES
mike@0 114 OFFSET
mike@0 115 LDLW -8
mike@0 116 LDIW
mike@0 117 CONST 0
mike@0 118 GLOBAL lib.print_num
mike@0 119 PCALL 1
mike@0 120 ! j := j+1
mike@0 121 LDLW -8
mike@0 122 CONST 1
mike@0 123 PLUS
mike@0 124 STLW -8
mike@0 125 LABEL L5
mike@0 126 LDLW -8
mike@0 127 LDLW -4
mike@0 128 JLEQ L4
mike@0 129 ! a[i][i+1] := 0;
mike@0 130 CONST 0
mike@0 131 LOCAL -448
mike@0 132 LDLW -4
mike@0 133 CONST 44
mike@0 134 TIMES
mike@0 135 OFFSET
mike@0 136 LDLW -4
mike@0 137 CONST 1
mike@0 138 PLUS
mike@0 139 STIW
mike@0 140 ! newline();
mike@0 141 CONST 0
mike@0 142 GLOBAL lib.newline
mike@0 143 PCALL 0
mike@0 144 ! i := i+1
mike@0 145 LDLW -4
mike@0 146 CONST 1
mike@0 147 PLUS
mike@0 148 STLW -4
mike@0 149 LABEL L2
mike@0 150 LDLW -4
mike@0 151 CONST 10
mike@0 152 JLT L1
mike@0 153 RETURN
mike@0 154 END
mike@0 155
mike@0 156 PROC MAIN 0 0 0
mike@0 157 ! pascal2()
mike@0 158 CONST 0
mike@0 159 GLOBAL _pascal2
mike@0 160 PCALL 0
mike@0 161 RETURN
mike@0 162 END
mike@0 163
mike@0 164 ! End
mike@0 165 ]]*)