Вы находитесь на странице: 1из 233

Rudolf Marek

Ucime se programovat
V jazyce Assembler pro PC

Computer Press
Brno

, -
2005

.
. .
: , 2005. 240 : .
ISBN 5-94387-232-9


.
,
.
.
,
(Windows, DOS, Linux), ,
.
, ,
-
, ,
.
. .

.. ..
Copyright Computer Press 2004 Ucime se programovat
vjazyce Assembler pro PC by Rudolf Marek, ISBN: 80-722-6843-0.
All rights reserved


(812) 567-70-25, 567-70-26
(044)516-38-66
www.nit.com.ru

9'i795943ll872326

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. .

12
15

2. 8

19

2 . 1 . 0
2.2. 86
2.3. :
2.4. 80386




2.5.

20
22
23
25
25
27
27
27
28

3.
3.1.
3.2.
3.3.
3.4.

30

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 OLD
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, RESW 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.

97

.
.

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.
:

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 .out
elf UNIX

10. DOS
10.1.
10.2.
10.3.
10.4.
10.5.

10.6.

10.7.

10.8.

10.9.
10.10.
10.11.

10.12.
10.13.


DOS
1 MB
DOS


. Hello, World!

-




/
(SEEK). ,



(MKDIR, RMDIR)
(CHDIR)
(GETCWD)





10.11.1.
10.11.2. grdb.exe

grdb


1 1 . Windows
11.1.
11.2.

11.3.
11.4.
11.5.

Windows-
11.2.1. API
11.2.2. Hello, World! Windows

DOS- Windows

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.

12.11.

12.12.
12.13.
12.14.
12.15.
12.16.
12.17.

.




-
Hello, World! Linux
: Asmutiis
Asmutiis
/ (I/O)







(MKDIR, RMDIR)
(CHDIR)
(GETCWD)
. .
IOCTL

. ALD
GAS

13.
,
13.1.
13.2.

13.3.
13.4.


-?
13.2.1. - (32- )
13.2.2. - (16- )
-
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
.
,
, 0301 ,
,
, .

.

, ,
,
,
,
.
,
, ,
DOS, Windows Linux, ,
,
.
, ,
.
:
, ,
;
;
;
;
, ;
Netwide
Assembler (NASM);
DOS, Windows Linux;

( ).

, - 514 ,
Asmutils
Linux- MPlayer.
10

jnsiBc

,
1. ,
( 2) ,
(1) (0).
,
,
( 8 16
). ,
.

1.1.

,
, . , ,
, :
,
0. ,
, .
, ,
,
:
= *z" -h *z"-i + ... -h *z4a *z^
n

n-1

a^ , a Z ,
10.
, 1234 :
1234 = 1*10^ + 2*10- -h 3*10^ -h 4*100

, .
12

1.


, . ,
1100, ,
, 12,
. (1100)2, ^^
: . ,
, 1100
:
(1100), = 1*2^ + 1*22 + 0*21 + 0*20
8+4 , (1100)2 ^^^ 12. ,
, . ,
12 .
, ,
15, (1111)2 = 1*^"^ + 1*"^ + 1*' + 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)
( 7).

, :
(77), = 7*8^ + 7*8 = 63
8 ATARI, ZX Spectrum . , .
10 ,
5 A-F:
= 10, = , = 12, D = 13, = 14, F = 15
13

, :
10 12 .
:
(524D)^, = 5*16^ + 2*16^ + 4*16^ + 13*16^ =
= 20 480 + 512 + 64 + 13 = 21 069
(DEAD)j^ = 13*16^ + 14*16^ + 10*16^ 4- 13*16 = 57 005
(DEADBEEF)j, = 13*16^ + 14*16^ + 10*16^ 4- 13*16^ +
4-11*16-^ + 14*162 + 14*16^ 4- 15*16 = 3 735 928 559
(COOl)^, = 12*163 4- 0*162 4- 0*16^ 4- 1 = 49 153
, ,
, , .

.
,
0. , 14
:
14/2 = 7

7/2 = 3

3/2 = 1

1/2 =

, 0.
,
(1110)2 13 :
13/2 = 6

6/2 = 3

3/2 = 1

1/2 = 0

, ,
0. , (1101)^.

123456 :
123456/16 = 7716
7716/16 = 482
14

1.

482/16 = 30

30/16 = 1

14 =

1/16 =

, 123 456 = (lE240)j^.


,

:
$0
h.
A-F, ,
, , ,
, ODEADh.
, 0x1234, $01234 01234h
(1234),,.

d. , 1234 1234d
(1234),.
, ,
1100b (1100),.
q: 12q (12)^^.

..., ..., .
( d h)
,
, .

1.2. .

. bit
binary digit .
1 , .
,
.
,
,
(), .
, : 1^
.
15

() ,
. 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
,
,
. 255 (
2'"^ 256 ).
, .
0 65 535 ( 2'^ = 65 536
). 4 294 967 295 (2^^ =
4 294 967 296 ).
-, ,
. 0-1
(0-0x1)
]
^

0-255
( - OxFF)
7
L.

2
1 0
BaiiT(byte)

1
- 65535
( - OxFFFF)

15 8 7 0

^ (word)^

1
1

1 - 4294967295
1 ( - OxFFFFFFFF)

31
16 15


(dword)

. 1.1.

16

1.

, ,
. .
, (KB, Kb, ).
, SI,
1000, 1024. 1024?
,
. 1024 2'^.
(, 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

>..
0

3
V

00
10

16

1
i

17

18

30
40
50
60
70
80
90

DO

F0

3Z

33

7
1
6

19

20

21

22

20

34

35

36

37

23
1

38

39

10

11

12

13

14

15

4.
26

27

28

29

30

31

45

46

47

>

62

63

24

25

40

41

48

49

50

51

52

S3

54

55

56

57

(Of)

64

+
43

44

58

59

60

61

<

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

34

85

86

87

88

89

90

91

92

93

94

95

97

98

99

100

101

102

103

104

105

106

107

109

110

0
111

116

117

119

X
120

115

V
118

113

129

114

112

121

122

126

127

130

131

132

133

134

135

136

137

133

123
i
139

<

144

145

146

147

148

149

151

152

153

154

155

150

160

161

162

163

164

165

166

167

168

181

178

183

184

185

200

201

96

128

176
L

177

192

194

208

193

209

210

211

180

179

f
195

196

197

212

213

1
214

230
246

229

>

<

242

243

244

245

226

240

241

141

142

143

156

157

159

158
e:

171

172

173

174

175

186
JL

187

188

189

191
J,

202

203

190
JL
ir
206

170

216

217

218

231

232

233

247

248

249

215

140

1
124

169

ff

199

228

225

198

227

224

182

108

125

1
204

220

219

205

1 1
221

222

207

223

CO

234

235

236

237

238

250

251

252

253

254

255

. 1.2. ASCII

18

42

239


86

...

86

80386

.
,
.
, ,
. ,
,
,
.
,
(
), ,
.
, ,
,
.

2.1. ...
ENIAC (Electronic
Numerical Integrator And Calculator),
.
, ,
.
ENIAC EDVAC
(Electronic Discrete Variable Automatic Computer), 1946 .
, , :
, ,
. EDVAC,
,
. :
1. :
(), , , /.
20

2. 86

2. (
ENIAC), .
3. ( )
( ,
,
).
4. ,
(1 1 ).
5. ,
( ),
(
). ,
, ( ,

).
6.
Oump) .
7. ( , )

.
, ,
.

. 1950- ,
, , .
,
. : .
(). ,

^ ,

J.

\
1

4- ^

L > -

. 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,
^ ..
23

, .
,
: , - ()
.

. :
,
.
- ()
.
, ,
(, )
.
, ,
, , (, ),
. ,
(barrel-shifter),
.

, .
,

reg

| |

. 2.2.

24

2. 86

. ,
.
:
, .
, .
( ).
. ,
,
. ,
.

2.4. 80386
80386 32-, ,
4 (2^^ ).
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

80386

1
1 AL

31

16 15

8 7

31

03
I

ES

1
1 CL 1

16 15

8 7

31

DX
1
DH 1 DL
16 15 8 7

31

16 15

FS

EDI

GS
15

0)

SS
0

15

DI
31

15

SI

ESI

15

>

31

15

( BL 1
16 15 8 7
"^ S

EDX

DS

X
0)

CS

> 2

16 15

15

31

16 15

31

16 15

ESP

SP

EIP

CRO

IP
31

X
-

CR1

16 15

313029

03

31

>1

31

. 2.3. 80386

"'

31

16 15

1 AL
8 7

. 2.4.

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 (IRQO 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:
01111:08, 0x34, 0x12
0x1114:
0x1111.
, , 3
. MOV.
08? 08
10111000b.
1011 MOV. 1011,
, MOV. (1)
, 16-.
. ( AL,
, ,
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-
.
,
, .
.
.

:

reg8-oepa 8- ;
regl6-oepa 16- ;
reg32-oepa 32- ;
m ;
immS 8- ;
imml6 16- ;
imm32 32- ;
segreg .

, : reg8/imm8-oepa
8-
8- .
, ,
: /18-
( 8- )
8- .

33

3.3.
, , , .
, ,
,
( ).
,
, : [].
, ,
, [ESI], ,
.
, , [0x594F].
,
. ,
. ,
ESI, , ,
[ESI].
, ESI
( 0) .
, , (,
1 ) ?
, . ,
,
[ESI -I- 1].
: [ 4- 4- 4].
, 4 ,
.
(, Effective Address) ,
( ).
80386
, :
[ 4- * 4]. 1
:
[ -

+ * 8 + E S I ]

[ESI]
, , [ESI -1- 4].
, 16- 32-
[], [],... [].
80286
, SI, D1,
.
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 345 67 8
; 2 ,
; 0x12345678
; 1
; 0x12345678

.
.

4 ,
0x12345678. ,
.
( : 00000000) 0x12345678.
35

MOV

86,
, ,
. ,
,
.

4.1. MOV
- ,
: .
, .
MOV, move (
), ,
:
MOV ,
MOV:
mov ,[number]
number
mov [number],bx
mov bx,ex
mov a l , 1
mov dli^cl
mov e s i , e d i
mov word [number]



number


AL 1
DH
CL
EDI
ESI
16- 1
"number''

86
. ,
number_one, number_two,
:
mov [number_two], [number_one]
/!!!
37

,
:
mov ,

[number_one]

mov [number_two],

/ 16-
/ ''number_one"
;
;"number_two"

MOV :
mov , 1

;! -
/.

BL
, , :
mov ,
mov a h ,

/
;''"
;

8- .
MOV ,
, . ,
, MOV ah,
. 8-
, BL, 16 . , 16-,
.
: ,
BL (AL):
mov ,
mov a l ,

; = , AL =
; AL BL

16- 32- .

MOV :
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV
MOV

38

/8,;regS
r/ml6 ,regis
/32 ,reg32
8,:r/mS
regis ,r/ml6
32 ,r/m32
8 ,.
immS
regis ,imml6
32 , imm32
/,:immS
r/ml6 ,immlS
/32 , imm32

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

mov e b p ,

0x12345678

0804808F

892DC0900408

mov dword [ + 0x80490c0] , ebp

,
, .
.
counter
(0804900).
, mov ebp, 0x12345678,
:
= 0x00000000 - 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 a t 0x080490C0 i n 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 a t 0x080490C0 i n 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, ).
.
, LITTLE_ENDIAN BIG_ENDIAN :

,
.
, , .
, , , ... ,
.

( )
, .
, ,
,

, (htonl, htons, ntohl, ntohs).
counter ,
, 0x12345678.

4.3.
MOV
, .

. 80386
, ,
80386.
.

40

4.

4.3.1. ADD SUB


(ADD) (SUB).
ADD , MOV:
ADD 1 , 2

ADD 1,
.
SUB:
SUB o l , 2
, 1-2, 1, 1 .
:
mov
mov
mov
add

,
,
dx,
dx,

ex

/ 8
;
; DX, DX =
;DX = DX +

( )
,
DX, DX. ADD
DX 4- DX,
.
ADD SUB:
add , 8
sub e c x , e b p
add b y t e [number]

sub word [ n u m b e r ] , 4
add dword [ n u m b e r ] , 4
sub b y t e [ n u m b e r ] ,
sub a ] i , a l

al

EAX = EAX + 8
ECX = ECX - EBP
4
number
1
( 0-2 55)
number = number 4
2
( 0-6553 5)
00000004
"number"
AL
"number"
AL ,

, AL (8- )
(255), 8?
mov a l , 255
add a l , 8

; AL 2 5 5 , OxFF
; 8

AL 7.
41

OxFF
11111111

