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