Pristine Ack-5.5
[Ack-5.5.git] / doc / em / app.exam.nr
1 .bp
2 .AP "AN EXAMPLE PROGRAM"
3 .PP
4 .na
5 .ta 4n 8n 12n 16n 20n
6 .nf
7  1      program example(output);
8  2      {This program just demonstrates typical EM code.}
9  3      type rec = record r1: integer; r2:real; r3: boolean end;
10  4      var mi: integer;  mx:real;  r:rec;
11  5
12  6      function sum(a,b:integer):integer;
13  7      begin
14  8              sum := a + b
15  9      end;
16 10
17 11      procedure test(var r: rec);
18 12      label 1;
19 13      var i,j: integer;
20 14              x,y: real;
21 15              b: boolean;
22 16              c: char;
23 17              a: array[1..100] of integer;
24 18
25 19      begin
26 20              j := 1;
27 21              i := 3 * j + 6;
28 22              x := 4.8;
29 23              y := x/0.5;
30 24              b := true;
31 25              c := 'z';
32 26              for i:= 1 to 100 do a[i] := i * i;
33 27              r.r1 := j+27;
34 28              r.r3 := b;
35 29              r.r2 := x+y;
36 30              i := sum(r.r1, a[j]);
37 31              while i > 0 do begin j := j + r.r1; i := i - 1 end;
38 32              with r do begin r3 := b;  r2 := x+y;  r1 := 0 end;
39 33              goto 1;
40 34      1:      writeln(j, i:6, x:9:3, b)
41 35      end; {test}
42 36      begin {main program}
43 37              mx := 15.96;
44 38              mi := 99;
45 39              test(r)
46 40      end.
47 .fi
48 .ad
49 .bp
50 The EM code as produced by the Pascal-VU compiler is given below. Comments
51 have been added manually.  Note that this code has already been  optimized.
52 .LP
53 .na
54 .nf
55 .ta 1n 24n
56         mes 2,2,2       ; wordsize 2, pointersize 2
57 \&.1
58         rom 't.p\e000'  ; the name of the source file
59         hol 552,\-32768,0       ; externals and buf occupy 552 bytes
60         exp $sum        ; sum can be called from other modules
61         pro $sum,2      ; procedure sum ; 2 bytes local storage
62         lin 8   ; code from source line 8
63         ldl 0   ; load two locals ( a and b )
64         adi 2   ; add them
65         ret 2   ; return the result
66         end 2   ; end of procedure ( still two bytes local storage )
67 \&.2
68         rom 1,99,2      ; descriptor of array a[]
69         exp $test       ; the compiler exports all level 0 procedures
70         pro $test,226   ; procedure test, 226 bytes local storage
71 \&.3
72         rom 4.8F8       ; assemble Floating point 4.8 (8 bytes) in
73 \&.4            ; global storage
74         rom 0.5F8       ; same for 0.5
75         mes 3,\-226,2,2 ; compiler temporary not referenced by address
76         mes 3,\-24,2,0  ; the same is true for i, j, b and c in test
77         mes 3,\-22,2,0
78         mes 3,\-4,2,0
79         mes 3,\-2,2,0
80         mes 3,\-20,8,0  ; and for x and y
81         mes 3,\-12,8,0
82         lin 20  ; maintain source line number
83         loc 1
84         stl \-4 ; j := 1
85         lni     ; lin 21 prior to optimization
86         lol \-4
87         loc 3
88         mli 2
89         loc 6
90         adi 2
91         stl \-2 ; i := 3 * j + 6
92         lni     ; lin 22 prior to optimization
93         lae .3
94         loi 8
95         lal \-12
96         sti 8   ; x := 4.8
97         lni     ; lin 23 prior to optimization
98         lal \-12
99         loi 8
100         lae .4
101         loi 8
102         dvf 8
103         lal \-20
104         sti 8   ; y := x / 0.5
105         lni     ; lin 24 prior to optimization
106         loc 1
107         stl \-22        ; b := true
108         lni     ; lin 25 prior to optimization
109         loc 122
110         stl \-24        ; c := 'z'
111         lni     ; lin 26 prior to optimization
112         loc 1
113         stl \-2 ; for i:= 1
114 2
115         lol \-2
116         dup 2
117         mli 2   ; i*i
118         lal \-224
119         lol \-2
120         lae .2
121         sar 2   ; a[i] :=
122         lol \-2
123         loc 100
124         beq *3  ; to 100 do
125         inl \-2 ; increment i and loop
126         bra *2
127 3
128         lin 27
129         lol \-4
130         loc 27
131         adi 2   ; j + 27
132         sil 0   ; r.r1 :=
133         lni     ; lin 28 prior to optimization
134         lol \-22        ; b
135         lol 0
136         stf 10  ; r.r3 :=
137         lni     ; lin 29 prior to optimization
138         lal \-20
139         loi 16
140         adf 8   ; x + y
141         lol 0
142         adp 2
143         sti 8   ; r.r2 :=
144         lni     ; lin 30 prior to optimization
145         lal \-224
146         lol \-4
147         lae .2
148         lar 2   ; a[j]
149         lil 0   ; r.r1
150         cal $sum        ; call now
151         asp 4   ; remove parameters from stack
152         lfr 2   ; get function result
153         stl \-2 ; i :=
154 4
155         lin 31
156         lol \-2
157         zle *5  ; while i > 0 do
158         lol \-4
159         lil 0
160         adi 2
161         stl \-4 ; j := j + r.r1
162         del \-2 ; i := i - 1
163         bra *4  ; loop
164 5
165         lin 32
166         lol 0
167         stl \-226       ; make copy of address of r
168         lol \-22
169         lol \-226
170         stf 10  ; r3 := b
171         lal \-20
172         loi 16
173         adf 8
174         lol \-226
175         adp 2
176         sti 8   ; r2 := x + y
177         loc 0
178         sil \-226       ; r1 := 0
179         lin 34  ; note the absence of the unnecessary jump
180         lae 22  ; address of output structure
181         lol \-4
182         cal $_wri       ; write integer with default width
183         asp 4   ; pop parameters
184         lae 22
185         lol \-2
186         loc 6
187         cal $_wsi       ; write integer width 6
188         asp 6
189         lae 22
190         lal \-12
191         loi 8
192         loc 9
193         loc 3
194         cal $_wrf       ; write fixed format real, width 9, precision 3
195         asp 14
196         lae 22
197         lol \-22
198         cal $_wrb       ; write boolean, default width
199         asp 4
200         lae 22
201         cal $_wln       ; writeln
202         asp 2
203         ret 0   ; return, no result
204         end 226
205         exp $_main
206         pro $_main,0    ; main program
207 \&.6
208         con 2,\-1,22    ; description of external files
209 \&.5
210         rom 15.96F8
211         fil .1  ; maintain source file name
212         lae .6  ; description of external files
213         lae 0   ; base of hol area to relocate buffer addresses
214         cal $_ini       ; initialize files, etc...
215         asp 4
216         lin 37
217         lae .5
218         loi 8
219         lae 2
220         sti 8   ; mx := 15.96
221         lni     ; lin 38 prior to optimization
222         loc 99
223         ste 0   ; mi := 99
224         lni     ; lin 39 prior to optimization
225         lae 10  ; address of r
226         cal $test
227         asp 2
228         loc 0   ; normal exit
229         cal $_hlt       ; cleanup and finish
230         asp 2
231         end 0
232         mes 5   ; reals were used
233 .fi
234 .ad
235 .PP
236 The compact code corresponding to the above program is listed below.
237 Read it horizontally, line by line, not column by column.
238 Each number represents a byte of compact code, printed in decimal.
239 The first two bytes form the magic word.
240 .LP
241 .Dr 33
242  173   0 159 122 122 122 255 242   1 161 250 124 116  46 112   0
243  255 156 245  40   2 245   0 128 120 155 249 123 115 117 109 160
244  249 123 115 117 109 122  67 128  63 120   3 122  88 122 152 122
245  242   2 161 121 219 122 255 155 249 124 116 101 115 116 160 249
246  124 116 101 115 116 245 226   0 242   3 161 253 128 123  52  46
247   56 255 242   4 161 253 128 123  48  46  53 255 159 123 245  30
248  255 122 122 255 159 123  96 122 120 255 159 123  98 122 120 255
249  159 123 116 122 120 255 159 123 118 122 120 255 159 123 100 128
250  120 255 159 123 108 128 120 255  67 140  69 121 113 116  68  73
251  116  69 123  81 122  69 126   3 122 113 118  68  57 242   3  72
252  128  58 108 112 128  68  58 108  72 128  57 242   4  72 128  44
253  128  58 100 112 128  68  69 121 113  98  68  69 245 122   0 113
254   96  68  69 121 113 118 182  73 118  42 122  81 122  58 245  32
255  255  73 118  57 242   2  94 122  73 118  69 220  10 123  54 118
256   18 122 183  67 147  73 116  69 147   3 122 104 120  68  73  98
257   73 120 111 130  68  58 100  72 136   2 128  73 120   4 122 112
258  128  68  58 245  32 255  73 116  57 242   2  59 122  65 120  20
259  249 123 115 117 109   8 124  64 122 113 118 184  67 151  73 118
260  128 125  73 116  65 120   3 122 113 116  41 118  18 124 185  67
261  152  73 120 113 245  30 255  73  98  73 245  30 255 111 130  58
262  100  72 136   2 128  73 245  30 255   4 122 112 128  69 120 104
263  245  30 255  67 154  57 142  73 116  20 249 124  95 119 114 105
264    8 124  57 142  73 118  69 126  20 249 124  95 119 115 105   8
265  126  57 142  58 108  72 128  69 129  69 123  20 249 124  95 119
266  114 102   8 134  57 142  73  98  20 249 124  95 119 114  98   8
267  124  57 142  20 249 124  95 119 108 110   8 122  88 120 152 245
268  226   0 155 249 125  95 109  97 105 110 160 249 125  95 109  97
269  105 110 120 242   6 151 122 119 142 255 242   5 161 253 128 125
270   49  53  46  57  54 255  50 242   1  57 242   6  57 120  20 249
271  124  95 105 110 105   8 124  67 157  57 242   5  72 128  57 122
272  112 128  68  69 219 110 120  68  57 130  20 249 124 116 101 115
273  116   8 122  69 120  20 249 124  95 104 108 116   8 122 152 120
274  159 124 160 255 159 125 255
275 .De