1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
# Rules for optimizing BCC assembler output
# Rules for loading short variables
mov %0$0[%2],%3
=
mov %0[%2],%3
mov %2,%[ax|bx|cx|dx]1
mov %[ax|bx|cx|dx]1,%2
=
mov %2,%1
# Breaks: a = b = 125;
# mov %[ax|bx|cx|dx]3,%[#|*]0%1
# mov %2[%4],%[ax|bx|cx|dx]3
# =
# mov word ptr %2[%4],%0%1
# This gets a few back.
mov %[ax|bx|cx|dx]3,%[#|*]0%1
mov %2[%4],%[ax|bx|cx|dx]3
mov %[ax|bx|cx|dx]3,%5
=
mov word ptr %2[%4],%0%1
mov %3,%5
mov %[ax|bx|cx|dx]3,%[#|*]0%1
mov %[ax|bx|cx|dx]2,%[ax|bx|cx|dx]3
=
mov %2,%0%1
# Breaks: a = b = 'a';
# mov al,%[#|*]0%1
# mov %2,al
# =
# mov byte ptr %2,%0%1
xor ax,ax
mov bx,%[#|*]0%1
mov [%2],ax
mov [%2+2],bx
=
mov word ptr [%2],#0
mov word ptr [%2+2],%0%1
mov ax,%[#|*]0%1
xor bx,bx
mov [%2],ax
mov [%2+2],bx
=
mov word ptr [%2],%0%1
mov word ptr [%2+2],#0
mov ax,%[#|*]4%1
mov bx,%[#|*]5%3
mov [%2],ax
mov [%2+2],bx
=
mov word ptr [%2],%4%1
mov word ptr [%2+2],%5%3
# Rules for incrementing short variables
mov %[ax|bx]2,%1
%[inc|dec]3 %[ax|bx]2
mov %1,%[ax|bx]2
=
%3 word ptr %1
mov %2,%1
# Rules for manipulating characters
inc %[si|di]*
inc %[si|di]*
mov al,-1[si]
mov -1[di],al
=
lodsb
stosb
inc %[si|di]*
inc %[si|di]*
mov al,-1[di]
mov -1[si],al
=
xchg si,di
lodsb
stosb
xchg si,di
inc si
mov al,-1[si]
=
lodsb
inc di
mov -1[di],al
=
stosb
dec %[si|di]*
dec %[si|di]*
mov al,1[si]
mov 1[di],al
=
std
lodsb
stosb
cld
dec %[si|di]*
dec %[si|di]*
mov al,1[di]
mov 1[si],al
=
std
xchg si,di
lodsb
stosb
xchg si,di
cld
dec si
mov al,1[si]
=
std
lodsb
cld
dec di
mov 1[di],al
=
std
stosb
cld
# Rules for manipulating short values
# This is the broken rule - Chad
#mov %[ax|bx]2,%1
#%[add|and|xor|sub|or]4 %[ax|bx]2,%3
#mov %1,%[ax|bx]2
#=
#mov %2,%3
#%4 %1,%2
mov %[ax|bx]2,%1
%[add|and|xor|or]4 %[ax|bx]2,%3
mov %3,%[ax|bx]2
=
mov %2,%1
%4 %3,%2
mov %[ax|bx]4,%1
%[add|and|xor|sub|or]2 %[si|di]3,%[ax|bx]4
=
%2 %3,%1
mov al,%1
xor ah,ah
%[add|and|xor|sub|or]2 ax,%[#|*]0%3
mov %1,al
=
%2 byte ptr %1,%0%3
# Rules for comparing short values
mov %[ax|bx]3,%1[%4]
cmp %[ax|bx]3,%[#|*]0%2
=
cmp word ptr %1[%4],%0%2
mov al,%1[%4]
cmp al,%[#|*]0%2
=
cmp byte ptr %1[%4],%0%2
mov %[ax|bx]3,%[ax|bx|cx|dx|si|di]2
cmp %[ax|bx]3,%1
=
cmp %2,%1
# General comparison rules
xor %1,%1
mov %2,%3
cmp %2,%1
=
mov %2,%3
cmp %2,#0
mov %1,%[#|*]0%2
mov %3,%4
cmp %3,%1
=
mov %3,%4
cmp %3,%0%2
mov %1,%2
cmp %1,%[#|*]00
%[jne |je ]4 %3
=
mov %1,%2
test %1,%1
%4 %3
%[and|xor|or]1 %2,%3
test %2,%2
=
%1 %2,%3
# General incrementation and decrementation rules
inc %3 ptr %1
mov %2,%1
dec %2
=
mov %2,%1
inc %3 ptr %1
dec %3 ptr %1
mov %2,%1
inc %2
=
mov %2,%1
dec %3 ptr %1
mov %1,%2
inc %3 ptr %2
push %1
=
push %3 ptr %2
inc %3 ptr %2
mov %1,%2
dec %3 ptr %2
push %1
=
push %3 ptr %2
dec %3 ptr %2
# Misc. rules
push word %[#|*]0%1
inc %[si|di]2
inc %[si|di]2
mov %[ax|bx|cx|dx]3,%*[bp]
mov %4[%[si|di]2],%[ax|bx|cx|dx]3
inc sp
inc sp
=
mov word ptr %(%4+2)[%2],%0%1
inc %2
inc %2
push %1
mov %[ax|bx|cx|dx]3,%2
%[add|sub|and|xor|or]4 %[ax|bx|cx|dx]3,%*[bp]
inc sp
inc sp
=
mov %3,%2
%4 %3,%1
push %1
mov %[ax|bx|cx|dx]3,%2
%[add|sub|and|xor|or]5 %[ax|bx|cx|dx]3,%6
%[add|sub|and|xor|or]4 %[ax|dx|cx|dx]3,%*[bp]
inc sp
inc sp
=
mov %3,%2
%5 %3,%6
%4 %3,%1
push %1
mov %[ax|bx|cx|dx]3,%*[bp]
%[add|sub|and|xor|or]4 %2,%[ax|bx|cx|dx]3
inc sp
inc sp
=
mov %3,%1
%4 %2,%3
mov %1,%[ax|bx|cx|dx]2
push %1
=
mov %1,%2
push %2
# Rules to remove superflous mov instructions
mov %1,%2
mov %1,%2
=
mov %1,%2
mov %1,%2
.%3:
mov %1,%2
=
.%3:
mov %1,%2
mov %1,%2
mov %2,%1
=
mov %1,%2
mov %1,%[ax|bx|cx|dx|eax|ebx|ecx|edx]2
mov %[ax|bx|cx|dx|eax|ebx|ecx|edx]3,%1
=
mov %1,%2
mov %3,%2
mov %1,%1
=
!mov %1,%1
# Sometimes the compiler puts a jump right after a ret or another jump
# (in if-else-if constructions). Eliminate the second unnecessary jump.
# Also it puts a jump to the next instruction in extensive if-else
# constructions. That jump can also be removed.
jmp .%1
jmp .%2
=
jmp %1
ret
jmp %2
=
ret
jmp .%1
.%1:
=
.%1
jmp .%1
.%2:
.%1:
=
.%2:
.%1:
jmp .%1
.%3:
.%2:
.%1:
=
.%3:
.%2:
.%1:
# When using register variables the compiler initializes them when
# they are found in the source code, and sets up the stack frame for
# other local variables lateron. Move initialization of register variables
# behind the add-sp instruction to make further optimization easier.
proc_start
mov %[si|di]3,%1
add sp,*%2
=
proc_start
add sp,*%2
mov %3,%1
proc_start
mov %[si|di]3,%1
dec sp
dec sp
=
proc_start
dec sp
dec sp
mov %3,%1
add sp,*%1
add sp,*%2
=
add sp,*%1+%2
dec sp
dec sp
add sp,*%1
=
add sp,*%1-2
add sp,*%1
dec sp
dec sp
=
add sp,*%1-2
inc sp
inc sp
%1 %2
inc sp
inc sp
=
%1 %2
add sp,*4
inc sp
inc sp
%1 %2
add sp,*%3
=
%1 %2
add sp,*%3+2
add sp,*%3
%1 %2
inc sp
inc sp
=
%1 %2
add sp,*%3+2
add sp,*%3
%1 %2
add sp,*%4
=
%1 %2
add sp,*%3+%4
|