Ïðîãðàììèðîâàíèå íà Àññåìáëåðå


Ïåðåäà÷à â ñïèñêå ïàðàìåòðîâ îäíîìåðíûõ ìàññèâîâ


Äëÿ îäíîìåðíûõ ìàññèâîâ – è èñõîäíûõ äàííûõ è ðåçóëüòàòîâ – ïåðåäàþòñÿ àäðåñà. Ýòî èñêëþ÷àåò íåîáõîäèìîñòü êîïèðîâàíèÿ èñõîäíûõ ìàññèâîâ.

    

Ïðèìåð 1.  Ñîñòàâèòü ôóíêöèþ äëÿ îáúåäèíåíèÿ äâóõ ñòðîê  è èñïîëüçîâàòü åå äëÿ îáúåäèíåíèÿ ñòðîê, ñîäåðæàùèõ ñëîâà "first  " è "second " â îäíó ñòðîêó.

IDEAL

P586

MODEL FLAT

Extrn ExitProcess:proc

DATASEG

str1   db   'first ', 0

str2   db   'second', 0

rez    db    (rez-str1-1) dup (?)

CODESEG

begin:



push     offset str1 offset str2 offset rez  ; ïåðåäà÷à àäðåñîâ ìàññèâîâ

call strcat  ;âûçîâ ïðîöåäóðû

call ExitProcess

proc strcat;

0

EBP

push ebp;

4

EIP

mov  ebp,  esp;

8

REZ

Push eax ebx ecx;

12

STR2

; for (I=0; str1[I]; I++) rez[I]=str1[i];

16

STR1

Mov EAX, [EBP+16];

Mov EBX, [EBP+8];

For1:

Mov CL, [EAX]

Test Cl, cl

Je break1

Mov [EBX], cl

Inc eax

Inc ebx

Jmp For1

Break1:

Mov EAX, [ebp+12]

For2:

Mov CL, [EAX]

Mov [EBX], cl

Test Cl, cl

Je break2

Inc eax

Inc ebx

Jmp For2

Break2:

Pop ecx ebx eax ebp

Ret 12

Endp

End begin

     Â ýòîì ïðèìåðå ïðèçíàêîì êîíöà èñõîäíûõ äàííûõ ÿâëÿåòñÿ  íóëåâîé çàâåðøèòåëü (ñèìâîë ñ êîäîì 0). Â ðåçóëüòèðóþùóþ ñòðîêó çàâåðøèòåëü çàïèñûâàåòñÿ ïîñëå êîïèðîâàíèÿ âòîðîé ñòðîêè. Äëÿ  ìàññèâîâ èñõîäíûõ äàííûõ (str1, str2) è ìàññèâà ðåçóëüòàòà (rez) ïåðåäàþòñÿ àäðåñà. Ýòî äåëàåòñÿ äëÿ  ýêîíîìèè  ñòåêîâîãî  ïðîñòðàí-

ñòâà è ÷èñëà êîìàíä, íåîáõîäèìûõ äëÿ ïåðåäà÷è ïàðàìåòðîâ.

Ïðèìåð 2.  Ñîñòàâèòü ïðîãðàììó äëÿ çàïèñè â òðåòèé ìàññèâ òåõ ÷èñåë ïåðâîãî, êîòîðûõ íåò âî âòîðîì.

ideal

p386

model   flat

extrn ExitProcess:proc

dataseg

x          dd        1,2,3,4,5,6


