-
Notifications
You must be signed in to change notification settings - Fork 15
/
asm.html
813 lines (730 loc) · 17.7 KB
/
asm.html
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
<html>
<head>
<title>ÀÑÑÅÌÁËÅÐÍÛÅ ÔÈ×È</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
</head>
<body bgcolor=#B0B0B0 text=#000000 link=#0000FF vlink=#2020C0 alink=#000000>
<h2 align=center>ÀÑÑÅÌÁËÅÐÍÛÅ ÔÈ×È</h2>
<p>Çäåñü ñîáðàíû íåêîòîðûå îòíîñÿùèåñÿ ê àññåìáëåðó ôè÷è,
íå ïóáëèêóåìûå â áîëüøèíñòâå îáû÷íûõ òóòîðèàëîâ.
Íå ñëåäóåò ýòîò òåêñò êàê-ëèáî êîììåíòèðîâàòü, è åñëè ñîäåðæàíèå Âàì ïîêàæåòñÿ
ñëèøêîì ïðîñòûì - òîãäà ñ÷èòàéòå, ÷òî ýòî äîêà äëÿ íà÷èíàþùèõ.<br>
Âñþ ïîëîæèòåëüíóþ ýíåðãèþ, ïîëó÷åííóþ îò ïðî÷òåíèÿ äàííîãî òåêñòà
íàäëåæèò èñïîëüçîâàòü âî ñëàâó äåñòðóêòèâíîãî âèðìýéêèíãà.</p>
<hr>
<a name=top>
<h3 align=center>ÑÎÄÅÐÆÀÍÈÅ</h3>
<ul>
<li><a href=#b>ÍÅÌÍÎÃÎ ÀÐÈÔÌÅÒÈÊÈ</a>
<ul>
<li><a href=#b1>CDQ</a>
<li><a href=#b2>NOT</a>
<li><a href=#b3>NEG</a>
<li><a href=#b4>TEST</a>
<li><a href=#b5>CMP</a>
<li><a href=#b6>SBB</a>
</ul>
<li><a href=#c>ÓÑËÎÂÍÎÅ ÂÛÏÎËÍÅÍÈÅ ÁÅÇ ÏÅÐÅÄÀ×È ÓÏÐÀÂËÅÍÈß</a>
<ul>
<li><a href=#c1>Ïðèìåð 1</a>
<li><a href=#c2>Ïðèìåð 2</a> (íàõîæäåíèå àáñîëþòíîãî çíà÷åíèÿ)
<li><a href=#c3>Ïðèìåð 3</a> (íàõîæäåíèå ìèíèìàëüíîãî çíà÷åíèÿ)
<li><a href=#c4>Ïðèìåð 4</a>
</ul>
<li><a href=#d>BCD-ÎÏÅÐÀÖÈÈ</a>
<ul>
<li><a href=#d1>Ïàðà ñëîâ î BCD</a>
<li><a href=#d2>Äåéñòâèå BCD-êîìàíä</a>
<li><a href=#d3>Î ïðèìåíèìîñòè BCD-÷èñåë</a>
<li><a href=#d4>Ïåðåâîä ÷èñåë â BCD è îáðàòíî</a>
<li><a href=#d5>Ïðèìåð èñïîëüçîâàíèÿ BCD-àðèôìåòèêè</a> (ïîäïðîãðàììà hexword)
</ul>
<li><a href=#e>ÊÎÌÀÍÄÛ MUL È DIV</a>
<ul>
<li><a href=#e1>Óìíîæåíèå íà êîíñòàíòó</a>
<li><a href=#e2>Èñïîëüçîâàíèå MUL âìåñòî DIV</a> (ãåíåðàòîð ñëó÷àéíûõ ÷èñåë)
</ul>
<li><a href=#f>Ñðàâíåíèå ASCIIZ-ñòðîê ñ èñïîëüçîâàíèåì õýø-ôóíêöèé</a>
<li><a href=#g>Âûâîä äåñÿòè÷íûõ ÷èñåë íà ýêðàí (itoa)</a>
</ul>
<hr><br>
<a name=b>
<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td width=10%> <td>
<h3 align=center>ÍÅÌÍÎÃÎ ÂÑÅÌ ÈÇÂÅÑÒÍÎÉ ÀÐÈÔÌÅÒÈÊÈ</H3>
<td width=10%><a href=#top>TOP</a></table>
<a name=b1>
<h4 align=center>CDQ (<u>c</u>onvert <u>d</u>ouble-word to <u>q</u>uad-word)</h4>
<p>Ïðåîáðàçîâàíèå äâîéíîãî ñëîâà (â EAX) â ó÷åòâåðåííîå ñëîâî (â EDX:EAX)<br>
Áîëåå ïîíÿòíûé ýêâèâàëåíò:</p>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<td width=50%>
<pre>
cdq
</pre>
<td>
<pre>
mov edx, eax
sar edx, 31
</pre>
</table>
<a name=b2>
<h4 align=center>NOT</h4>
<p>Èíâåðñèÿ âñåõ áèòîâ îïåðàíäà.</p>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<td width=50%>
<pre>
not eax
</pre>
<td>
<pre>
xor eax, 0FFFFFFFFh
</pre>
</table>
<a name=b3>
<h4 align=center>NEG</h4>
<p>Èçìåíåíèå çíàêà îïåðàíäà.<br>
</p>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<td width=33%>
<pre>
neg eax
</pre>
<td width=33%>
<pre>
xor eax, 0FFFFFFFFh
inc eax
</pre>
<td>
<pre>
eax' = ~eax+1 = -eax
</pre>
</table>
<a name=b4>
<h4 align=center>TEST</h4>
<p>Òî æå ÷òî è AND, íî áåç èçìåíåíèÿ ñîäåðæèìîãî îïåðàíäîâ.
(Èçìåíÿþòñÿ òîëüêî ôëàãè)</p>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<td width=50%>
<pre>
test eax, 123
</pre>
<td>
<pre>
push eax
and eax, 123
pop eax
</pre>
</table>
<a name=b5>
<h4 align=center>CMP</h4>
<p>Ïîíÿòíî, âñå ñëûøàëè ïðî CMP. ;-) Âîò àëüòåðíàòèâíàÿ òî÷êà çðåíèÿ.<br>
Òî æå, ÷òî è SUB, íî áåç èçìåíåíèÿ ñîäåðæèìîãî îïåðàíäîâ.
(Èçìåíÿþòñÿ òîëüêî ôëàãè).<br>
</p>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<td width=50%>
<pre>
cmp eax, 123
</pre>
<td>
<pre>
push eax
sub eax, 123
pop eax
</pre>
</table>
<a name=b6>
<h4 align=center>SBB (<u>s</u>u<u>b</u>tract with <u>b</u>orrow)</h4>
<p>Âû÷èòàíèå ñ çàåìîì. Êðîìå âòîðîãî îïåðàíäà, èç ïåðâîãî âû÷èòàåòñÿ åùå
è CF (Carry Flag, ôëàã ïåðåíîñà).<br>
Íàèáîëåå èíòåðåñíà ôîðìà êîìàíäû ñ îäèíàêîâûìè îïåðàíäàìè.  ýòîì ñëó÷àå
â êàæäûé áèò ðåãèñòðà çàãðóæàåòñÿ çíà÷åíèå CF, òî åñòü ðåãèñòð=0 åñëè CF=0
è ðåãèñòð=0xFFFFFFFF åñëè CF=1.<br>
</p>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<td width=50%>
<pre>
sbb eax, eax
</pre>
<td>
<pre>
if (cf == 0)
eax = 0;
else
eax = -1;
</pre>
</table>
<hr><br>
<a name=c>
<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td width=10%> <td>
<h3 align=center>ÓÑËÎÂÍÎÅ ÂÛÏÎËÍÅÍÈÅ ÁÅÇ ÏÅÐÅÄÀ×È ÓÏÐÀÂËÅÍÈß</h3>
<td width=10%><a href=#top>TOP</a></table>
<p>À âîò íåñêîëüêî ïðèâîäÿùèõ â óæàñ ïðèìåðîâ ñ èñïîëüçîâàíèåì
âûøåïåðå÷èñëåííûõ êîìàíä.
Àññåìáëåðíûé êîä ïðèìåðîâ âçÿò èç êàêîé-òî äîêè ïî îïòèìèçàöèè äëÿ ïíÿ.</p>
<a name=c1>
<h4 align=center>Ïðèìåð 1</h4>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<td width=50%>
<pre>
cmp eax, ebx
sbb ecx, ecx
</pre>
<td>
<pre>
if (eax >= ebx)
ecx = 0;
else
ecx = -1;
</pre>
</table>
<a name=c2>
<h4 align=center>Ïðèìåð 2<br>(íàõîæäåíèå àáñîëþòíîãî çíà÷åíèÿ)</h4>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<th>
<th>eax >= 0
<th>eax < 0
<th>
<tr>
<td width=25%>
<pre>
mov edx, eax
sar edx, 31
xor eax, edx
sub eax, edx
</pre>
<td width=25%>
<pre>
edx = eax
edx = 0
eax ^= 0
eax -= 0
</pre>
<td width=25%>
<pre>
edx = eax
edx = 0xFFFFFFFF=-1
eax' = ~eax
eax'' = eax'-edx =
= ~eax-(-1) =
= ~eax+1 =
= -eax
</pre>
<td>
<pre>
if (a < 0) a = -a;
//a = abs(a)
</pre>
</table>
<a name=c3>
<h4 align=center>Ïðèìåð 3<br>(íàõîæäåíèå ìèíèìàëüíîãî çíà÷åíèÿ)</h4>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<th>
<th>ebx >= ebx
<th>ebx < eax
<th>
<tr>
<td width=25%>
<pre>
sub ebx, eax
sbb ecx, ecx
and ecx, ebx
add eax, ecx
</pre>
<td width=25%>
<pre>
ebx'= ebx-eax
ecx = 0
ecx' = 0
eax' += 0
</pre>
<td width=25%>
<pre>
ebx' = ebx-eax
ecx = 0xFFFFFFFF
ecx' = ebx' = ebx - eax
eax' = eax + ecx' =
= eax + (ebx - eax) =
= ebx
</pre>
<td>
<pre>
if (a > b) a = b;
//a = min(a,b);
</pre>
</table>
<a name=c4>
<h4 align=center>Ïðèìåð 4</h4>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<th>
<th>eax = 0
<th>eax != 0
<th>
<tr>
<td width=25%>
<pre>
cmp eax, 1
sbb eax, eax
and ecx, eax
xor eax, -1
and eax, ebx
or eax, ecx
</pre>
<td width=25%>
<pre>
cf = 1
eax = 0FFFFFFFFh
ecx' = ecx
eax' = 0
eax'' = 0
eax''' = ecx
</pre>
<td width=25%>
<pre>
cf = 0
eax = 0
ecx' = 0
eax' = 0FFFFFFFFh
eax'' = ebx
eax''' = eax'' | ecx' =
= ebx | 0 =
= ebx
</pre>
<td>
<pre>
if (a != 0)
a = b;
else
a = c;
</pre>
</table>
<hr><br>
<a name=d>
<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td width=10%> <td>
<h3 align=center>ÂÑÅÌÈ ËÞÁÈÌÛÅ BCD-ÎÏÅÐÀÖÈÈ</h3>
<td width=10%><a href=#top>TOP</a></table>
<p>Çàìå÷åíî, ÷òî ìíîãèå ïðîãðàììèñòû áîÿòñÿ òàêèõ êîìàíä, êàê AAA, AAS, ASS ;-) è íåêîòîðûõ äðóãèõ.
Ïðè÷èíà ýòîãî (ïî Ôðýéäó) êðîåòñÿ ãëóáîêî â äåòñòâå è ñâÿçàíà ñ ñåêñóàëüíûìè
ïåðåæèâàíèÿìè, íå ïîääàþùèìèñÿ ïñèõîàíàëèçó. ;-))</p>
<a name=d1>
<h4 align=center>Ïàðà ñëîâ î BCD</h4>
<p>BCD -- (<u>b</u>inary <u>c</u>oded <u>d</u>ecimal) -- äâîè÷íî-äåñÿòè÷íûå ÷èñëà.
Âûäóìàâøèå èõ ðàçðàáîò÷èêè áûëè ëåíòÿè è ìàçåðôàêåðû,
ïîýòîìó BCD-÷èñëà ÷èñëà áûâàþò äâóõ âèäîâ -- óïàêîâàíûå è íåóïàêîâàííûå.</p>
<ul>
<li>Packed BCD -- ýòî áàéòû, êàæäàÿ òåòðàäà (4 áèòà) êîòîðûõ èçìåíÿåòñÿ îò 0 äî 9,
òî åñòü 00h,01h,...,09h,10h,11h,...,99h.
<li>Unpacked BCD -- ýòî ïðîñòî áàéòû, èçìåíÿþùèåñÿ îò 0 äî 9, à ñòàðøàÿ èõ
òåòðàäà ðàâíà 0.
</ul>
<p>Ïîíÿòíî, ÷òî íîðìàëüíûå àðèôìåòè÷åñêèå (ADD/SUB/MUL/DIV) îïåðàöèè ñ òàêèìè
÷èñëàìè íåâîçìîæíû, ïîýòîìó âìåñòî òîãî ÷òîáû èõ âûêèíóòü íà ïîìîéêó ëþäè
çàíèìàþòñÿ ñ íèìè ìàçîõèçìîì â âèäå êîìàíä AAA, AAS, AAM, AAD, DAA è DAS.</p>
<p>Êîìàíäû DAA è DAS ïðåäíàçíà÷åíû äëÿ ðàáîòû ñ Packed BCD.
Êîìàíäû AAA, AAS, AAM è AAD ïðåäíàçíà÷åíû äëÿ Unpacked BCD.
Êîìàíäà AAD -- ýòî êîððåêöèÿ ÏÅÐÅÄ äåëåíèåì, âñå îñòàëüíûå -- êîððåêöèè
ÏÎÑËÅ ñîîòâåòñòâóþùèõ îïåðàöèé.</p>
<a name=d2>
<h4 align=center>Äåéñòâèå BCD-êîìàíä</h4>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<th width=50%>AAA
<th>AAS
<tr>
<td align=center><u>A</u>scii <u>A</u>djust after <u>A</u>ddition
<td align=center><u>A</u>scii <u>A</u>djust after <u>S</u>ubstraction
<tr>
<td align=center>ASCII-êîððåêöèÿ ïîñëå ñëîæåíèÿ
<td align=center>ASCII-êîððåêöèÿ ïîñëå âû÷èòàíèÿ
<tr>
<td>
<pre>
if ((al & 0x0F) > 9) || (AF == 1)
{
al = (al + 6) & 0x0F
ah = ah + 1
AF = 1
CF = 1
}
else
{
CF = 0
AF = 0
}
</pre>
<td>
<pre>
if ((al & 0x0F) > 9) || (AF == 1)
{
al = al - 6
al = al & 0x0F
ah = ah - 1
AF = 1
CF = 1
}
else
{
CF = 0
AF = 0
}
</pre>
<tr>
<th width=50%>AAM
<th>AAD
<tr>
<td align=center><u>A</u>scii <u>A</u>djust after <u>M</u>ultiplication
<td align=center><u>A</u>scii <u>A</u>djust before <u>D</u>ivision
<tr>
<td align=center>ASCII-êîððåêöèÿ ïîñëå óìíîæåíèÿ
<td align=center>ASCII-êîððåêöèÿ ïåðåä äåëåíèåì
<tr>
<td colspan=2 align=center>îïåðàíä ïî óìîë÷àíèþ = 10
<tr>
<td>
<pre>
AAM xx al' = al mod xx
ah' = al div xx
AAM 0 div 0
AAM 1 mov ah, al
mov al, 0
</pre>
<td>
<pre>
AAD xx add al, ah * xx
mov ah, 0
AAD 0 mov ah, 00
AAD 1 add al, ah
mov ah, 0
</pre>
<tr>
<th width=50%>DAA
<th>DAS
<tr>
<td align=center><u>D</u>ecimal <u>A</u>djust after <u>A</u>ddition
<td align=center><u>D</u>ecimal <u>A</u>djust after <u>S</u>ubstraction
<tr>
<td align=center>äåñÿòè÷íàÿ êîððåêöèÿ ïîñëå ñëîæåíèÿ
<td align=center>äåñÿòè÷íàÿ êîððåêöèÿ ïîñëå âû÷èòàíèÿ
<tr>
<td>
<pre>
if ((al & 0x0F) > 9) || (AF == 1)
{
al = al + 6
AF = 1
}
else
AF = 0
if (al > 0x9F) || (CF == 1)
{
al = al + 0x60
CF = 1
}
else
CF = 0
</pre>
<td>
<pre>
if ((al & 0x0F) > 9) || (AF == 1)
{
al = al - 6
AF = 1
}
else
AF = 0
if (al > 0x9F) || (CF == 1)
{
al = al - 0x60
CF = 1
}
else
CF = 0
</pre>
</table>
<a name=d3>
<h4 align=center>Î ïðèìåíèìîñòè BCD-÷èñåë</h4>
<p>Ýòî ìîæåò ïîêàçàòüñÿ ñòðàííûì, íî BCD èíîãäà èñïîëüçóþòñÿ â ðåàëüíîé æèçíè.</p>
<p>Íàïðèìåð ñàìûé òèïè÷íûé âèðìýéêåðñêèé îáúåêò -- òàéìåð êîìïüþòåðà,
òî áèøü âðåìÿ è äàòà, õðàíÿùèåñÿ â CMOS, íàõîäÿòñÿ â Packed BCD ôîðìàòå.</p>
<p>Ôëàã AF (Auxilary Flag) -- ôëàã âñïîìîãàòåëüíîãî ïåðåíîñà --
ñóùåñòâóåò êàê ðàç äëÿ BCD àðèôìåòèêè. Ðàáîòàåò òàê æå, êàê è CF, íî äëÿ áèòà 3 à íå 7.</p>
<p>È äàæå â íàáîðå êîìàíä ñîïðîöåññîðà ïðèñóòñòâóþò êîìàíäû
äëÿ ðàáîòû ñ BCD: FBLD -- çàãðóçèòü è FBSTP -- ñîõðàíèòü ÷èñëî â BCD ôîðìàòå.</p>
<a name=d4>
<h4 align=center>Ïåðåâîä ÷èñåë â BCD è îáðàòíî</h4>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<th width=50%>BCD --> Binary
<th>Binary --> BCD
<tr>
<td>
<pre>
aam 16
aad 10
</pre>
<td>
<pre>
aam 10
aad 16
</pre>
</table>
<a name=d5>
<h4 align=center>Ïðèìåð èñïîëüçîâàíèÿ BCD-àðèôìåòèêè<br></h4>
<p>Êîãäà-òî îäèí çíàêîìûé (íàïèñàâøèé êàê-òî âåñüìà êðóòîé âèðóñ)
çàäàë ìíå èíòåðåñíóþ çàäà÷êó -- íàïèñàòü ïîäïðîãðàììó âûäà÷è
øåñòíàäöàòèðè÷íîãî ÷èñëà íà ýêðàí (DOS, ÷èñëî â AX)
-- íàèìåíüøåé âîçìîæíîé äëèíû.</p>
<p>Âîò íåñêîëüêî âàðèàíòîâ ðåøåíèÿ:</p>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<th>20 áàéò
<th>21 áàéò
<th>21 áàéò
<th>21 áàéò
<tr>
<td width=25%>
<pre>
hexword:
mov cx, 4
cycle:
rol ax, 4
push ax
<b>
and al,15
daa
add al,-10h
adc al,'0'+10h
</b>
int 29h
pop ax
loop cycle
ret
</pre>
<td width=25%>
<pre>
hexword:
mov cx, 4
cycle:
rol ax, 4
push ax
<b>
and al,15
sub al,'A'-'0'
aam '9'-'A'+1
add al,'A'
</b>
int 29h
pop ax
loop cycle
ret
</pre>
<td width=25%>
<pre>
hexword:
mov cx, 4
cycle:
rol ax, 4
push ax
<b>
and al, 15
aam 10
aad 'A'-'0'
add al, '0'
</b>
int 29h
pop ax
loop cycle
ret
</pre>
<td>
<pre>
hexword:
mov cx, 4
cycle:
rol ax, 4
push ax
<b>
and al, 15
add al, 90h
daa
adc al, 'A'-1
daa
</b>
int 29h
pop ax
loop cycle
ret
</pre>
</table>
<hr><br>
<a name=e>
<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td width=10%> <td>
<h3 align=center>ÍÅÑÊÎËÜÊÎ ÑËÎÂ Î ÊÎÌÀÍÄÀÕ MUL È DIV</h3>
<td width=10%><a href=#top>TOP</a></table>
<a name=e1>
<h4 align=center>Óìíîæåíèå íà êîíñòàíòó</h4>
<p>Ïðè óìíîæåíèè ÷èñåë íà ïîñòîÿííûé ìíîæèòåëü (äëÿ óâåëè÷åíèÿ ñêîðîñòè)
âìåñòî MUL ìîæíî èñïîëüçîâàòü ñåðèè èç SHL è ADD(OR).<br>
Ïðèìåð: Äëÿ ìíîãèõ íå ñåêðåò, ÷òî â äåìêàõ òðåáóåòñÿ ïèñàòü êîä, âûïîëíÿþùèéñÿ ñ íàèáîëüøåé ñêîðîñòüþ.
Ðàññìîòðèì òèïè÷íûé ïðèìåð -- óìíîæåíèå íà 320.</p>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<td width=50%>
<pre>
imul eax, 320
</pre>
<td>
<pre>
shl eax, 6 ; *64
mov ebx, eax
shl ebx, 2 ; *256
add eax, ebx
</pre>
</table>
<a name=e2>
<h4 align=center>Èñïîëüçîâàíèå MUL âìåñòî DIV</h4>
<p>Ðàññìîòðèì èíòåðåñíûé ñëó÷àé. Ãåíåðàòîð ñëó÷àéíûõ ÷èñåë, àëãîðèòì -
ïî÷òè êàê ó ÒóðáîÏàñêàëÿ 7.0, ïåðèîä 2^32.</p>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<td>
<pre>
randseed dd ?
; void randomize() { randseed = GetTickCount(); }
randomize: call GetTickCount
mov randseed, eax
ret
; DWORD random() { return randseed = randseed * 0x8088405 + 1; }
random: mov eax, randseed
imul eax, 8088405h
inc eax
mov randseed, eax
ret
; DWORD rnd(DWORD range) { return random() % range; }
rnd: xchg ebx, eax ; now EBX=range
call random
xor edx, edx
<b> mul ebx ; (*)</b>
xchg edx, eax
ret
</pre>
</table>
<p>Îáðàòèì âíèìàíèå íà ñòðî÷êó ñ (*), òî áèøü íà MUL.<br>
Íåî÷åâèäíî, íî ôàêò - çäåñü åãî äåéñòâèå ïî÷òè òàêîå æå êàê è ó DIV.
Áîëåå òîãî - â ýòîé ñèòóàöèè MUL ðàáîòàåò ïðèìåðíî â 1.5 ðàçà áûñòðåå,
à â ñëó÷àå íóëåâîãî ïàðàìåòðà -- rnd(0) -- â ðåçóëüòàòå âîçâðàùàåòñÿ 0,
òîãäà êàê ïðè DIV ïðîèçîøëà áû îøèáêà äåëåíèÿ.</p>
<p>Íåñëîæíî äîãàäàòüñÿ, ÷òî âîçíèêàåò òàêàÿ ñèòóàöèÿ òîëüêî â êîíêðåòíîì
ñëó÷àå, êîãäà îïåðàíä äëÿ DIV(MUL) -- ñëó÷àéíîå 16- èëè 32-áèòíîå ÷èñëî.
</p>
<a name=f>
<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td width=10%> <td>
<h3 align=center>Ñðàâíåíèå ASCIIZ-ñòðîê ñ èñïîëüçîâàíèåì õýø-ôóíêöèé</h3>
<td width=10%><a href=#top>TOP</a></table>
<p>Ïðåäïîëîæèì, ÷òî íàì íóæíî ñðàâíèòü äâå ASCIIZ-ñòðîêè (îáû÷íûå çàêàí÷èâàþùèåñÿ íóëåì òåêñòîâûå ñòðîêè).
Ðàññìàòðèâàåìîå äåéñòâèå - ïîñ÷èòàòü îò ñòðî÷åê íåêîòîðóþ õýø-ôóíêöèþ (êîíòðîëüíóþ ñóììó, etc.), è ñðàâíèâàòü óæå
ýòè ôóíêöèè.
Ïðè èçâåñòíîì íàáîðå ñòðî÷åê è/èëè îãðàíè÷åíèÿõ íà íèõ, îøèáêè èñêëþ÷åíû.
Ïðåèìóùåñòâî òàêîãî ñðàâíåíèÿ â ñëó÷àÿõ:</p>
<ul>
<li>â ñëó÷àå áîëüøîãî êîëè÷åñòâà ñòðîê è çàðàíåå ïîäñ÷èòàííûõ êîíòðîëüíûõ ñóìì
-- óâåëè÷èâàåòñÿ ñêîðîñòü ðàáîòû ïðîãðàììû
<li>â ñëó÷àå âèðóñîâ -- óìåíüøàåòñÿ ðàçìåð êîäà/äàííûõ
è çàòðóäíÿåòñÿ èçó÷åíèå (äèñàññåìáëèðîâàíèå)
</ul>
<p>Âîò êóñîê êîäà, êîòîðûé ïîçâîëÿåò âûòàñêèâàòü èç ýêñïîðòà
àäðåñà ôóíêöèé ïî õýøàì èõ èìåí.</p>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<td>
<pre>
<b>calchash macro procname
hash = 0
irpc c, <procname>
; rol 7
hash = ((hash shl 7) and 0FFFFFFFFh) or (hash shr (32-7))
hash = hash xor "&c"
endm
endm</b>
mov_h macro reg, procname
calchash procname
mov reg, hash
endm
j_GetProcAddress dd ?
mov_h edi, GetProcAddress
call findfunc
jz exit
mov j_GetProcAddress, eax
; find function's address in export table
;
; input: EBX=imagebase va, ECX=export table va, EDI=name csum
; modify: EDX, ESI
; output: ZF=1, EAX=0 (function not found)
; ZF=0, EAX=function va
findfunc: xor esi, esi ; current index
search_cycle: lea edx, [esi*4+ebx]
add edx, [ecx].ex_namepointersrva
mov edx, [edx] ; name va
add edx, ebx ; +imagebase
<b>
xor eax, eax ; calculate hash
calc_hash: rol eax, 7
xor al, [edx]
inc edx
cmp byte ptr [edx], 0
jne calc_hash
</b>
cmp eax, edi ; compare hashs
je name_found
inc esi ; index++
cmp esi, [ecx].ex_numofnamepointers
jb search_cycle
xor eax, eax ; return 0
ret
name_found: mov edx, [ecx].ex_ordinaltablerva
add edx, ebx ; +imagebase
movzx edx, word ptr [edx+esi*2]; edx=current ordinal
; sub edx, [ecx].ex_ordinalbase ; -ordinal base
mov eax, [ecx].ex_addresstablerva
add eax, ebx ; +imagebase
mov eax, [eax+edx*4]; eax=current address
add eax, ebx ; +imagebase
ret ; return address
</pre>
</table>
<hr><br>
<a name=g>
<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td width=10%> <td>
<h3 align=center>Âûâîä äåñÿòè÷íûõ ÷èñåë íà ýêðàí (itoa)</h3>
<td width=10%><a href=#top>TOP</a></table>
<p>Äîñòàòî÷íî èíòåðåñíàÿ çàäà÷à, ê òîìó æå ïðîöåäóðà âûâîäà ÷èñëà
âåñüìà óíèâåðñàëüíà.</p>
<table width=100% border=1 cellspacing=0 cellpadding=0>
<tr>
<td>
<pre>
itoa: xor edx, edx
mov ebx, 10 ; îñíîâàíèå ñèñòåìû ñ÷èñëåíèÿ
div ebx
push edx ; çàïîìíèòü îñòàòîê îò äåëåíèÿ
or eax, eax ; ÷àñòíîå == 0 ?
jz @@1
call itoa ; ðåêóðñèâíûé âûçîâ (åñëè åñòü ÷òî äåëèòü)
@@1: pop eax
add al, '0'
... ; âûâîä ñèìâîëà (stosb, INT 29h, etc.)
ret
</pre>
</table>
<hr><br>
<p al