annotate lab4/test/pascal.p @ 0:bfdcc3820b32

Basis
author Mike Spivey Thu, 05 Oct 2017 08:04:15 +0100
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 (* Pascal's triangle with a 2-dim array *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 const n = 10;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 proc pascal2();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 var i, j: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 var a: array n of array n+1 of integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 i := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 while i < n do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 a[i][0] := 1; j := 1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 print_num(a[i][0]);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 while j <= i do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 a[i][j] := a[i-1][j-1] + a[i-1][j];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 print_char(' '); print_num(a[i][j]);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 j := j+1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 a[i][i+1] := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 newline();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 i := i+1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 pascal2()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 end.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 1 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 1 2 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 1 3 3 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 1 4 6 4 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 1 5 10 10 5 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 1 6 15 20 15 6 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 1 7 21 35 35 21 7 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 1 8 28 56 70 56 28 8 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 1 9 36 84 126 126 84 36 9 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 >>*)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 (*[[
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 @ picoPascal compiler output
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 .include "fixup.s"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 .global pmain
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 @ proc pascal2();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 .text
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 _pascal2:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 sub sp, sp, #440
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 @ i := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 mov r4, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 .L2:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 @ while i < n do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 cmp r4, #10
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 bge .L1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 @ a[i][0] := 1; j := 1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 mov r6, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 mov r1, #44
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 mul r1, r4, r1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 str r6, [r0]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 mov r5, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 @ print_num(a[i][0]);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 mov r0, r6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 .L5:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 @ while j <= i do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 cmp r5, r4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 bgt .L7
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 @ a[i][j] := a[i-1][j-1] + a[i-1][j];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 mov r1, #44
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 mul r1, r4, r1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 lsl r7, r5, #2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 ldr r0, [r8, #-4]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 ldr r1, [r8]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 str r0, [r1]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 @ print_char(' '); print_num(a[i][j]);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 mov r0, #32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 bl print_char
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 mov r1, #44
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 mul r1, r4, r1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94 lsl r1, r5, #2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
96 ldr r0, [r0]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
97 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
98 @ j := j+1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
100 b .L5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
101 .L7:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
102 @ a[i][i+1] := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
103 mov r0, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
105 mov r2, #44
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
106 mul r2, r4, r2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
108 lsl r2, r4, #2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
110 str r0, [r1, #4]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
111 @ newline();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
112 bl newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
113 @ i := i+1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
115 b .L2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
116 .L1:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
117 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
118 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
119
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
120 pmain:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
121 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
122 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
123 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
124 @ pascal2()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
125 bl _pascal2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
126 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
127 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
128
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
129 @ End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
130 ]]*)