ALàààààààààààààààààà - ¨õ÷ºû¹ªðª - ñðùª
AXàààààààààààààààààà - ¨õ÷ºû¹ªðª - ¸ûþòþ
EAXàààààààààààààààà - ¨õ÷ºû¹ªðª Ö ôòþùýþõ ¸ûþòþ
EDX, EAXàààààà - ¨õ÷ºû¹ªðª Ö 2 ôòþùýv¿ ¸ûþòð
àààà ¦¨øüõ¨ 3.à Tþ¸ªðòøª¹ ¯ºýú¡ø¦ ôû  ¸¨ðòýõýø  "ôûøýýv¿" ¢ø¸õû. Lºýú¡ø  ôþûöýð òþ÷ò¨ð•ðª¹ 0, õ¸ûø ¢ø¸ûð ¸þòÿðôð¦ª, 1 Ö õ¸ûø ÿõ¨òþõ ¢ø¸ûþ ñþû¹°õ òªþ¨þóþ ø Ö1 ò ÿ¨þªøòýþü ¸ûº¢ðõ.
ideal
p686
modelàà flat
extrn ExitProcess:proc
dataseg
xààààààààà ddààààààà 1000 dup (0ffffffffh)
nààààààààà ddààààààà (n-x)/4
yààààààààà ddààààààà 1000 dup (0ffffffffh)
resàààààà ddààààààà ?
t1àààààààà ddààààààà ?
t2àààààààà ddààààààà ?
codeseg
begin:
pushàààà [n] offset x offset yàà
rdtsc
movàààà ecx,eax
callààààà m_cmp
movàààà [res], eax
rdtsc
subàààààà eax, ecx
movàààà [t1], eax
pushàààà [n] offset x offset yàà
rdtsc
movàààà ecx,eax
callààààà m_cmp
movàààà [res], eax
rdtsc
subàààààà eax, ecx
movàààà [t1], eax
pushàààà [n] offset x offset yàà
rdtsc
movàààà ecx,eax
callààààà m_cmp1
movàààà [res], eax
rdtsc
subàààà eax,ecx
movàààà [t2], eax
call ExitProcess
;for (i=n-1; i>=0; i--){ààààààààààààààààààààààà ebpààààà 0
;ààà if (x[i]>y[i]) return -1;à ààà eipàààààà 4
;ààà if (x[i]<y[i]) return 1;àà àààà yààààààààà 8
;}ààààààààààààààààààààààààààààà àààààààààààààà xààààààààà 12
;return 0;ààààààààààààààààààààà àààààààààààààààààààààà nààààààààà 16
proc ààà m_cmp
push ebp;
movà ebp,à esp;
Pushà ebx ecx edx
movàààà ecx, [ebp+16]; n
movàààà eax, [ebp+12]; &x
movàààà ebx, [ebp+8] ; &y
fori:
movàààà edx, [eax+ecx*4-4]
cmpàààà edx, [ebx+ecx*4-4]
jbàààààààà short m1; x[i]< y[i]
jaàààààààà short m2; x[i]> y[i]
loopàààà fori
subàààààà eax, eax
m1:
movàààà eax, -1
jmpààààà short m3
m2:
movàààà eax, 1
m3:
popààààà edx ecx ebx ebp
Retàààààà 12
Endp
proc ààà m_cmp1
push ebp;
movà ebp,à esp;
Pushàà ecx esi edi
movàààà ecx, [ebp+16]; n
movàààà esi, [ebp+12]; &x
leaàààààà esi, [esi + ecx*4-4]
movàààà edi, [ebp+8] ; &y


leaàààààà edi, [edi + ecx*4-4]
std
repeàààà cmpsd
jbàààààààà short m11; x[i]< y[i]
jaàààààààà short m12; x[i]> y[i]
subàààààà eax, eax
m11:
movàààà eax, -1
jmpààààà short m13
m12:
movàààà eax, 1
m13:
cld
popààààà edi esi ecx ebp
Retàààààà 12
Endp
end begin
¦ðüõªøü, ¢ªþ ë¡ø¯¨v¬ ¢ø¸û𠸨ðòýøò𦪸 , ýð¢øýð  ¸þ ¸ªð¨°õù ¡ø¯¨v. ¦ðöôð  ¡ø¯¨ð ¸¨ðòýøòðõª¸  úðú ñõ÷÷ýðúþòþõ ¢ø¸ûþ. T¨ðòýõýøõ ÿ¨þôþûöðõª¸  ôþ ªõ¿ ÿþ¨, ÿþúð ýðùôõü ¨ð÷ývõ ¡ø¯¨v øûø ýõ ¸¨ðòýøü ò¸õ ¡ø¯¨v ¢ø¸ûð.
+ñ¨ðªøªõ òýøüðýøõ ýð ýþòvõ úþüðýôv ôû  þÿ¨õôõûõýø  úþûø¢õ¸ªòð ªðúªþò ôû  òvÿþûýõýø  ¯ºýú¡øù. ¦þüðýôð RDTSC (Read Time-stamp counter) - òvÿþûý õª¸  ôû  ÿ¨þ¡õ¸¸þ¨þò, ýð¢øýð  ¸ PENTIUM MMX ø òþ÷ò¨ð•ðõª ò ¨õóø¸ª¨õ EAX
+ªþñv þÿ¨õôõûøª¹, ÿþôôõ¨öøòðõª¸  ûø ôðýýð  úþüðýôð ôû  ÿ¨þ¡õ¸¸þ¨ð, ø¸ÿþû¹÷ºõª¸  úþüðýôð CPUID×CPU Identification
¦ªð úþüðýôð ªðúöõ ÿþ÷òþû õª þÿ¨õôõûøª¹ ÿ¨þø÷òþôøªõû  ÿ¨þ¡õ¸¸þ¨ð, ¸õüõù¸ªòþ, úþªþ¨þüº þý ÿ¨øýðôûõöøª, üþôõû ø òõ¨¸øø, ð ªðúöõ ô¨ºóº¦ øý¯þ¨üð¡ø¦. -û  þÿ¨õôõûõýø  ªøÿð òvôðòðõüþù øý¯þ¨üð¡øø ø¸ÿþû¹÷ºõª¸  ¨õóø¸ª¨ EAX ( ªðñû 11.3). ¦þüðýôð CPUID üþöõª òvÿþûý ª¹¸  ôû  û¦ñþóþ º¨þòý  ÿ¨øòøûõóøù.
Tðñûø¡ð 11.3

