-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimized_code.asm
646 lines (646 loc) · 6.79 KB
/
optimized_code.asm
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
.MODEL SMALL
.STACK 1000H
.DATA
number DB "00000$"
arr DW 5 DUP (0000H)
a DW 1 DUP (0000H)
.CODE
f PROC
PUSH BP
MOV BP, SP
MOV AX, [BP+8]
CALL print_output
CALL new_line
L8:
MOV AX, [BP+6]
CALL print_output
CALL new_line
L7:
MOV AX, [BP+4]
CALL print_output
CALL new_line
L6:
MOV CX, 441
PUSH CX
MOV CX, 1
LEA SI, arr
ADD SI, CX
ADD SI, CX
PUSH BP
MOV BP, SI
POP AX
POP CX
MOV [BP], CX
MOV BP, AX
L5:
MOV CX, 555
PUSH CX
MOV CX, 0
LEA SI, arr
ADD SI, CX
ADD SI, CX
PUSH BP
MOV BP, SI
POP AX
POP CX
MOV [BP], CX
MOV BP, AX
L4:
MOV CX, 1
LEA SI, arr
ADD SI, CX
ADD SI, CX
PUSH BP
MOV BP, SI
MOV CX, [BP]
POP BP
MOV [BP+6], CX
L3:
MOV AX, [BP+6]
CALL print_output
CALL new_line
L2:
MOV CX, 0
LEA SI, arr
ADD SI, CX
ADD SI, CX
PUSH BP
MOV BP, SI
MOV CX, [BP]
POP BP
JMP f_L
L1:
f_L:
POP BP
RET
f ENDP
recursive PROC
PUSH BP
MOV BP, SP
MOV CX, [BP+4]
PUSH CX
MOV CX, 1
POP AX
CMP AX, CX
JE L12
JMP L11
L12:
MOV CX, 1
JMP recursive_L
L11:
MOV CX, [BP+4]
PUSH CX
MOV CX, 0
POP AX
CMP AX, CX
JE L13
JMP L10
L13:
MOV CX, 0
JMP recursive_L
L10:
MOV CX, [BP+4]
PUSH CX
MOV CX, 1
POP AX
SUB CX, AX
NEG CX
PUSH CX
CALL recursive
ADD SP, 2
PUSH CX
MOV CX, [BP+4]
PUSH CX
MOV CX, 2
POP AX
SUB CX, AX
NEG CX
PUSH CX
CALL recursive
ADD SP, 2
POP AX
ADD CX, AX
JMP recursive_L
L9:
recursive_L:
POP BP
RET
recursive ENDP
v PROC
PUSH BP
MOV BP, SP
MOV CX, 3
MOV a, CX
L16:
MOV CX, a
CMP CX, 0
JE L15
JMP L17
L17:
SUB SP, 2
L20:
MOV CX, 1
MOV [BP-2], CX
L19:
MOV AX, [BP-2]
CALL print_output
CALL new_line
L18:
ADD SP, 2
L15:
MOV AX, a
CALL print_output
CALL new_line
L14:
v_L:
POP BP
RET
v ENDP
main PROC
MOV AX, @DATA
MOV DS, AX
PUSH BP
MOV BP, SP
SUB SP, 2
SUB SP, 2
SUB SP, 2
SUB SP, 2
SUB SP, 10
L70:
MOV CX, 5
MOV [BP-8], CX
L69:
CALL v
L68:
MOV AX, [BP-8]
CALL print_output
CALL new_line
L67:
MOV CX, 0
MOV [BP-2], CX
L71:
MOV CX, [BP-2]
PUSH CX
MOV CX, 5
POP AX
CMP AX, CX
JL L72
JMP L66
L72:
MOV CX, [BP-2]
PUSH CX
MOV CX, 1
POP AX
ADD CX, AX
PUSH CX
MOV CX, [BP-2]
PUSH BP
MOV BX, CX
ADD BX, BX
ADD BX, -18
ADD BP, BX
POP AX
POP CX
MOV [BP], CX
MOV BP, AX
L74:
MOV CX, [BP-2]
MOV AX, CX
INC CX
MOV [BP-2], CX
MOV CX, AX
JMP L71
L66:
MOV CX, 4
MOV [BP-2], CX
L65:
L75:
MOV CX, [BP-2]
MOV AX, CX
DEC CX
MOV [BP-2], CX
MOV CX, AX
CMP CX, 0
JE L64
JMP L76
L76:
MOV CX, [BP-2]
PUSH BP
MOV BX, CX
ADD BX, BX
ADD BX, -18
ADD BP, BX
MOV CX, [BP]
POP BP
MOV [BP-4], CX
L78:
MOV AX, [BP-4]
CALL print_output
CALL new_line
L77:
JMP L75
L64:
MOV CX, 2
MOV [BP-6], CX
L63:
MOV CX, [BP-6]
PUSH CX
MOV CX, 0
POP AX
CMP AX, CX
JG L80
JMP L79
L80:
MOV CX, [BP-6]
MOV AX, CX
INC CX
MOV [BP-6], CX
MOV CX, AX
JMP L62
L79:
MOV CX, [BP-6]
MOV AX, CX
DEC CX
MOV [BP-6], CX
MOV CX, AX
L62:
MOV AX, [BP-6]
CALL print_output
CALL new_line
L61:
MOV CX, 2
NEG CX
MOV [BP-6], CX
L60:
MOV CX, [BP-6]
PUSH CX
MOV CX, 0
POP AX
CMP AX, CX
JL L82
JMP L81
L82:
MOV CX, [BP-6]
MOV AX, CX
INC CX
MOV [BP-6], CX
MOV CX, AX
JMP L59
L81:
MOV CX, [BP-6]
MOV AX, CX
DEC CX
MOV [BP-6], CX
MOV CX, AX
L59:
MOV AX, [BP-6]
CALL print_output
CALL new_line
L58:
MOV CX, 121
MOV [BP-6], CX
L57:
MOV CX, [BP-6]
NEG CX
MOV [BP-6], CX
L56:
MOV CX, 5
MOV [BP-2], CX
L55:
MOV CX, [BP-2]
PUSH CX
MOV CX, [BP-6]
POP AX
ADD CX, AX
MOV [BP-6], CX
L54:
MOV AX, [BP-6]
CALL print_output
CALL new_line
L53:
MOV CX, 4
NEG CX
MOV [BP-6], CX
L52:
MOV CX, [BP-6]
PUSH CX
MOV CX, 4
POP AX
IMUL CX
MOV CX, AX
MOV [BP-6], CX
L51:
MOV AX, [BP-6]
CALL print_output
CALL new_line
L50:
MOV CX, 19
MOV [BP-4], CX
L49:
MOV CX, 4
MOV [BP-2], CX
L48:
MOV CX, [BP-4]
PUSH CX
MOV CX, [BP-2]
POP AX
CWD
IDIV CX
MOV CX, AX
MOV [BP-6], CX
L47:
MOV AX, [BP-6]
CALL print_output
CALL new_line
L46:
MOV CX, [BP-4]
PUSH CX
MOV CX, [BP-2]
POP AX
CWD
IDIV CX
MOV AX, DX
MOV CX, AX
MOV [BP-6], CX
L45:
MOV AX, [BP-6]
CALL print_output
CALL new_line
L44:
MOV CX, 111
PUSH CX
MOV CX, 222
PUSH CX
MOV CX, 333
PUSH CX
CALL f
ADD SP, 6
PUSH CX
MOV CX, 444
POP AX
SUB CX, AX
NEG CX
MOV [BP-6], CX
L43:
MOV AX, [BP-6]
CALL print_output
CALL new_line
L42:
MOV CX, 5
PUSH CX
CALL recursive
ADD SP, 2
MOV [BP-6], CX
L41:
MOV AX, [BP-6]
CALL print_output
CALL new_line
L40:
MOV CX, 2
MOV [BP-6], CX
L39:
MOV CX, 1
MOV [BP-2], CX
L38:
MOV CX, [BP-2]
PUSH CX
MOV CX, [BP-6]
POP AX
CMP AX, 0
JE L84
JMP L85
L84:
CMP CX, 0
JE L86
L85:
MOV CX, 1
L86:
MOV [BP-4], CX
L37:
MOV AX, [BP-4]
CALL print_output
CALL new_line
L36:
MOV CX, [BP-2]
PUSH CX
MOV CX, [BP-6]
POP AX
CMP AX, 0
JE L88
CMP CX, 0
JE L88
JMP L89
L88:
MOV CX, 0
JMP L90
L89:
MOV CX, 1
L90:
MOV [BP-4], CX
L35:
MOV AX, [BP-4]
CALL print_output
CALL new_line
L34:
MOV CX, 2
MOV [BP-6], CX
L33:
MOV CX, 0
MOV [BP-2], CX
L32:
MOV CX, [BP-2]
PUSH CX
MOV CX, [BP-6]
POP AX
CMP AX, 0
JE L92
JMP L93
L92:
CMP CX, 0
JE L94
L93:
MOV CX, 1
L94:
MOV [BP-4], CX
L31:
MOV AX, [BP-4]
CALL print_output
CALL new_line
L30:
MOV CX, [BP-2]
PUSH CX
MOV CX, [BP-6]
POP AX
CMP AX, 0
JE L96
CMP CX, 0
JE L96
JMP L97
L96:
MOV CX, 0
JMP L98
L97:
MOV CX, 1
L98:
MOV [BP-4], CX
L29:
MOV AX, [BP-4]
CALL print_output
CALL new_line
L28:
MOV CX, [BP-6]
CMP CX, 0
JNE L99
MOV CX, 1
JMP L100
L99:
MOV CX, 0
L100:
MOV [BP-4], CX
L27:
MOV AX, [BP-4]
CALL print_output
CALL new_line
L26:
MOV CX, [BP-4]
CMP CX, 0
JNE L101
MOV CX, 1
JMP L102
L101:
MOV CX, 0
L102:
MOV [BP-4], CX
L25:
MOV AX, [BP-4]
CALL print_output
CALL new_line
L24:
MOV CX, 12
PUSH CX
MOV CX, 2
PUSH CX
MOV CX, 89
POP AX
CWD
IDIV CX
MOV CX, AX
PUSH CX
MOV CX, 3
PUSH CX
MOV CX, 33
POP AX
SUB CX, AX
NEG CX
PUSH CX
MOV CX, 64
PUSH CX
MOV CX, 2
POP AX
IMUL CX
MOV CX, AX
POP AX
ADD CX, AX
POP AX
CWD
IDIV CX
MOV AX, DX
MOV CX, AX
POP AX
ADD CX, AX
PUSH CX
MOV CX, 3
POP AX
SUB CX, AX
NEG CX
PUSH CX
MOV CX, 3
PUSH CX
MOV CX, 59
PUSH CX
MOV CX, 9
POP AX
CWD
IDIV CX
MOV CX, AX
PUSH CX
MOV CX, 2
POP AX
IMUL CX
MOV CX, AX
POP AX
ADD CX, AX
PUSH CX
MOV CX, 4
POP AX
SUB CX, AX
NEG CX
POP AX
ADD CX, AX
MOV [BP-4], CX
L23:
MOV AX, [BP-4]
CALL print_output
CALL new_line
L22:
MOV CX, 0
JMP main_L
L21:
main_L:
MOV AX, 4CH
INT 21H
ADD SP, 18
POP BP
main ENDP
new_line proc
push ax
push dx
mov ah, 2
mov dl, 0Dh
int 21h
mov ah, 2
mov dl, 0Ah
int 21h
pop dx
pop ax
ret
new_line endp
print_output proc
push ax
push bx
push cx
push dx
push si
lea si, number
mov bx, 10
add si, 4
cmp ax, 0
jnge negate
print:
xor dx, dx
div bx
mov [si], dl
add [si], '0'
dec si
cmp ax, 0
jne print
inc si
lea dx, si
mov ah, 9
int 21h
pop si
pop dx
pop cx
pop bx
pop ax
ret
negate:
push ax
mov ah, 2
mov dl, '-'
int 21h
pop ax
neg ax
jmp print
print_output endp
END main