Академический Документы
Профессиональный Документы
Культура Документы
2005
.
. .
: , 2005. 240 : .
ISBN 5-94387-232-9
.
,
.
.
,
(Windows, DOS, Linux), ,
.
, ,
-
, ,
.
. .
.. ..
Copyright Computer Press 2004 Uiime se programovat
vjazyce Assembler pro PC by Rudolf Marek, ISBN: 80-722-6843-0.
All rights reserved
(812) 567
- 7 0 " 2 5 . 567-70-26
(044)516-38-66
www.nit.com.ru
a VQ Vo 1 "7 li t.
ISBN 5-94387-232-9
,
, 2005
,
, , 2005
0 0 0 .
000350 23 1999 .
198097, . -, . , . 29.
08.08.05. 70x100 1/16.
. . 15 . .
5000 . 293
190005, -, ., 29
10
1.
11
1.1.
1.2. .
2. 86
2.1.
2.2. 86
2.3. :
2.4. 80386
2.5
3.
12
15
19
20
22
23
25
25
27
27
27
28
30
3.1.
3.2.
3.3.
3.4.
31
33
34
35
4.
36
4.1. MOV
4.2.
4.3.
4.3.1. ADD SUB
4.3.2. INC DEC
4.3.3.
4.3.4.
NEG
CBW
CWD
CDQ
CWDE
4.3.5.
MUL IMUL
DIV IDIV
4.4.
AND
OR
XOR
NOT
( )
37
39
40
41
43
44
46
46
46
47
47
47
48
48
50
51
51
52
52
53
53
5.
5.1.
5.2. IF THEN
5.2.1. TEST
5.2.2. JMP
5.2.3. Jx
5.3.
IF GOTO
LOOP ,
.
LOOPZ LOOPNZ
5.4.
?
PUSH POP:
PUSHA/POPA PUSHAD/POPAD:
PUSHF/POPF PUSHFD/POPFD:
CALL RET:
INT IRET:
6.
6.1.
CLI STI
STD CLD
6.2. XCHG
6.3. LEA
6.4.
STOSx
LODSx
CMPSx
SCASx
REP REPZ
6.5. / (I/O)
IN OUT
. NOP
6.6.
SHR SHL
SAL SAR
RCR RCL
ROR ROL
6.7.
DB, DW DD
RESB, RESWn RESD . .
TIMES ..
INCBIN
EQU
SEG
6.8.
ALIGN
55
56
57
57
58
59
63
63
65
66
67
67
68
70
71
71
73
76
77
77
77
78
78
79
79
80
80
80
80
84
84
86
86
87
89
89
90
90
90
91
91
92
92
93
93
93
94
94
95
96
7.
7.1.
7.2.
7.3.
8.
8.1.
8.2. .
8.3.
(swapping)
8.4.
8.5.
, BIOS
9. NASM
9.1.
9.2.
9.3.
9.4.
NASM
%define, %undef
%macro %endmacro
%assign
%if
? %ifdef, %infndef
%include
9.5.
BITS
SECTION SEGMENT ...
EXTERN, GLOBAL COMMON
CPU
ORG
9.6.
:
97
98
98
99
99
100
101
101
102
107
111
112
113
113
114
114
115
116
116
117
117
120
120
121
122
123
123
123
124
125
126
126
127
128
128
129
130
130
131
131
131
132
132
134
134
134
135
135
bin
OMF 16-
Win32 32-
aout aoutb UNIX
coff a.out
elf UNIX
10. DOS
10.1.
10.2.
10.3.
10.4.
10.5.
DOS
1 MB
DOS
. Hello, World!
10.6. -
/
(SEEK)
10.7.
(MKDIR, RMDIR)
(CHDIR)
(GETCWD)
10.8.
10.9.
10.10.
10.11.
10.11.1.
10.11.2. grdb.exe
grdb
10.12.
10.13.
1 1 . Windows
11.1.
11.2. Windows-
11.2.1. API
11.2.2. Hello, World! Windows
11.3.
11.4. DOS- Windows
11.5.
136
136
137
137
138
138
138
139
140
142
143
144
146
146
147
148
153
153
154
154
155
158
160
161
162
163
163
163
163
165
165
166
166
166
167
168
168
169
169
172
172
180
185
186
187
187
187
188
190
190
190
12. Linux
12.1.
12.2.
12.3.
12.4.
12.5.
12.6.
12.7.
12.8.
12.9.
12.10.
Man-
Hello, World! Linux
: Asmutils
Asmutils
/ (I/O)
12.11.
(MKDIR, RMDIR)
(CHDIR)
(GETCWD)
12.12. . .
IOCTL
12.13.
12.14. . ALD
12.15. GAS
12.16. .
12.17.
13.
,
13.1.
13.2. -?
13.2.1. - (32- )
13.2.2. - (16- )
13.3. -
13.4. Pascal-
191
192
193
194
194
195
195
197
199
200
201
201
202
202
203
206
207
209
209
210
210
210
211
212
215
216
216
217
218
219
220
223
224
226
14.
229
15.
230
86
.
,
, ,
,
, .
.
, ,
,
,
,
.
,
, ,
DOS, Windows Linux, ,
,
.
, ,
.
:
, ,
;
;
;
;
, ;
Netwide
Assembler (NASM);
DOS, Windows Linux;
( ).
, - 514 ,
Asmutils
Linux- MPlayer.
10
,
0 1. ,
( 2) ,
(1) (0).
,
,
( 8 16
). ,
.
1.1.
,
, . , ,
, :
,
0. ,
, .
, ,
,
:
1
1.
, . ,
1100, ,
, 12,
. (1100)2,
: . ,
, 1100
:
(1100), = 1*23 + 1*22 + 0*2' + 0*2
8+4 , (1100)2 12. ,
, . ,
12 .
, ,
15, (1) 2 = I* 8 + I* 4 + I* 2 + 1*' = 15.
16 :
1000
1001
10
10
1010
11
11
1011
100
12
1100
101
13
1101
110
14
1110
111
15
1111
, , 16
(10000)2.
( 8)
( 0 7).
, :
(77)8 = 7*8' + 7*8 - 63
8 ATARI, ZX Spectrum . , .
10 ,
5 A-F:
= 10, = 11, = 12, D = 13, = 14, F = 15
13
, :
10 12 .
:
(524D)16 = 5*163 + 2*162 + 4*16 + 13*16 =
= 20 480 + 512 + 64 + 13 = 21 069
(DEAD) 16 13*163 + 14*162 + 10*16' + 13*16 = 57 005
(DEADBEEF)16 = 13*167 + 14*166 + 10*165 + 13*164 +
+ 11*163 + 14*162 + 14*16' + 15*16 = 3 735 928 559
(001)16 12*163 + 0*162 + 0*16' + 1 = 49 153
, ,
, , .
.
,
0. , 14
:
14/2 = 7
7/2 = 3
3/2 = 1
1/2 = 0
, 0.
,
(1110)2.
13 :
13/2 - 6
6/2 = 3
3/2 = 1
1/2 = 0
, ,
0. , (1101)2.
123456 :
123456/16 = 7716 0
7716/16 = 482
14
1.
482/16 = 30
30/16 = 1
14 =
1/16 = 0
1.2. .
. bit
binary digit .
0 1 , .
,
.
,
,
(), .
, : 24
.
15
() ,
. 0.
0 ( ). LSB-
(Least Significant Bit ).
MSB- (Most Significant Bit ).
,
(nibble). ,
, ,
, .
, , IBM,
.
(word)
(dword double word). PC-
:
1 = 8
1 (word) = 2 = 16
1 (dword) = 4 = 32
, '
,
. 0 255 (
28 256 ). -'
, .
0 65 535 ( 216 = 65 536 ~
). 0 4 294 967 295 (232
4 294 967 296 ).
-, , ^
. -'
0-1
(0 - 0x1)
31 16 15
0
(dword)
0 - 4294967295
(0 - OxFFFFFFFF)
. 1.1.
16
1.
, ,
. .
, (KB, Kb, ).
, SI,
1000, 1024. 1024?
,
. 1024 210.
(, MB, Mb, ), G (, GB, ), (, , )
(, , ) 1024
, , 1 = 1024, , 1 = 1 * 1024 = 1024 * 1024 - 1 048
576 . , , .
, .
.
, .
.
ASCII- (American Standard Code for Information Interchange).
128 , ,
.
ASCII (. 1.2).
. ,
40 01. , 0x41
( 65 ) '' ASCII-.
ASCII- 0x20 ( 32d).
32 .
LF , OxD
CR .
CR LF ,
,
\. ,
-: , Windows ( DOS)
(CR, LF OxD, ), UNIX
(LF ).
128 256
. , ,
, ,
, ..
, 866 ( DOS)
1251 (Windows).
17
. 1.2. ASCII
18
86
..
86
80386
.
,
.
, ,
. ,
,
,
.
,
(
), ,
.
, ,
, -;
.
2.1. ...
ENIAC (Electron'
Numerical Integrator And Calculator),
.
,
.
ENIAC EDVA
(Electronic Discrete Variable Automatic Computer), 1946
, , :
, ,
. EDVAC,
,
:
1. :
(), , , /.
20
2. 86
2. (
ENIAC), .
3. ( )
( ,
,
).
4. ,
(1 1 ).
5. ,
( ),
(
). ,
, ( ,
).
6.
(jump) .
7. ( , )
.
, ,
.
. 1950- ,
, , .
,
. : .
(). ,
. 2.1.
21
, ,
.
: .
1970- ,
.
? !
, , Apple.
Apple ,
,
.
1981 IBM
IBM PC XT.
, .
IBM PC XT 8-
Intel 8088.
(PC, Personal Computer),
.
2.2. 86
16- 86, 8086,
Intel 1978 . 5, 8
10 20- 1
.
8- , Intel
8088, 8086,
8- .
1982 Intel 80286,
, ,
24-, . 16
. (
),
.
, ,
.
86.
, 1986 , Intel 80386 DX,
( ) 32-.
80386 SX, 80386 DX,
22
2. 86
16- .
20, 25 33 . 80386
,
80387.
1989 Intel
80486DX, 80486DX/2 80486DX/4,
. 80486SX, 80486DX,
.
8 -.
1993 Pentium.
, ,
, 20 8086.
, 86, Intel,
: AMD, Cyrix, NEC, IBM.
80386,
.
2.3. :
.
, ,
, (
, pin).
, ,
.
. ,
.
7 :
( , )
.
,
. (1 0)
. , 16- 8-
24 (16 8) ,
. , 27 (00011011 )
8- ,
(LSB), 1, 1,
0 ..
23
, .
,
: , - ()
.
. :
,
.
- ()
.
, ,
(, )
.
, ,
, , (, ),
. ,
(barrel-shifter),
.
, .
,
24
2. 86
. ,
.
:
, .
, .
( ).
. ,
,
. ,
.
2.4. 80386
80386 32-, ,
4 (232 ).
32-,
32 ( int
32-).
,
(. 2.3) .
.
. ,
, EDX (, , ). ,
,
(. 2.4).
80386 80286,
16-. 80386 ,
?
16- ( 80286)
16 , . ,
( 80386, 80286) 8-
AL.
0x12345678,
0x5678 (0x56 0x78 AL), 0x1234
.
: , , ,
BL ..
25
26
2. 86
80386 ESI, EDI ( SI, DI 16- ).
:
, .. : (Source
Index), (Destination Index), (Base Pointer).
: ESI, EDI
.
, .
.
ESI, EDI 16- 32-
.
.
(,
).
( )
. 16-, ,
80286.
: CS (Code
Segment, ) EIP (IP) ,
; SS (Stack
Segment, ) ESP (SS:SP) .
DS, ES, FS, GS (Data, Extra, F G )
.
ESP (SP) ,
(86- ).
.
EIP (IP, Instruction Pointer) .
, .
, .
(
) EFLAGS. ,
- .
32 ,
:
27
ZF (Zero Flag) 1,
.
SF (Sign Flag) 1,
.
OF (Overflow Flag) 1,
(overflow),
, .
CF (Carry Flag) 1,
( ,
).
IF (Interrupt Flag) 1,
.
DF (Direction Flag)
, .
,
.
80386 80387 (
),
.
.
, .
,
,
.
.
16- 32- ,
()) .
2.5.
,
,
,
.
.
. .
, .
28
2. 86
, (
) .
. , .
.
86 256
. 256 (
) .
.
IRQ (Interrupt Requests). ISA
16 (IRQ0 IRQ15).
,
.
,
, ,
, .
IRQ0-IRQ15.
: ,
() ,
. (maskable),
(non-maskable).
IF 0. ,
NMI, ,
, .
, .
.
29
3.1.
,
.
. RISC
(Reduced Instruction Set Computer)
. RISC
RISC I. ,
(, ),
.
RISC- .
( ,
),
. RISC
,
.
, CISC
(Complex Instruction Set Computer)
. CISC ,
. 86-
CISC.
0x1234 .
MOV , 0x1234.
,
( 7 ).
. MOV , 0x1234
:
31
0x11:
0x1111: 08, 0x34, 0x12
0x1114:
0x1111.
, , 3
. MOV.
08? 08
.
1011 MOV. 1011,
, MOV. (1)
, 16-.
. ( AL,
0, ,
8-).
,
. ,
, IP ( ) 0x1111.
,
, ,
.
, ,
0x1111 ( 08),
() . , ,
, 16-
, 0x1112 0x1113 (
, 0x1234).
.
.
, .
IP 3 ( 3 ),
, ,
0x1114 .
, ,
IF. (0),
; (1),
,
.
, ,
.
, .
32
3.
3.2.
, , .
;
, .
, .
(,
0x123), (
).
, 32-, 16-, 8-
. ,
(). MOV , 0x1234
: , 16-
.
,
, .
.
.
:
regS- 8- ;
regl6-onepaHfl 16- ;
reg32-onepaHfl 32- ;
m ;
imm8 8- ;
imml6 16- ;
imm32 32- ;
segreg .
, : reg8/imm8-onepaHfl
8-
8- .
, ,
: R/imm8-onepaHfl
( 8- )
8- .
2 . 293
33
3.3.
, , , .
, ,
,
( ).
,
, : [].
, ,
, [ESI], ,
.
, , [0x594F].
,
. ,
. ,
ESI, , ,
[ESI].
, ESI
( 0) .
, , (,
1 ) ?
, . ,
,
[ESI + 1].
: [ + + 4].
, 4 ,
.
(, Effective Address) ,
( ).
80386
, :
[ + * 4].
:
[ - + * 8 + E S I ]
[ESI]
, , [ESI + 4].
, 16- 32-
[], [],... [].
80286
, SI, DI,
.
34
3.
, .
. ,
()
. :
ES:[ESI]
:
[ES:ESI]
,
,
.
3.4.
, , ,
. :
_ []
. ,
.
. BYTE (8-
), WORD (16-) DWORD (32-).
,
.
,
. ,
, :
mov dword [ 0x12345678 ],0
mov word
[ 0x12345678 ],0
mov b y t e
[ 0x12345678 ],0
; 4 ,
; 0x12 345678
; 2 ,
/ 0x12345678
; 1
; 0x12345 678
.
.
4 ,
0x12345678. ,
.
( : 00000000) 0x12345678.
35
MOV
86,
, ,
. ,
,
.
4.1. MOV
- ,
: .
, .
MOV, move (
), ,
:
MOV ,
MOV:
mov a x , [ n u m b e r ]
number
mov [number],bx
number
mov b x , e x
mov a l , 1
AL 1
mov d h , 1
DH
CL
mov e s i , e d i
EDI
ESI
mov word [number]
16- 1
"number"
86
. ,
number_one, number_two,
:
mov [number_two], [number_one]
;!!!
37
.
,
:
mov ax, [number_one]
mov [number_two],
; 16-
; "number_one"
;
;"number_two"
MOV :
mov ,
bl
;! -
;.
BL
, , 0 :
mov ,
mov a h , 0
;
;""
;
8- .
MOV ,
, . ,
0, 0 MOV ah,
. 8-
, BL, 16 . , 16-,
.
: ,
BL (AL):
mov ,
mov al, bl
; = 0 , AL =
; AL BL
16- 32- .
MOV :
MOV r / m 8 , r e g 8
MOV r / m l 6 , r e g l 6
MOV r/m32,reg32
MOV r e g 8 , r / m 8
MOV r e g l 6 , r / m l 6
MOV r e g 3 2 , r / m 3 2
MOV reg8,imm8
MOV r e g l 6 , i m m l 6
MOV reg32,imm32
MOV r/m8,imm8
MOV r/ml6,imml6
MOV r/m32,imm32 '
38
4.
4.2.
. ,
, 0x12345678,
, 32- counter.
0x12345678 co
unter:
mov e b p , 0x12345678
mov [ c o u n t e r ] , ebp
; 0x12345678
;
; " c o u n t e r " ()
, ,
. , ,
, , , ,
.
?
.
, , : ,
,
, :
0804808
BD78563412
0804808F
' 892DC0900408
mov e b p , 0x12345678
mov dword [ + 0x80490c0] , ebp
,
, .
.
counter
(0804900).
, mov ebp, 0x12 345 67 8,
:
= 0x00000000 ebx = 0x00000000 = 0x00000000 edx = 0x00000000
esp = 0xBFFFF910 ebp = 0x00000000 e s i = 0x00000000 edi = 0x00000000
ds = 0x0000002B es = 0x0000002B fs = 0x00000000 gs = 0x00000000
ss = 0x0000002B cs = 0x00000023 eip = 0x0804808A eflags = 0x00200346
Flags: PF ZF TF IF ID
0x12345678.
(0804900), :
Dumping 64 b y t e s of memory s t a r t i n g at 0x080490C0 in hex
080490C0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
39
MOV, 0x12345678
0804900
:
Dumping 64 b y t e s of memory s t a r t i n g at 0x080490C0 in hex
080490C0: 78 56 34 12 00 00 00 00 00 00 00 00 00 00 00 00 xV4
(0x12345678)
0804900, - . , 86-
LITTLE_ENDIAN,
(little-end-first,
). BIG_ENDIAN (, Motorola)
:
(big-end-first, ).
.
, LITTLEENDIAN BIG_ENDIAN :
,
.
, , .
, , , ... ,
.
( )
, .
, ,
,
(htonl, htons, ntohl, ntohs).
counter ,
, 0x12345678.
4.3.
MOV
, .
. 80386
, ,
80386.
.
40
4.
ax,
,
dx,
dx,
8
6
ax
; 8
; 6
; DX, DX =
;DX = DX +
( )
,
DX, DX. ADD
DX + DX,
.
ADD SUB:
add e a x , 8
sub e c x , e b p
add b y t e [number]
[number],
sub a h , a l
al
EAX = EAX + 8
ECX = ECX - EBP
4
number
1
( 0-255)
number = number 4
2
( 0-65535)
00000004
"number"
AL
"number"
AL ,
, AL (8- )
(255), 8?
mov a l , 255
add a l , 8
; AL 255, OxFF
; 8
AL 7.
41
0x107 (263 ). ?
AL 8- (
255). , , ,
CF .
,
. ADC (Add With
Carry ) SBB (Subtract With Borrow
):
ADC o l , o2
SBB o l , o2
; o l = Ol + 2 + CF
; o l = o l - 2 - CF
, ADD SUB,
CF.
. ,
. EDX:EAX ( DX:AX)
. 16 , a DX 16
. 80286 32-
, 32- .
: DX:AX OxFFFF ( = OxFFFF, DX = 0).
8 DX:AX:
mov
mov
add
adc
ax,
dx,
ax,
dx,
Oxffff
0
8
0
;AX = OxFFFF
;DX = 0
;AX = AX + 8
; DX
ADD 8 .
, CF.
DX 0 CF.
ADC CF DX (DX 1).
OxFFFF 8 (0x10007) DX:AX
(DX=1,AX=0007).
42
4.
80386 32-
:
mov e a x , O x f f f f
add eax, 8
; = OxFFFF
; = + 8
32-
0x10007. 64-
EDX:EAX , DX:AX.
4.3.2. INC
DEC
.
INC , a DEC .
, ADD SUB,
:
INC Ol
DEC Ol
;ol = ol + 1
;ol = ol - 1
! CF.
, , (
43
) , ,
.
AL :
;AL = AL + 1
add a l , l
;AL = AL + 1
inc a l
16- number:
;
; word
4.3.3.
.
,
, , .
.
0 255.
-128 127. 0 127
,
128 255: -1 255 (OxFF), -2 254 (OxFE) ..
-50 206. :
1
, .
(mapping).
2 ( 0 65535).
-32768 32767.
4 , -2 147 483 648 2 147 483 647.
(integer).
256(0x100)
: 4, -4,386, -8000
45000 , ,
16 (2 ).
, ,
16 .
: 2 16.
216 = 65 536,
0 65 535. :
65 536 / 2 = 32 768. ,
-32 768 32 767 (0
!).
44
206(0)
1
128
1
. 4.4.
1
4.
, 4, <0, 32 767>,
4. -4
, <-32 768, 0>.
65 536 4 = 65 532. 386
. -8 000 ,
65 536 8 000 = 57 536 -8 000 .
, , 45 000 ,
.
ADD SUB. ,
, -6 7
2 . 7 , -6
65 536 6 = 65 530 (OxFFFA). ,
(7 65 530)? :
mov ax,OxFFFA
mov d x , 7
add a x , d x
; = -6
;DX = - 6
;AX = AX + DX
: 0xFFF4 ( CF, ).
0xFFF4 = 65 524.
: -12 (65 536 65 524 = 12).
.
, ,
(,
):
mov , [ - 1 ]
;
; ,
-1 OxFFFF,
: MOV AX, [BX+OxFFFF].
CF, , .
45
4.3.4.
NEG
80386 ,
.
NEG (negation, ):
NEG r/m8
NEG /16
NEG /32
NEG,
. NEG ,
.
: 8, 16 32 .
b y t e [number]
; ,
; , 8-
;
; 8- number
:
,
.
.
.
.
,
. ,
.
CBW
CBW () AL ,
AL
( 0x00 OxFF = 11111111b,
AL). ? ,
, .
15
I ! AL I
15
87
. 4.5. CBW
46
4.
CWD
CWD DX,
DX:AX.
. 4.6. CWD
CDQ
CDQ EDX,
EDX:EAX.
CWDE
CWDE ( )
,
, .
. 4.7. CWDE
:
mov a l , -l
cbw
;AL = -1 ( OxFF)
;
CBW OxFFFF,
- 1 . (1) ,
AL .
47
.
mov a x , 4
cwd
; = 4
/ DX
4. , CWD,
DX:AX.
DX ,
0. 0 DX.
CWD DX,
, , 0x8000.
4.3.5.
:
.
MUL, DIV.
ADD SUB.
:
IMUL IDIV.
, MUL DIV.
.
,
.
, :
.
,
, .
ADD SUB, MUL, DIV, IMUL, IDIV
.
MUL IMUL
MUL
:
MUL r/m8
MUL r/ml 6
MUL r/m32
8- 8-
. AL. (
) .
48
4.
(r/m8) * AL ->
16- 16-
. .
DX:AX.
(r/ml6) * -> DX:AX
32- ,
EDX.EAX.
(r/m32) * -> EDX:
.
1: , CL,
:
mov a l , bh
mul cl
;AL = AL
; = AL * CL CL
.
: 4862, DX:AX:
mov ax, 486
mul
; = 486
; * -> DX:AX
2: , 8-
r a d i u s l , 16- d i a m e t e r l :
mov a l , 2
mul byte [ r a d i u s l ]
mov [ d i a m e t e r l ] , a x
; AL = 2
; AX = r a d i u s * 2
; diameter <- AX
, 16-
DX:AX, - 32- ?
16- ,
32- .
IMUL ,
. , IMUL
, MUL,
.
, IMUL
,
. ,
.
imul e d x , e c x
imul e b x , [ s t h i n g ]
imul ,6
; = * 6
, IMUL
, .
, ,
:
imul edx,ecx,7
imul ebx,[sthing],9
imul ecx,edx,ll
EDX = ECX * 7
"sthing" 9,
EBX
= EDX * 11
DIV IDIV
MUL, DIV
:
- DIV r/m8
DIV r/ml 6
DIV r/m32
, (
MUL). 8- ()
8- .
. : AL, .
/ (r/m8) -> AL, ->
16- 16-
. DX:AX.
DX:AX (DX , ).
DX:AX / (r/ml6) -> , -> DX
32- EDX:EAX,
EDX:EAX ( , EDX).
EDX:EAX / (r/m32) -> , -> EDX
IDIV ,
, DIV.
.
1: 13 2, BL, :
mov
mov
div
mov
50
,13
cl,2
cl
bx,ax
= 13
CL = 2
CL
,
4.
2: - ,
16- diameterl, radiusl,
.
mov a x , [ d i a m e t e r l j
mov , 2
div
mov [ r a d i u s l ] , a l
AX = d i a m e t e r l
2
4.4.
: (, AND),
(, OR), (XOR) (NOT).
.
AND
AND ol 2.
ol. ,
ADD: 8-, 16- 32- ,
.
AND o l , o2
AND (. 4.1).
AND
4.1
a AND b
(1 AND 0).
mov a l , 1
mov b l , 0
and a l . b l
AL = one
BL = z e r o
AL = AL and BL = 0
, :
mov a l , 1
and a l , 0
;AL = one
; AL = AL and 0 = 1 and 0 = 0
51
.
OR
OR ol 2.
ol. ,
AND.
OR o l , o2
OR (. 4.2).
OR
4.2
a OR b
.
( ) mask 1.
or byte [mask],1
XOR
ol 2.
1. , .
:
XOR o l , 2
XOR (. 4.3).
XOR
4.3
a XOR b
: (( ) )
.
mov al,0x55
al,0xAA
xor al,0xAA
52
; AL = 0x55
;AL = AL xor OxAA
; AL - 0x55
4.
NOT
,
.
:
NOT r/m8
NOT r/ml 6
NOT r/m32
NOT (. 4.4).
NOT
NOT a
4.4
N O T
NEG:
mov al,00000010b
mov bl,al
not al
neg bl
;AL = 2
;BL = 2
;
;11111101b = OxFD (-3)
;a
/: 11111110 = OxFE (-2)
( )
. , 16- 16
0 1.
. 16
, (/)
1 . , ,
.
, , ,
. ,
.
: , ..
.
53
. ,
, ,
.
(
) OR.
,
, . ,
,
10000001. , 0 OR
X X.
( 0),
, ,
, ,
AND. 1 AND X X,
.
.
. AL .
1. .
or a l , 10101010b
; 1
. , AL, 0- 7-
, .
.
and a l ,
01111110b
; 1
;
XOR ,
. 1 ,
(0 1, 1 0),
0. XOR , .
XOR
: XOR
(),
(XOR) .
54
IF THEN
: ,
.
,
, .
5.1.
.
.
, , .
,
,
.
,
, .
j.
1
2
. 5.1.
56
5.
5.2. IF THEN
IF-THEN.
.
, .
. 5.2.
5 . 2 . 1 . TEST
TEST .
, , 8,
16 32 .
o l , 2
compare, .
SUB: 2 ol. ,
.
,
.
TEST ,
.
. TEST
.
:
, 4
d l , a h
[ d i a m e t e r l ] ,
; 4
; DL
,- " d i a m e t e r l "
57
cmp a x , [ d i a m e t e r l ]
,
t e s t ax, 00000100b
; " d i a m e t e r l "
;
,-
;( )
5.2.2. JMP
jmp
. ( IP
CS),
. :
JMP [_]
JMP GOTO,
.
, jump, .
,
. (
) ,
. :
, ,
.
:
(short), (near) (far).
jmp. ,
near.
(
) .
() 8- ,
-128 127 .
8-
(I).
(near) .
IP,
( 64 );
EIP,
4 .
far IP CS,
.
CS.
58
5.
. ,
, .
.
:
mov , 4
new_loop :
mov bx,
; = 4
; new_loop
;
new__loop ,
:
jmp new_loop
; new_loop
new_loop.
, :
:
jmp s t a r t
finish:
/ s t a r t
; " f i n i s h "
;-
start:
jmp f i n i s h
; " s t a r t "
/ " f i n i s h "
:
, .
, 128 .
2 ,
3 , 5 , .
,
s h o r t , :
near_label:
; " n e a r _ l a b e l "
;
jmp short near_label
/ "near_label"
5.2.3. Jx
.
,
59
.
.
. :
, , ,
.
:
Jx _
:
jz
jc
js
jo
is_true
is_true
is_true
is_true
;
;
;
/
;OF = 1
is_true,
is_true,
is_true,
is_true,
ZF = 1
CF = 1
SF = 1
, :
jnz is_true ; is_true, ZF =
JNC, JNS JNO.
, ( ,
jump) (. . 5.1).
1==2
1=2
1!=2
>2
JE(JZ)
JNE(JNZ)
Jump,
Jump,
Jump,
0
Jump,
0
JE(JZ)
JNE(JNZ)
Jump,
Jump,
Jump,
0
Jump,
1>2
5.1
2
1=<2
1>=2
JA(JNBE)
JB(JNAE)
JNA(JBE)
JNB(JAE)
Jump,
Jump,
Jump,
Jump,
Jump,
Jump,
Jump,
Jump,
JG(JNLE)
JL(JNGE)
JNG(JLE)
JNL(JGE)
Jump,
Jump,
Jump,
Jump,
Jump,
Jump,
Jump,
Jump,
.
(
). ,
: equal , above , below ,
zero , greater , less . , JE Jump
if Equal (, ), JNE Jump if Not Equal (,
), JA Jump if Above (, ) ..
60
5.
MUL DIV,
. ,
.
128 : .
128 ,
, jmp,
:
jz far_jump
far_jump:
jmp f a r f i n i s h
; ZF = 1, far_jump
;
; ""
, IF-THEN
. i f_three,
3.
, .
:
, 3
; 3
JZ,
:
jz i s _ t h r e e
; " i s _ t h r e e " , = 3
,
(JZ JNZ )
. = 3, jz
i s _ t h r e e ,
jz .
CL AL.
, BL 1, AL ,
CL, BL=2, AL CL, BL=3.
cmp a l , c l
jz w r i t e _ l
cmp a l , c l
ja w r i t e _ 2
mov , 3
end_if:
; AL CL
; w r i t e _ l , AL = CL
; AL CL
; w r i t e _ 2 , AL > CL
; 3 BL
; , IF
write_l:
mov b l , l
jmp end_if
write_2:
mov bl,2
jmp end_if
; w r i t e _ l
;BL = 1
; end_if
; write_2
;BL = 2
; end_if
61
. 5.3.
(jmp end_if),
. :
w r i t e _ l ,
.
jmp end_if .
:
mov , 1
cmp a l , c l
je end_if
mov , 2
cmp a l , c l
ja end_if
mov , 3
e n d _ i -.
; BL = 1
; AL CL
; , AL
CL
;BL = 2
; AL CL
; , AL > CL
;BL = 3
;
, ,
. MOV ,
:
mov , 1
cmp a l , c l
je end__if
mov b l , 2
ja end_if
mov , 3
end i f :
62
BL = 1
AL CL
, AL
CL
BL = 2
, AL > CL
BL = 3
5.
,
:
if (al == c l ) = 1 e l s e if (al > cl) = 2 e l s e = 3;
5.3.
, ,
, .
.
. 5.4.
, :
( FOR),
;
( WHILE), ,
;
( UNTIL), ,
.
IF GOTO
(
). ,
, .
, ,
. ,
- .
63
FOR :
FOR_START:
1=0
FOR_LOOP:
...
;
;
; ()
;
1=1+1
;
IF I < 10 THEN
;
GOTO FOR_LOOP
;
FOR_FINISH:
;
10 .
. ( ),
1. : 10,
, 10,
.
. ,
IF GOTO,
FOR. ( I)
:
for_start:
mov ecx,0
for_loop:
64
; = 0
;
5.
...
;
i n c
; 1
, 1 0
; 10
jnz for_loop
; , f o r _ l o o p
for_finish:
; = 10,
FOR. , ,
, , I.
for_start:
mov dword [i],0
for_loop:
...
i n c dword [ i ]
cmp dword [ i ] , 1 0
jnz for_loop
for_finish:
; dword 1 = 0
;
;
/ i 1
,- i 10
; , f o r _ l o o p
; ,
,
, ,
.
,
DEC ZF .
: (=10),
, 1. ZF , ,
0 :
for_start:
mov , 1 0
for_loop:
...
dec
jnz for_loop
for_finish:
; = 1 0
;
; .
; 1
; 0, f o r _ l o o p
; 0,
:
for
( i = 0 ; i < 10;i++)
{}
LOOP ,
, 80386, , 86- CISC (
), . ,
,
. ? CISC-,
3 3<. 293
65
.
LOOP:
LOOP
MUL, LOOP .
, .
( ). . LOOP
() ,
, . 128
( ).
FOR LOOP:
for_start:
mov ex, 10
for_loop:
...
loop for_loop
for_finish:
;CX = 10 10
;
;
; , 0,
; for_loop
;
, .
.
LOOPZ LOOPNZ
LOOPZ
. , :
, , 10 , ,
3.
, .
LOOPZ
LOOPNZ
LOOPZ :
,
ZF . LOOPE.
:
for_start:
mov cx,10
f_1:
= 10
FOR
-
66
5.
,3
loopz for_loop
for
finish:
; 3?
;=-1; , =3,
; for_locp
; = 0 3,
LOOPNZ ,
: () 0
ZF 0. LOOPNE.
5.4.
- ,
.
, .
?
, . , .
, , .
, ,
. ,
, . FIFO (First
In First Out) , .
67
. 5.7. LIFO
LIFO. , ,
.
PC- ,
. SS:SP (SS:ESP)
(Stack Segment) (Stack Pointer).
,
. ,
,
, SP (ESP).
16- 32- .
PUSH POP.
PUSH POP:
PUSH 16-
32- . :
PUSH ol
68
5.
:
push eax
PUSH
:
sub e s p , 4
mov [ s s : e s p ] , e a x
; ESP 4 ( - 4-
;)
/
( sizeof,
) push ol
:
(E)SP=(E)SP-sizeof(ol)
ol -> S S : [ ( E ) S P ]
, POP,
( ).
, PUSH ( ,
32- , 32 ).
POP MOV ADD:
mov e a x , [ s s : e s p ]
add e s p , 4
/
/""
/ dword
:
push eax
push e s i
;
/ ESI
pop eax
pop e s i
/
/ ESI
ESI: , ESI,
(
ESI) ,
, ESI.
16- .
mov ,0x1234
mov bx,0x567 8
push ax
push bx
. . .
pop bx
/ = 0x1234
; = 0x5 67 8
;
/
/
/
69
. 5.8.
PUSH
0x0000. SS:SP. , SP
OxFFFE. PUSH AX
2 OxFFFC, ( )
0x1234. , PUSH BX,
SP 2 (OxFFFA)
0x5678. POP BX 0x5678
, 2. OxFFFC,
0x1234.
, 8- .
IP (EIP) , PUSH/POP:
-, , .
PUSHA/POPA PUSHAD/POPAD:
. PUSHA,
. PUSHA
16- .
.
PUSHA
80386, 32- (
).
PUSHAD POPAD.
70
5.
( ):
(), (), (E)DX, (E)BX, (E)SP, (E)BP, (E)SI, (E)DI
:
pusha
;
; ,
;
;
PUSHF/POPF PUSHFD/POPFD:
. 16- 16-,
PUSHF POPF. ,
32-, 32- PUSHFD
POPFD.
16
,
PUSHF POPF.
cmp ax,bx
pushf
...
add d i , 4
popf
jz equal
;
; ,
;,
;
; = , "equal"
CALL RET:
.
:
.
CALL,
RET. :
CALL _
RET
CALL
. , ,
. JMP, CALL
71
IP (EIP).
.
JMP, CALL .
near. f a r ,
CS IP (EIP).
RET,
IP (EIP).
,
CALL.
CALL far,
IP (EIP), CS:
RETF, RET.
:
RETF RET , ,
IP (EIP) CS.
13 ,
, .
, ,
, .
.
a d d _ i t .
.
,
. ADD ,
:
add_it:
push eax
add eax,ebx
mov ecx,eax
pop eax
ret
;
;EAX = +
/
;
;
add_it 4 8:
mov eax,4
mov ebx,8
call add it
; = 4
; = 8
; add_it
;
(
pop eax)? RET 72
5.
, ,
, .
, RET:
, ,
.
a d d _ i t ,
POP PUSH:
add_it:
mov e c x , e a x
add e c x , e b x
ret
; ( )
; ( ) ,
;
;
INT IRET:
. ,
, .
,
.
, :
.
INT.
, ,
.
, .
INT 8- ,
.
INT op
,
(ISA PCI). ,
, (IRQ, interrupt
requests). 16 ,
16 ISA .
, , ,
.
.
DOS, IRQ 0 ( ) 8 ().
,
, ,
, , .
CS, ()1 .
73
. .
, .
, .
, ,
CS ()1.
.
, DOS,
21- (0x21) , :
i n t 0x21
DOS
IRET, (E)IP, CS
. :
IRET
, 21-
(. 5.9). ,
16- .
. 5.9. CS IP
.
INT,
. .
:
pushf
push cs
74
/
; CS
5.
push ip
; .
. :
c a l l here
here:
,
: JMP far.
: ,
,
0x0000:0x0000.
. IP ( ),
CS.
0x21 00000:(021*4),
:
jmp far [0x21*4]
;
;, , DS=0,
;
;00000: 0x0000)
INT PUSHF
CALL far:
pushf
c a l l far [0x21*4]
;
; CS IP
; "jump"
. ,
.
75
XCHG
LEA
/ (I/O)
86.
.
6.1.
. ,
IF ID, .
STI
CLI (Clear Interrupt) STI (Set Interrupt)
IF. ,
.
(1), . CLI (0)
. ,
STI:
cli
...
sti
; DOS!
;
/ - , ,
;
;
STD CLD
:
STD
CLD
6.2. XCHG
. ,
, , , ,
XCHG (exchange ),
.
XCHG o l , o 2
MOV, ol 2.
8-, 16- 32-,
, MOV.
xchg ,
xchg
xchg
xchg
xchg
/
;
ebx,ecx
;
al,ah
;
dl,ah
;
byte [variable],cl ;
.
NOP
AL
DL
CL
6.3. LEA
Load Effective Address,
.
(
). ,
, .
LEA o l ,
[o2]
LEA , ,
:
lea edi, [ebx*4+ecx]
; EDI ,
; EDI = *4+
, LEA,
:
. LEA
, .
78
6.
6.4.
(
), ( , ).
/ 0
. 6.1.
86- ,
.
, , LOOP,
.
. 8-
, .
(byte). ,
16- , W (word).
32- , D
(double word).
, ,
.
(DF).
STOSx
STOSx (STOre String) :
STOSB
STOSW
STOSD
STOSB AL ,
ES:(E)DI,
( DF) (E)DI,
AL . DF=0, (E)DI
1, 1.
DI EDI .
, STOSW, ,
, a (E)DI / 2. STOSD
, a E(DI) / 4.
79
.
eld
stosw
; DF,
; ES:[DI] ES:[EDI] (
; ) (E)DI 2
LODSx
6.
- .
LOOP: ,
() . ()
. REP
MOVS STOS:
rep
movsb
:
rep
stosb
() D S : ( E ) S I
E S : ( E ) D I .
- memcpy()
() A L E S : ( E ) D I .
: - m e m s e t ( )
; SCASB
; CMPSB
,
, strlenQ.
. 6.2. - strlen()
,
ES:(E)DI. ( +
) . ES:(E)DI
, () .
6.1.
6.1. strlen()
strlen:
push eax
EAX
xor ecx,ecx
(=0),
: mov ecx,
xor eax,eax
EAX = 0
dec ecx
= - 1. OxFFFFFFFF
eld
DF = 0, ,
repne scasb
neg ecx
( )
pop eax
ret
. 6.3. ES:(E)DI
strlen
16-
: .
,
, . , ES:(E)DI
.
CALL,
.
call strlen
82
; s t r l e n
6.
,
strcmp().
. 6.4. - strcmp
: ES:(E)DI, a
DS:(E)SI. ,
0, ,
. strcmp () 6.2.
6.2. strcmp()
strcmp:
push edx
push edi
call strlen
mov edx,ecx
mov edi,esi
;push ds
;push ds
;pop es
call strlen
;pop ds
cmp ecx,edx
jae .length_ok
; EDX
/ EDI
;
; EDX
;EDI = ESI
; DS
;
; ES (ES = DS)
;
; DS
; ?
;, ()
;
83
mov ecx,edx
.length_ok:
pop edi
eld
repe empsb
pop edx
ret
;
; EDI
;DF =
; ,
;
/
; EDX
;
stremp ,
DS ES .
,
.
, .
. 6.5
.
ECX=0xA
. 6.5. stremp
6.5. / (I/O)
/
/.
( )
.
IN OUT.
IN OUT
IN , a OUT
, .
IN
IN
IN
IN
IN
84
a l , dx
ax, dx
eax, dx
a l , imm8
ax, imm8
OUT
OUT
OUT
OUT
OUT
dx, a l
dx, ax
dx, eax
imm8, al
imm8, ax
6.
IN /,
DX, AL/AX/EAX. ,
AL/AX/EAX DX, .
OUT . ,
IN, : .
/,
, . 6.1.
/
6.1
0000-001f :dma1
0020-003f: p i d
0040-005f: timer
0060-006f: keyboard
0070-007f: rtc
OOaO-OObf: pic2
OOcO-OOdf: dma2
DMA-
OOfO-OOff: fpu
0170-0177 :ide1
IDE- (Secondary)
01f0-01f7:ide0
IDE- (Primary)
0220-022f: soundblaster
0290-0297 :w83781d
0376-0376 : idel
IDE- ()
03c0-03df:vga+
03f2-03f5: floppy
03f6-03f6: ideO
IDE- ()
()
03f8-03ff: lirc_serial
PCI
ACPI
ACPI
USB
85
. :
in a l , 0 x 6 0
/ 0x60
; ( - )
. NOP
No Operation,
, .
?
/, .
: ,
, .
NOP. ,
:
jmp short delayl
delayl:
2 ,
:
jmp short $+2
; 2
.
,
, .
,
,
JMP .
0x80:
out 0x80,al
6.6.
,
. .
() ,
. () ,
.
, .
86
6.
( ) ,
.
,
.
, .
SHR SHL
SHR SHL
.
.
5 0101b. 2
10, 01010b.
, , , , 5
10: , .
. ,
16 (2 4)
4 .
0 0 0 0 0 1 0 1 0
I
. 6.6. 5 2
,
.
SHL :
SHL o l , 2
, .
, .
.
, CL
. 32 ,
, 32.
CF,
. CF (SF)
t
87
.
(OF). ,
,
( SF OF 1).
, SHL, :
SHR o l ,
CF, .
SHR . 6.7.
. 6.7. SHR
,
.
: SHR, 16,
:
shr ,4
; 4
:
BBL.
16- ,
16 . CF,
,
JC. CF , BL.
mov , 0
mov ex, 16
repeat:
s h r ax, 1
jnc not_one
inc
not_one:
loop repeat
; BL=0
; = 16
; 1 ,
; CF
; 0,
; BL 1
; 16
BL
, 0.
88
6.
SAL SAR
SAL SAR
( ). SAL ,
SAR -- .
:
SAL o l , 2
SAR o l , 2
SAR , ,
. , , CF.
, SHL SHR.
. 6.8. SAR
RCR RCL
(). RCR
, SHR,
CF.
CF. RCL RCR,
.
:
RCR o l , 2
RCL o l , 2
. 6.9. RCR
. 6.10. RCL
89
.
ROR ROL
: ROR
,
CF; ROL .
ROR o l , 2
ROL o l , 2
RCR RCL.
. 6.11. ROR
6.7.
, , ,
. MOV AX, [number],
,
number. ,
.
.
(
). ,
.
.
:
, .
DB, DW DD
DB (define byte),
. :
;
; : 0x5 5,
;05, 0x57
db '',0x55
;
;.
; 0x61, 0x55
db 'Hello',13,10,'$' ; .
; 0x48, 0x65, , ,
;0x6F, OxD, OxA, 0x24
db 0x55
db 0x55,0x56,0x57
90
6.
, ,
DW (define word):
dw 0x1234
dw ' a '
;034, 0x12
;0x61, 0x00:
, number:
number dd 0x1
; number
; 1
number ,
0x00000001 .
RESB,
RESWH
RESD
. ,
, .
, . ,
, .
: RESB ( ),
RESW ( ) RESD ( ).
:
resb 1
resb 2
resw 2
resd 1
number
; 1
; 2
; 4 (2 )
; 4
resd l
; 4
;"number"
buffer
r e s b 64
,- 64
; buffer
, NASM,
. RESx.
TIMES
TIMES ,
.
91
.
, DUP
Borland TASM. ,
.
, 64 Hello:
many_hello: times 64 db 'Hello'
, ,
. ,
32
TIMES:
buffer db "Hello"
times 32-($-buffer) db '
;
/
;
32-($-buffer) 27, $-buffer
, 5.
'
TIMES ,
:
times 5 inc eax
;5 INC EAX
:
inc
inc
inc
inc
inc
eax
eax
eax
eax
eax
INCBIN
.
:
i n c b i n "sound.wav"
i n c b i n "sound.wav",512
incbin "sound.wav",512,80
;
/ 512
/ 512
/ 80
EQU
, .
.
EQU :
four EQU 4
92
; .
/
6.
SEG
,
16- . ,
.
SEG
, :
mov a x , s e g c o u n t e r ; ,
; c o u n t e r
mov e s , a x
/
/.
;
mov b x , c o u n t e r
; ()
/ c o u n t e r . ES:BX
; c o u n t e r
mov c x , e s : [ b x ]
/
;
, .
SEG .
6.8.
, ,
.
, , ,
,
.
,
.
,
.
.
ALIGN
, ,
, .
, .
.
,
, . , 93
.
, .
.
, .
, .
ALIGN.
, , ,
:
align 4
a l i g n 16
; , 4
; , 16
,
,
. , ?
MOV, 0?
XOR ( ,
0):
,
; 0,00
,
mov eax,0
; 08,0,0,0,0
, XOR ,
, .
:
,
inc eax
; = 0
/ 1
1. DEC
INC, - 1 .
, ,
INC , ADD. ,
add eax,4
; 4
:
inc
inc
inc
inc
94
eax
eax
eax
eax
; 1
6.
, INC, DEC ,
64- , .
INC DEC,
.
,
.
LEA,
. :
lea ebx,[ecx+edx*4+0x500]
lea ebx, [eax+eax*4-l]
l e a ebx, [+*8]
lea ecx,[eax+ebx]
;
; + EDX*4 + 0x500
; = *5 - 1
; = *9
; = +
- 0.
, OR
TEST. ,
m ,0
jz i s _ z e r o
; 0 ?
;? is_zero
:
; OR
or ,
; (ZF),
;? is_zero
jz i s _ z e r o
OR ,
. , :
OR , (ZF) 1.
,
0.
jz:
dec
jz now_zero
;, 0
, .
TEST, ,
, .
SF 1, ,
1. , TEST
: , SF
( 1 (1 AND 1 = 1)
, SF=1):
95
t e s t eax,eax
js is_negative
; TEST
;
;, SF=1
(, LOOP ).
:
, .
,
. near,
128 , (short).
- .
96
,
.
- ,
. ,
, .
7.1.
,
.
: 32- , numb e r l number2, r e s u l t .
, .
, r e
sult:
mov eax,[number1]
mov ebx,[number2]
add eax,ebx
mov [result],eax
;
; = number2
; = +
; r e s u l t
number1 dd 8
; numberl
; 8
; number2
; 2
; result
number2 dd 2
r e s u l t dd 0
, :
mov eax,[numberl] ;EAX = "numberl"
add eax,[number2] ;EAX = EAX + number2
mov [result],eax ; result
98
7.
: 32- . EDI
, .
EDX.
32- ,
EDI. 4 ,
, , 4 .
mov e d x , [ e d i ]
add e d x , [ e d i + 4 ;
; EDX
; , EDX
EDI:
mov e d i , n u m b e r s
mov e d x , [ e d i ]
add e d x , [ e d i + 4 ]
/ EDI numbers
; -
; EDX
;
numbers dd l
/ numbers
; 1 2,
; EDX 3
dd 2
;
;
, .
: 8- , ESI
. .
8- ,
32- .
,
.
; ESI
mov esi,array
; =
mov ebx,0
; =
mov eax,ebx
again:
mov al,[esi]
; AL
inc esi
; .
add ebx,eax
; = +
cmp a1,0
;AL ?
jnz again
; again, AL
array db 1,2,3,4 5,6,7,8,0 ; .
; () 3 6
99
. 7.1. -
: , .
, .
SHR, CF,
, .
p u s h ax
shr ,1
pop ax
jc odd
even:
;
; CF
;
; CF = 1, odd
; , -
odd:
; ,
, :
test al,l
j z even
; 1, TEST
;ZF ( ) , t e s t
; 0, = 0,
;
odd:
even:
; ,
, AL, .
.
100
7.
: , AL,
,
.. .
, 0x15, 00010101b.
: 10101000b, 08.
, .
AL ,
,
, .
. , SHR ( ),
CF ( ) , RCL,
. 8 ,
.
mov , 8
theloop:
shr a l , l
rcl ah,l
loop theloop
; = 8
; AL 1 ,
; CF
/ 1 ,
; CF
; 8
: ,
.
,
10 (). AL ,
. :
, YES.
, :
mov ,0
d i v bl
cmp a h , 0
jz y e s
:
;BL = 10 -
; BL
; = 0?
; , YES
; ,
yes :
101
7.2.
, .
:
.
,
.
? 10
, 0 (.
ASCII, . 1.2). ,
. :
#include <unistd.h>
void main(void) {
unsigned int number;
char remainder;
number=12345 678;
while (number 1= 0)
{
remainder = (number % 10)+'0';
/* remainder = number mod 10 + char('0') */
number /=10; /* number = number div 10*/
printf("%c",remainder);
}
}
. 7.2. -
102
7.
, , :
12345678, 87654321,
, .
?
, , ,
, .
,
. , .
,
.
.
,
EDI ( ),
.
( ).
c o n v e r t .
while
, ,
.
.
convert , 7.1.
7.1.
( )
convert:
mov ecx,0
mov ebx,1
.dividei
mov edx,0
div ebx
; =
; = 010
;EDX = 0
; , ,
; EDX
; ASCII- 0
;
;
;? ( 0?)
; , .divide
; ,
; ,
;
.reverse:
pop eax
103
.
mov [edi] al
add e d i , 1
dec ecx
;
;
; ,
;
cmp ecx,0
; ?
jnz . reverse
;?
ret
;?
.
, .
MOV ecx, 0 XOR ecx, ecx.
, 10 (
0), 10 BL:
.
0 OR eax,eax (
TEST e a x , e a x ) .
:
mov [ e d i ] , a l
add e d i , 1
:
stosb
, .
LOOP. , 7.2.
7,2.
( )
convert:
,
xor ebx,ebx
mov , 1 0
.divide:
xor edx,edx
div ebx
add dl,'0'
push edx
inc ecx
or eax,eax
jnz .divide
104
; =
; =
; = 010
;EDX = 0
; , ,
; EDX
; ASCII- 0
;
;
;? ( 0?)
; 0, .divide.
; , ,
;
7.
. reverse:
pop eax
stosb
;
; AL ,
;EDI, EDI 1
loop . r e v e r s e
;=-1, ,
ret
;?
, .
,
:
MOV b y t e [ e d i ] , 0
LOOP RET.
b y t e ,
, EDI.
. ,
.
(, , , EDX EDI). ,
,
.
:
mov eax,0x12345678
mov e d i , b u f f
c a l l convert
; ,
;
; EDI
;
;
, ,
. ,
. EDI
DI ( ). CALL
.
.
convert
. ,
.
, ASCII-
. , ASCII-
(. 1.2).
16, F, ASCII-
, 9. :
105
.
9, .
ASCII- ,
HexDigit:
HexDigit:
cmp d l , 1 0
jb .less
add d l , ' A ' -10
ret
.less:
or d l , ' 0 '
ret
; DL 0-15,
;
;
; DL 10
;,
;10 ' ' , 11 ' ' . .
;
;
;
convert, 10
0x10, 16.
,
. ,
N- ,
.
, , ,
. ,
PUSHAD POPAD.
, 7.3,
:
7.3.
( )
NumToASCII
= 32-
ebx =
edi = -
:
NumToASCII:
pushad
xor esi,esi
convert_loop:
106
ESI = 0:
7.
xor edx,edx
div ebx
call HexDigit
push edx
inc esi
test eax,eax
jnz convert_loop
eld
write_loop:
pop eax
stosb
dec esi
test esi,esi
jnz write_loop
mov byte [edi],0
popad
ret
;EDX =
; , ,
; EDX
; ASCII
,- EDX
;
;? ( = 0)
; 0,
; DF:
;
;
; ES:(E)DI
;
;? (ESI = 0)
; 0,
;
;
;
; ! ! !
7.3.
, .
(readln, scanf),
.
c o n v e r t _ c h a r , '0'-'9' 0-9,
'A'-'F' ''- 1015 (-OxF). ,
, AL,
ASCII-.
.
.
convert_char:
sub al,'0'
emp al, 10
jb done
add al,'0'
and al,0x5f
; ASCII-
; 10,
;
;
; JB ,
;
;AL =
.-
107
done:
ret
; 10
; 0-15.
; F,
; 4 AL
: ,
.
, .
ASCII- .
0-9, :
done; , .
ASCII (. 1.2)
(
),
( 0x5F).
, '' , '' .. , ,
AND
0x00 OxOF.
,
. , . ,
, , ,
. , , ,
.
,
,
NEG ().
.
:
= an*zn + an-1*zn-1 + ... + a1*z1 + a0*z (n )
, 1234 :
1234 = 1*103 + 2*102 + 3*10' + 4*10
, 0x524D
:
3
7.
,
. ,
, .
1234 :
1234 = ((((1)*10 + 2)*10 + 3)*10) + 4
, ,
, ..
.
. 7.3. -
7.4.
109
.
7.4.
ASCIIToNum
;
;
;
;
;
esi = ,
0x0
=
:
=
ASCIIToNum:
push esi
xor eax,
xor ebx,ebx
cmp byte [esi]
jnz .next
inc esi
.next:
lodsb
or al,al
j z .done
call convert_char
imul ebx,ecx
add ebx,eax
jmp short .next
.done:
xchg ebx,eax
pop esi
cmp byte [esi],'-'
jz .negate
ret
.negate:
neg eax
ret
110
;
; = 0
; = 0:
; ?
; ,
;
; '- '
; AL
; ?
;
; AL
; ,
;
;
;
; ESI
; ?
;,
;,
;
:!!!
,
,
() .
.
8.1.
1950- .
:
, ,
, .
,
.
.
(1960- ).
.
,
.
.
,
. .
,
.
. ,
, ,
, ,
1964 .
112
8.
: , , ,
, ?
.
8.2.
.
, ,
.
,
, . ,
, - ,
.
.
( ,
) .
.
. 8.1.
, .
UNIX (Linux) init.
, .
DOS
COMMAND.COM.
. (kill),
. ,
( init), .
113
,
.
,
. , .
,
.
, ,
.
, - ,
,
. ,
.
.
,
, , , ,
.
. ,
,
. ,
, .
.
,
(, process control block).
UNIX
:
, ,
.
: ,
.
,
.
.
,
, : - ,
, .
, ,
, , (-).
114
8.
. 8.2.
DOS , DOS
:
.
:
.
( , ..).
.
.
.
(PID, Process ID).
.
.
.
.
, (,
) , .
(Round
Robin). .
.
,
.
. ,
. ,
115
,
.
.
8.3.
,
.
.
.
, ,
, .
.
. 8.4.
116
8.
, .
, .
. ,
. .
,
. , ,
.
.
,
.
(swapping)
,
.
.
( !) .
, .
.
, () ,
,
.
: ,
.
,
, .
.
,
, ,
.
, 4 .
,
.
117
. 8.5.
(MMU, Memory Management Unit),
.
,
.
. 8.6.
. ,
, ()
.
(read/write/execute) .
.
()
(. 8.7).
118
8.
. 8.7.
MMU ,
.
.
.
,
( 1) ()
.
,
( ) , .
( 0),
( MMU) (Page
Not Found), .
.
,
, .
, ,
, 119
. ,
.
.
() ,
.
86-
4 . ,
, ,
, , .
.
8.4.
,
, .
, .
,
.
, ? ,
,
. ,
, .
,
,
, , , .
,
.
.
,
.
, .
- (IBM 390, AS/400).
, .
,
, .
DOS
UNIX.
120
8.
.
.
( ).
, .
.
,
, , .
,
. DOS \, UNIX /.
.
.
.
,
, . ,
.
UNIX /
:
, /
.
0,
(stdin). (stdout)
(stderr). 1,
2.
,
/. ,
,
. DOS,
UNIX, ,
. DOS UNIX
:
Is > f i l e l
( ) filel
Is, , ,
. Is
,
.
, .
121
(), ^
().
, /
(. 8.8). ,
. , , .
,
: , .
. 8.8.
, .
, ,
512 . /
.
.
,
. ,
, . ,
.
60 (
).
, ,
.
(
) ,
0 . ,
, .
122
8.
, .
, . : ,
,
, . -
, .
(partition table).
( )
( ) .
, .
8.5.
.
, ROM (read-only memory). ROM
/ (BIOS, Basic Input Output
System). BIOS
, ,
POST (Power On Self Test).
BIOS
. BIOS
, , ,
( 0), CD-ROM .. ( 0)
.
MBR (Master Boot Record). BIOS
MBR
.
, ,
.
.
( ) .
,
: .
123
, BIOS
BIOS (Basic Input/Output System /)
PC.
.
DOS BIOS
.
BIOS
,
. BIOS
(. 8.1).
BIOS
8.1
0x10
( )
0x13
0x14
0x15
0x16
0x17
( )
BIOS,
BIOS .
BIOS ,
.
. 8.9.
124
9 NASM
NASM
NASM
(Netwide Assembler), (
) LGPL.
http://nasm.sourceforge.net.
MASM (Microsoft Assembler) TASM (Turbo Assembler Borland),
NASM
, .
9.1.
( )
:
(, ).
. (;),
.
, ,
'V ( ).
(
).
9.2.
,
. NASM
.
. , ,
126
9. NASM
dl, ''-
I
&
>>
+ * / % // %%
+ - ~
9.1
OR
XOR
AND
, , (
), ,
, (NOT)
:
mov eax,((5*6+2)+(040<<2))/8
; :
;MOV e a x , 0 x 2 4
:
mov c l , ~ l
;: mov cl,11111110b
;:
mov c l , 0 x F E
9.3.
JMP CALL,
, . ,
. ,
,
.
,
,
127
. , NASM,
.
,
, .
(, )
, ? ,
,
.
: ,
.
subprog:
subprog2:
; subprog
;
;
;
; subprog2
.local_loop:
.11_1:
9.4. NASM
,
.
,
.
. ,
, , ,
.
NASM ,
.
%.
( ).
, %i
(%idefine %define ..).
%define, %undef
, ,
.
:
128
9. NASM
%define a v e r a g e ( a , b )
((()+(b))/2)
:
mov a l , a v e r a g e ( 3 , 7 )
, :
mov a l , 5
%define , :
% d e f i n e SEC_IN_MIN
60
% d e f i n e SEC_IN_HOUR SEC_IN_MIN * 60
, :
% d e f i n e USE_MMX
, , , ,
%ifdef () %ifndef ( ).
%undef.
%macro %endmacro
%define ,
( ). , ,
%macro %endmacro.
. ,
. %endmacro .
%macro subtract 3
sub %1,%2
sub %1,%3
%endmacro
s u b t r a c t :
subtract
eax,ecx,[variablel]
:
sub eax,ecx
sub eax,[variablel]
. NASM
. ,
2-3 , 2 3 .
,
:
%macro a d d i t 2-3 0
a d d %1,%2
a d d %1,%3
%endmacro
5 . 293
129
a d d i t , ,
, ,
0:
a d d i t eax ebx
:
add eax,ebx
add eax,0
%assign
%assign ()
. ,
. ,
%assign i i+1
1. %assign
.
%if
, NASM
. ,
.
:
%:<>
; if e l i f
;, .
%:<2>
; %elif %else
; ,
%else
; ,
; %else %endif
%endif
%elif %else .
%elif, %else ( ) .
.
: = , < , > , < = , > = , < > (,
, , , , ).
== != = .
|, , && (OR, XOR, AND), .
130
9. NASM
? %ifdef, %infndef
%ifdef .
:
%define TEST_IT
%ifdef TEST_IT
cmp eax,3
%endif
, %ifdef ( ,3)
, TEST_IT
% define.
%ifdef %ifndef.
,
.
%include
%include ,
. .
%include
:
%include macro.mac
,
, . ,
:
%ifndef MACROS_MAC
%define MACROS_MAC
;
%endif
.
, .
9.5.
NASM ,
. NASM , MASM TASM,
.
131
.
BITS
,
.
16- (BITS 16) 32- (BITS 32).
,
NASM.
, 80386,
.
.
16- . 80386
32- . , 32-
16- ?
, . 3,
. Intel
, 32- ,
? .
16- MOV ,01234 08,
0x34, 0x12.
32- MOV ,000001234
0x66, 08, 0x34, 0x12, 0x00, 0x00.
16- () 32-
0x66 (
0x67). 16- .
32-.
32- 32 . 08, 0x34, 0x12, 0x00, 0x00 (
0x66) MOV ,000001234.
BITS ,
.
SECTION SEGMENT
, ,
: , (
) (, ,
,
).
SECTION
SEGMENT.
132
9. NASM
.text,
.data, .bss.
, ( 9.1)
9.1. ,
/
; , ,
;()2005 .. <ivan@ivanov.com>
SECTION .text
; .text
;
mov eax,2
EAX = 2
mov ebx,5
EBX = 5
add eax,ebx
EAX = EAX + EBX
add eax,[statl]
EAX = EAX + statl
mov [dynl],eax
dynl
SECTION .data
; DB, DW and DD
;
.data
statl: dd 1
SECTION .bss
;B ,
/. .bss
; RESB, RESW RESD.
; ,
;. .
;RES* ,
;
dynl: resd l
;
(.stack).
, .
SECTION S E G M E N T
.
.
. , ,
.
133
.
EXTERN, GLOBAL COMMON
13 (
) ,
,
.
EXTERN (extern) .
,
, - .
.
GLOBAL
().
COMMON GLOBAL
, .bss.
COMMON
,
.
CPU
CPU,
.
80386, CPU.
,
86- .
CPU
,
.
CPU 808 6
;
; I n t e l 8086
; . . 286, 386, 486, P e n t i u m . . .
ORG
ORG
, .
, ORG - (
), ORG
0x100. DOS
, 0x100.
134
9. NASM
,
NASM ,
.
MASM TASM, NASM
ORG .
ORG: ORG,
. MASM TASM
:
ORG
;
;
ORG 510
DW 055
; NASM ORG
; TIMES:
ORG 0
;
;
TIMES 510-($-$$) DB 0
DW 0XAA5 5
9.6.
Netwide Assembler (NASM)
86- . NASM
, .
-f. ,
.
:
.
()
.
, (
)
.
135
.
, (linking),
.
, ,
, ,
( EXTERN GLOBAL),
.
bin
bin .
.
.
, h e l lo . asm:
; ,
/ 0x0.
; DB " H e l l o w o r l d ! " ,
;
s t r i n g : db "Hello world!",Oxd,Oxa
, -f:
nasm -t bin hello.asm
h e l l o , Hello world.
- , hello
,
ASCII-, .
bin (, ,
). ALIGN,
, . ,
, 16:
section
. t e x t align=16
bin ,
DOS (.COM .SYS) .
BITS 16.
, bin,
ORG.
OMF 16-
OMF (Object Module Format) ,
Intel, Turbo Assembler.
MASM NASM .
136
9. NASM
OMF .obj,
OBJ. .obj
.
, obj 16-
, NASM 32- .
NASM 32-
Borland, 32- ,
, Microsoft.
OBJ , ,
SEGMENT (SECTION).
,
NASM.
, OBJ IMPORT,
11, Windows.
IMPORT DLL,
DLL.
OBJ- ( ).
,
. (entry point)
( ) ..start:.
OBJ,
-1 obj.
Win32 32-
Microsoft Visual C+ +
32- Win32.
COFF (Common Object File Format),
. ,
COFF,
coff, .
Win32,
-f Win32.
aout aoutb UNIX
a.out (Assembler and link editor OUTput files)
Linux.
a.outb BSD- (NetBSD, FreeBSD OpenBSD). NASM ,
-f aout Linux -f aoutb BSD.
137
.
coff a.out
COFF (Common Object File Format)
a.out.
UNIX, Windows NT,
( ) , , DJGPP.
C + + .
-f coff,
COFF. .
elf UNIX
ELF (Executable and Linkable Format)
, . UNIX-
. ,
Linux, Solaris x86, UnixWare, SCO-UNIX UNIX
System V, .
..
-f elf.
.
,
, ..
,
.
NASM
-g. OBJ ELF.
10
DOS
DOS
DOS
640 KB
( 1981)
DOS ( MS-DOS, Microsoft Disk Operating System)
IBM PC.
/,
8- 16- .
DOS .
, .
,
UNIX .
DOS 640 , ,
.
(
8086),
,
, . 80286 80386
. , ,
. ,
.
.1.
8086 20-
220 (1 ) . ,
16-: 20
?
. ,
,
16- , 16.
140
10. DOS
. 10.1.
,
:
mov a l ,
[ds:si]
; AL
; d s : s i
, DS = 0559, a SI 0x100.
20- :
0559 * 0x10 (0x10 16) + 0x100
: 05590 + 0x100 =
0550.
, (,
) (offset).
, 0550
, , 0558 0x200.
, , .
16-,
64 . ,
64 , .
. ,
, 10.4.
16- .
DOS-, DOS, 86- .
141
10.2. DOS
, - ,
DOS 1 .
10.1. ,
, .
10.1
(:)
0x0000:0x0000
0x0040:0x0000
BIOS
00????:00000
DOS kernel ( )
00????:00000
, ,
00????:00000
()
COMMAND.COM
00????:00000
00????:00000
(,
..)
00????:00000
0:000
( )
:0
( )
08:0000
80:0 - 0000:000
ROM
(Read Only Memory)
0xF000:OxO000
ROM BIOS
. . 256
, , 4 . 1 .
, 0x400 (.. 0x0040:0x0000),
BIOS. ,
, .
, - ,
Ralf Brown Interrupt List .
.
COMMAND.COM,
.
TSR .
, ,
142
10. DOS
.
, ,
.
,
DOS (
), .
.
, .
640 , ..
000:00. 640 1
.
64 .
.
00800:00000
, . 00800:00000
. . ASCII , .
,
ROM (Read Only Memory): BIOS BIOS
.
10.3. 1 MB
640 ,
, .
80286 24- ,
20 , .. 1 .
0x0F000,
BIOS. BIOS ,
OxFFFF.
20 ,
1 .
,
OxOFFFF. 1
, - 21- 1.
- , 20 (21- 20,
). 20 21-
64 .
143
64 ,
. 640 1
, ROM-. -
?
, . Lotus, Intel Microsoft
, LIM EMS (Expanded
Memory Specification). 640 1
64 KB, , 16
. (page frame), EMS
1 .
80286 EMS- ,
80386
.
1
XMS (Extended Memory Specification).
HIMEM.SYS,
1 1
.
, . ,
.
10.4. DOS
DOS
.
,
.SYS. .
CONFIG.SYS.
.
( .
. ) .
. 64 , 256
.
(.. 64 KB).
0x100.
(CS, ES, DS SS),
SP OxOFFFE, a
0x100,
.
144
10. DOS
0 0x100 .
PSP (Program Segment Prefix).
PSP-
. 0x80 ,
.
-, 0x1234
(. . 10.2):
-
10.2
(:)
0x1234:0x0000
, .
0x1234:0x0080
0x1234:0x0100
0x100 COM-
01234:0????
()
01234:0????
()
01234:0????
(.bss)
,
0x1234:0xFFFE
. ,
, 64 .
.
.
,
. , ,
, (call far),
(CS IP).
(retf). ,
SEG
. ,
, .
, .
, (..
)?
,
0x0000:0x0000,
0x0000:0x0000 0x0000:0xFFFF,
0x1000:0x0000 .
, (..
0x0000, 0x1000 ..),
(relocation table).
145
.
.
,
,
.
.
, .
10.5.
. ,
, ,
.
0x21.
. DOS
. ,
, .
,
.
DOS 04,
.
:
:
0x4
AL =
, AL
, ,
.
, , .
, exit .
( 10.1).
10.1. DOS
SECTION .text
; DOS.
; ,
;
org 0x100
; , 0x100
146
10. DOS
mov ah,0x4C
mov al,0
int 0x21
;
;
; 0
;
f i n i s h . a s m .
,
.
f i n i s h . asm nasm:
nasm -f bin - finish.com finish.asm
-f , -
f i n i s h , com.
f i n i s h . c o m ,
finish Enter.
.
. Hello, World!
.
,
.
, $ (0x24).
:
:
= 0x09
DS:DX = , $
DOS
0x02, ASCII DL.
:
:
= 0x02
DL = ASCII-
, ,
Hello, World! .
f i n i s h , asm.
. d a t a hello,
, Hello, World!, CR (OxD)
LF (), ,
$ ( 10.2).
147
SECTION .text
; "Hello, World!'
; (E0L - End Of Line)
org 0x100
;
mov ah,0x9
; DOS
mov dx,hello
; DX
int 0x21
; DOS
mov ah,0x4C
; DOS
mov al,0
;
int 0x21
;
SECTION .data
hello DB "Hello, World!",0xd,0xa,'$'
;
DS:
.
h e l l o . asm
nasm -f bin - hello.com h e l l o . a s m .
h e l l o .
DOS
0x01, getchar readkey
Pascal.
:
:
= 0x01
:
AL ,
()
0x01 .
( 0x08 ,
).
AL 0,
(extended) ASCII-, Page
Up, Page Down, Home, End, F1..F12 ..
0x01 AL ASCII-
.
,
Enter. DOS ,
.
148
10. DOS
:
:
:
=
DS:DX =
,
.
,
(1-254), (ASCII 7) Enter.
, OxOD (
Enter).
mov a h ,
mov dx, s t r i n g
i n t 0x21
; ,
,- DX ()
; DOS
s t r i n g db 5,0,0,0,0,0,0
mov a h ,
;
mov d x , s t r i n g
; DX () s t r i n g
i n t 0x21
; DOS
s t r i n g db 5,0,0,0,0,0,0
int 0x21
Enter.
7 , 4
Enter. ,,,
Enter, string :
s t r i n g d b 5,3,0x65,0x66,0x67,OxOD,0
,
OxD,
(ABC), OxD.
.
1: ,
.
Enter ( 10.3).
DOS- 0x01,
. , ,
DOS- 0x02.
149
.
10.3. ,
;
SECTION .text
again:
;DOS-
mov ah,0x01
;
DOS
int 0x21
;
DL
mov dl,al
;
"Enter"?
cmp dl, OxD
; ,
j z endprog
;DOS-
mov ah, 0x02
; DOS
int 0x21
;
jmp again
endprog:
mov ah,0x4C
;DOS-
int 0x21
; DOS
;
echo . asm. echo . com
nasm -f bin - echo.com echo.asm
2: ,
Enter ( 10.4).
DOS- ,
,
DOS- 0x02.
10.4. ,
Enter
SECTION .text
;
; .
org 0x100
; COM
mov ah, OxOA
;DOS-
mov dx,string
; DX
int 0x21
; DOS
xor ax,ax
;
mov al,[string+1] ;
inc dx
;
add dx, ax
; ,
;
mov s i,dx
; DX SI
150
10. DOS
std
;
/
print_next_char:
lodsb
; DS:SI SI 1
cmp s i , s t r i n g + 1
; ? ( )
jb endprog
; ,
mov d l , a l
; DL
mov ah,0x02
;DOS-
i n t 0x21
; DOS
jmp p r i n t _ n e x t _ c h a r ;
endprog:
mov ah,0x4C
mov a l , 0
i n t 0x21
SECTION .data
s t r i n g db 254,0
times 253 db ' '
;
;-
; 0
; DOS
;
r e v e r s e . asm ,
:
nasm -f bin - reverse.com reverse.asm
,
.
, .
,
.
mov ah, OxOA
mov d x , s t r i n g
i n t 0x21
;DOS-
; DX
; DOS
, Enter,
. ,
.
,
;
mov a l , [ s t r i n g + 1 ]
;
DX
string. () ,
DX + 2 + _ 1 (
, ).
DX+2 , ,
OxOD ( Enter). ,
.
151
.
, 2 1 1,
. ,
.
inc dx
add dx,ax
mov s i , d x
;
; ,
;
; DX SI
, ,
AL, ,
, .
. LODSB
, SI,
.
1: ,
.
std
print_next_char:
lodsb
;
;
; DS:SI SI 1
LODSB AL , [DS:SI] , SI
1 . LODSB
s t r i n g + 1 , SI
( ).
(JZ), ,
, .
, .
cmp s i , s t r i n g + 1
jb e n d p r o g
; ?
; ,
. , AL,
DL, DOS .
LODSB, .
mov d l , a l
mov ah,0x02
i n t 0x21
jmp p r i n t _ n e x t _ c h a r
;
; DL
;DOS-
; DOS
;
04.
endprog:
mov ah,0x4C
mov al,0
int 0x21
152
;DOS-
; 0
; DOS
10. DOS
.
SECTION .data
s t r i n g db 254,0
times 253 db ' '
/
,
.
( , 254 ),
0 . TIMES,
253 .
10.6. -
8, ,
. DOS
. - -
.
() ,
.
.
DOS Windows
: 8 3
. Windows ,
256- , .
, 8+3.
0x3D.
DS:DX (DS , a DX ). AL
.
(AL=0).
.
CF=1, CF=0.
;.
= 0x3D
DS:DX = ,
0x0
AL. = :
AL = 0
AL = 1
AL = 2
:
CF = 0 ,
= ,
CF= 1 ,
= :
= 0x0002
= 0x0003
153
.
,
:
:
:
CF = 0
=
=
CF = 1 ,
=
, ,
, .
( 10.5).
10.5. -
SECTION .text
org 0x100
mov ax, 0x3D00
mov dx,file_name
int 0x21
jc error
mov bx,ax
mov ah,
int 0x21
mov al,0
endprog:
mov ah,4Ch
int 0x21
error:
mov al,1
jmp short endprog
DOS ,
-
, DS
DOS
? , error
,
DOS
DOS
:
; DOS
; DOS
; :
;
;endprog
SECTION . d a t a
f i l e _ n a m e d b " t e x t . t x t " , 0 ; t e x t . t x t
, 10.5 ,
:
. DOS- 0x3F.
154
10. DOS
.
(
),
.
:
:
= 0X3F
=
:
CF = 0
= ,
( , EOF)
:
CF=1
=
DS:DX =
=
.
, ,
DS:DX.
, :
.
( ,
, ).
CF=1,
.
, ,
DOS 0x09. ,
$, 0x09.
, 0x09,
,
/, .
0x0001.
0x40. ,
:
:
:
= 0x40
=
DS:DX =
= ,
:
:
CF = 0
= ,
:
CF = 1
=
155
DS:DX , ,
. ,
: .
, ,
. , , ,
.
, t e x t . t x t
.
, t e x t . t x t ,
0x3D.
0x3F, .
0x40.
.
10.6.
%define B_LENGTH 8 0
%define STDOUT 0x0001
SECTION .text
org 0x100
mov bp,STDOUT
mov ax, 0x3D00
mov dx,file_name
int 0x21
jc error
mov bx,ax
read_next:
mov ah,0x3F
mov dx,buffer
mov cx,B_LENGTH
int 0x21
jc error
or ax,ax
jz end_reading
mov ex,ax
156
;
;
;
;
;
; ;
; . DS
;
; DOS
;? , error
;
;
; DOS
;
;
; DOS
;? , error
; 0 ?
; ,
; ,
;
;
; ,
; ,
10. DOS
mov ah,0x40
xchg bp,bx
int 0x21
xchg bp,bx
jmp read_next
end_reading:
mov ah, 0x3E
int 0x21
mov al,0
endprog:
mov ah,4Ch
int 0x21
; DOS
;
; ,
;
;
; DOS
/
; read_next,
;
; DOS
; DOS
; :
; DOS
;
; DOS.
;
error:
; :
mov al,1
jmp short endprog
; endprog
SECTION .data
file_name db "text.txt' 0
; text.txt
SECTION .bss
buffer RESB B_LENGTH
;
;buffer B_LENGTH
list it. asm
nasm listit.asm - listit.com -f bin
t e x t . t x t
:
echo I'll BE BACK! > text.txt
t e x t . t x t
.
,
.
.
, !
157
.
/
0x3D 6 ,
, ,
.
:
/
=
AL = 0x00 ( 0)
=
CX =
DX =
DS;DX =
:
:
CF = 0
=
CX = (. )
:
CF = 1
: ,
(
).
DX.
.
4 ( DL):
DL = 0000b , (
).
DL = 0001b , .
DL = 0010b ( ),
.
4 ( DH):
DH = 0000b , .
DH = 0001b , .
,
DX 0x0012.
, ,
.
( )
. DOS UNIX, ,
, DOS :
0: 1, .
1: 1, .
2: 1, .
3: 1, .
158
10. DOS
4: 1, .
5: 1, .
6-15: .
0x20:
.
.
,
.
= 0: .
= 1: .
= 2: .
(CF=0)
( 0x3D).
:
= 1: .
= 2: .
= 3: .
l i s t i t . a s m ( 10.6) ,
. ( )
.
, . ,
:
mov ax, 6C00h
; DOS /
/ :
;06 0 AL
mov ex, 0x20
;
mov dx, 0x12
;
mov s i , other_file_name ;
i n t 0x21
; DOS
jc e r r o r
;
mov bp,
;
, MOV bp,STDOUT.
:
:
mov ah,0x3
mov bx,bp
i n t 0x21
; DOS
;
, , :
other_file_name db " t e x t l . t x t " , 0
;
159
text.txt textl.txt.
, ,
,
.
copy.asm :
nasm -f bin - copy.com copy.asm
(SEEK)
,
, .
(, 1 2
), /.
seek(), DOS 0x42.
:
= 0x42
AL =
=
CX:DX =
:
:
CF = 0
DX:AX =
CX =
:
CF = 1
=
.
CX:DX. ,
, ,
4 ( FAT16 2 ).
16 , a DX 16 .
AL ,
CX:DX :
AL = 0: (SEEK_SET),
CX:DX.
AL = 1: (SEEK_CUR).
AL = 2: (SEEK_END).
DX:AX.
SEEK
. 0x42 :
mov
mov
xor
xor
int
160
ax,0x4202
bx,filedes
dx,dx
cx,cx
0x21
=
DX = 0
= 0
DOS
10. DOS
DX:AX
.
,
.
0x41:
:
= 0x41
DS:DX =
:
:
CF = 0
:
CF=1
=
,
, .
0x43:
:
= 0x43
AL = 0x00
DS:DX =
:
=
AL = 0x01
DS:DX =
:
CF = 0
=
:
CF=1
=
0x56.
,
, ,
.
:
= 0x56
DS:DX =
ES:DI =
:
: CF = 0
:
CF=1
=
: text.txt
.
. :
( 10.7).
6 . 293
161
.
10.7.
SECTION .text
;
org 0x100
/ DOS 0x5 6
mov ah, 0x5 6
; DX
mov dx,src
mov di,dest
; DI
int 0x21
;
mov ax,0x4c00
int 0x21
SECTION .data
src db "text.txt",0
dest db "\text.txt",0
rename.asm .
, .
,
. :
SECTION .data
dest db "\"
src db "text.txt",0
.
, , .
Windows 95 8+3 .
, 8+3, . , LinuxRulez
LINUXR~1.
,
Windows. Ralf
Brown Interrupt List (http://www.ctyme.com/rbrown.htm).
0x70. AL
( ), ,
AL, .
, 0x3D
:
0706, .
162
10. DOS
10.7.
. MKDIR, RMDIR, CHDIR .. ,
.
(MKDIR, RMDIR)
0x39,
. .
,
.
:
= 0x39
=
DS:DX =
:
: CF = 0
:
CF = 1
=
(CHDIR)
(CHDIR). DOS,
, .
, DOS .
:
= 0x3
DS:DX =
:
: CF = 0
:
CF=1
=
(GETCWD)
,
. 0x47 ,
.
:
:
- 0x47
DL = :
DL = 0x00
DL = 0x01 :
: CF = 0
:
CF = 1
=
DS:SI = ,
163
.
,
0x19:
:
= 0x19
:
AL = :
AL 0x00 :
AL = 0x01 :
10.8. pwd,
,
10.8. , ,
SECTION .text
org 0x100
mov ah,0x19
i n t 0x21
add byte [buffer] ,al
xor dl,dl
mov ah,0x47
mov si,buffer+3
int 0x21
mov ah,0x40
mov bx,0x0001
mov cx,BUFF_LEN
mov dx,buffer
int 0x21
mov ax,0x4c00
int 0x21
SECTION .data
buffer db "A:\"
times 64 db " "
db 0x0D,0x0A
BUFF_LEN equ $-buffer
164
; DOS
; DOS
; '',
/
;
;
;
; DOS
;
; ":\"
; DOS
,- DOS
;
;
/ DOS
; DOS
; DOS
;
;
; $ ,
; .
; ,
10. DOS
:
:
=
DL = 0x00 :
DL = 0x01 :
AL =
10.8.
, DOS,
.
DOS,
. ,
04.
04.
, .
, 1,
.
:
= 04
=
ES = ,
: CF = 0
CF=1
=
=
16 .
- 64 (
0x1000 ). 0x1000
:
mov ah, 0x4A
mov bx, 0x1000
i n t 0x21
jc error
;
;- ; ES
;
0x48, .
165
.
0x48
. ,
. .
:
= 0x48
=
:
CF = 0
=
:
CF=1
=
= ( )
, 64 , .
, ,
04.
mov ah,0x48
mov bx,0x1000
i n t 0x21
jc e r r o r
p u s h es
mov e s , a x
; DOS
; 64 KB
; DOS
; CF = 1, ,
;
;
; ES
; ES
()
. 0x49:
:
= 0x49
ES =
:
: CF=0
CF=1
=
10.9.
argc ( ) argv (
). DOS ,
0x81 .
OxOD 166
10. DOS
0x80 ( ).
.
. : ,
0x81 ( 10.9).
10.9. ,
SECTION .text
%define STDOUT 1
org 0x100
mov ah,0x40
mov bx,STDOUT
mov dx,0x81
xor ex,ex
mov cl,[0x80]
mov di,ex
add di,dx
;
;
;-
; DOS
inc di
mov byte [di],OxA
inc
int
mov
int
ex
0x21
ax,0x4c00
0x21
;
;
; CL
; DI
;
; [OxOD]
; DI
;
; ,
,- EOL
; 1
;
;
; DOS
10.10.
, ,
: (CF=1), .
.
=0x59.
Ralf Brown Interrupt List.
10.3 .
DOS-
10.3
0x0002
0x0003
0x0005
167
.
. 10.3
0x0008
0x0009
OxOOOf
0x0010
0x0011
10.11.
10.11.1.
.
, : -
, .
(debugger).
- bug () ,
, ,
.
debugger.
, ,
..
DOS ( Windows)
debug.exe. Turbo Debugger,
Borland,
. IDA (Interactive
Disassembler) DataRescue.
( open, load).
, :
. : ( ,
) (
). ,
,
( ) .
(
,
).
168
10. DOS
( step). .
,
, .
step-over,
,
. ,
.
go continue.
(
breakpoints). ,
.
, ,
..
(,
) .
(, , ,
, result), ,
. ,
,
.
grdb, .
.11.2. g r d b . e x e
grdb (Get Real Debugger, )
.
, http://www.members.tripod.com/~ladsoft/grdb.htm.
, .
grdb.
:\>grdb
GRDB v e r s i o n 3.6 C o p y r i g h t (c) LADsoft 1997-2002
History enabled
eax:00000000 ebx:00000000 ecx:00000000 edx:00000000 esi:00000000
edi:00000000 ebp:00000000 esp:0000FFEE eip:00000100 eflags:00000202
NV UP EI PL NZ NA PO NC
169
00000069
.
t. t
Enter:
->t
:00001900 ebx:00000000 :00000069 edx:00000000 esi:00000000
edi:00000000 ebp:00000000 esp:0000FFEE eip:00000102 eflags:00000202
NV UP EI PL NZ NA PO NC
ds: 10FB es:10FB fs:10FB gs:10FB SS:10FB cs:10FB
10FB:0102 CD 21
int
21
->
t ,
0x19. INT 21,
. ( CS:IP)
. t Enter
DOS, AL .
->t
:00001902 ebx:00000000 :00000069 edx:00000000 esi:00000000
edi:00000000 ebp:00000000 esp:0000FFEE eip:00000104 eflags:00000202
NV UP EI PL NZ NA PO NC
ds: 10FB es:10FB fs:10FB gS:10FB ss:10FB cs:10FB
10FB:0104 00 06 24 01 add
[0124],al
ds:[0124]=41
->
AL 02, :.
ADD [buffer],al,
AL ''. , buffer
. :
buffer, () 0124.
d 124. d
, ,
.
170
10. DOS
- > d 124
" 10FB:0120
10FB:0130
10FB:0140
10FB:0150
10FB:0160
10FB:0170
10FB:0180
10FB:0190
lOFBiOlAO
20
20
20
20
20
20
20
20
20
20
20
20
-41
20-20
20-20
20-20
20-20
20
20
20
20
5
20
20
20
20
20-20
20-20
20-20
20-20
0D-0A
20
20
20
20
C7
20
20
20
20
06
20-20
20-20
20-20
20-20
8C-CD
20
20
20
20
01
20
20
20
20
00
20 : \
20
20
20
EB
->
ADD t. :\
:\ ( ). d:
- > d 124
10FB:0120
- 4 3 5 2 0 - 2 0 20 20 2 0 - 2 0 20 20 20 : \
,
.
t
g (go),
.
.
() .
,
, .
( step over),
.
,
:
-> 100
10FB:0100
10FB:0102
10FB:0104
10FB:0108
10FB:010A
10FB:010C
10FB:010F
10FB:0111
10FB:0113
10FB:0116
10FB:0119
B4
CD
00
30
B4
BE
CD
B4
BB
B9
BA
19
21
06
D2
47
27
21
40
01
45
24
mov
int
24 01 add
xor
mov
mov
01
int
mov
00
mov
mov
00
01
mov
ah,0019
21
[0124],al
dl,dl
ah,0047
si,0127
21
ah,0040
bx,0001
ex,0045
dx,0124
171
.
10FB:011C CD 21
10FB:011E B8 00 4C
->
int
mov
21
ax,4C00
q.
grdb
grdb .10.4.
grdb
10.4
I <filename> [p]
<filename> () []
a <addr>
<addr>
num addr.
0 F, 16
b<num>,addr
b
,
.
7. ,
,
.
, ASCIIToNum:
; ASCIIToNum
;
;
;
;
172
e s i = , 0x0
=
:
=
10. DOS
DOS,
16- . () SI,
ESI.
.
,
.
.
SkipSpace, .
,
, .
; SkipSpace
; si =
; :
; si = ,
SkipSpace:
.again:
lodsb
; AL DS:SI,
/ SI
cmp a l , ' '
;
jz again
; ,
ret
, ,
. ,
0x81, OxD.
. ,
, . ,
. ,
.
; SkipNum
; : SI =
; : DI =
SkipNum:
mov di,si
.find_end_of_numberi
inc di
cmp [di],'0'
;DI
;DI=DI+1
; ASCII-
;
173
.
j b .end_found
cmp [ d i ] , ' 9 '
ja .end_found
jmp .find_end_of_number
.end_found:
ret
; ,
; ASCII-
9
; ,
;
:
mov si, 0x81
call SkipNum
mov [di],0
mov ecx,10
call ASCIIToNum
mov edx,eax
mov si,di
inc si
call Skipspace
call SkipNum
mov [di],0
call ASCIIToNum
add e a x , e d x
; SI
;
;
;
;
; , SI
;
; EDX
; SI
;
;
;
;
;
; , SI
;
; ,
; EDX
.
,
.
. DI
, , .
SI,
DI.
,
.
.
,
.
NumToASCII:
174
10. DOS
; NumToASCII
;
;
;
;
;
eax = 32-
ebx =
edi =
:
mov ebx,10
mov di,buffer
call NumToASCII
mov ah,0x40
mov d x , d i
; DOS
;
; DS:DX
;
; DI
;
;
,
. ,
. :
.
, .
mov cx,25
i n t 0x21
mov ax, 0x4c00
;
i n t 0x21
25 :
buffer times 2 5 db ' '
.
SECTION . t e x t
org 0x100
mov si, 0x81
call SkipNum
mov [di],0
mov
call ASCIIToNum
mov edx,eax
mov si,di
inc si
call Skipspace
call SkipNum
/ SI
;
;
;
;
; , SI
;
; EDX
; SI
;
;
;
175
mov
[di],0
call ASCIIToNum
add eax,edx
mov ebx,10
mov di,buffer
call NumToASCII
mov ah,0x40
mov dx,di
mov ex,2 5
int 0x21
mov ax, 0x4c00
int 0x21
;
;
; , SI
;
; ,
/ EDX
;
;
; DI
;
;
;
; DX,
; DI
;
; DOS
;
; SkipSpace -
; si =
; :
; si = ,
SkipSpace:
.again:
lodsb
AL DS:SI,
SI
cmp al,' '
;
jz again
,
ret
; SkipNum
; : SI =
; : DI =
SkipNum:
mov di,si
;DI
.find_end_of_number:
;DI=DI+1
inc di
emp [di],'0'
; ASCII-
jb .end_found
; ,
emp [di],'9'
; ASCII- 9
ja .end_found
; ,
jmp .find_end_of_number
;
.end_found:
ret
176
10. DOS
. * * * * * *****
SECTION .data
buffer times 2 5 db ' '
;
.
:
nasm -f bin - t e s t . c o m t e s t . a s m
t e s t . a s m : 6 : e r r o r : o p e r a t i o n s i z e not s p e c i f i e d
t e s t . a s m : 1 3 : e r r o r : symbol ' S k i p s p a c e ' undefined
t e s t . a s m : 1 5 : e r r o r : o p e r a t i o n s i z e not s p e c i f i e d
t e s t . a s m : 4 1 : e r r o r : symbol ' a g a i n ' undefined
t e s t . a s m : 5 3 : e r r o r : o p e r a t i o n s i z e not s p e c i f i e d
t e s t . a s m : 5 5 : e r r o r : o p e r a t i o n s i z e not s p e c i f i e d
t e s t . a s m : 1 4 1 : e r r o r : phase e r r o r d e t e c t e d at end of assembly.
, . .
6:
mov [ d i ] , 0
, :
mov byte [ d i ] , 0
,
. ,
Skipspace SkipSpace. .
13 ,
6. 41 'again':
jz
.again
53 55 .
'byte' .
, . ,
. :
mov
mov
mov
int
ah,0x4 0
dx,di
cx,25
0x21
/
; DX, DI
;
; DOS
,
0x01 10.
.
45 50. :
C : \ t e s t 45 50
945
7 . 293
177
95, 945. ?
. grdb:
:>grdb
50
Size: 000000E1
->
:00000000 ebx:00000000 :0000001 edx:00000000 e s i : 0 0 0 0 0 0 8 1
edi:00000000 ebp:00000000 esp:0000FFEE eip:00000103 eflags:00000202
NV UP EI PL NZ NA PO NC
178
10. DOS
NV UP E I MI NZ AC PE CY
ds: 10FB es:10FB fs:10FB gs:10FB SS:10FB cs:10FB
10FB:0112 66 89 C2
mov
edx,eax
->
01,
45. 01 945.
? , ,
, .
, , SI:
->d s i
10FB;0080
10FB:0090
10FB:00A0
10FB:00B0
10FB:OOCO
10FB:00D0
10FB:00E0
10FB:00F0
10FB:0100
->
,
. :
( 20). :
SkipSpace,
.
. :
C : \ t e s t 3 45 50
5
, .
ASCIIToNum:
->
:00000034 ebx:00000000 :0000000A edx:00000000 esi:00000083
edi:00000084 ebp:00000000 esp:0000FFEE eip:00000112 eflags:00000287
NV UP EI MI NZ NA PE CY
ds: 10FB es:10FB fs:10FB gs:10FB ss:10FB cs:10FB
10FB:0112 E8 6D 00
call
0182
->d si
10FB:0080
35-00 35 30 0D-01 01 01 01-01 01 01 01 5.50
10FB:0090 01 01 01 01-01 01 01 01-01 01 01 01-01 01 01 01
10FB:00A0 01 01 01 01-01 01 01 01-01 01 01 01-01 01 01 01
10FB:00B0 01 01 01 01-01 01 01 01-01 01 01 01-01 01 01 01
10FB:00C0 01 01 01 01-01 01 01 01-01 01 01 01-01 01 01 01
10FB:00D0 01 01 01 01-01 01 01 01-01 01 01 01-01 01 01 01
179
.
10FB:00E0 01 01 01 01-01 01 01 01-01 01 01 01-01 01 01 01
10FB:00F0 01 01 01 01-01 01 01 01-01 01 01 01-01 01 01 01
10FB:0100 BE 81 00 E8-41 00 E8 44-00 C6 05 00-66 B9 0A 00 . .. . A . . D . . . . f . . .
->
, SkipSpace,
, 5 0
5 + 0 = 5. LODSB SI
, , . ,
. :
SkipSpace:
.again:
lodsb
cmp al,' '
jz again
dec si
ret
; AL DS:SI,
; SI
;
; ,
, , ,
:
C:\test4 45 50
95
. !
!
.12.
DOS ,
, .
, , .
.
.
,
, , .
0x33,
( , )
.
. ,
. , DOS
, DOS
( ). , 180
10. DOS
DOS, ,
. , , ,
, .
,
Scroll Lock. ,
, .
.
DOS ,
.
, 0x60
- , ASCII- ,
, . -
Scroll Lock 0x46.
IRQ1,
- , ASCII-
.
BIOS.
, IRQ 1
(int 0x9), .
- 0x60. Scroll
Lock, - .
( )
.
iret.
, .
, ,
.
color,
. (
) . , ,
0800 .
, : 0800:00001,
800:, 0800:00005 .. .
, , 0x04.
80x25 ,
.
181
.
color:
push ax
push ex
push si
push es
xor si,si
mov ax, 0xB800
mov es,ax
mov ex,80*25
.repeat:
inc si
mov byte [es:si],0x4
inc si
dec ex
jnz .repeat
pop es
pop si
pop ex
pop ax
ret
;
; ,
;
; SI
;
;
;
SI 1
; 0x04
; 1
; 1
; . r e p e a t , > 0
;
.
, ,
04:
SECTION . t e x t
call color
mov a x , 0x4c00
i n t 0x21
color:
IRQ 1:
new_handler:
push ax
i n a l , 0x60
emp a l , 0x46
jnz pass_on
call color
pass_on:
pop ax
jmp f a r [ c s : o l d _ v e c t o r ]
;
; -
; 0x46 (Scroll-Lock)
; , pass_on
;
;
;
;
o l d _ v e c t o r
( ).
182
10. DOS
CS,
.
old_vector,
.
setup.
setup:
cli
xor ax,ax
mov es,ax
;
;
;
;
mov ax,new_handler
; new_handler
;
xchg ax,[es:0x9*4]
;
; .
;
;,
mov [ds :old__vector] ,ax
;
; old_vector
mov ax,cs
; CS
xchg ax,[es:0x9*4+2]
;
;,
;
mov [ds:old_vector+2],ax ; 2
/ old_vector
sti
;
ret
;
, .
0x31.
:
= 0x31
AL =
DX = ,
r e s i d e n t . asm
10.10.
10.10.
SECTION .text
org 0x100
jmp initialize
183
new_handler:
push ax
in al, 0x60
cmp al, 0x46
jnz pass_on
call color
pass_on:
pop ax
jmp far [cs:old_vector]
color:
push ax
push ex
push si
push es
xor si, si
mov ax, 0xB800
mov es ,ax
mov ex,80*25
.repeat:
inc si
mov byte [es : si] ,!
,0x4
inc si
dec ex
jnz .repeat
pop es
pop si
pop ex
pop ax
ret
old_.vector dd 0
initialize:
call setup
mov ax,0x3100
mov dx,initialize
shr dx,4
inc dx
int 0x21
setup:
cli
184
;
; -
; 0x4 6 (Scroll-Lock)
; , pass_on
;
;
;
;
;
;,
;
; SI
;
;
;
,- SI 1
; 0x04
; 1
; 1
; .repeat, > 0
;
;
;
; DOS:
;
; :
;
; initialize
; 16
/ 1
;
;
;
10. DOS
;
;
;
; new_handler
mov ax,new_handler
;
;
xchg ax,[es:0x9*4]
;
; .
;
; ,
;
mov [ds:old_vector],ax
;
; old_vector
mov ax,cs
; CS
xchg ax,[es:0x9*4+2]
;
;,
mov [ds:old_vector+2],ax ;
; 2
; old_vector
sti
;
ret
;
:
,
mov e s , a x
10.13.
:
www.ctyme.com/rbrown.htm HTML-
(Ralf Brown's Interrupt List);
http://programmistu.narod.ru/asm/lib_l/index.htm
IBM PC .
185
Windows
Windows-
DOS-
Windows
11.1.
- Microsoft Windows
DOS.
,
. UNIX-
(Linux, BSD .), Windows
(GUI) .
11.2. Windows-
Windows-
API-. API (Application
Programming Interface) ,
.
GUI , API
. ,
, .
,
Hello, World! .
.
11.2.1. API
DOS
0x21. Windows API.
API (DLL).
,
, : user32.dll, kernel32.dll ..
API , , Borland Delphi (
win32.hlp). Delphi, win32.zip
( , win32.hlp):
ftp://ftp.borland.com/pub/delphi/1echpubs/delphi2/win32.zip
187
//
//
//
//
,
. ,
0. ,
. , .
, ,
. () ,
_.
API ExitProcess,
( DOS), .
Windows- ,
win32.inc, API-
(, HWND LPCTSTR dword)
. :
%include win32n.inc;
API-,
, EXTERN
IMPORT:
EXTERN
IMPORT
EXTERN
IMPORT
MessageBoxA
/MessageBoxA
MessageBoxA u s e r 3 2 . d l l
;
ExitProcess
/ E x i t P r o c e s s
ExitProcess kernel32.dll
; -
user32.dll
kernel32.dll
, DOS, : .
SECTION CODE USE32 CLASS=CODE
SECTION DATA USE32 CLASS=DATA
;
;
, API.
13, ,
, ,
API.
188
11. Windows
STDCALL.
( , ),
( ).
PUSH,
CALL. He .
11.1.
11.1. Hello, World!
Windows
%include win32n.inc
/
EXTERN MessageBoxA
;MessageBoxA
IMPORT MessageBoxA user32.dll
; - user32.dll
EXTERN ExitProcess
;ExitProcess
IMPORT ExitProcess kernel32.dll ; kernel32.dll
SECTION CODE USE32 CLASS=CODE
;
; ,
..start:
,-
; .
push UINT MB_OK
; :
;
push LPCTSTR title
;-
; - ,
push LPCTSTR banner
;
;
push HWND NULL
;:
; API.
call [MessageBoxA]
;
;
; ExitProcess
push UINT NULL
;
call [ExitProcess]
SECTION DATA USE32 CLASS=DATA
banner db 'Hello world!',OxD,OxA,0 ;
; EOL
t i t l e db 'Hello',0
;
, , NASM
Windows, : http://nasm.sourceforge.net. NASM
,
.
alink, : http://alink.sourceforge.net.
msgbox.asm. nasmw -fobj:
:\WIN32>NASMW -fobj msgbox.asm
189
msgbox.obj,
alink:
C:\WIN32>ALINK -oPE msgbox
- . Windows
. msgbox.exe,
.
11.3.
, ,
VM86, .
( cli, popf .),
( in out)
, ,
.
DOS BIOS .
, DOS
8+3 (8 , 3 ).
,
.
,
.txt .asm.
11.5.
,
Windows:
http://win32asm.cjb.net
http://rsl.szif.hu/~tomcat/win32
http://asm.shadrinsk.net/toolbar.html
190
Linux
:
Asmutils. Asmutils
. ALD
GAS
12.1.
Linux .
Linux , (-)
. Linux
86-.
, .
, Linux
. ,
, ,
,
.
86-
.
:
.
( )
(
).
.
. , ,
(. 8).
(, , 32- ),
Linux 4 . 4
. ?
0 (4 ), ,
, 4
. ,
, ,
.
192
12. Linux
12.2.
, 4
.
? ,
: , ,
() . , ,
. Linux
, ELF (Executable and
Linkable Format). ELF-.
,
.
:
0x08048000
.text
.data
( )
.bss
( )
.stack
OxBFFFFFFF (3 )
0x08048000 ( 128 ).
.
(
).
.bss .stack
, .
- ,
,
( 1 ).
( .text, .data, .bss).
.bss .stack,
? ,
. (page fault)
.
, .
193
12.3.
,
?
, .
:
.
,
. :
ESP
argc
argv[0]
(dword)
argv[1]
argv[argc-1]
NULL
env[0]
env[1]
erw[n]
NULL
POP,
- . ,
(argc),
.
argc > 1, ,
.
, .
12.4.
DOS
0x21. Linux :
0x80. ,
?
, 0x80
( ) .
.
194
12. Linux
DOS, (syscalls)
, .
.
, , ..
: , , EDX, ESI EDI. 2.4.x
.
12.5.
.
, ,
. .
, ,
,
.
Linux man-,
, ,
.
12.6. M a n -
DOS Windows Linux .
( Manual Pages )
, Linux. ,
man- ( Linux-)
, man-
. .
, ,
. DOS
=04. ?
Linux. unistd.h,
/usr/snVlinux/include/asm:
#ifndef _ASM_I3 8 6_UNISTD_H_
#define _ASM_I3 86_UNISTD_H_
/*
* This f i l e c o n t a i n s the system c a l l numbers.
*/
#define
NR_exit
1
#define
NR_fork
2
195
.
#define
#define
#define
#define
NR_read
NR_write
NR_open
NR_close
3
4
5
6
#define _ s y s c a l 1 1 ( t y p e , n a m e , t y p e l , a r g l )
type name(typel a r g l ) \
{ \
long
res; \
asm
v o l a t i l e ( i n t $0x80 \
: =a ( res) \
: 0 ( NR_##name), b ( ( l o n g ) ( a r g l ) ) ) ;
syscall_return(type,
res); \
Linux.
NR_exit:
#define
NR_exit
, , , 1.
( ) 2 .
, exit(). :
man 2 e x i t
man-:
_EXIT(2)
Linux Programmer's Manual
, _EXIT(2)
NAME
_exit, _Exit terminate the current process
SYNOPSIS
#include <unistd.h>
void _exit(int status);
#include <stdlib.h>
void _Exit(int status);
DESCRIPTION
The function _exit terminates the calling process immedi
ately. Any open file descriptors belonging to the process
are closed; any children of the process are inherited by
process 1, init, and the process's parent is sent a SIGCHLD
signal. The value status is returned to the
parent process as the process's exit status, and can be col
lected using one of the wait family of calls. The function
_Exit is equivalent to _exit.
RETURN VALUE
These functions do not return.
196
12. Linux
'exit' ( DOS)
() . 0
.
:
mov ,1
mov ebx,0
int 0x80
; exit
; 0
;
.
Linux. ELF
-f elf.
Id , . 12.1.
Id
-o<name>
-s
12.1
() <name>
Id ,
_start.
Hello, World! 12.1.
; .
;
/
; write
; STDOUT 1
;
; Hello, World!
;
;
; 1 exit
;
;
int 0x80
mov eax,1
mov ebx,0
int 0x80
SECTION .data
hello db Hello, world!,Oxa
l e n equ $ - h e l l o
;
;
;
:
nasm
-f
elf h e l l o . a s m
:
Id -S - hello hello.
- . -s
,
.
198
12. Linux
:
./hello
Hello, World!
12.8. :
Asmutils
Asmutils ,
.
LIBC.
Asmutils
.
NASM 86-.
, , Asmutils
(
).
: BSD (FreeBSD, OpenBSD, NetBSD), UnixWare, Solaris AtheOS.
Asmutils
,
.
12.2 , hello
Asmutils.
12.2. Hello World! Linux
Asmutils
%include system.inc
CODESEG
;
START:
;
;
sys_write STDOUT,hello,len
;
; write
; write
sys_exit 0
; exit
DATASEG
;
hello db Hello, World!,Oxa
len equ $-hello
END
199
.
. FreeBSD,
MCONFIG.
Asmutils http://asm.sourceforge.net/asmutils.html,
Asmutils. Asmutils
GNU, .
Asmutils ,
.
.
, NASM (
RedHat-, Debian-).
asmutils-0.17.tar.gz.
/doc, /src /inc. MCONFIG.
, ,
, .
/src Makefile.
, ( ).
hello ( .asm).
make.
( ).
12.9. Asmutils
.
, .
sys_,
, man-. Linux
, man-
. .
sys_exit 0 :
mov ,1
mov ebx,0
i n t 0x80
; 1 e x i t
; 0
;
.
rtn,
sys_exit [rtn], :
mov ,1
mov e b x , [ r t n ]
i n t 0x80
200
; 1 e x i t
; r t n
;
12. Linux
,
, EMPTY
.
EMPTY .
12.10.
/ (I/O)
Linux , Linux UNIX-
, :
,
.
() ,
, .
Linux (man 2 open).
:
i n t openfconst char *pathname, i n t f l a g s ) ;
i n t open(const char *pathname, i n t f l a g s , mode_t mode);
( ), ,
(/ .).
.
.
12.2 (
man-).
12.2
0_RDONLY
0_WRONLY
0_RDWR
0_CREAT
O_TRUNC
0_APPEND
,
( NFS)
0_LARGEFILE
201
.
0_CREAT,
. ,
.
, :
S_RWXU
S_RGRP
S_ROTH
, name,
:
sys_open name, 0_RDWR, EMPTY
t e s t eax,eax
,-
js . e r r o r _ o p e n
;
; 7
DB:
name
DB
my_file_which_has_a_very_long_name.txt,0
| (
OR). , ,
, 700 (//
, ):
sys_open name, 0_RDWR I 0_CREAT, S_IRWXU
t e s t eax,eax
js . e r r o r _ o p e n ; e r r o r _ o p e n ,
...
;
DOS, .
close(), sys_close.
.
, :
sys_close eax
.
, ,
, .
read:
202
12. Linux
ssize_t read(int fd, void *buf, size_t count);
,
, ,
, .
, .
sys_read ,
STDIN
.
, :
, , ,
, .
, :
ssize_t write(int fd, const void *buf, size_t count);
,
Enter
. ,
ASCII.
, , ,
Ctrl + .
Enter read
, .
'' 'z' '' 'Z'.
STDOUT.
Asmutils
CODESEG, DATASEG,
UDATASEG.
system.inc ( 12.3).
12.3. ,
Enter
%include system.inc
%define MAX_DATA 10
CODESEG
START:
again:
;
sys_read STDIN,read_data,MAX_DATA
test eax.eax
;? ( )
203
.
j s endprog
add ecx,eax
compare_next:
dec ecx
cmp byte [ecx],'a'
jb no_conversion
cmp byte [ecx],'z'
ja no_conversion
sub byte [ecx],0x20
;?
;?
;
;
; ,
;
; + 1
;
; < '',
;,
/
; > 'z'
;
;
; 0x2 0
no_conversion:
cmp ecx,read_data
jz printit
; ?
;? ,
;
jmp short compare_next /
printit:
/
;.
/
/
/ ,
/
/ ,
/ EDX,
/ w r i t e
s y s _ w r i t e STDOUT,read_data, eax
jmp s h o r t a g a i n
/ .
endprog:
s y s _ e x i t 255
/ 25 5
UDATASEG
/
r e a d _ d a t a r e s b MAX_DATA
END
;
, ,
MAX_DATA.
sys_read.
.
.
.
:
3, ( 204
12. Linux
, ).
POP .
, (). ,
.
, ,
, .
.
12.4.
12.4.
%include system.inc
%define BUFF_LEN 409 6
CODESEG
START:
pop eax
;
;
cmp ,3
/ 3
jae enough_params
; ,
mov eax,255
; , 255
endprog:
sys_exit eax
;
enough_jpara:ms:
pop ebx
; .
; ,
;
pop ebx
; .
sys_open EMPTY,0_RDONLYI0_LARGEFILE
;
test eax,eax
;? ...
j s endprog
mov ebp,eax
; EBP
pop ebx
; EBX
sys_open EMPTY,0_WRONLYI0_LARGEFILEI0_CREAT|OJTRUNC,S_IRWXU
/ ,
,- 700
test eax,eax
js endprog
;?
mov ebx,eax
; EBX
copy_next:
xchg ebp,ebx
; EBX EBP,
; EBX
sys_read EMPTY,buff,BUFF_LEN
/ 1
test eax,eax
/
205
.
js e n d _ c l o s e
jz e n d _ c l o s e
xchg e b p , e b x
;?
; ?
; ,
; -
s y s _ w r i t e EMPTY,EMPTY,
; ,
;
t e s t ,
js e n d p r o g
;?
jmp s h o r t copy_next ;
end_close:
s y s _ c l o s e EMPTY
;
xchg e b p , e b x
;
s y s _ c l o s e EMPTY
jmp s h o r t endprog
;
UDATASEG
buff r e s b BUFF_LEN
; 4
END
/ . ,
,
, ,
.
/
lseek:
off_t lseek(int fildes, off_t offset, int whence);
, , ,
, ,
:
SEEK_SET ;
SEEK_CUR ;
SEEK_END .
lseek
.
: lseek, :
sys_lseek [fd], , SEEK_END
,
, .
206
12. Linux
, ,
, ? DOS
. UNIX-
,
:
.
UNIX-
.
, : ,
,
. ( ) ,
,
. ,
( ).
link:
i n t l i n k ( c o n s t char *oldpath, const char *newpath);
,
( ).
(symlinks).
, .
.
, .
, .
symlink:
i n t symlink(const char *oldpath, const char *newpath);
, link.
, .
unlink,
. ,
.
i n t u n l i n k ( c o n s t char *pathname);
.
0, .
rename:
i n t rename(const char *oldpath, const char *newpath);
207
.
link:
(, ) .
,
.
symhard. asm,
( 12.5).
1 2 .
. ,
, ,
.
12.5.
%include system.inc
CODESEG
START:
pop ebx
cmp ebx,2
jz ok
endprog:
sys_exit 0
ok:
pop ebx
pop ebx
sys_link EMPTY,one
sys_symlink EMPTY,two
jmp short endprog
DATASEG
one DB 1,0
two DB 2,0
END
;
/
; ,
;,
;
;,
;
;
;
; ^
;
;
;
;
;
;
Makefile make.
.
1 2. 1
, 2 . - , ,
,
.
208
12. Linux
./symhard ./symhard
, Is -1,
chown, chmod rm .
12.11.
DOS, Linux ,
. Asmutils
, .
(MKDIR, RMDIR)
mkdir:
int mkdir(const char *pathname, mode_t mode);
, ,
.
, open.
12.6 my_directory
/ tmp.
12.6.
%include system.inc
CODESEG
;
START:
;
sys_mkdir name, S_IRWXU ; , 0700
sys_exit 0
;
DATASEG
name DB <</tmp/my_directory, 0
END
.
( chmod) -:
q . , 0700
700q.
RMDIR,
:
int rmdir(const char *pathname);
8 . 293
209
.
(CHDIR)
chdir:
i n t c h d i r ( c o n s t char * p a t h ) ;
,
sys_exit
s y s _ c h d i r name
(GETCWD)
getcwd, ,
Linux 2.0 ( 2.4-2.6).
: ,
, , :
long sys_getcwd(char *buf, unsigned long size)
, :
sys_getcwd path,PATHSIZE
mov e s i , e b x
xor edx,edx
.next:
i n c edx
lodsb
or al,al
jnz .next
mov b y t e [ e s i - 1 ] ,
; p a t h
;
; ESI
/ EDX
; EDX path
; AL, ESI
; ?
;?
n
;
;
sub esi,edx
;
sys_write STDOUT,esi,EMPTY ; STDOUT
; EDX
sys_exit_true
;
12.12. .
.
IOCTL
read.
Enter .
(, ).
IOCTL.
210
12. Linux
, ,
IOCTL.
IOCTL Input/Output Control /
. IOCTL,
,
/.
Linux (
), IOCTL- TCGETS TCSETS. ,
TCGETS, ,
. man-
termios.
, , Asmutils.
,
,
ICANON ECHO. 0.
TCGETS,
TCSETS.
B_STRUC,
system.inc.
mov edx,termattrs
sys_ioctl STDIN,TCGETS
mov eax,[termattrs.c_lflag]
push eax
and eax,-(ICANONI ECHO)
mov [termattrs.c_lflag],eax
sys_ioctl STDIN, TCSETS
pop dword [termattrs.c_lflag]
EDX
ECHO ICANON
U D A T A S E G :
termattrs B_STRUC termios,.c_lflag
, .
12.13.
,
. ,
. DOS, ,
,
.bss ( ,
).
.bss , .
211
.
- ,
(
heap.asm Asmutils).
,
.bss, brk:
void * b r k ( v o i d *end_data_segment);
.bss. .bss.
,
. brk :
sys_brk 0
;
add eax,____
;
sys_brk eax
/
.bss .
12.14. . ALD
UNIX-
gdb, -
, .
,
ALD (Assembly Language
Debugger).
, ,
GNU.
http://ald.sourceforge.net. 86-
ELF ,
.
ALD
.
aid:
aid
Assembly Language Debugger 0.1.3
Copyright (C) 2000-2002 Patrick Aiken
ald>
convert:
ald> load convert
echo: ELF I n t e l 80386 (32 b i t ) , LSB, Executable, Version 1
212
12. Linux
(current)
Loading debugging symbols...(no symbols found)
ald>
,
. ALD
s (step).
:
ald> s
= 0x00000000 ebx = 0x00000000 = 0x00000000 edx = 0x00000000
esp = 0xBFFFF8CC ebp = 0x00000000 esi = 0x00000000 edi = 0x00000000
ds = 0x0000002B es = 0x0000002B fs = 0x00000000 gs = 0x00000000
ss = 0x0000002B cs = 0x00000023 eip = 0x08048082 eflags =0x000000346
Flags: PF ZF TF IF
08048082 5A
pop edx
, , pop edx.
0x8048082.
ZF ( ).
( s), Enter.
, int 0x80,
( = 0x00000003):
ald>
= 0x00000003 ebx = 0x00000000 = 00804908 edx = OxOOOOOOOA
esp = 0xBFFFF8D0 ebp = 0x00000000 esi = 0x00000000 edi = 0x00000000
ds = 0x0000002B es = 0x0000002B fs = 0x00000000 gs = 0x00000000
ss = 0x0000002B cs = 0x00000023 eip = 0x0804808D eflags =0x00000346
Flags: PF ZF TF IF
0804808D CD80
i n t 0x80
EDX (OxOOOOOOOA = lOd)
10 . ,
.
(examine): e :
ald> e
Dumping 64 bytes of memory starting at 0x080490C8 in hex
080490C8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
080490D8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
080490E8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
080490F8: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
213
.
s
int 0x80. ,
Enter. , :
ald> e
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
080490E8:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
080490F8:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
,
. .
, ,
Ctrl+.
help , a help
_ . . 12.3
ALD.
ALD
12.3
load <filename>
()
step [n]
() . step
s
next [n]
step,
disassemble
. d.
, d 0x08048061
continue
( )
examine
.
.
, , , edx 0x08048000
register
help
. ,
, , help examine
break <addr>
(breakpoint) addr
Ibreak
quit
. q
ALD (
, breakpoints). 214
12. Linux
,
.
.
0.1.7
( ),
.
,
-g nasm.
Asmutils MCONFIG DEBUG=.
12.15. GAS
UNIX
GAS. gcc, -
.
gcc,
. :
.
GAS NASM: NASM-
( MASM TASM) Intel, a GAS
AT&T, Intel
.
Hello, World!,
AT&T ( 12.7).
12.7. Hello, World!, GAS
.data
#
msg:
.ascii Hello, world!\n#
len = . msg
#
.text
#
# _start ,
#
.global _start
#
_start:
movl $len,%edx
movl $msg,%ecx
movl $l,%ebx
#
#
#
#
s t d o u t :
-
-
- STDOUT = 1
215
movl $4,%eax
int $0x80
movl $0,%ebx
movl $1,%
int $0x80
#
#
#
#
#
#
'write'
:
'exit'
12.16.
Linux, http://linuxassembly.org.
(, ),
.
Web-, 514
( httpd.asm).
12.17.
NASM :
-V
-9
-f <fmt>
-fh
- <name>
-I <path>
216
<fmt> (. 9)
<name>
-?
Pascal-
, .
,
, .
,
Pascal.
13.1.
(, ):
.
,
: .
,
.
. ,
,
, .
,
CALL,
RET. CALL
PUSH, .
( CALL)
.
,
.
(E)SP .
, , ,
. ,
218
13.
,
.
,
, RET.
- ,
.
13.2. -?
() SP ,
, .
()
, .
().
() ,
() (E)SP.
,
().
. 13.1. (stackframe)
219
, , ,
(), -.
(
, ), .
(), , -.
,
.
13.2.1. - (32- )
.
32- ,
Linux:
i n t a d d i t ( i n t a , i n t b)
i n t d = a + b;
r e t u r n d;
i n t main(void) {
i n t e;
e = addit(0x55,0xAA);
}
0x55 addit,
.
( main) :
080483F4
080483F5
080483F7
080483FA
080483FD
08048402
08048404
08048409
0804840
0804840
08048411
08048413
08048414
55
895
8318
83C4F8
68000000
655
E8C7FFFFFF
83410
890
8945FC
89
5D
push ebp
mov dword
sub dword
add dword
push Oxaa
push 0x55
call near
add dword
mov dword
mov dword
mov dword
pop ebp
retn
ebp, esp
esp, 0x18
esp, 0xfffffff8
+0xffffffc7
esp, 0x10
eax, eax
[ebp+Oxfc], eax
esp, ebp
main EBP (
) .
ESP, . , SUB ADD,
0x18 0x8 ,
. , 8
.
220
13.
,
, .
,
. PUSH (0x55
) . , CALL, addit.
addit.
(. . 13.1).
addit
. 13.1
0XBFFFF860 = ESP
0 x 0 8 0 4 8 4 0 9 ( )
0XBFFFF864
0x00000055
0XBFFFF868
0xBFFFF86C-0xBFFFF84
main
0xBFFFF888
0xBFFFF88C = EBP
0xBFFFF8C8 - U B C
0xBFFFF890
400392
32- ,
, CALL.
32- ,
addit. ,
. ,
main,
libc, main.
main .
addit:
080483D0
080483D1
080483D3
080483D6
080483D9
080483DC
080483DF
0804832
0804835
0804837
0804839
080483F0
080483F2
080483F3
55
895
8318
84508
8550
8D0C02
894DFC
8B55FC
89D0
07
8DB42600000000
89
5D
push ebp
mov dword ebp, esp
sub dword esp, 0x18
mov eax, dword [ebp+0x8]
mov edx, dword [ebp+Oxc]
lea ecx, [eax+edx]
mov dword [ebp+Oxfc], ecx
mov edx, dword [ebp+Oxfc]
mov dword eax, edx
jmp short +0x7
lea esi, [esi+OxO]
mov dword esp, ebp
pop ebp
retn
221
addit .
, - main.
addit:
ESP. SUB d,
+b.
addit,
SUB (. 13.2).
addit 13.2
0xBFFFF844 = ESP
0XBFFFF848 - 0xBFFFF854
0xBFFFF858 = - 4 (+OXFFFFFFFC) : d
0XBFFFF85C =
0xBFFFF88C m a i n
( - )
0xBFFFF860 = EBP + 4
0 x 0 8 0 4 8 4 0 9 ( main)
0xBFFFF864=EBP + 8
0x00000055
0xBFFFF86C - 0xBFFFF84
main
0xBFFFF888
: main
0xBFFFF88C
0xBFFFF8C8 - U B C
OxBFFFF890
0400392
, (dword)
,
. , d,
.
main. addit,
main main.
addit EDX
:
080483D6
080483D9
84508
8550
0x8?
- ,
8 ( -)
. ,
8 + 4 = 12 .
. 13.3 ( ),
.
222
13.
13.3
[ebp 4]
[ebp + 0]
[ebp+ 4]
[ebp + 8]
[ebp + OxC]
addit 0x55. ,
, .
addit ,
EDX, LEA.
. addit -
:
080483F0
080483F2
89
5D
,
- main.
(main),
.
.
, (char, short),
, 4 .
13.2.2. - (16- )
, -, 16-
, :
(4 ), (2 );
32- 16- (..
, SP ESP ..);
, DX:AX.
,
32- , (
80286 ).
16- -
. ,
.
223
13.3. -
_. , printf
_printf. ELF
( Linux), .
-, printit,
plus
. printit ,
printf.
Linux.
- :
const int plus = 6;
void printit(int);
int main(void) {
printit(5);
}
plus 6.
printit. main,
printit 5.
plus printf:
e x t e r n plus
extern printf
gcc
ELF, .
include proc, arg
endproc, :
%include
224
misc/c32.mac
13.
printit.
: push ebp mov ebp,esp. arg
. ,
arg .
4 ( 32.).
printit
%$what arg
:
mov eax,[ebp + %$what] ;
add eax,[plus]
;
; plus
push eax
;
; printf
push strl
; -
;
call printf
/ p r i n t f
endproc
; e n d p r o c ,
;
;( )
13.1.
13.1. -
%include misc/c32.mac
section .text
extern plus
extern printf
global printit
proc printit
%$what arg
mov eax,[ebp + %$what]
add eax,[plus]
push eax
;
;
; . p l u s
; . p r i n t f
; p r i n t i t
; p r i n t i t ,
; what
;
; p l u s
;
; p r i n t f
push strl
;
call printf
; p r i n t f
endproc
; e n d p r o c ,
;
;( )
;
section .data
strl db SUM = S>d,0x0A;,0x0
; ,
;
;
225
printit.asm :
nasm -f e l f p r i n t i t . a s m
- main.c
gcc, printit.o:
- printit main.c printit.o
printit, ,
SUM = .
16- ,
16.. endproc
NEAR FAR-. FAR-
:
%define FARCODE
.
, ,
:
%macro c g l o b a l l
g l o b a l _%1
%define %1 _%1
%endmacro
%macro c e x t e r n 1
e x t e r n _%1
%define %1 _%1
%endmacro
cglobal cextern
global extern.
.
13.4. Pascal-
. ,
( ) - ( 16 ). .
,
,
.
, , .
, FAR-.
, ,
226
13.
retf , ,
.
(. 13.4).
[-...]
13.4
[ + 0]
(2 )
[ + 2]
IP (2 )
[ + 4]
CS (2 )
[ + 6]
[+...]
, .
writeln , addit
.
{$L a d d i t . o b j }
uses c r t ;
var p l u s : i n t e g e r ;
function
addit(x:integer):longint;far;external;
begin
p l u s := 6;
writelnCSUM = ', a d d i t ( 5 ) ) ;
end.
addit, , .
external. , addit
, longint
(4 ) FAR. He $L,
addit,
.
addit, addit.asm.
Borland Turbo Pascal
obj, ,
. CODE, CSEG
, _, CONST ,
_DATA,
DATA, DSEG, , _BSS.
.text, .data .bss,
, -,
, .
227
addit 16- ,
integer 2 . longint (4 )
DX:AX. ,
-, 16..
addit 13.2.
13.2.
SECTION ._TEXT
%define PASCAL
%include misc/cl6.mac
extern plus
global addit
proc addit
%$what arg
xor dx,dx
mov ax,[bp+%$what]
add ax,[plus]
adc dx,0
endproc
FAR-
plus
addit
addit
-
what
DX
= what
= + plus
arg
. 16- 2 .
longint ,
, 4 .
, :
bp
; proc
bp,sp
; proc
dx,dx
;DX=0
ax,[bp+0 6] ; 1-
;
C S 007
03065200 add
ax,[0052] ;
; plus
CS 0082
81D20000 adc
dx,0000
;
CS 0086
89
mov
sP/bp
; endproc
CS 0088
5D
pop
; endproc
bp
CS 0089
0200
retf
0002
; endproc
;
.
CS
CS
CS
CS
228
0076
0077
0079
007
55
895
31D2
84606
push
mov
xor
mov
,
,
, , , .
,
,
.
,
(, SSE, 3DNow),
.
,
DOS, Windows Linux.
,
,
.
.
15
15.
MOV
XCHG
PUSH
POP
ADD
SUB
MUL
DIV
INC ( 1)
DEC ( 1)
AND ( )
OR ( )
XOR
NOT
TEST
SHR ()
SHL ()
RCR
RCL
JMP
LOOP , () 0
JZ (ZF)
JC (CF)
JNZ (ZF)
JNC (CF)
CALL
RET
INT
REP , () 0
MOVSx
CMPSx
SCASx
231