AL

+
00001000
imm8
00000111
AL

. 4.1. 255 (OxFF) + 8

0x107 (263 ). ?
AL 8- (
255). , , ,
CF .
,
. ADC (Add With
Carry ) SBB (Subtract With Borrow
):
ADC 1, 2
SBB 1, 2

;1 = 1 + 2 + CF
;1 - 1 -- 2 - CF

, ADD SUB,
CF.

. ,
. EDX:EAX ( :)
. 16 , DX 16
. 80286 32-
, 32- .
: DX:AX OxFFFF ( = OxFFFF, DX = 0).
8 DX:AX:
mov
mov
add
ado

, Oxffff
dx,
, 8
dx,

; = OxFFFF
;DX =
; = + 8
; DX

ADD 8 .
, CF.
DX CF.
ADC CF DX (DX 1).
OxFFFF 8 (0x10007) DX:AX
(DX=1, =0007).
42

4.
DX

0x0000

OxFFFF

ADD , 8

0x0008

0x0000

0x0007

0x0000

ADC DX,

0x0001

0x0007

. 4.2. OxFFFF 0x0008

80386 32-
:
mov ,
add ,

Oxffff
8

; - OxFFFF
; = + 8

OxOOOOFFFF |

0x00000008

imm32

0x00010007

. 4.3. 32- 80386

32-
0x10007. 64-
EDXiEAX , DX:AX.

4.3.2. INC
DEC
.
INC , DEC .
, ADD SUB,
:
INC 1
DEC 1

;1 - 1 + 1
;1 = 1 - 1

! CF.
, , (
43

) , ,
.
AL :
;AL = AL + 1
add a l , 1
;AL = AL + 1
inc a l
16- number:
inc word [number] ;
/ word

4.3.3.

.
,
, , .
.
255.
-128 127. 127
,
128 255: -1 255 (OxFF), -2 254 (OxFE) ..
-50 206. :
1
, . 1
(mapping).
2 ( 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.
2^^ = 65 536,
65 535. :
65 536 / 2 = 32 768. ,
-32 768 32 767 (
!).

44

206(0)

t
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 ,OxFFFA
mov d x , 7
add a x , d x

; = -6, 65 53 0 OxFFFA
;DX = 7
;AX = AX + DX

65 530 -h 7 = 65 537 = 0x10001,


, .
, !
,

.
. NASM
,
:
mov , -
mov dx, -
add a x , d x

; = -
; DX =^ -
; =^ + DX

: 0xFFF4 ( CF, ).
0xFFF4 = 65 524.
: -12 (65 536 65 524 = 12).
.
, ,
(,
):
mov , [ - 1 ]

/
; ,

-1 OxFFFF,
: MOV , [BX-hOxFFFF].
CF, , .
45

4.3.4.

NEG

80386 ,
.
NEG (negation, ):
NEG /8
NEG r/ml6
NEG r/m32

NEG,
. NEG ,
.
: 8, 16 32 .
neg
neg
neg b y t e [number]

; ,
; , 8-
;
/ 8- number

:
,
.
.
.
.
,
. ,
.
CBW

CBW () AL ,
AL
( 0x00 OxFF = lib,
AL). ? ,
, .
15

AL

I I I I 1 I 1 I

, AL
I I I I I I I II I I I
15
87

1}

, 4.5. CBW

46

4.
CWD

CWD DX,
:.
CWD
DX

DH I DL

I AL

DH

DL
DX

AL

. 4.6.

CWD

CDQ

CDQ EDX,
EDXiEAX.
CWDE

CWDE ( )
,
, .

CWDE

1 AL

'

, AL 1

. 4.7.

CWDE

:
mov a l ,
cbw

-1

;AL = - 1 ( OxFF)
;

CBW OxFFFF,
- 1 . (1) ,
AL .
47

.
mov ,
cwd

; = 4
; DX

4. , CWD,
DX:AX.
DX ,
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 /8
MUL / m l
MUL / 3 2

8- 8-
. AL. (
) .
48

4.
(/8)

* AL - >

16- 16-
. .
DX:AX.
(r/ml6)

* -> DX:AX

32- ,
EDX:EAX.
(/32)

* - > EDX:

.
1: , CL,
:
mov a l ,
mul 1

bh

;AL = AL
; = AL * CL CL

.
: 486^ DX:AX:
mov ,
mul

486

; = 4 8 6
; * - > 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 b y t e [ 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
d i a m e t e r < - AX

, 16-
DX:AX, - 32- ?
16- ,
32- .
IJVIUL ,
. , IMUL
, MUL,
.
,
,
. ,
.
imul e d x , e c x
imul ebx, [stliing]

EDX - EDX * ECX


3 2-
''stiling" ,

49

imul ,

; - *

, IMUL
, .
, ,
:
imul
imul
imul

edx,ecx,l
ebxsthing],9
ecx,edx,11

EDX = ECX * 7
' ' s t h i n g " 9,

= EDX * 11
DIV IDIV

MUL, DIV
:
DIV /8
DIV r/ml6
DIV r/m32

, (
MUL). 8- ()
8- .
. : AL, - .
/ (/8) -> AL, -^
16- 16-
. DX:AX.
DX:AX (DX , ).
DX:AX / (r/ml6) ^ , -> DX
32- EDX:EAX,
EDX: ( , EDX).
EDX:EAX / (r/m32) -^ , -^ EDX
IDIV ,
, DIV.
.
1: 13 2, BL, :
mov
mov
div
mov

50


1,2
1
bx,

- 13
CL - 2
CL
,

4.

2: ,
16- diameterl, radiusl,
.
mov
mov
div
mov

,[diameterl]
,2

[radiusl],al

; = d i a m e t e r l
; 2
;
;

4.4.
: (, AND),
(, OR), (XOR) (NOT).
.
AND

AND 1 2.
1. ,
ADD: 8-, 16- 32- ,
.
AND 1 , 2

AND (. 4.1).
4.1

AND

aANDb


(1 AND 0).
mov
mov
and

al,1
bl,0
al,bl

AL = o n e
BL = z e r o
AL = AL a n d BL = 0

TOT , :
mov 1 , 1
and a l ,

; A L =- o n e

; AL = AL a n d 0

1 and 0

51

.
OR

OR 1 2.
1. ,
AND.
OR o l ,

OR (. 4.2).
OR

1
1

4.2

aORb


.
( ) mask 1.
or byte

[mask],1
XOR

1 2.
1. , .
:
XOR 1 , 2

XOR (. 4.3).
XOR

4.3

XOR b

: (( ) )
X.
mov al,0x55
al,
al, ,

52

AL - 0x55
AL = AL
AL 0x55

4.
NOT

,
.
:
NOT r/m8
NOT /ml
NOT r/m32

NOT (. 4.4).
NOT

4.4
NOT

NOT
NEG:
mov al,00000010b
mov bl,al
not al
neg

AL = 2
BL = 2

11111101b - OxFD (-3)

: 11111110 = OxFE (-2)

( )


. , 16- 16
1.
. 16
, (/)
1 . , ,
.

, , ,
. ,

.
: , ..

.

53


. ,
, ,
.
(
) OR.
,
, . ,
,
10000001. , 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 ,
( 1, 1 0),
0. XOR , .
XOR
: XOR
(),
(XOR) .

54


IF THEN


: ,
.
,
, .

5.1.
.

.
, , .

,
,
.
,
, .

1
2

. 5.1.

56

5.

5.2. IF THEN

IF-THEN.

.
, .

, 5.2.

5 . 2 . 1 . TEST
TEST .
, , 8,
16 32 .
1 , 2

compare, .
SUB: 2 1. ,
.
,
.
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

, [ d i a m e t e r 1 ]
cmp ,
t e s t , 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-
(1).
(near) .
IP,
( 64 );
EIP,
4 .
far IP CS,
.
CS.

58

5.


. ,
, .
.
:
mov , 4
new_loop:
mov b x ,

; == 4
; new_loop
;

new_loop ,
:
jmp new_loop
; new_loop

new_loop.

, :
:
jmp s t a r t
/ s t a r t
finish:

; ' ' f i n i s h "

...

;-

start:
; ' ' s t a r t "
jmp f i n i s h
/ " 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 s h o r t n e a r _ l a b e l

; " n e a r _ l a b e l "

5.2.3. Jx

.
,

59

.
.
. :
, , ,
.
:
Jx _
:

jz
jc
js
jo

is_true
is_true
is_true
is_true

;
;
;
;
;0F = 1

is_true,
is_true,
is_true,
is_true,

ZF = 1
CF = 1
SF = 1

, :
jnz i s _ t r u e ; i s _ t r u e , ZF =^
JNC, JNS JNO.

, ( ,
jump) (. . 5.1).

1==2
1=2

1!=2
1<>2

JE(JZ)

JNE(JNZ)

Jump,

Jump,

Jump,
0

Jump,
0

JE(JZ)

JNE(JNZ)

Jump,

Jump,

Jump,
0

Jump,

5.1
1>2

1<2

1=<2

1>=2

JA(JNBE)

JB(JNAE)

JNA(JBE)

JNB(JAE)

Jump,

Jump,

Jump,

Jump,

Jump,

Jump,

Jump,

Jump,

JNL(JGE) 1

JG(JNLE)

JL(JNGE)

JNGCJLE)

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
; ZF = 1, far_jump
;
far_jump:
jmp far f i n i s h
; ''"
, IF-THEN
. i f _ t h r e e ,
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 write_l
cmp a l , c l
ja write_2
mov b l , 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
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

)4

'

. 5.3.

(jmp end_if),
. :
; w r i t e ^ l ,
.
jmp end_if .
:
mov 1
a l , c l
j e end_if
mov b l , 2
cmp al,cl
ja end_if
mov bl,3
end_if:

; BL = 1
; AL CL
/ , AL = CL
;BL = 2
; AL CL
; , AL > CL
;BL - 3
/

, ,
. MOV ,
:
mov b l , 1
cmp 1 , 1
j e end_if
mov b l , 2
ja end_if
mov b l , 3
end if:

62

BL = 1
AL CL
, AL
BL = 2
, AL
BL = 3

CL
CL

5.

,
:
if (al =- cl) bl = 1 e l s e if (al > cl) bl = 2 e l s e bl = 3;

5.3.
, ,
, . ^
.

. 5.4.


, :
( FOR),
;
( WHILE), ,
;
( UNTIL), ,
.
IF GOTO

(
). ,
, .
, ,
. ,
- .
63

FOR :
FOR_START:
1 = 0
FOR_LOOP:
I-I + l
IF I < 10 THEN
GOTO FOR_LOOP
FOR_FINISH:

;
/
;
; ()
;
;
;
;
;


