comparison lab4/test/array.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 (* Local and global arrays *)
2
3 var i: integer;
4
5 var a: array 10 of integer;
6
7 proc foo();
8 var j: integer;
9 var b : array 10 of integer;
10 begin
11 print_string("foo"); newline();
12 j := 2; b[0] := 1; b[1] := 1;
13 while 10 > j do
14 b[j] := b[j-2] + b[j-1];
15 print_char(' '); print_num(b[j]);
16 j := 1+j
17 end;
18 newline();
19 end;
20
21 begin
22 print_string("baz"); newline();
23 i := 2; a[0] := 1; a[1] := 1;
24 while i < 10 do
25 a[i] := a[i-2] + a[i-1];
26 print_char(' '); print_num(a[i]);
27 i := i+1
28 end;
29 newline();
30 foo()
31 end.
32
33 (*<<
34 baz
35 2 3 5 8 13 21 34 55
36 foo
37 2 3 5 8 13 21 34 55
38 >>*)
39
40 (*[[
41 @ picoPascal compiler output
42 .include "fixup.s"
43 .global pmain
44
45 @ proc foo();
46 .text
47 _foo:
48 mov ip, sp
49 stmfd sp!, {r4-r10, fp, ip, lr}
50 mov fp, sp
51 sub sp, sp, #40
52 @ print_string("foo"); newline();
53 mov r1, #3
54 set r0, g1
55 bl print_string
56 bl newline
57 @ j := 2; b[0] := 1; b[1] := 1;
58 mov r4, #2
59 mov r0, #1
60 str r0, [fp, #-40]
61 mov r0, #1
62 str r0, [fp, #-36]
63 .L4:
64 @ while 10 > j do
65 cmp r4, #10
66 bge .L6
67 @ b[j] := b[j-2] + b[j-1];
68 add r0, fp, #-40
69 lsl r1, r4, #2
70 add r5, r0, r1
71 ldr r0, [r5, #-8]
72 ldr r1, [r5, #-4]
73 add r0, r0, r1
74 str r0, [r5]
75 @ print_char(' '); print_num(b[j]);
76 mov r0, #32
77 bl print_char
78 add r0, fp, #-40
79 lsl r1, r4, #2
80 add r0, r0, r1
81 ldr r0, [r0]
82 bl print_num
83 @ j := 1+j
84 add r4, r4, #1
85 b .L4
86 .L6:
87 @ newline();
88 bl newline
89 ldmfd fp, {r4-r10, fp, sp, pc}
90 .ltorg
91
92 pmain:
93 mov ip, sp
94 stmfd sp!, {r4-r10, fp, ip, lr}
95 mov fp, sp
96 @ print_string("baz"); newline();
97 mov r1, #3
98 set r0, g2
99 bl print_string
100 bl newline
101 @ i := 2; a[0] := 1; a[1] := 1;
102 mov r0, #2
103 set r1, _i
104 str r0, [r1]
105 set r4, _a
106 mov r0, #1
107 str r0, [r4]
108 mov r0, #1
109 str r0, [r4, #4]
110 .L8:
111 @ while i < 10 do
112 set r0, _i
113 ldr r4, [r0]
114 cmp r4, #10
115 bge .L10
116 @ a[i] := a[i-2] + a[i-1];
117 set r0, _a
118 lsl r1, r4, #2
119 add r4, r0, r1
120 ldr r0, [r4, #-8]
121 ldr r1, [r4, #-4]
122 add r0, r0, r1
123 str r0, [r4]
124 @ print_char(' '); print_num(a[i]);
125 mov r0, #32
126 bl print_char
127 set r0, _a
128 set r1, _i
129 ldr r1, [r1]
130 lsl r1, r1, #2
131 add r0, r0, r1
132 ldr r0, [r0]
133 bl print_num
134 @ i := i+1
135 set r4, _i
136 ldr r0, [r4]
137 add r0, r0, #1
138 str r0, [r4]
139 b .L8
140 .L10:
141 @ newline();
142 bl newline
143 @ foo()
144 bl _foo
145 ldmfd fp, {r4-r10, fp, sp, pc}
146 .ltorg
147
148 .comm _i, 4, 4
149 .comm _a, 40, 4
150 .data
151 g1:
152 .byte 102, 111, 111
153 .byte 0
154 g2:
155 .byte 98, 97, 122
156 .byte 0
157 @ End
158 ]]*)