comparison lab4/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 with a 2-dim array *)
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 @ picoPascal compiler output
43 .include "fixup.s"
44 .global pmain
45
46 @ proc pascal2();
47 .text
48 _pascal2:
49 mov ip, sp
50 stmfd sp!, {r4-r10, fp, ip, lr}
51 mov fp, sp
52 sub sp, sp, #440
53 @ i := 0;
54 mov r4, #0
55 .L2:
56 @ while i < n do
57 cmp r4, #10
58 bge .L1
59 @ a[i][0] := 1; j := 1;
60 mov r6, #1
61 add r0, fp, #-440
62 mov r1, #44
63 mul r1, r4, r1
64 add r0, r0, r1
65 str r6, [r0]
66 mov r5, #1
67 @ print_num(a[i][0]);
68 mov r0, r6
69 bl print_num
70 .L5:
71 @ while j <= i do
72 cmp r5, r4
73 bgt .L7
74 @ a[i][j] := a[i-1][j-1] + a[i-1][j];
75 add r0, fp, #-440
76 mov r1, #44
77 mul r1, r4, r1
78 add r6, r0, r1
79 lsl r7, r5, #2
80 add r0, r6, #-44
81 add r8, r0, r7
82 ldr r0, [r8, #-4]
83 ldr r1, [r8]
84 add r0, r0, r1
85 add r1, r6, r7
86 str r0, [r1]
87 @ print_char(' '); print_num(a[i][j]);
88 mov r0, #32
89 bl print_char
90 add r0, fp, #-440
91 mov r1, #44
92 mul r1, r4, r1
93 add r0, r0, r1
94 lsl r1, r5, #2
95 add r0, r0, r1
96 ldr r0, [r0]
97 bl print_num
98 @ j := j+1
99 add r5, r5, #1
100 b .L5
101 .L7:
102 @ a[i][i+1] := 0;
103 mov r0, #0
104 add r1, fp, #-440
105 mov r2, #44
106 mul r2, r4, r2
107 add r1, r1, r2
108 lsl r2, r4, #2
109 add r1, r1, r2
110 str r0, [r1, #4]
111 @ newline();
112 bl newline
113 @ i := i+1
114 add r4, r4, #1
115 b .L2
116 .L1:
117 ldmfd fp, {r4-r10, fp, sp, pc}
118 .ltorg
119
120 pmain:
121 mov ip, sp
122 stmfd sp!, {r4-r10, fp, ip, lr}
123 mov fp, sp
124 @ pascal2()
125 bl _pascal2
126 ldmfd fp, {r4-r10, fp, sp, pc}
127 .ltorg
128
129 @ End
130 ]]*)