.
1. :
,
.
(

10 .
( ),
10,
10,

1=1

1=1+ 1

10

. 5.5. FOR

. ,
IF GOTO,
FOR. ( I)
:
for_start:
mov ,
for_loop:

64

/
;

5.

...
inc

jnz for_loop
for_finish:

;
; 1
/ 10
; , 1:_1
; = 10,

FOR. , ,
, , I.
for_start:
mov dword [i],0
for_loop:
...

;1 dword 1 = 0
;
;

inc dword [i]


cmp dword [ 1 ] 0
jnz for__loop
for_finish:

/ i 1
; i 10
; , for_loop
; ,

,
, ,
.
,
DEC ZF .
: (-),
, 1. ZF , ,
:
for_start:
mov ,10
f_1:
...
dec
jnz for_loop
for_finish:

; = 1 0
;
;
; 1
; , for_loop
; ,


:
for

( i = 0 ; i < 10;i++)

{}

LOOP ,

, 80386, , 86- CISC (


), . ,
,
. ? CISC-,

65


LOOP:
LOOP

MUL, LOOP .
, .
( ). . LOOP
() ,
, . 128
( ).
FOR LOOP:
for_start:
mov
for_loop:
...

; = 10 10
;
;

loop for_loop

; , ,
; for_loop
for_finish:
;
, ; .
.
LOOPZ LOOPNZ

LOOPZ
. , :
, , 10 , ,
3.
, .
LOOPZ
LOOPNZ

LOOPZ :
,
ZF . LOOPE.

:
for_start:
mov ,10
for_loop:

- 10

FOR
-

66

5.

,3
loopz for_loop
for

3?
=-1; , =3 ,
f o r _ l o o p
= 3 ,

finish:

LOOPNZ ,
: ()
ZF 0. LOOPNE.

5.4.

- ,
.

, .
?

, . , .
, , .
, ,
. ,
, . FIFO (First
In First Out) , .

FIFO

\(\^ 1
|(b|0|Q|G^|
&

|(b|0|Q|C?|
|(bH|Q|(?|

\^(?\ 1
. 5.6. FIFO

67

LIFO (Last In First Out)


, . ,
- , ,
. , - .
, ,
.
LIFO

(bUD

GkDK?
(bkDC?
(bUDQ (?
(bUD
. 5.7. LIFO

LIFO. , ,
.
- ,
. SS:SP (SS:ESP)
(Stack Segment) (Stack Pointer).
,
. ,
,
, SP (ESP).
16- 32- .
PUSH POP.
PUSH POP:

PUSH 16-
32- . :
PUSH 1

68

5.

:
push

PUSH
:
sub e s p , 4
mov [ s s : e s p ] , e a x

; ESP 4 ( 4-
;)
;

( sizeof,
) push 1
:
(E)SP=(E)SP-sizeof(ol)
o l -> 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
push e s i

;
; ESI

pop
pop e s i

;
; ESI


ESI: , ESI,
(
ESI) ,
, ESI.

16- .
mov ,0x1234
mov bx,0x5 67 8
push
push bx
...
pop bx

; - 0x1234
; - 0x5 67 8
/
/
/
/

69

OxFOOO

OxFOOO

0
0

(
)

PUSH

OOiOO
AX=1234
BX = 5678

OxFOOO

0
0

SP=OxFFFC

34

OxFFFE

00

\'^\
lOoJ

AX=1234
BX = 5678

SP--OxFFFA

OxFOOO

ll

[rfol
0|0J

POPBX

PUSH BX

?? ,??

?? I ??

SP=OxFFFE

ll

78 56

OxFFFC

34

12

OxFFFE

00

lOoJ

00 1 00
34|12|

SP=OxFFFC

00 1 00 1

OxFFFE

AX=1234
BX=5678

AX=1234
BX = 5678

. 5.8.

PUSH
0x0000. SS:SP. , SP
OxFFFE. PUSH
2 OxFFFC, ( )
0x1234. , PUSH BX,
SP 2 (OxFFFA)
0x5678. POP BX 0x5678
, 2. OxFFFC,
0x1234.
, 8- .
IP (EIP) , ; PUSH/POP:
-, , .
PUSHA/ PUSHAD/POPAD:


. PUSHA,
. PUSHA
16- .
.
PUSHA
80386, 32- (
).
PUSHAD POPAD.
70

5.

( ):
(),

(),

(E)DX,

(),

(E)SP,

(),

(E)SI,

(E)DI

:
pusha
/
; ,
;

;
P U S H F / P O P F M P U S H F D / P O P F D :


. 16- 16-,

PUSHF POPF. ,
32-, 32- PUSHFD
POPFD.
16
,
PUSHF POPF.
,
;
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 ,

, .
, ,
, .

.
add_it.
.
,
. ADD ,
:
add_it:
push
add eax,ebx
mov ,
pop
ret

/
; = +
;
;
;

add_it 4 8:
mov , 4
mov e b x , 8
c a l l add i t

- 4
- 8
add_.it

(
pop )? RET 72

5.

, ,
, .
, RET:
, ,
.
a d d _ i t ,
POP PUSH:
add_it:
mov G c x , e a x
add e c x , e b x
ret

/ ( )
/ ( ) ,
/
/
INT IRET:

. ,

, .
,
.
, :
.
.
, ,
.
, .
INT 8- ,
.
INT

,
(ISA PCI). ,
, (IRQ, interrupt
requests). 16 ,
16 ISA .
, , ,
.
.
DOS, IRQ ( ) 8 ().
,
, ,
, , .
CS, ()1 .
73


. .
, .

, .
, ,
CS ()1.
.
, DOS,
21- (0x21) , :
i n t 0x21
DOS

IRET, ()1, CS
. :
IRET

, 21-
(. 5.9). ,
16- .

0000:0000
INTOO
^

INT01
\ /

IN 1 UX^l

0000:0x84(0x21*4)

. 5.9. CS IP

.
INT,
. .
:
pushf
;
push CS
; CS
74

5.

push i p

; .
/ . :
;11 here
;here:

,
: JMP far.
: ,
,
0x0000:0x0000.
. IP ( ),
CS.
0x21 00000:(021*4),
:
jmp f a r

[0x21*4]

/
; , , DS=0,
;
;: 0x0000)

INT PUSHF
CALL far:
pushf
c a l l far

[0x21*4]

/
/ CS IP
/ ''jump''


. ,
.

75

XCHG

LEA

/ (I/O)

?^1^ vV'l 1!1?6 '>.


86.
.

6.1.


. ,
IF ID, .
CLi STI

CLI (Clear Interrupt) STI (Set Interrupt)


IF. ,
.
(1), . CLI (0)
. ,
STI:
cli
...

sti

; - DOS!
;
/ - , ,
/
/
STD CLD

STD CLD DF.


,
. CLD (
), STD ( ).
77

:
STD
CLD

6.2- XCHG

. ,
, , , ,
XCHG (exchange ),
.
XCHG 1 , 2

MOV, 1 2.
8-, 16- 32-,
, MOV.
xchg ,
xchg
xchg
xchg
xchg

/
;
ebx,ecx
;
al,ah
;
dl,ah
;
b y t e [ v a r i a b l e ] , 1 /

.
NOP

AL
DL
CL

6.3. LEA

Load Effective Address,
.
(
). ,
, .
LEA 1 ,

[2]

LEA , ,
:
lea edi,

[ebx*4+ecx]

; EDI ,
; EDI = '^4+

, LEA,
:
. LEA
, .
78

6.

6.4.
(
), ( , ).

43
C

0x6F 0x6D 0x70 0x75 0x74 0x65 0x72 0x00

. 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.
- D1 EDI .
, STOSW, ,
, (E)DI / 2. STOSD
, E(DI) / 4.
79

clcl
stosw

; DF,
/ ES:[DI] ES:[EDI] (
/ ) (E)DI 2
LODSx

LODSx (LOaD String) :


LODSB
LODSW
LODSD
:
AL, .
DS:(E)SI. DF ,
SI 1/2/4 (, W, D),
1/2/4.
CMPSx

CMPSx ( String) :
CMPSB
CMPSW
CMPSD
CMPSB , ES:(E)DI,
DS:(E)SI SI DI
DF. CMPSB CMPSD ,
, SI DI
(2 4).
SCASx

SCASx (SCAn String) :


SCASB
SCASW
SCASD
SCASB/W/D AL/AX/EAX
[ES:(E)DI]. (E)DI
DF.
REP REPZ

REP (Repeat) .
: ,
80

6.

- .
LOOP: ,
() . ()
. REP
MOVS STOS:
rep
movsb
:

stosb

{) D S : ( E ) S I
E S : ( E ) D I .
- memcpy()

; () AL E S : ( E ) D I .
; - memset()

REPZ ( REPE), LOOPZ,


. ,
() , ZF .
REPNZ ( REPNE).
SCAS CMPS:
repz
scasb
:
repz
cmpsb

; SCASB

; CMPSB

,
, strlen().
I

. 6.2. - strlen()

81

,
ES:(E)DL ( +
) . ES:(E)D1
, () .
6.L
6.1. |1|11|:
strlen:
push

,
dec
eld
repne scasb
neg
pop
ret


(=0),
: mov ,
=
= - 1. OxFFFFFFFF
DF = , ,

( )


ES:(E)DI

/0

ES:(E)DI
0x11

. 6.3. ES:(E)DI
strlen

16-
: .
,
, . , ES:(E)DI
.
CALL,
.
call strlen
; s t r l e n
82

6.

,
strcmp().

>

. 6.4. - strcmp

: ES:(E)DI,
DS:(E)SI. ,
, ,
. strcmp () 6.2.
6.2,, s t i ^ ^ ^ ^ j
strcmp:
push edx
push edi
call strlen
miov edx, eex
mov edi,esi
;push ds
;push ds
;pop es
call strlen
;pop ds
cmp ecx,edx
jae .length_ok

EDX
EDI

EDX
ED1 = ESI
DS

ES (ES - DS)

DS
?
, ()

83

mov ecx.edx
.length_ok:
pop edi
eld
repe cmpsb
pop edx
ret

;
; EDI
;DF -
; ,
/
;
; EDX
;

strcmp ,
DS ES .
,
.
, .
. 6.5
.
ES:EDI

L1

/
ES:EDI

EI

ECX = OxA

DS:ESI

/
DS:ESI ^-

. 6.5. strcmp

6.5. / (I/O)
/
/.
( )
.
IN OUT.
IN OUT

IN , OUT
, .
IN
IN
IN
IN
IN
84

al,
ax,
eax,
al,
ax,

dx
dx
dx
iininS
immS

OUT
OUT
OUT
OUT
OUT

dx, a l
d x , ax
d x , eax
immS, a l
iiranS, ax

6.

IN /,
DX, AL/AX/EAX. ,
AL/AX/EAX DX, .
OUT . ,
IN, : .
/,
, . 6.1.

1 0000-001f :dma1
1 0020-003f:pic1
0040-005f: timer
0060-006f:keyboard
1 0070-007f: rtc

6.1
DMA (Direct Memory Access)

(RTC, real time clock)

0080-008f : dma page reg

DMA page register

OOaO-OObf: pic2

OOcO-OOdf: dma2
1 OOfO-OOff: fpu

DMA-

1 0170-0177 :ide1

- (Secondary)

1 01f0-01f7:ide0

- (Primary)

0213-0213 : isapnp read

(plug-and-play) ISA

0220-022f: soundblaster

1 0290-0297 :w8378Id

1 0376-0376 :ide1

IDE- ()

03c0-03df:vga+
1 03f2-03f5 : floppy
1 03f6-03f6 : ideO


IDE- ()

03f7-03f7: floppy DIR

()

03f8-03ff: lirc_serial

0a79-0a79 : isapnp write

(plug-and-play) ISA
()

0cf8-0cff:PCIconf1

PCI

4000-403f: Intel Corp. 82371AB/EB/MB PIIX4 ACPI

ACPI

5000-501f : Intel Corp. 82371AB/EB/MB PIIX4 ACPI

ACPI

eOOO-eOlf: Intel Corp. 82371AB/EB/MB PIIX4 USB

USB

fOOO-fOOf: Intel Corp. 82371 AB/EB/MB PIIX4 IDE

85


. :
in al,0x60

;
; ( - )
. NOP

No Operation,
, .

?
/, .
: ,
, .
NOP. ,

:
jmp short delayl
delayl:

2 ,
:
jmp s h o r t $+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 .

iiiiii^i:

'

iiiiiSi%

\. 6.6. 5 2

,
.
SHL :
SHL 1 , 2

, .
, .
.
, CL
. 32 ,
, 32.
CF,
. CF (SF)

87

(OF). ,
,
( SF OF 1).
, SHL, :
SHR 1 , 2

CF, .
SHR . 6.7.

-^-
SHR
SF
. 6.7. SHR

,
.
: SHR, 16,
:
shr , 4

; 4

:
BBL.

16- ,
16 . CF,
,
JC. CF , BL.
mov ,0
mov ex
repeat:
shr
jnc not_one
inc
not_one:
loop repeat

; BL=0
; = 1
; 1 ,
/ CF
; ,
; BL 1
; 16

BL
, 0.

88

6.
SAL SAR

SAL SAR
( ). SAL ,
SAR .
:
SAL 1 , 2
SAR 1 , 2

SAR , ,
. , , CF.
, SHL SHR.

SAR

SF

. 6.8. SAR

RCR RCL

(). RCR
, SHR,
CF.
CF. RCL RCR,
.
:
RCR 1 , 2
RCL 1 , 2

RCR

SF

. 6.9. RCR

444H-H-4-4 4-R1
RCL

SF

. 6.10. RCL

89

.
ROR ROL

: ROR
,
CF; ROL .
ROR 1 , 2
ROL 1 , 2

RCR RCL.

^TTTW
ROR

SF

, 6.11, ROR

6.7.
, , ,
. MOV , [number],
,
number. ,
.
.
(
). ,
.
.
:
, .
DB, DW DD

DB (define byte), ^
. :
db 0x55
db 0x55,0x56,0x57
db '',0x55

db 'Hello',13,10,'$'

90


: 0x55,
0x56, 0x57

.
0x61, 0x55
.
0x48, 0x65, , ,
0x6F, OxD, , 0x24

6.

, ,
DW (define word):
dw 0x1234
dw '.'

; 0 3 4 , 0x12
;01, 0x00:

DD (define double word) ,


:
dd 0x12345678
;078 0x56 0x34 0x12
dd 1.2 3 45 672 0 ;

, number:
number dd 0x1

; number
; 1

number ,
0x00000001 .
RESB,

RESWM RESD


. ,
, .

, . ,

, .
: RESB ( ),
RESW ( ) RESD ( ).
:
resb 1
resb 2
resw 2
resd 1
number

resd 1

buffer

resb 64

;
;
;
;
;
'number''
;
;

1
2
4
4
4

(2 )

64
buffer


, NASM,
. RESx.
TIMES

TIMES ,
.
91

, DUP
Borland TASM. ,
.
, 64 Hello:
many_hello: times 64 db 'Hello'

, ,
. ,
32
TIMES:
b u f f e r db " H e l l o "
times 32-($-buffer)

db ' '

;
;
/

32-($-buffer) 27, $-buffer


, 5.
TIMES ,
:
t i m e s 5 i n c

;5 INC

:
inc
inc
inc
inc
inc

INCBIN

.
:
i n c b i n "sound.wav''
i n c b i n ''sound .wav" , 512
i n c b i n "sound.wav'\ 512,80

;
; 512
; 512
; 80

EQU

, .
.
EQU :
f o u r 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:
; c o u n t e r
mov c x , e s : [ b x ]
/
;


, .
SEG .

6.8.
, ,
.
, , ,
,
.
,
.
,
.
.
ALIGN

, ,
, .
, .
.
,
, . , 93

, .

.
, .
, .
ALIGN.
, , ,
:
align 4
/ , 4
a l i g n 16
/ , 16

,
,
. , ?
MOV, ?
XOR ( ,
0):
,
; 0,00
,
mov ,0
; 08,0,0,0,0
, XOR ,
, .
:
,
; =
inc

; 1

1. DEC
INC, - 1 .

, ,
INC , ADD. ,

add ,4
; 4
:
inc
; 1
inc
inc
inc
94

6.

, INC, DEC ,
64- , .
INC DEC,
.
,
.
LEA,
. :
l e a ebx ecx+Gdx'^4 + 0x500]
l e a +*4~1]
lea ebx,[eax+eax*8]
lea ecx,[eax+ebx]

;
; + EDX*4 + 0x500
; = *5 - 1
; = ^-9
; = +

- 0.
, OR
TEST. ,
, 0
jz is_zero

; ?
; ? i s _ z e r o

:
or ,
jz is_zero

; OR
; (ZF),
; ? i s _ z e r o

OR ,
. , :
OR , (ZF) 1.
,
0.
jz:
dec
j z now_zero

; ,

, .
TEST, ,
, .
SF 1, ,
1. , TEST
: , SF
( 1 (1 AND 1 = 1)
, SF = 1):
95

t e s t ,
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
mov
add
mov

,[number1]
ebx,[number2]
eax,ebx
[result],eax

number1 dd 8
number2 dd 2
result dd 0


= number2
- +
result
; numberl
/ 8
; number2
; 2
; r e s u l t

, :
mov e a x , [ n u m b e r l ]
add e a x , [ n u m b e r 2 ]
mov [ r e s u l t ] , e a x

98

; = ''numberl"
;EAX - EAX + number2
; r e s u l t

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 1

/ numbers
/ 1 2,
/ EDX 3
/
/

dd 2

, .

: 8- , ESI
. ^
8- ,
32- .
,
.
mov esi,array
mov ebx,0
mov eax,ebx
again:
mov al,[esi]
inc esi
add ebx,eax
cmp a1,0
jnz again
array db 1,2,3,4,

/ ESI
/EBX =
/EAX =
/ AL
/ .
/ = +
/AL ?
/ again, AL
,6,7,8,0 / .
/ () 3 6

99

. 7.7. -

: , .
, .
SHR, CF,
, .
p u s h
shr ,1
pop
j c odd
even:

/
; CF
/
; CF = 1, odd
; ,

odd:

; , --

, :
test 1
j z even

; 1, TEST
;ZF ( ) , t e s t
; , - ,
;

odd:
even:

; ,

, AL, .
.
100

7.

: , AL,
,
.. .
, 0x15, 00010101b.
: 10101000b, 08.
, .
AL , 61
,
, .

. , SHR ( ; ),
CF ( ) , RCL,
. 8 ,
.
mov , 8
theloop:
shr 1
1
loop theloop

; = 8
/ AL
; CF
;
;
; 8

1 ,
1 ,
CF

: ,
.
,
10 (). AL ,
. :
, YES.
, :
mov bl,OxA
div
ah,
jz yes
:

;BL = 10 -
; BL
; = 0 ?
; , YES
; ,

yes :

101

7.2.
, .
:
.
,
.
? 10
, (.
ASCII, . 1.2). ,
. :
#includG < u n i s t d . h >
void main(void) {
u n s i g n e d i n t number;
char remainder;
number=12345678;
w h i l e (number != 0)
{

r e m a i n d e r = (number % 10) + ' 0 ' ;


/ * r e m a i n d e r = number mod 10 + c h a r ( '0 ) * /
number /==10; / * number = numbe]r d i v 10
p r i n t f ( " % c " , remainder) ;
}
}

^y^ ^ v
4 ^ 0>^
|

10

^r

1
j

1
. 7.2. -

102

7.

, , :
12345678, 87654321,
, .
?
, , ,
, .
,

. , .
,
.
.
,
EDI ( ),
.
( ).
c o n v e r t .
while
, ,
.
.
convert , 7.1.
7,1. *)|^1::|^
( ) i J ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ H
convert:
mov ,
mov
.divide:
mov edx,
div ebx

;ECX = 0
;EBX = 010

add e d x , ' 0 '


push edx
inc ecx
cmp eax,0
jnz .divide

EDX - 0
EAX EBX, EAX,
EDX
ASCII-


? ( ?)
, .divide
,
,

.reverse:
pop eax

'

103

mov [ e d i ] , al
add e d i , 1
dec ecx
cmp ecx,0
jnz . reverse
ret



,

?
?
?

.
, .
MOV ecx, XOR , .
, 10 (
0), 10 BL:
.
OR , (
TEST , ).
:
mov [edi],al
add edi,1

:
stosb
, .

LOOP. , 7.2.
7.2.
( )

convert:
,
ebx,ebx
mov bl
.divide:
edx,edx
div ebx
add d l ^ O '
push edx
inc ecx
or eax,eax
jnz .divide

104

ECX - 0
EBX = 0
EBX - 010
EDX = 0
EBX, ,
EDX
ASCII-


? ( ?)
, .divide.
, ,

7.

. reverse:
pop
stosb
loop .reverse
ret


AL ,
EDI, EDI 1
=-1, ,
?

, .
,
:
MOV b y t e

[edi],0

LOOP RET.
b y t e ,
, EDI.
. ,
.

(, , , EDX EDI). ,
,
.
:
mov , 0 x 1 2 3 45 67 8
mov e d i , b u f f
c a l l coPxVert

; ,
;
; EDI
;
;

, ,
. ,
. EDI
DI ( ). CALL
.
.
convert
. ,
.
, ASCII-
. , ASCII-
(.2).
16, F, ASCII-
, 9. :
105

9, .
ASCII- ,
HexDigit:
HexDigit:

cmp d l
jb .less
add d l , ' A ' - I O
ret
.less:
or d l , ' 0 '
ret

; DL 0 - 1 5 ,
;
;
/ DL 10
;,
;10 '', 11 '' ..
;
;
;

convert, 10
0x10, 16.
,
. ,
N- ,
.
, , ,
. ,
PUSHAD POPAD.
, 7.3,
:
7,3,
( )

NumToASCII

= 32-
-
edi = -
:

NumToASCII:
pushad
xor esi,esi
convert_loop:

106



ESI - :

7.

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 - 0
EAX EBX , EAX,
EDX
ASCII
EDX

? ( - 0)
,
DF:

/
/ ES:(E)DI
/
;? (ESI = 0)
; ,
;
/
/
/!!!

7.3.
, .

(readln, scanf),
.

c o n v e r t _ c h a r , '0'-'9' 0-9,
''-' ''- 10-15 (OxA-OxF). ,
, AL,
ASCII-.
.
.
convert_char:
sub al,'0'
1 0

jb done
add al,'0'
and al,0x5f

/ ASCII-
/ 10,
/
/
/ JB ,
/
/AL =
/

107

sub al,'A'-IO
and al,OxOf

done :
ret

10
0-15.
F,
4 AL
; ;


: ,
.
, .
ASCII- .
0-9, :
done; , .
ASCII (. 1.2)
, (
),
( 0x5F).
, '' , ' .. , ,
AND
0x00 OxOF.
,
. , . ,
, , ,
. , , ,
.
,
,
NEG ().
.
:
= a^*z" + a^_j*z"~^ -f ... -h a^*z^ -f- ^*7^ (n )
, 1234 :
1234 = 1*10^ + 2*10^ + 3*10^ -h 4*10^
, 0x524D
:
(524D)^^ = 5*16^ + 2*162 + 4*16^ + 13*16^ = 21 069
, :
convert_char,
,
. ,
108

7.

,
. ,
, .
1234 :
1234 = ((((1)*10 + 2)*10 + 3)0) + 4
, ,
, ..

.

-^S^
?.

NEG

. 7.3. -


7.4.

109

7.4.
ASCIIToNum
esi = ,
0x0
=
:
=
ASCIIToNum:
push esi
xor ,
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

;
; =
; = :
; ?
; ,
;
; '- '
; AL
; ?
/
; AL
; ,
;
;
/
/ ESI
/ ?
/,
/,
/
/!!!

,
,
() .
.

8.1.
1950- .
:
, ,
, .
,

.

.

(1960- ).
.
,
.

.
,

. .
,

.

. ,
, ,
, ,
1964 .
112

8.


: , , ,
, ?
.

8.2.
.
, ,
.

,
, . ,
, - ,
.
.
( ,
) .
.

init

xdm

Xfree86
<-
xdm

window
maker

^-

sshagent

xterm

. 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

->

\ I
J

1, 8.3. Round Robin

,

.
.

8.3.
, pea
.
.

.
, ,
, .

.

. 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 /
:
, /
.
,
(stdin). (stdout)
(stderr). 1,
2.
,
/. ,
,
. DOS,
UNIX, ,

. DOS UNIX
:
Is > f i l e l

( ) filel
Is, , ,
. Is
,
.
, .
121

(),
().
, /
(. 8.8). ,
. , , .
,
: , .

1 Z.
2

5
6

/i
3
2
1
3
2
1

Pvic. 8.8.

, .
, ,
512 . /
.
.
,
. ,
, . ,
.
60 (
).
, ,
.
(
) ,
. ,
, .

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

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

MOV , [array+4].
:
Array, 4.
, :
add

dl,''~10

' ' - 1 0 ,
ADD dl, 55.
,
. (. 9.1)
.
, NASM,

1
^
'


+ -

*/%//%%
+ - ~

9.1

OR
XOR
AND


, , (
), ,
, (NOT)

:
mov

,{(5*6+2)+(04 0<<2))/8

; :
;MOV , 0 2 4

:
mov 1,~1

;: mov 1,11111110b
;: mov

1,OxFE

9.3.
JMP CALL,
, . ,
. ,
,
.
,
,
127

. , NASM,
.
,
, .
(, )
, ? ,
,
.
: ,
.
subprog:
/ subprog
;
.11_1:
/
;
subprog2:
; subprog2
.local_loop:

9.4. NASM
,

.
,
.
. ,
, , ,
.
NASM ,
.
%.
( ).

, %i
(%idefine %define ..).
%define, %undef

, ,
.
:
128

9. NASM
%define

average(a,b)

((()+(b))/2)

:
mov

al,average(3,7)

, :
mov

al,5

%define , :
%define
%define

SECIN_MIN
60
SEC_JN_HOUR SEC_IN_MIN * 60

, :
%define

USEMMX

, , , ,
%if def () %ifndef ( ).
%undef.
%macro %endmacro

%defme ,
( ). , ,
%macro %endmacro.
. ,
. %endmacro .
%macro subtract 3
sub %1,%2
sub %1,%3
%endmacro

s u b t r a c t :
subtract ,,[variablel]

:
sub ,
sub ,[variablel]

. NASM
. ,
2-3 , 2 3 .
,
:
%macro a d d i t
a d d %1,%2
add %1
%endmacro

2-3

129

a d d i t , ,
, ,
0:
a d d i t

:
add e a x , e b x
add ,
%assign

%assign ()
. ,
. ,
%assign i i+1
1. %assign
.
%if

, NASM
. ,
.
:
%if<ycnoBHe>
; i f e l i f
; , .
%elif<ycnoBHe2>
; % e l i f %else
; ,
^else
; ,
; %else %endif
^endif

%elif %else .
%elif, %else ( ) .
.
: =, <, >, < = , > = , (,
, , , , ).
= = ! = = .

1 1 , ^ ^ , && (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.

,
, . ,
:
% i f n d e f MACROS_MAC
% d e f i n e 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

;T^^^^nporpafj^^^^^^^^^^^^^^^^^^^^^

; ,
; , ,
;()2005 .. <ivan(iivanov. >
SECTION .text
/ .text
;
mov ,2
= 2
mov ebx,5
= 5
add ,ebx
= +
add ,[statl]
= + statl
mov [dynl],eax

dynl
SECTION .data
; DB, DW and DD
;
.data
statl: dd 1
SECTION .bss
,
. .bss
RESB, RESW RESD,
,
. ,
RES* ,

dynl: resd 1
;
(.stack).
, .
SECTION SEGMENT
.
.

. , ,
.

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
;
;
TIMES 510-($-$$) DB
DW 055

9.6.
Netwide Assembler (NASM)
86- . NASM

, .

~f. ,
.
:

.
()
.
, (
)
.
135

, (linking),
.
, ,
, ,
( EXTERN GLOBAL),

.
bin

bin .
.
.
, he 11 . asm:
; ,
/ 0x0.
; DB " H e l l o w o r l d ! " ,
;
s t r i n g : db " H e l l o w o r l d ! " , O x d ,

, -f:
nasm - f b i n h e l l o . a s m

h e l l o , Hello world.
- , hello
,
ASCII-, .
bin (, ,
). ALIGN,
, . ,
, 16:
section .text align-16

b i n ,
DOS (. .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,
-f obj.
Win32 32-

Microsoft Visual -+
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 .out

COFF (Common Object File Format)


a.out.
UNIX, Windows NT,
( ) , , DJGPP.
- + .
-f coff,
COFF. .
elf -- UNIX

ELF (Executable and Linkable Format)


, . UNIX-
. ,
Linux, Solaris 86, UnixWare, SCO-UNIX UNIX
System V, .
..
-f elf.


.
,
, ..
,
.
NASM
-g. OBJ ELF.

138

DOS

DOS

DOS

640 KB
( 1981)
DOS ( MS-DOS, Microsoft Disk Operating System)
IBM PC.
/,
8- 16- .
DOS .
, .
,
UNIX .
DOS 640 , ,
.
(
8086),
,
, . 80286 80386
. , ,
. ,
.

10.1.
8086 20-
2^^ (1 ) . ,
16-: 20
?
. ,
,
16- , 16.
140

10. DOS

SI

15

DI

15

AL

8 7

+ + + + + + + + + + ++\

20-

. 10.1.

,
:
mov a l ,

[ds:si]

; AL
; d s : s i

, DS = 0559, SI 0x100.
20- , :
0559 * 0x10 (0x10 16) + 0x100

: 05590 4- 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
1

(:)

1 0x0000:0x0000

1 0x0040:0x0000

BIOS

1 ????:

DOS kernel ( )

????:

, ,

????:

()
C0MMAND.COM

1 ????:

(TSR, Terminate and Stay Resident)

????:
1 ????:
1 :
0x06000:0x0000
1 0x06800:0x0000

(,
.)

( )

( )

0 x 0 0 8 0 0 : 0 x 0 0 0 0 - :

ROM
(Read Only Memory)

OxFOOO:OxOOOO

ROM BIOS


. . 256
, , 4 . 1 .
, 0x400 (.. 0x0040:0x0000),
BIOS. ,
, .
, - ,
Ralf Brown Interrupt List .

.
COMMAND.COM,
.
TSR .
, ,
142

10, DOS

.
, ,
.
,
DOS (
), .

.
, .
640 , ..
:. 640 1
.
64 .
.
800:
, . 00800:00000

. . ASCII , .
,
ROM (Read Only Memory): BIOS BIOS
.

10.3. 1 MB
640 ,
, [ .
80286 24- ,
20 , .. 1 .
OxOFOOO,
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,
0x100,
.
144

10. DOS

0x100 .
PSP (Program Segment Prefix).
PSP-
. 0x80 ,
.
-, 0x1234
(. .10.2):
-

10.2

(:)

0x1234:0x0000

, .

0x1234:0x0080

0x1234:0x0100

0x100 -

0x1234:0x7???

()

01234:0????

()

01234:0????

0x1234:0xFFFE

(.bss)


. ,
, 64 .
.
.
,
. , ,
, (call far),
(CS IP).
(retf). ,
SEG
. ,
, .
, .
, (..
)?
,
0x0000:0x0000,
0x0000:0x0000 OxOOOO:OxFFFF,
0x1000:0x0000 .
, (..
0x0000, 0x1000 ..),
(relocation table).
145

.
,
,

.
, .

10.5.

. ,
, ,
.
0x21.
. DOS
. ,
, .

,
.
DOS 04,
.
:

:
:
= 04
AL =

, AL
, ,
.
, , .
, exit .
( 10.1).
1|||||1|||||^^

D Q | | >

SECTION .text
; DOS.
; ,
;
org 0x100
; , 0x10 0

146

10. DOS

mov ah,04
mov al,
int 0x21
;

;
;

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
1 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

.
10.2. ^ DOS

SECTION .text
; "Hello, World!'
; {EOL - 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.
:
:
1 = 0x01

:
AL ,
()

0x01 .
( 0x08 ,
).
AL ,
(extended) ASCII-, Page
Up, Page Down, , End, F1..F12 ..
0x01 AL ASCII-
.
,
Enter. DOS , , .
148

10. DOS
:
:
=
1 DS.DX =


,
.
,
(1-254), (ASCII 7) Enter.
, OxOD (
Enter).
mov ah,
mov dx,string
int 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 db

5,3,0x65,,0x67,OxOD,

,
OxD,
(ABC), OxD.
.
1: ,
.
Enter ( 10.3).
DOS- 0x01,
. , ,
DOS- 0x02.

149

I^MiliiHliMi^^^^'^'^
1|||||1|111111^

;
SECTION .text
again:
;DOS-
mov ah,0x01
;
DOS
int 0x21
;
DL
mov dl,al
;
"Enter"?
cmp d l , OxD
; ,
jz endprog
;DOS-
mov ah, 0x02
; DOS
int 0x21
;
jmp again
endprog:
;DOS-
mov ah,0x4C
; DOS
int 0x21
;
echo . asm. echo . com

nasm -f b i n - echo.com echo.asm
2: ,
Enter ( 10.4).
DOS- ,
,
DOS- 0x02.

|||

SECTION .text
;
; .
org 0x100
COM
mov ah,
DOS-
mov dx,string
DX
int 0x21
DOS
xor ax,ax

mov al, [string+1]
inc dx

add dx,
,

mov s i,dx
DX SI

150

10. DOS

std

/
/

print_next_char:
lodsb
/ DS:SI SI 1
s i , s t r i n g + l
/ ? ( )
j b endprog
/ ,
mov d l , a l
/ DL
mov a h , 0 x 0 2
/DOS-
i n t 0x21
/ DOS
jmp p r i n t _ n e x t _ c h a r
/
endprog:
mov ah,0x4C
/DOS-
mov al,0
/
int 0x21
/ DOS
SECTION .data
string db 254,0
t i m e s 253 db ' '
/
/
r e v e r s e , asm ,
:
nasm - f b i n - r e v e r s e . c o m

reverse.asm

,
.
, .
,
.
mov a h ,
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 (

, ).
D X + 2 , ,
OxOD ( Enter). ,
.
151

, 2 1 1,
. ,
.
i n c dx
add d x , a x
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), ,
, .
, .
s i , s t r i n g + 1
j b endprog

/ ?
/ ,

. , AL,
DL, DOS .
LODSB, .
mov d l , a l
mov a h , 0 x 0 2
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-
/
/ DOS

10. DOS

,
SECTION . d a t a
s t r i n g db 2 5 4 , 0
t i m e s 2 53 db ' '

,
.
( , 254 ),
. TIMES,
253 .

10.6. -
8, ,
. DOS
. - -
.
() ,
.
.
DOS Windows
: 8 3
. Windows ,
256- , .
, 8-1-3.

0x3D.
DS:DX (DS , DX ). AL
.
(AL=0).
.
CF=1, CF=0.
:

= 0x30
DS:DX = ,
0x0
AL = :
AL = 0
AL = 1
AL = 2

:
CF = 0 ,
= ,

CF = 1 ,
= :
- 0x0002
= 0x0003

153

,
:
:

:
CF = 0


=
=

CF = 1 ,
=

, ,
, .
( 10.5).

illli^HiiiiiiHRW
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 db ' ' t e x t . t x f , 0 ; t e x t . t x t

, 10.5 ,
:
. DOS- 0x3F.
154

10. DOS

.
(
),
.
:

:
= OxSF
=
DS:DX =

=

:
CF = 0
= ,
( , EOF)
:
CF = 1
=

.
, ,
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.
, .
0x40.
.
10.6.
%define B_LENGTH 80
%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
?
,
,


,
,

10. DOS

mov ah,0x40
xchg bp,bx

int 0x21
xchg bp,bx
jmp read_next
end_reading:
mov ah,
int 0x21
mov al,0
endprog:
mov ah,4Ch
int 0x21

; DOS
;
; ,
/
;
; DOS
;
; read__next,
;
/ DOS
; DOS
; :
; DOS
;
; DOS.
;

error:
:
mov al,1
endprog
jmp short endprog
SECTION .data
file_name db ''text . txt'\ 0 ; t e x t . t x t
SECTION .bss
b u f f e r RESB BLENGTH
;
;
; b u f f e r B_.LENGTH
l i s t i t . asm
nasm l i s t i t . a s m -- l i s t i t . c o m - f

bin

t e x t . t x t
:
echo I ' l l

BE BACK! > t e x t . t x t

t e x t . t x t
.
,
.
.
, !

157

.
/

0x3D ,
, ,
.
:
/
= 0x60
AL = 0x00 ( 0)
=
=
DX =
DS:DX =

:
:
CF = 0
=
= (. )
:
CF=1
=


: ,
(
).
DX.
.
4 ( DL):
DL = , (
).
DL = 0001b , .
DL = 0010b ( ),
.
4 ( DH):
DH = , .
DH = 0001b , .
,
DX 0x0012.
, ,
.
( )
, DOS UNIX, ,
, DOS :

158

0:
1:
2:
3:

1,
1,
1,
1,

.'
.
.
.

10. DOS

4: 1, .
5: 1, .
6-15: .
0x20:
.
.
,
.
= 0: .
= 1: .
= 2: .
(CF=0)
( 0x3D).
:
= 1: .
= 2: .
= 3: .
l i s t i t .asm ( 10.6) ,
. ( )
.
, . ,
:
mov , GCOOh

DOS CO/
:
AL

mov , 0x2 0

mov d x , 0x12
mov s i , other_file_name
DOS
i n t 0x21

jc error

mov b p , a x

, MOV bp,STDOUT.
:
:
mov a h , 0x3
mov b x , b p
i n t 0x21

/ DOS
;

, , :
o t h e r _ f i l e _ n a m e db " t e x t l . t x t " ,

;
159

text.txt textl.txt.
, ,
,
.
.asm :
nasm -f bin - copy.com copy.asm
(SEEK)

,
, .
(, 1 2
), /.
seekQ, DOS 0x42.
:


= 0x42
AL =
=
CX;DX =

:
:
CF = 0
DX:AX =
=
:
CF=1
=

.
CX:DX. ,
, ,
4 ( FAT16 2 ).
16 , 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

,0x4202
bx,filedes
dx,dx
ex,ex
0x21


=
DX =
-
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).
161

10.7. ||:^^^^11<||1^
SECTION . t e x t

o r g 0x100
/ DOS 0x5 6
mov a h , 0x5 6
;
DX
mov d x , s r c
; DI
mov d i , d e s t
i n t 0x21
;
mov a x , 0 x 4 c 0 0
i n t 0x21
SECTION . d a t a
s r c db " t e x t . t x t ' \ 0
d e s t db ' 4 t e x t . t x t " , 0

rename.asm .
, .
,
. :
SECTION .data
dest db '' \ "
src db "text.txt'\0

.
, , .

Windows 95 8- .

, 84-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 .
:

:

=
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
int 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 OxOD,OxOA
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, 04
;- ; ES
mov bx, 0x10 00
;
i n t 0x21
jc e r r o r

0x48, .
165

0x48
. ,
. .
:


= 0x48
=

:
CF = 0
=
:
CF=1
=
= ( )

, 64 , .
, ,
04.
mov ah,0x4 8
mov bx,0x1000
int 0x21
jc error

push es
mov es,ax

; DOS
; 4 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).
.. , | ^ ^ ^ ^ ^

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

ine di
mov byte [di],OxA
ine
int
mov
int

ex
0x21
ax,0x4e00
0x21



CL
DI

[OxOD]
DI

0x0,
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, .

10.11.2. grdb.exe

grdb (Get Real Debugger, )


.
: http://www.members.tripod.com/'-'ladsoft/grdb.htm.
, .
grdb.
C:\>grdb
GRDB v e r s i o n . C o p y r i g h t () L A D s o f t 1 9 9 7 - 2 0 0 2
History enabled
eax:00000000 ebxiOOOOOOOO ecx:00000000 edx:00000000 e s i : 0 0 0 0 0 0 0 0
e d i : 0 0 0 0 0 0 0 0 ebp:00000000 espiOOOOFFEE e i p : 0 0 0 0 0 1 0 0 e f l a g s : 0 0 0 0 0 2 0 2
NV UP E I PL NZ NA PO NC

169

.
d s : 10FB e s : 1 0 F B f s : 1 0 F B g s i l O F B s s : 1 0 F B
1 0 F B : 0 1 0 0 74 I E
jz
0120
->

csilOFB

(->) ,
. pwd.com (
). 1
pwd.com (1 load, ).
.
->1 pwd.com
Size: 00000069

.
t. t
Enter:
->t
:00001900 ebx:00000000 :00000069 edx:00000000 esi:00000000
edi:00000000 ebp:00000000 espiOOOOFFEE eip:00000102 eflags:00000202
NV UP EI PL NZ NA PO NC
ds: lOFB es:10FB fs:10FB gsrlOFB ssilOFB cs:10FB
10FB:0102 CD 21
int
21
->

t ,
0x19. INT 21,
. ( CSiIP)
. t Enter
DOS, AL ^ .
->t
:00001902 :00000000 :00000069 edx:00000000 esi:00000000
edi:00000000 ebp:00000000 esp:OOOOFFEE eip:00000104 eflags:00000202
NV UP EI PL NZ NA PO NC
ds: lOFB 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
. :
buffef, () 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
OD-OA

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

60 26 8A 47-01 32 E4 40-Dl EO 03 D8-26 8A OF 32 '&.G.2.@. . . .&. .2


ED OB C9 74-OF 43 53 26-8B IF E8 C6-00 5B 73 41 . . .t.CS&
[sA
43 43 E2 F2-2E C7 06 8C-CD 03 00 EB-34 26 8A 47 CC
4&.G
01 32 E4 40-Dl EO 03 D8-26 8A 07 32-E4 Dl EO 40 .2
&. . 2 . . .@

->
ADD t. :\
:\ ( ). d:
->d

124

10FB:0120

-43 5 20-20 20 20 20-20 20 20 20 :\

,
.
t
g (go),
.
.
() .
,
, .
( step over),
.
,
:
->

100

lOFB-.OlOO
10FB:0102
10FB:0104
10FB:0108
lOFBrOlOA
lOFBrOlOC
lOFBrOlOF
lOFBiOlll
10FB:0113
10FB:0116
10FB:0119

4
CD
00
30
B4
BE
CD
B4
BB
B9
BA

19
mov
21
int
OG 24 01 add
D2
xor
47
mov
27 01
mov
21
int
40
mov
01 00
mov
45 00
mov
24 01
mov

ah,0019
21
[0124],al
dl, dl
ah,0047
si,0127
21
ah,0040
bx,0001
ex,0045
dx,0124

171

.
lOFBiOllC CD 21
lOFBiOllE 8 00 4C
->

int
mov

21
ax,4C00

q.
grdb

grdb . 10.4.
grdb

<filename> () []

1 <filename> []
t

<addr>

<addr>


addr.
0 F, 16

b<num>,addr

10.4

,
.

7. ,
,
.
, ASCIIToNum:
ASCIIToNum
Gsi = / cipoi^, 0x0
=
:
-
172

10. DOS

DOS,
16- . () SI,
ESI.
.
,
.
.
SkipSpace, .
,
, .
SkipSpace
SI =
:
s i = ,
SkipSpace:
.again:
lodsb
; AL D S : S I ,
/ S1
a l , ' '
;
jz again
; ,
ret

, ,
. ,
0x81, OxD.
. ,
, . ,

. ,

.
SkipNum
: SI =
: DI =
SkipNum:
mov di,si
.find_end_of_number:
inc di
cmp [di],'0'

;DI
;DI=DI+1
; ASCIT-
;

173

jb .end_found
cmp [di],'9'
ja .end_found
jmp .find_end_ofnumber
.end_found:
ret

; ,
; ASCII-
; 9
; ,
; ;

A :
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,
DL
,
.
.
,
.
NumToASCII:

174

10. DOS

NumToASCII

= 3 2-
=
edi =
:

mov ebx,1

mov di,buffer
call NumToASCII

; EBX
; DI
;
/

,
. ,
. :
.
, .
mov a h , 0 x 4 0
mov d x , d i

; DOS
;
; DS:DX
;

mov , 2 5
i n t 0x21
mov , 0400
;
i n t 0x21
25 :
b u f f e r t i m e s 2 5 db ' '

.
SECTION .text
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
mov d i , b u f f e r
call NumToASCII
mov ah,0x40
mov dx,di
mov
int
mov
int

ex,2 5
0x21
ax, 0x4c00
0x21



, S1

,
EDX


DI



DX,
DI

DOS

SkipSpace -
S1

:
si = ,
SkipSpace:
.again:
/ AL DS:SI,
lodsb
; SI
cmp a1,' '
;
jz again
; ,
ret
SkipNum ~
: S1 =
: DI =
SkipNum:
;DI
mov di,si
. find_end_, of number
;DI=D1+1
inc di
/ ASCII-
cmp [di],'0'
; ,
jb .end_found
; ASCII- 9
cmp [di],'9'
; ,
ja .end_found
;
j mp .f i ndend_o f_numbe r
.end_found:
ret

176

10. DOS

; ***** *****
SECTION .data
buffer times 2 5 db ' '
;
.
:
nasm -f bin -o tes :.com t e s t . a s m
test asm 6: error: o p e r a t i o n s i z e not s p e c i f i e d
test asm 13 error : symbol 'Skipspace' undefined
test asm 15 error : operation s i z e not s p e c i f i e d
test asm 41 error : symbol ' a g a i n ' undefined
test asm 53 error : o p e r a t i o n s i z e not s p e c i f i e d
test asm 55 error ; o p e r a t i o n s i z e not s p e c i f i e d
r r o r : phase e r r o r d e t e c t e d at end of assembly.
test asm: 141 : eerroi
, . .
6:
mov [ d i ] , 0
;
, :
mov byte [ d i ] , 0
;
,
. ,
Skipspace SkipSpace. .
13 ,
6. 41 'again':
jz .again
53 55 .
'byte' .
, . ,
. :
mov ah,0x40
;
mov d x , d i
; DX, DI
mov ,2 5
/
i n t 0x21
/ DOS
,
0x01 10.
.
45 50. ; :
: \ t e s t 45 50
945

177

95, 945. ?
. grdb:
:>grdb
GRDB version . Copyright
->

() LADsoft 1997-2002

'1 test.com 45 5 ' :


->1 test.com 45 50
Size: 0000001
->

, ,
.
, ASCIIToNum .
ASCIIToNum MOV .
ASCIIToNum.
- > 1 t e s t . c o m 45 50
Size: 0000001
->
:00000000 :00000000 :0000001 edx:00000000 esi:00000081
edi:00000000 ebp:00000000 esp:OOOOFFEE eip:00000103 eflags:00000202
NV UP EI PL NZ NA PO NC
ds: lOFB es:10FB fs:lOFB gs:lOFB ss:10FB cs:lOFB
10FB:0103 E8 44 00
call
014A
->p
eax:00000000 ebx:00000000 ecx:OOOOOOEl edx:00000000 esi:00000081
edi:00000084 ebp:00000000 esp:0000FFEE eip:00000106 eflags:00000287
NV UP EI MI NZ NA PE CY
ds: lOFB es:10FB fs:10FB gs:10FB ss:10FB CS:10FB
10FB:0106 C6 05 00
mov
byte [di],0000
ds:[0084]-20
->p
eax:00000000 ebx:00000000 ecx:OOOOOOEl edx:00000000 esi:00000081
edi:00000084 ebp:00000000 esp:0000FFEE eip:00000109 eflags:00000287
NV UP EI MI NZ NA PE CY
ds: lOFB es:10FB fs:10FB gs:10FB ss:10FB CS:10FB
10FB:0109 66 B9 OA 00 00 00 mov ecx,OOOOOOOA
->P
eax:00000000 ebx:00000000 ecx:OOOOOOOA edx:00000000 esi:00000081
edi:00000084 ebp:00000000 esp:0000FFEE eip:0000010F eflags:00000287
NV UP EI MI NZ NA PE CY
ds: lOFB es:10FB fs:10FB gs:10FB ss:10FB CS:10FB
10FB:010F E8 6D 00
call
017F
->p
eax:000003Bl ebx:00000000 ecx:0000000A edx:00000000 esi:00000081
edi:00000084 ebp:00000000 esp:0000FFEE eip:00000112 eflags:00000297

178

10. DOS
NV UP EI MI NZ AC PE CY
ds: lOFB es:10FB fs:10FB gs:lOFB ss:10FB cs:10FB
10FB:0112 66 89 C2
mov
edx,eax
->

01,
45. 01 945.
? , ,
, .
, , SI:
->d s i
10KB:0080
20 34 35-00 35 30 OD-01 01 01 01-01 01 01 01 45.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
lOFBrOOBO 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
lOFBrOODO 01 01 01 01-01 01 01 01-01 01 01 01-01 01 01 01
lOFBiOOEO 01 01 01 01-01 01 01 01-01 01 01 01-01 01 01 01
lOFBiOOFO 01 01 01 01-01 01 01 01-01 01 01 01-01 01 01 01
10FB:0100 BE 81 00 E8-44 00 C6 05-00 66 B9 OA-00 00 00 E8 ....D....f
->

,
. :
( 20). :
SkipSpace,
.
. :
C:\test3
5

45 50

, .
ASCIIToNum:
->
:00000034 :00000000 :0000000 edx:00000000 esi:00000083
edi:00000084 ebp:00000000 espiOOOOFFEE eip:00000112 eflags:00000287
NV UP EI MI NZ NA PE CY
ds: lOFB esrlOFB fs:10FB gsilOFB ssilOFB csilOFB
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
lOFEiOOBO 01 01 01 01-01 01 01 01-01 01 01 01-01 01 01 01
lOFBiOOCO 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

.
lOFB-.OOEO 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
.- BE 81 00 8-41 00 8 44-00 05 00-66 9 00 . . . .. .D. . . .f. . .
->

, SkipSpace,
, 5
5 + 0 = 5. LODSB SI
, , . ,

. :
SkipSpace:
.again:
lodsb
a l , ' '
jz again
dec s i
ret

/ AL D S : S I ,
/ SI
/
/ ,

, , ,
:
: \ t e s t 4 45 50
95

. !
!

10.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,
0800:00003, 0800:00005 .. .
, , 0x04.
80x25 ,
.

181

.
color:
push
push
push si
push es
xor si,si
mov ax, OxBSOO
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
/ .repeat, >
/

.
, ,
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
cmp a l , 0x4 6
jnz pass_on
call color
pass_on:
pop ax
jmp f a r [ c s : o l d _ v e c t o r ]

/ AX
/ -
/ 0x4 6 ( S c r o l l - L o c k )
/ , p a s s _ o n
/


o l d _ v e c t o r
( ).
182

10. DOS

CS,
.
old_vector,
.
setup.
setup:
cli
xor ax, ax
moV e s, ax

;
; 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

.
newhandler:
push
in al, 0x60
cmp al, 0x4 6
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, OxBSOO
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

; AX
; -
/ 0x4 6 (Scroll--Lock)
; , pass_on
;
;
/
/
;
/,
/

/ SI
/
/
/
/ SI 1
/ 0x04
/ 1
/ 1
/ .repeat, >
/

DOS:

:

initialize
16
1

10. DOS

AX


mov ax,new_handler
new_handler

xchg ax,[es:0x9*4]


.

,

mov [ds:old_vector],ax

old_vector
mov a x , c s
CS
xchg ax :0x9*4+2]

,

mov [ds:old_vector+2],ax
2
old_vector
sti

ret

:
,
mov e s , a x

nasm -f bin - resident.com resident.asm.

, Scroll Lock
. (
)
DOS, - Windows.

10.13.
:
www.ctyme.com/rbrown.htiTi HTML-
(Ralf Brown's Interrupt List);
http://programmistu.narod.ru/asm/lib__l/index.htm
IBM PC .

185

11
Windows

Windows-

DOS-
Windows

""1"^'-

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/techpubs/delphi2/win32.zip

187

11.2.2. Hello, World!


Windows
.
API- MessageBoxA,
ExitProcess.
Windows API MessageBoxA :
int MessageBox(
HWND hWnd,
LPCTSTR IpText,
LPCTSTR IpCaption,
UINT
);

//
//
//
//

,
. ,
0. ,
. , .
, ,
. () ,
_.
API ExitProcess,
( DOS), .
Windows- ,
Win32.inc, API-
(, HWND LPCTSTR dword)
. :
%include w i n 3 2 n . i n c ;

,
, EXTERN
IMPORT:
EXTERN
IMPORT
EXTERN
IMPORT

MessageBoxA
;MessageBoxA
MessageBoxA u s e r 3 2 . d l l
;
ExitProcess
/ExitProcess
ExitProcess kernel32.dll
;


u s e r 3 2 . d l l

- k e r n e l 3 2 . d l l

, DOS, : .
SECTION CODE USE32 CLASS^CODE
SECTION DATA USE32 CLASS=DATA

;
;

, API.
13, ,
, ,
API.
188

11. Windows

STDCALL.
( , ),
( ).
PUSH,
CALL. .
ILL
1 1 . 1 .

^y^j^jilig^iaiiMiiBiii

%include win32n.
EXTERN MessageBoxA
IMPORT MessageBoxA
EXTERN E x i t P r o c e s s
IMPORT E x i t P r o c e s s
SECTION CODE USE32
..start:

^^^ (^ai J
;MessageBoxA
user32.dll
; a u s e r 3 2 . d l l
; E x i t P r o c e s s
k e r n e l 3 2 . d l l ; a - k e r n e l 3 2 . d l l
CLASS-CODE
/
; ,
/
p u s h UINT _
/ .
; :
p u s h LPCTSTR t i t l e
/
/-
p u s h LPCTSTR b a n n e r
/ - ,
/
push HWND NULL
/ / :
c a l l [MessageBoxA]
/ API.
/
/
p u s h UINT NULL
/ E x i t P r o c e s s
c a l l [ExitProcess]
/
SECTION DATA USE32 CLASS^DATA
b a n n e r db ' H e l l o w o r l d ! ' , O x D , O x A , 0
/
/ EOL
t i t l e db ' H e l l o ' , 0
/

, , NASM
Windows, : http://nasm.sourceforge.net. NASM
,
.
alink, : http://alink.sourceforge,net.
msgbox.asm. nasmw -fobj:
C:\WIN32>NASMW - f o b j

msgbox.asm

189

msgbox.obj,
alink:
:\WIN32>ALINK

- m s g b o x

- . Windows
. msgbox.exe,
.

11.3.
, ,
VM86, .
( , popf .),
( in out)
, ,
.
DOS BIOS .

11.4. DOS- Windows


DOS~poe Windows
MS DOS. DOS- cmd
( -^ -> cmd). VM86
DOS. NASM
C:\NASM, DOS,
:
cd

:\NASM

, 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
. ?
(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]

env[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. -
DOS Windows Linux .
( Manual Pages )

, Linux. ,
man- ( Linux-) etc , man-
. .
, ,
. DOS
=04. ?
Linux. unistd.h,
/usr/src/linux/include/asm:
# i f n d e f _ASM_I3 8 6_UNISTD_H_
# d e f i n e _ASM_I3 8 6_UNISTD_H_
/*
* This f i l e c o n t a i n s t h e system c a l l numbers.
V
#define
,NR_exit
1
#define
NR_fork
2
195

.
#define
#define
#define
#define

NR_read
NR_write
NR_open
NR_close

3
4
5

#define _syscalll(type,name,typel,argl) \
type name(typel argl) \
{ \
long
res; \
asm
volatile (int $0x80 \
: =a ( res) \
: 0 { NR_##name), b ((long)(argl))); \
syscall_return(type, res); \
}
Linux.
NR_exit:
#define

NR_exit

, , , I.
( ) 2 .
, exit(). :
man 2 e x i t
man-:
_1(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)
() .
.
:
mov
mov e b x , 0
i n t 0x80

; e x i t
;
/

12-7. Hello, World! Linux



. , 8
(STDIN, ), (STDOUT,
) (STDERR, ). Hello,
World! STDOUT
. STDOUT ,
, .
unistd.h, write().
:
man 2 w r i t e
WRITE(2)
Linux Programmer's Manual
NAME
write write to a file descriptor
SYNOPSIS

WRITE(2)

#include <unistd.h>
s s i z e _ t w r i t e ( i n t fd, const void *buf, s i z e _ t c o u n t ) ;
, -, ,
. ?
,
- . , ,
, ,
.
write : ,
, ( buf),
, .
,
.

nasm, Id,
197

.
Linux. ELF
-f elf.
Id , . 12. L
Id

-o<name>
-S

12.1

() <name>

Id ,
_start.
Hello, World! 12.1.

12.1. ^ World!
SECTION .text
global _start
_start:
mov eax,4
mov ebx,1
mov ecx,hello
mov edx,len

; .
;
;
; - write
; STDOUT 1
;
; Hello, World!
;
;
; 1 exit
;
;

int 0x80
mov eax,1
mov ebx,0
int 0x80
SECTION .data
hello db Hello, world!,
len equ $ - hello

/
;
;

:
nasm -f elf hello.asm
A :
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.

. |.~^^^^

u^xxv^^^de s_^ stem, ^nc


CODESEG
;
START:
;
;
sys_write STDOUT,helloen
;
; write
; write
sys_exit
; exit
DATASEG
;
hello db Hello, World!,
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 :
mov
; 1 e x i t
mov ebx,0
;
i n t 0x80
/
.
rtn,
sys^exit [rtn], :
mov
; 1 e x i t
mov e b x , [ r t n ]
; r t n
i n t 0x80
;
200

12. Linux

,
, EMPTY
.
EMPTY .

12.10.
/ (I/O)
Linux , Linux UNIX-
, :
,
.

() ,
, .
Linux (man 2 open).
:
i n t open(const 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-).

0_RDONLY

0_WRONLY

0_RDWR

0_CREAT

0_TRUNC

0_APPEND

12.2

0_LARGEFILE

,
( NFS)
4

201

0_CREAT,
. ,
.
, :

S_IRWXU

SJRGRP

S_IROTH

, name,
:
s y s _ o p e n name, 0_RDWR, EMPTY
t e s t eax,eax
; EAX
js .error_open
;
; 7


DB:
DB iny_f i l e _ w h i c h _ h a s _ a _ v e r y _ l o n g _ n a m e . t x t ,

| (
OR). , ,
, 700 (//
, ):
s y s _ o p e n name, 0_RDWR I 0_CREAT, S_IRWXU
t e s t eax,eax
j s . e r r o r _ o p e n / e r r o r _ o p e n ,
...
;

DOS, .
cIoseQ, sys__cIose.
.
, :
sys_close

.
, ,
, .
read:
202

12. Linux

s s i z e _ t r e a d ( i n t fd, void *buf, s i z e _ t c o u n t ) ;


,
, ,
, .
, .
sys_read ,
STDIN
.

, :
, , ,
, .
, :
s s i z e _ t w r i t e ( i n t fd, const void *buf, s i z e _ t c o u n t ) ;
,
Enter
. ,
ASCII.
, , ,
Ctrl -i- .
Enter read
, .
'' 'z' '' 'Z'.
STDOUT.
Asmutils
CODESEG, DATASEG,
UDATASEG.
system.inc ( 12.3).

%include system.inc
%define MAXIDATA 10
CODESEG
START:
again:
;
sys__read STDIN, read_data, MAX_DATA
test eax^eax
;? ( EAX)

203

.
j S endprog

add

compare_next:
dec ecx
cmp byte [ecx],'a'
jb no_conversion
cmp byte [ecx], ' '
ja no_conversion
sub byte [ecx],0x20

; ?
;? EAX
;
;
; ,
;
; + 1
;
; < '',
/,
;
; > 'Z'
;
;
; 0x2

no_conversion:
cmp e c x , r e a d _ d a t a
jz p r i n t i t

?
? ,

jmp s h o r t compare_next ;

printit:
.

,

,
EDX,
w r i t e
s y s _ w r i t e STDOUT,read_data, e a x
jmp s l i o r t a g a i n
; .
endprog:
; 2 5 5
s y s _ e x i t 2 55
/
UDATASEG
r e a d _ d a t a r e s b MAX_DATA
END
;
, ,
MAXIDATA.
sys_read.
.
.
.
:
3, ( 204

12. Linux

, ).
POP .
, (). ,
.
, ,
, .
.
12.4.

12.4. |||

%include system.inc
%define BUFFLEN 409 6
CODESEG
START:
pop eax
;; EAX
;
cmp eax,3
; 3
jae enough_params
;; ,
mov eax,255
; , 255
endprog:
;
sys_exit eax
enough_params:
pop ebx
/ .
; ,
;
pop ebx
; .
sy s_open EMPTY, 0__RDONLY I 0_LARGEFILE
;
test eax,eax
;? ...
j s endprog
mov ebp,eax
; EBP
pop ebx
; EBX
sys_open EMPTY,0_WRONLYI0_LARGEFILEI0_CREATI0_TRUNC,S_IRWXU
; ,
; 700
test eax,eax
js endprog
/?
mov ebx,eax
; EBX
copy_next:
xchg ebp,ebx
; EBX EBP,
; EBX A
sys_read EMPTY,buff,BUFF_LEN
/ 1
test eax,eax
/

205

j s end_close
jz end_close
xchg ebp^ebx

;?
; ?
; ,
; -
s y s _ w r i t e EMPTY,EMPTY,
/ ,
/
t e s t ,
j s endprog
/?
jmp s h o r t c o p y _ n e x t /
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 e n d p r o g
/
UDATASEG
buff r e s b BUFF_LEN
/ 4
END

/ . ,
,
, ,
.
/
Iseek:

off_t l s e e k ( i n t f i l d e s , off_t o f f s e t ,

i n t whence)/

, , ,
, ,
:
SEEK_SET ;
SEEK_CUR ;
SEEK_END .
Iseek
.
: Iseek, :
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,
. ,
.
int unlink(const char ^pathname);

.
, .
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:
i n t 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 / , 07 00
sys_exit 0
/
DATASEG
name DB /tmp/mydirectory
END

.
( chmod) -:
q . , 0700
700q.
RMDIR,
:
i n t rmdir(const char ^pathname)/

209

.
(CHDIR)

chdir:
int. chdir (const char *path) ;
,
sys__exit
sys_chdir name
(GETCWD)

getcwd, ,
Linux 2.0 ( 2.4-2.6).
: ,
, , :
long sys_getcwd(char *buf, unsigned long s i z e )
, :
sys_getcwd path,PATHSIZE ; path
;
mov esi,ebx
; ESI
; EDX
xor edx,edx
.next:
inc edx
EDX path
AL, ESI
lodsb
?
or al,al
?
jnz .next
mov byte [esi-1] , n


sub esi,edx

sys_write STDOUT,esi,EMPTY STDOUT
EDX
sys_exit_true

12.12. .
.
lOCTL

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 e d x , t e r m a t t r s
; EDX
s y s _ i o c t l STDIN,TCGETS
/
mov , [ t e r m a t t r s . c__lf lag]
/
push
;
and ,~(ICANONI ECHO)
; ECHO ICANON
mov [ t e r m a t t r s . c _ l f l a g ] ,
/
s y s _ i o c t l STDIN, TCSETS
/
pop dword [ t e r m a t t r s . c _ l f l a g ] /
UDATASEG:
t e r m a t t r s B_STRUC t e r m i o s , . c _ l f l a g
, .

12.13.
,
. ,
. DOS, ,
,
.bss ( ,
).
.bss , .
211

- ,
(
heap.asm Asmutils).
,
.bss, :
void * brk(void *end__data_seginent) ;


.bss. .bss.
,
. :
sys_brk
;
add ,____
;
sys_brk
;
.bss .

12.14. . ALD
UNIX-
gdb, -
, .
,
ALD (Assembly Language
Debugger).
, ,
GNU.
http://aId.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
esp - OxBFFFFSCC ebp
ds - 0x0000002B es ss - 0x0000002B cs Flags: PF ZF TF IF
08048082

5A

= 0x00000000 = 0x00000000 edx = 0x00000000


-- 0x00000000 esi - 0x00000000 edi = 0x00000000
0x0000002B fs = 0x00000000 gs = 0x00000000
0x00000023 eip = 0x08048082 eflags =0x000000346
pop

edx

, , pop edx.
0x8048082.
ZF ( ).
( s), Enter.
, int 0x80,
( = 0x00000003):
ald>
= 0x00000003 = 0x00000000 - 00804908 edx =
esp - 0XBFFFF8D0 ebp - 0x00000000 esi = 0x00000000 edi - 0x00000000
ds = 0x00000023 es - 0x0000002 fs - 0x00000000 gs = 0x00000000
ss = 0X0000002B' cs = 0x00000023 eip = 0x0804808D eflags =0x00000346
Flags: PF ZF TF IF
0 8 0 4 8 0 8 D CD80

int

0x80

EDX ( = lOd)
10 . ,
.
(examine): :
a l d >
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. , :
a l d >
Dumping 64 bytes of memory starting at 0x080490C8 in hex
080490C8: 61 73 6D 20 72 75 6C 65 7A OA 00 00 00 00 00 00
asm rulez
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

,
. .
, ,
Ctrl+.
help , help
_ . . 12.3
ALD.
ALD

12.3

load <filename>

()

set args <args>

step [n]

() . step
s

next [n]

step,

disassemble


. d.
!^ , d 0x08048061

continue

( )

exannine


.
.
, , , 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, GAS
AT&T, Intel
.
Hello, World!,
AT&T ( 12.7).
|1|0^

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,%
i n t $0x80
movl $0,%ebx
movl $ 1
i n t $0x80

#
#
#
#
#
#

'write'

:

'exit'

12.16.

Linux, http://!inuxassembly.org.
(, ),
.
Web-, 514
( httpd.asm).

12.17.
NASM :

-V

-f <f mt>
-fh
- <name>
1

216

-1 <path>

<fmt> (. 9)

<name>

-?

Pascal-


, .
,
, .
,
Pascal.

13.1.

(, ):
.
,
: .
,
.

. ,
,

, .
,
CALL,
RET. CALL
PUSH, .
( CALL)
.
,
.

(E)SP .
, , ,
. ,
218

13.

,
.
,
, RET.
- ,
.

13.2. -?

() SP ,
, .
()
, .
().
() ,
() (E)SP.
,
().

(E)SP

.
.
()

()

>

,
.
()

<-

. 13.1. (stackframe)

219

, , ,
(), -.
(
, ), .
(), , -.
,
.

13.2.1. - (32- )
.
32- ,
Linux:
i n t a d d i t ( i n t , 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

p u s h ebp
mov dword
sub dword
add dword
p u s h Oxaa
p u s h 0x55
c a l l near
add dword
mov dword
mov dword
m.ov dword
pop ebp
retn

ebp, esp
e s p , 0x18
esp,
OxfffffffS

+0xffffffc7
e s p , 0x10
e a x , eax
[ebp+Oxfc], eax
e s p , ebp

main EBP (
) .
ESP, . , SUB ADD,
0x18 0x8 ,
. , 8
.
220

13.

,
, .
,
. PUSH (0x55
) . , CALL, addit.
addit.
(. . 13.1).
addit

. 13.1

1 0XBFFFF860 = ESP

0x08048409 ( )

1 0XBFFFF864

0x00000055

0XBFFFF868

0XBFFFF86C -0xBFFFF84

main

0XBFFFF888

0xBFFFF88C = EBP

0XBFFFF8C8 - LIBC

0XBFFFF890

0400392

32~ ,
, CALL.
32- ,
addit. ,
. ,
main,
, 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 d w o r d e b p , e s p
s u b dword e s p , 0x18
mov e a x , d w o r d [ e b p + 0 x 8 ]
mov e d x , d w o r d [ e b p + O x c ]
l e a ecx,
[eax+edx]
mov d w o r d [ e b p + O x f c ] , e c x
mov e d x , d w o r d [ e b p + O x f c ]
mov d w o r d e a x , e d x
jmp s h o r t +0x7
lea esi,
[esi+OxO]
mov d w o r d e s p , e b p
pop ebp
retn

221

addit .
, - main.
addit:
ESP. SUB d,
a-f-b.
addit,
SUB (. 13.2).
addit
1

13.2

no

|0xBFFFF844=ESP

1 0XBFFFF848 - 0xBFFFF854

I 0xBFFFF858 = EBP 4 (+OxFFFFFFFC) : d


0xBFFFF88C ~ main
( -)

0xBFFFF85C = EBP
|0xBFFFF860 = EBP + 4

0x08048409 ( main)

|0xBFFFF864=EBP + 8

0x00000055

|0xBFFFF868 = EBP + 0xC

1 0xBFFFF86C - 0xBFFFF84

main

10xBFFFF888

: main

0xBFFFF88C

0xBFFFF8C8 - LIBC

0XBFFFF890

0400392

, (dword)
,
. , d,
.
main. addit,
main main.
addit EDX
:
080483D6
080483D9

84508
8550

mov , dword
mov e d x , dword

[ebp+0x8]
[ebp+Oxc]

0x8?
- ,
8 ( -)
. ,
8 + 4 = 12 .
. 13.3 ( ),
.
222

13.

13,3

[ - 4]

[ + 0]

[ + 4]

[ + 8]

[ + ]

addit 0x55. ,
, .
addit ,
EDX, LEA.
. addit -
:
080483F0
080483F2

89
5D

mov dword e s p , ebp


pop ebp

,
- jnain.
(main),
.
.
, (char, short),
, 4 .

13.2.2. - (16- )
, -, 16-
, :

(4 ), (2 );
32- 16- (..
, SP ESP ..);
, DX:AX.
,
32- , (
80286 ).
16- -
. ,
.
223

- medium, large huge


CALL FAR, CALL NEAR.
,
, .
,
2 , FAR.
FAR- RETF, RETN.
NASM ,
, -
-.

13.3. -

_. , printf
_printf. ELF
( Linux), .
-, printit,
plus
. printit ,
printf.
Linux.
- :
const i n t plus == ;
void p r i n t i t ( i n t ) ;
i n t main(void) {
printit(5) ;
}
plus 6.
printit. main,
printit 5.

plus printf:
extern plus
extern printf

gee
ELF, .
include , arg
endproc, :
%include misc/c32.mac
224

13.

printit.
: push ebp mov ebp^esp. arg
. ,
arg ,
4 ( 32.).
printit
%$what arg
:
mov ,[ebp + %$whatj
add ,[plus]



plus
push

printf
push strl
-~

call printf
p r i n t f
endproc
e n d p r o c ,

( PIX ) ^-
13.1.

1 3 . 1 . 6|1<>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



. plus
. printf
printit
printit,
what

^ plus

printf
push strl

call printf
printf
endproc
endproc ,

{ PDC )

section .data
; ,
strl db SUM - %d.,OxOA 0x0
;
; ^^

225

printit.asm :
nasm - f e l f

printit.asm

- main.
gcc, printit.o:
gcc - p r i n t i t main. p r i n t i t . o
printit, ,
SUM = 11.
16- ,
16.. endproc
NEAR FAR-. FAR-
:
% d e f i n e FARCODE

.
, ,
:
%macro cglobal 1
global _%1
%define %1 _%1
%endmacro
%macro cextern 1
extern _%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 ;
v a r p l u s : integersf u n c t i o n a d d i t (x: i n t e g e r ) : l o n g i n t ; f a r ; e x t e r n a l s begin
p l u s := 6;
writeln('SUM = ', a d d i t ( 5 ) ) ;
end.

addit, , .

external. , addit
, ^ longint
(4 ) FAR. $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 ._
%define PASCAL
% include mi s/1.mac
extern plus
global addit
proc addit
%$what arg
xor dx,dx
mov ax,[bp+%$whatj
add ax,[plus]
adc dx,0
endproc


FAR-


plus
addit
addit
-

what
DX
= what
= - p l u s


arg
. 16- 2 .
longint ,
, 4 .
, :

C S :007
55
push
bp
895
mov

C S :0077
bp, sp
CS

:0079
:007

31D2
84606

xor
mov

dx,dx
ax,[bp+0 6

CS

.007

03065200

add

ax, [0052]

CS

0082
0086
0088
0089

81D20000
89
5D
02 00

adc
mov
pop
retf

dx,0000
sp, bp
bp
0002

CS

CS
CS
CS

DX-0
1-


p l u s

e n d p r o c
e n d p r o c
e n d p r o c


.
228

,
,
, , , .

,
,
.

,

(, SSE, 3DNow),
.
, ;

~- DOS, Windows Linux.
,
,
.


.

&i

^v
'-I %

15.


MOV

XCHG

PUSH
POP
ADD
SUB
MUL
1
DIV
INC ( 1)
DEC ( 1)
AND ( )
OR ( )
1
XOR
NOT

TEST
SHR ()
SHL ()

RCR
RCL
JMP ~

LOOP , () 0
JZ (ZF)
JC (CF)

JNZ (ZF)
JNC (CF)
CALL
RET

INT
REP , () 0
MOVSx

i
CMPSx
SCASx

231


003

004
005
006
007
008
009

Oil
012
013
014
015
016
017

019
020
021
022
023
024
025
026
027
028
1 HZV

04

85 ^
06
07
86
09

?
811
81OF
19
11
1>!
13
14
15;
16
17
18
1^
Ifi
IB
1
1
IE
IF
28
21
22
23
24
25
26
27

030
031
032
033
034
035
036
037
038
039
4 y.}i
041 25
042 2fi

044
845
846
047
848
049
050
851
052

2B
2C
2D
2E
2F
36
31
32
33
34

:-1

854
55
856
857
058
859
060
61
862
063

36
37
38
39

3B f
3C <
M) =
3E >
3F ?
4 P
41
42
43
44 D
45 E
46 F
4? G
4 H
4? I
4 J
4B
4C L
4D M
4E N 1
4P 0
58 p !
51 Q 1
52
s:<
54
55

1 643

0&i. 01 Q
002 82 Q

*
K
-i
t11

41
5
^
t

r
i
t1-

f*

i.
T

"
^
^

^
1
f

>

*^_J

Plh4

865
066
867
866
069
878
71
nvv
873
074
875
876
077
878
879
OSO
881
082

884
085

0
1
2
3
4
h
6
7
8
9
f

JJ J

56
087 57
08g 53
089 59
090 5
091 SB
092 5C
093 5D
094 5E
095 5F
9 WA
097 61
098 2
099 63
100 G4
101 &5
i02 66
103 7
14 hK
105 5?
106 &
107 ISR
108 6C
109 &D
110 6E
111 Gr
112 70
113 71
114 72
11b /
116 74
117 75
118 76
119 77
120 78
I2i 7
122 7ft
123 7B
124 7Z
125 7D
i;!fi 7h
127 7F
128

1 086

J"

1
1
j
!
!

z[

12? 81 u

\
]

1 135
i 13&

13
131
132
133
134

137
138

W'i

d.

140
141
142
143
144
145
14b
147
143
149

d
e
f
9'
h
i
J
1<
1

IFirt

151
152
153
154
155
156
157

PI

P
Cr
r

I
u.

IfiK
i

V
%.'
X j
v 1

z
<
1

>
"
'^

159
1&9
161
1G2
163
154
1&5
1&&
167
1&9
IhV
179
171

82
83
04
85:
86
87
88
89
8
KH
8C
8D
8E

e
a
a
a
a
9
i

1 172 ^

173
174
175
176
177
178
179
180
181

1.

1
i.

ffi
^
n

183
184
185
106
187
188
189
19
191
192
193
194
195
196
197
198
199
200

o
r
90
91
92
v:^
94
9
9fi
97
98
99
9ft
9B ^
9C
9D V
9b ..
9F X

fil 1
2
ft3 U.
A4
ftG "
6
A7 ~
8
flV
ftft
Ji

1
2

4
5

7
8
9

<

^
i

\
\
\
\\

1
1
^1

II

il

J
BF

.1 JL
2
\
4
5 \
6 1=
7 \\
Li
>?1
CV II
^
202 Cfi
203 11
204 \\

205;

206
207
20
209
210
211

CF

CF

1
2

7.1 7. D4
213 5
214 6

X.

II

^
L1

1 215

216
217
216
219
228
221
222
223
224

D7 \\

245
246
247
24G
249
258
251
252
253
254

D3 i
D? -*
DI) 1
1(3 I
DC
DD 1
DE 1
DF
9 I
K1
E2

4
5
6 J.I
7 X
5



F.r.
E D Jtf

E F II
F9 =
F1 +
F2 >
F3 L
4 f
F5 J
F6 -f
F7 ~
F9 "
F9 FA FD NT
FC
FD =
FE 1

'J\^}

KK

/!!

226
227
228
229
230
231
232
''

234
235
26
237
238
239
248
241
242
243
V.44

. 15.1. ASCII ( )

232