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

       

Ôóíêöèè è êîíñòàíòû äëÿ ðàáîòû ñ êîíñîëüþ


Ïðè ðàáîòå ñ êîíñîëüþ íåîáõîäèìî:

·        îïðåäåëèòü íîìåðà ñòàíäàðòíûõ óñòðîéñòâ (ñì. ôàéë winuser.h);

·        îïðåäåëèòü äåñêðèïòîð äëÿ çàäàííîãî óñòðîéñòâà;

·        îáðàòèòüñÿ ê ôóíêöèè ââîäà – âûâîäà.

Èíôîðìàöèÿ î íîìåðàõ óñòðîéñòâ è òðåáóåìûõ ôóíêöèÿõ ïðèâåäåíà íèæå.

Íîìåðà ñòàíäàðòíûé óñòðîéñòâ

STD_INPUT_HANDLE         (DWORD)-10 ââîä

STD_OUTPUT_HANDLE     (DWORD)-11âûâîä

STD_ERROR_HANDLE        (DWORD)-12 âûâîä îøèáîê

Îïðåäåëåíèå äåñêðèïòîðà óñòðîéñòâà:

HANDLE GetStdHandle(    DWORD nStdHandle     // íîìåð óñòðîéñòâà



   );      

Åñëè îøèáêà, ôóíêöèÿ âîçâðàùàåò INVALID_HANDLE_VALUE, êîòîðîìó ñîîòâåòñòâóåò çíà÷åíèå –1.

Ââîä äàííûõ ñî ñòàíäàðòíîãî óñòðîéñòâà:

BOOL ReadConsole(

    HANDLE hConsoleInput,   // äåñêðèïòîð êîíñîëè

    LPVOID lpBuffer,   // àäðåñ áóôåðà, êóäà ÷èòàòü äàííûå

    DWORD nNumberOfCharsToRead,          // êîëè÷åñòâî ÷èòàåìûõ ñèìâîëîâ

    LPDWORD lpNumberOfCharsRead,         // àäðåñ êîëè÷åñòâà ïðî÷èòàííûõ ñèìâîëîâ

    LPVOID lpReserved           // ðåçåðâ, äîëæåí áûòü NULL

   );      

Ôóíêöèÿ âîçâðàùàåò true â ñëó÷àå óñïåõà è false â ñëó÷àå îøèáêè[19]

BOOL WriteConsole(

    HANDLE hConsoleOutput,            // äåñêðèïòîð êîíñîëè

    CONST VOID *lpBuffer,    // àäðåñ áóôåðà äëÿ çàïèñè

    DWORD nNumberOfCharsToWrite,          // êîëè÷åñòâî ñèìâîëîâ

    LPDWORD lpNumberOfCharsWritten,     // àäðåñ êîëè÷åñòâà ñèìâîëîâ, êîòîðûå äåéñòâèòåëüíî çàïèñàíû

    LPVOID lpReserved           // Ðåçåðâ, äîëæåí áûòü NULL

   );      

Ïðèìåð ïðîãðàììû äëÿ ââîäà – âûâîäà ñòðîêè ñèìâîëîâ.

ideal

p386

model   flat

extrn ExitProcess:proc

extrn GetStdHandle:proc

extrn ReadConsoleA:proc

extrn WriteConsoleA:proc

dataseg

include "win.inc"

din  dd   0

dout dd   0

buf  db   80 dup (?)

siz  dd   ?

mystr     db   'Input string, please', 13, 10, 0

codeseg

begin:

push     STD_INPUT_HANDLE


push     str1
push     0
call      MessageBoxA
endm
Ôàéë äëÿ ïðîâåðêè ìàêðîñà
Ideal
p586
model   flat
extrn ExitProcess:proc
extrn MessageBoxA:proc
include "win.inc"
dataseg
t   db      'Information', 0
text    db      'Hello, world!', 0
codeseg
begin:
ShowMessage <offset text>, <offset t>, 0
call    ExitProcess
end     begin
 