T¿þôývõ ôðýývõ
Tv¿þôývõ ôðýývõ
EAX = 0
EAX Ö üðú¸øüðû¹ýþõ ÷ýð¢õýøõ, úþªþ¨þõ üþöýþ ÷ðôðò𪹠ýð ò¿þôõ (þñv¢ýþ 2)
EBX, EDX, ECX Ö ÿ¨þø÷òþôøªõû¹ Ö -û  PENTIUM ¸ª¨þúð ëGenuineIntel¬ ¨ð¸ÿ¨õôõûõýð ªðú: EBXà ?756e6547h (* "Genu", ñºúòð G ò BL *)
EDXà ?49656e69h (* "ineI", ñºúòð i ò -DL *)
ECXà ?6c65746eh (* "ntel", ñºúòðà n ò CL *)
EAX = 1
EAX Ö òõ¨¸ø  ÿ¨þ¡õ¸¸þ¨ð (ªøÿ, ¸õüõù¸ªòþ, üþôõû¹, step)
EBX - ¨õ÷õ¨ò
ECX- ¨õ÷õ¨ò
EDX Ö øý¯þ¨üð¡ø  þñ þ¸þñõýýþ¸ª ¿ ÿ¨þ¡õ¸¸þ¨ð
EAX = 2
EAX Öøý¯þ¨üð¡ø  þ ú¤°õ
EBX -Öøý¯þ¨üð¡ø  þ ú¤°õ
ECX- Öøý¯þ¨üð¡ø  þ ú¤°õ
EDX Ö Öøý¯þ¨üð¡ø  þ ú¤°õ

¦ð¸¸üþª¨øü ¸þôõ¨öøüþõ ªþû¹úþ ýõúþªþ¨v¿ ñøªþò ¨õóø¸ª¨ð EDX:
23 Ö ÿþôôõ¨öøòðõª¸  MMX Ö ªõ¿ýþûþóø ;
18 Ö ÿ¨þ¡õ¸¸þ¨ ÿþôôõ¨öøòðõª 96 ñøªývù ºýøúðû¹ývù ýþüõ¨ ÿ¨þ¡õ¸¸þ¨ð.
4 Ö ÿþôôõ¨öøòðõª¸  úþüðýôð RDTSC
¦¨øüõ¨ 4. Tþ¸ªðòøª¹ ¯ºýú¡ø¦, úþªþ¨ð  òþ÷ò¨ð•ðõª þªòõª 1, õ¸ûø úþüðýôð RDTSC ÿþôôõ¨öøòðõª¸  ø 0 ò ÿ¨þªøòýþü ¸ûº¢ðõ.
ideal
p686
modelàà flat
extrn ExitProcess:proc
dataseg
resàààààà ddààààààà ?
codeseg
begin:
callààààà IsReadCount
movàààà [res], eax
call ExitProcess
proc ààà IsReadCount
movàààà eax, 1
cpuid
movàààà eax, 1
testàààààà edx, 1 shl 4
jnzààààààà short m1
subàààààà eax, eax
m1:
Retàààààà
Endp
end begin

Ñîäåðæàíèå ðàçäåëà