Ïðèìåð 2. Ñîñòàâèòü ôóíêöèþ gets äëÿ ââîäà ñòðîêè ñ êëàâèàòóðû, êîòîðàÿ â ðåçóëüòàòå ôîðìèðóåò ñòðîêó ñ íóëåâûì çàâåðøèòåëåì. Ïðèçíàê êîíöà ââîäèìîé ñòðîêè- ñèìâîë <Enter>.
ideal
p586
model  flat
include "win.inc"
extrn   GetStdHandle:proc
extrn   ReadConsoleA: proc
codeseg
proc     gets
public  gets
arg       mystr:dword
local    s:dword=r
push     ebp     
mov     ebp, esp
sub       esp, r
push    ebx ecx
push     STD_INPUT_HANDLE
call      GetStdHandle; ebp-0; eip-4; ;&str-8
mov     ebx, [ebp+8]
push     0
lea       ecx, [ebp-4]
push     ecx    
push     80 ebx eax
call      ReadConsoleA
sub       [s], 2
add      ebx, [s]
mov     [byte ptr ebx], 0
mov     eax, [ebp+8]
pop     ecx ebx
mov     esp, ebp
pop      ebp
ret        4
endp
end
Ãëàâíàÿ ïðîãðàììà äëÿ ïðîâåðêè ôóíêöèè ââîäà ñòðîêè èìååò âèä:
ideal
p386
model   flat
extrn ExitProcess:proc
extrn GetStdHandle:proc
extrn ReadConsoleA:proc
extrn WriteConsoleA:proc
extrn    gets:proc
dataseg
include "win.inc"
din       dd        0
dout     dd        0
buf       db        80 dup (?)
siz        dd        ?
mystr   db        'Input string, please', 13, 10, 0
codeseg
begin:
push     STD_INPUT_HANDLE
call      GetStdHandle
mov     [din], eax
push     STD_OUTPUT_HANDLE
call      GetStdHandle
mov     [dout], eax
push     0 offset siz  22 offset mystr [dout]
call      WriteConsoleA
push     offset buf
call      gets
call ExitProcess
end begin
Ïðèìåð 3. Ñîñòàâèòü ôóíêöèþ äëÿ âûâîäà ñòðîêè ñ íóëåâûì çàâåðøèòåëåì íà ñòàíäàðòíîå óñòðîéñòâî


Ôóíêöèÿ:
Ideal
p586
model  flat
include "win.inc"
extrn   GetStdHandle:proc
extrn   ReadConsoleA: proc
extrn   WriteConsoleA:proc
dataseg
buffer   db        80 dup (?)
codeseg
proc     gets
public  gets
arg       mystr:dword
local    s:dword=r
push     ebp     
mov     ebp, esp
sub       esp, r
push    ebx ecx
push     STD_INPUT_HANDLE
call      GetStdHandle;
mov     ebx, [ebp+8]
push     0
lea       ecx, [ebp-4]
push     ecx    
push     80 ebx eax
call      ReadConsoleA
sub       [s], 2
add      ebx, [s]
mov     [byte ptr ebx], 0
mov     eax, [ebp+8]
pop     ecx ebx
mov     esp, ebp
pop      ebp
ret        4
endp
proc     puts
public  puts
arg       mystr1:dword
local    s1:dword=r1
push     ebp     
mov     ebp, esp
sub       esp, r1
push    ebx ecx edx esi
push     STD_OUTPUT_HANDLE
call      GetStdHandle
mov     ebx, [ebp+8]; &
; len
mov     edx, 0
mov     esi, offset          buffer
m2:
mov     cl, [ebx+edx]
mov     [esi+edx], cl
test       cl, cl
jz          short m1
inc       edx
jmp      m2
m1:
push     0
lea       ecx, [ebp-4]
push     ecx    
push     edx
push    offset buffer
push     eax
call      WriteConsoleA
mov     eax, [ebp+8]
pop     esi edx ecx ebx
mov     esp, ebp
pop      ebp
ret        4
endp
end
Ãëàâíàÿ ïðîãðàììà äëÿ ïðîâåðêè ôóíêöèè âûâîäà ñòðîêè èìååò âèä:
Ideal
p386
model   flat
extrn ExitProcess:proc
extrn GetStdHandle:proc
extrn ReadConsoleA:proc
extrn WriteConsoleA:proc
extrn    gets:proc
extrn   puts:proc
dataseg
include "win.inc"
din       dd        0
dout     dd        0
buf       db        80 dup (?)
siz        dd        ?
mystr   db        'Input string, please', 13, 10, 0
codeseg
begin:
push     offset mystr
call      puts
push     offset buf
call      gets
push     offset buf
call      puts
call ExitProcess
end begin

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