Вы находитесь на странице: 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

.
, 2005
9'i795943ll872326
,
ISBN 5-94387-232-9 , , 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. 12
1.2. . 15

2. 8 19

2 . 1 . 0 20
2.2. 86 22
2.3. : 23
2.4. 80386 25
25
27
27
27
2.5. 28

3. 30

3.1. 31
3.2. 33
3.3. 34
3.4. 35

4. 36

4.1. MOV 37
4.2. 39
4.3. 40
4.3.1. ADD SUB 41
4.3.2. INC DEC 43
4.3.3. 44
4.3.4. 46
NEG 46
CBW 46
CWD 47
CDQ 47
CWDE 47
4.3.5. 48
MUL IMUL 48
DIV IDiV , 50
4.4. 51
AND 51
OR . 52
XOR 52
NOT 53
( ) 53
5. 55

5.1. 56
5.2. IF THEN 57
5.2.1. TEST 57
5.2.2. JMP 58
5.2.3. Jx 59
5.3. 63
IF GOTO 63
LOOP , 65
.
LOOPZ LOOPNZ 66
5.4. 67
? 67
PUSH POP: 68
PUSHA/POPA PUSHAD/POPAD:
70
PUSHF/POPF PUSHFD/POPFD:
71
CALL RET: 71
INT IRET: 73

6. 76

6.1. 77
CLI STI 77
STD OLD 77
6.2. XCHG 78
6.3. LEA 78
6.4. 79
STOSx 79
LODSx 80
CMPSx 80
SCASx 80
REP REPZ 80
6.5. / (I/O) 84
IN OUT 84
. NOP 86
6.6. 86
SHR SHL 87
SAL SAR 89
RCR RCL 89
ROR ROL 90
6.7. 90
DB, DW DD 90
RESB, RESW RESD . . 91
TIMES . . 91
INCBIN . . . . . . . . 92
EQU 92
SEG 93
6.8. 93
ALIGN 93
94
94
95
96
7. 97

7.1. 98
98
99
. 99
. 100
101
101
7.2. 102
7.3. . 107

8. 111

8.1. 112
8.2. . 113
113
114
114
115
8.3. 116
116
(swapping) 117
117
8.4. 120
120
121
122
123
8.5. 123
123
, BIOS 124

9. NASM 125

9.1. . 126
9.2. 126
9.3. 127
9.4. NASM 128
~ %define, %undef 128
~ %macro %endmacro 129
%assign 130
%if 130
? %ifdef, %infndef 131
%include 131
9.5. 131
BITS 132
SECTION SEGMENT .,. 132
EXTERN, GLOBAL COMMON
134
CPU
134
ORG 134
9.6. 135
: 135
bin ~ 136
OMF 16- 136
Win32 32- 137
aout aoutb UNIX 137
coff .out 138
elf UNIX 138
138

10. DOS 139

10.1. 140
10.2. DOS 142
10.3. 1 MB 143
10.4. DOS 144
10.5. 146
146
. Hello, World! 147
148
10.6. - 153
153
154
154
155
/ 158
(SEEK). , 160
161
162
10.7. 163
(MKDIR, RMDIR) 163
(CHDIR) 163
(GETCWD) 163
10.8. 165
165
166
166
10.9. 166
10.10. 167
10.11. 168
10.11.1. 168
10.11.2. grdb.exe 169
169
grdb 172
172
10.12. 180
10.13. 185

1 1 . Windows 186

11.1. 187
11.2. Windows- 187
11.2.1. API 187
11.2.2. Hello, World! Windows 188
11.3. 190
11.4. DOS- Windows 190
11.5. 190

8
12. Linux 191

12.1. . 192
12.2. 193
12.3. 194
12.4. 194
12.5. 195
12.6. - 195
12.7. Hello, World! Linux 197
12.8. : Asmutiis 199
12.9. Asmutiis 200
12.10. / (I/O) 201
201
202
202
203
206
207
12.11. 209
(MKDIR, RMDIR) 209
(CHDIR) 210
(GETCWD) 210
12.12. . .
IOCTL 210
12.13. 211
12.14. . ALD 212
12.15. GAS 215
12.16. 216
12.17. 216

13.
, 217

13.1. 218
13.2. -? 219
13.2.1. - (32- ) 220
13.2.2. - (16- ) 223
13.3. - 224
13.4. Pascal- 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 1 0

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 :

1 0 8 1000

1 1 1 9 1001
2 10 10 1010
3 11 11 1011
4 100 12 1100
5 101 13 1101
6 110 14 1110
7 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 1
3/2 = 1 1
1/2 = 1
, 0.
,
(1110)2-
13 :
13/2 = 6 1
6/2 = 3
3/2 = 1 1
1/2 = 0 1
, ,
0. , (1101)^.

123456 :
123456/16 = 7716
7716/16 = 482 4

14
1.

482/16 = 30 2
30/16 = 1 14 =
1/16 = 1
, 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 6 5 4 3 2 1 0 1
L. BaiiT(byte)

- 65535
( - OxFFFF)
15 8 7 0

^^ (word)^
1 1 - 4294967295
1 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 1 2 3 4 7 8 9 1 D E F
V 4 1
00 1 2 3 4 6 7 8 9 10 11 12 13 14 15

10
i 1
4.
16 17 18 19 20 21 22 23 25 26 27 28 29 30 31
24
1 % 1
( ) + /
20 3Z 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

30 0 1 2 3 4 5 6 7 8 9 < > ?
48 49 50 51 52 S3 54 55 56 57 58 59 60 61 62 63

40 (Of) D F G 1 J L M N 0
64 66 67 68 69 70 71 72 73 74 75 76 77 78 79

50 Q R S V W X Y Z [ ]
A

80 81 82 83 34 85 86 87 88 89 90 91 92 93 94 95

60 b d f 9 h 1 J 1 m n 0
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

q S t U V W X z 1 }
70 114 115 117 118 119 120 122 126 127
112 113 116 121 123 124 125
. . e i i A A
80 128 129 130 131 132 133 134 136 137 133 139 140 141 142 143
135
6 6 6 u < . f
90 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
i 6 - e:
1 i
160 161 162 163 164 165 167 168 169 170 171 172 173 174 175
166
J J J
176 177 178 1 180 181 1 1
183
1
184 185 186
1
187 188 189 190
1
191
179 182
L JL JL J,
192 193

194 195 196
f
197 198
1 200
ff
201 202 203
1 205 ir
207
199 204 206
1 1 L 4 J
DO
208

209

210 211 212

213
1
214 216 217

218
1
220
1 1 223
215 219 221 222
- Q 5 CO n
224 226 227 228 229 231 234 235 236 238 239
225 230 232 233 237
D
> < f J
F0 240 241 242 243 246 247 248 249 250 251 252 253 254 255
244 245

. 1.2. ASCII

18
(

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 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 DS
31 16 15 8 7 X 15 0
0)

1 03
ES
I
( BL 1
31 16 15 8 7 "^ S 15 0
>
1
FS
1 CL 1 )
31 16 15 8 7 15 0
DX 1 2
EDX DH 1 DL GS 0)
31 16 15 8 7 15 0
ESI SI SS
31 16 15 15 0

EDI DI > 2 CS
31 16 15 I 15


31 16 15

ESP SP CRO
03
31 16 15 31
X
-
EIP IP CR1
31 16 15 0 31 >1

)

313029 2 1
/
. 2.3. 80386


"'
1 AL
31 16 15 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


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 ; 4 ,
; 0x12 345 67 8
mov word [ 0x12345678 ] , 0 ; 2 ,
; 0x12345678
mov b y t e [ 0x12345678 ] , 0 ; 1
; 0x12345678

.
.

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

35
4

MOV


86,
, ,
. ,
,
.

4.1. MOV
- ,
: .
, .
MOV, move (
), ,
:
MOV ,
MOV:
mov ,[number] number

mov [number],bx
number
mov bx,ex

mov a l , 1 AL 1
mov dli^cl 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 , [number_one] / 16-
/ ''number_one"
mov [number_two], ;
;"number_two"
MOV :
mov , 1 ;! -
/.
BL
, , :
mov , /
mov a h , ;''"
;
8- .
MOV ,
, . ,
, MOV ah,
. 8-
, BL, 16 . , 16-,
.
: ,
BL (AL):
mov , ; = , AL =
mov a l , ; AL BL
16- 32- .

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

38
4.

4.2.
. ,
, 0x12345678,
, 32- counter.
0x12345678 co
unter:
mov e b p , 0x12345678 ; 0x12345678
mov [ c o u n t e r ] , ebp ;
; " 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 , 8 / 8
mov , ;
mov dx, ex ; DX, DX =
add dx, ;DX = DX +
( )
,
DX, DX. ADD
DX 4- DX,
.
ADD SUB:
add , 8 EAX = EAX + 8
sub e c x , e b p ECX = ECX - EBP
add b y t e [number] 4
number
1
( 0-2 55)
sub word [ n u m b e r ] , 4 number = number 4
2
( 0-6553 5)
add dword [ n u m b e r ] , 4 00000004
"number"
sub b y t e [ n u m b e r ] , al AL
"number"
sub a ] i , a l AL ,

, AL (8- )
(255), 8?
mov a l , 255 ; AL 2 5 5 , OxFF
add a l , 8 ; 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 ;1 = 1 + 2 + CF
SBB 1, 2 ;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 , Oxffff ; = OxFFFF
mov dx, ;DX =
add , 8 ; = + 8
ado dx, ; 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 , Oxffff ; - OxFFFF
add , 8 ; = + 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 ;1 - 1 + 1
DEC 1 ;1 = 1 - 1
! CF.
, , (
43
.

) , ,
.
AL :
add a l , 1 ;AL = AL + 1
inc a l ;AL = AL + 1

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 206(0)
45000 , ,
16 (2 ).
, ,
16 .

: 2 16.
2^^ = 65 536, t
65 535. : 128

65 536 / 2 = 32 768. , 1
-32 768 32 767 (
!). . 4.4.
1

44
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 ; = -6, 65 53 0 OxFFFA
mov d x , 7 ;DX = 7
add a x , d x ;AX = AX + DX
65 530 -h 7 = 65 537 = 0x10001,
, .
, !
,

.
. NASM
,
:
mov , - ; = -
mov dx, - ; DX =^ -
add a x , d x ; =^ + 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 ; , 8-
;
neg b y t e [number] / 8- number
:
,
.
.
.
.
,
. ,
.

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

15
. AL
I I I I 1 I 1 I

, AL
15
I I I I I I I II I I I
87 1}
, 4.5. CBW

46
4.

CWD
CWD DX,
:.

CWD
DX
DH I DL I AL

DH DL AL
DX
. 4.6. CWD

CDQ
CDQ EDX,
EDXiEAX.

CWDE
CWDE ( )
,
, .

CWDE

1 1 1 AL

'
, AL 1
1
. 4.7. CWDE

:
mov a l , -1 ;AL = - 1 ( OxFF)
cbw ;
CBW OxFFFF,
- 1 . (1) ,
AL .

47
.

mov , 4 ; = 4
cwd ; 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 , bh ;AL = AL
mul 1 ; = AL * CL CL
.
: 486^ DX:AX:
mov , 486 ; = 4 8 6
mul ; * - > 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 AL = 2
mul b y t e [ r a d i u s l ] AX = r a d i u s * 2
mov [ d i a m e t e r l ] , a x 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 EDX - EDX * ECX
imul ebx, [stliing] 3 2-
''stiling" ,

49
.

imul , ; - *
, IMUL
, .
, ,
:
imul edx,ecx,l EDX = ECX * 7
imul ebxsthing],9 ' ' s t h i n g " 9,

imul ecx,edx,11 = 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 - 13
mov 1,2 CL - 2
div 1 CL
mov bx, ,

50
4.

2: ,
16- diameterl, radiusl,
.
mov ,[diameterl] ; = d i a m e t e r l
mov ,2 ; 2
div ;
mov [radiusl],al ;

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

AND
AND 1 2.
1. ,
ADD: 8-, 16- 32- ,
.
AND 1 , 2
AND (. 4.1).
AND 4.1

b aANDb

0 0 0
0 1 0
1 0 0
1 1 1 1


(1 AND 0).
mov al,1 AL = o n e
mov bl,0 BL = z e r o
and al,bl AL = AL a n d BL = 0
TOT , :
mov 1 , 1 ; A L =- o n e
and a l , ; AL = AL a n d 0 1 and 0

51
.

OR
OR 1 2.
1. ,
AND.
OR o l , 2
OR (. 4.2).
OR 4.2

b aORb

1 0 0

1 1 1

1 1 0 1
1 1 1


.
( ) mask 1.
or byte [mask],1

XOR
1 2.
1. , .
:
XOR 1 , 2
XOR (. 4.3).
XOR 4.3

b XOR b

0 0 0
0 1 1
1 0 1
1 1 0 J

: (( ) )
X.
mov al,0x55 AL - 0x55
al, AL = AL
al, , AL 0x55

52
4.

NOT
,
.
:
NOT r/m8
NOT /ml
NOT r/m32
NOT (. 4.4).
NOT 4.4

NOT

0 1
1 0

NOT
NEG:
mov al,00000010b AL = 2
mov bl,al BL = 2
not al
11111101b - OxFD (-3)
neg
: 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.
.

.
, , .

,
,
.
,
, .

. 5.1.

56
5.

5.2. IF THEN

IF-THEN.

.
, .


1 2 3

, 5.2.

5 . 2 . 1 . TEST
TEST .
, , 8,
16 32 .
1 , 2
compare, .
SUB: 2 1. ,
.
,
.
TEST ,
.
. TEST
.
:
, 4 ; 4
d l , a h ; DL
[ d i a m e t e r l ] , ; " d i a m e t e r l "

57
.

, [ d i a m e t e r 1 ] / " d i a m e t e r l "
cmp , ;
t e s t , 00000100b ; '
; ( )

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 ; == 4
new_loop: ; new_loop
mov b x , ;
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 is_true ; is_true, ZF = 1
jc is_true ; is_true, CF = 1
js is_true ; is_true, SF = 1
jo is_true ; is_true,
;0F = 1
, :
jnz i s _ t r u e ; i s _ t r u e , ZF =^
JNC, JNS JNO.

, ( ,
jump) (. . 5.1).
5.1

1==2 1!=2
1>2 1<2 1=<2 1>=2
1=2 1<>2

JE(JZ) JNE(JNZ) JA(JNBE) JB(JNAE) JNA(JBE) JNB(JAE)


Jump, Jump, Jump, Jump,
Jump, Jump,


Jump, Jump, Jump, Jump,
Jump, Jump,

0 0

JE(JZ) JNE(JNZ) JG(JNLE) JL(JNGE) JNGCJLE) JNL(JGE) 1
Jump, Jump, Jump, Jump,
Jump, Jump,


Jump, Jump, Jump, Jump,
Jump, Jump,

0

.
(
). ,
: 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 ; AL CL
jz write_l ; w r i t e _ l , AL - CL
cmp a l , c l ; AL CL
ja write_2 ; w r i t e _ 2 , AL > CL
mov b l , 3 ; - 3 BL
end_if: / , IF

write_l: ; w r i t e _ l
mov ;BL = 1
jmp end_if / end_if
write_2: ; write_2
mov bl,2 ;BL = 2
jmp end_if ; end_if
61
.

I J

/ )4 '

. 5.3.

(jmp end_if),
. :
; w r i t e ^ l ,
.
jmp end_if .
:
mov 1 ; BL = 1
a l , c l ; AL CL
j e end_if / , AL = CL
mov b l , 2 ;BL = 2
cmp al,cl ; AL CL
ja end_if ; , AL > CL
mov bl,3 ;BL - 3
end_if: /
, ,
. MOV ,
:
mov b l , 1 BL = 1
cmp 1 , 1 AL CL
j e end_if , AL CL
mov b l , 2 BL = 2
ja end_if , AL CL
mov b l , 3 BL = 3
end if:

62
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: ;
10 .
. ( ),
1. : 10,
, 10,
.

( )


1=1


1=1+ 1


^ 10 :

. 5.5. FOR

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

64
5.

... ;
inc ; 1
/ 10
jnz for_loop ; , 1:_1
for_finish: ; = 10,
FOR. , ,
, , I.
for_start:
mov dword [i],0 ;1 dword 1 = 0
for_loop: ;
... ;
inc dword [i] / i 1
cmp dword [ 1 ] 0 ; i 10
jnz for__loop ; , for_loop
for_finish: ; ,
,
, ,
.
,
DEC ZF .
: (-),
, 1. ZF , ,
:
for_start:
mov ,10 ; = 1 0
f_1: ;
... ;
dec ; 1
jnz for_loop ; , for_loop
for_finish: ; ,

:
for ( i = 0 ; i < 10;i++) {}

LOOP ,
, 80386, , 86--
CISC (
), . ,
,
. ? CISC-,

65
.


LOOP:
LOOP
MUL, LOOP .
, .
( ). . LOOP
() ,
, . 128
( ).
FOR LOOP:
for_start:
mov ; = 10 10
for_loop: ;
... ;
loop for_loop ; , ,
; for_loop
for_finish: ;
, ; .

.
LOOPZ LOOPNZ
LOOPZ
. , :
, , 10 , ,
3.
, .
LOOPZ
LOOPNZ
LOOPZ :
,
ZF . LOOPE.

:
for_start:
mov ,10 - 10
for_loop:
FOR

66
5.

,3 3?
loopz for_loop =-1; , =3 ,
f o r _ l o o p
for finish: = 3 ,
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 ; ESP 4 ( 4-
;)
mov [ s s : e s p ] , e a x ;
( 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 ; - 0x1234
mov bx,0x5 67 8 ; - 0x5 67 8
push /
push bx /
... /
pop bx /

69
.

OxFOOO OxFOOO 0

OxFOOO 0

OxFOOO
[rfol
ll 0|0J

0 0
ll

PUSH PUSH BX POPBX
(
)

?? I ?? ?? ,?? SP--OxFFFA 78 56 00 1 00
SP=OxFFFC 34 OxFFFC 34 12 SP=OxFFFC 34|12|
\'^\
SP=OxFFFE OOiOO OxFFFE 00 lOoJ OxFFFE 00 lOoJ OxFFFE 00 1 00 1
AX=1234 AX=1234 AX=1234 AX=1234
BX = 5678 BX = 5678 BX=5678 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 - 4
mov e b x , 8 - 8
c a l l add i t 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
^ \ / N


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
6

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 ; - DOS!
... ;
/ - , ,
/
sti /

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 , / .
; NOP
xchg ebx,ecx ;
xchg al,ah ; AL
xchg dl,ah ; DL
xchg b y t e [ v a r i a b l e ] , 1 / 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 0x6F 0x6D 0x70 0x75 0x74 0x65 0x72 0x00

C o m p u t e r / 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.
- D1 EDI .
, STOSW, ,
, (E)DI / 2. STOSD
, E(DI) / 4.

79
.

clcl ; DF,
stosw / 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 {) D S : ( E ) S I
E S : ( E ) D I .
- memcpy()
:

stosb ; () AL E S : ( E ) D I .
; - memset()
REPZ ( REPE), LOOPZ,
. ,
() , ZF .
REPNZ ( REPNE).
SCAS CMPS:
repz ; SCASB
scasb
:
repz ; CMPSB
cmpsb
,
, strlen().

I j

[ j

. 6.2. - strlen()

81
.

,
ES:(E)DL ( +
) . ES:(E)D1
, () .
6.L

6.1. |1|11|:
1
strlen:
push
, (=0),
: mov ,
, =
dec = - 1. OxFFFFFFFF -

eld DF = , ,
repne scasb
neg ( )

pop
ret

ES:(E)DI

[ 0 _ /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().

( j


>

. 6.4. - strcmp

: ES:(E)DI,
DS:(E)SI. ,
, ,
. strcmp () 6.2.

6.2,, s t i ^ ^ ^ ^ j
strcmp:
push edx EDX
push edi EDI
call strlen
miov edx, eex EDX
mov edi,esi ED1 = ESI
;push ds DS
;push ds
;pop es ES (ES - DS)

call strlen
;pop ds DS
cmp ecx,edx ?
jae .length_ok , ()

83
.

mov ecx.edx ;
.length_ok:
pop edi ; EDI
eld ;DF -
repe cmpsb ; ,
/
;
pop edx ; EDX
ret ;
strcmp ,
DS ES .
,
.
, .
. 6.5
.

ES:EDI DS:ESI
L1 EI
/ /
ES:EDI DS:ESI ^-
ECX = OxA ^
. 6.5. strcmp

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

IN OUT
IN , OUT
, .
IN al, dx OUT dx, a l
IN ax, dx OUT d x , ax
IN eax, dx OUT d x , eax
IN al, iininS OUT immS, a l
IN ax, immS OUT iiranS, ax

84
6.

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

1 0000-001f :dma1 DMA (Direct Memory Access)


1 0020-003f:pic1
0040-005f: timer
0060-006f:keyboard
1 0070-007f: rtc (RTC, real time clock)
0080-008f : dma page reg DMA page register
OOaO-OObf: pic2
OOcO-OOdf: dma2 DMA-
1 OOfO-OOff: fpu
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 ; BL=0
mov ex ; = 1
repeat:
shr ; 1 ,
/ CF
jnc not_one ; ,
inc ; BL 1
not_one:
loop repeat ; 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 : 0x55,
0x56, 0x57
db '',0x55
.
0x61, 0x55
db 'Hello',13,10,'$' .
0x48, 0x65, , ,
0x6F, OxD, , 0x24
90
6.

, ,
DW (define word):
dw 0x1234 ; 0 3 4 , 0x12
dw '.' ;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 ; 1
resb 2 ; 2
resw 2 ; 4 (2 )
resd 1 ; 4
number resd 1 ; 4
'number''
buffer resb 64 ; 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 ; 512
i n c b i n "sound.wav'\ 512,80 ; 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] ;
; + EDX*4 + 0x500
l e a +*4~1] ; = *5 - 1
lea ebx,[eax+eax*8] ; = ^-9
lea ecx,[eax+ebx] ; = +


- 0.
, OR
TEST. ,
, 0 ; ?
jz is_zero ; ? i s _ z e r o
:
or , ; OR
; (ZF),
jz is_zero ; ? 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 , ; TEST
;
js is_negative ;, SF=1


(, LOOP ).
:
, .
,
. near,
128 , (short).
- .

96
7


,
.
- ,
. ,
, .

7.1.
,
.


: 32- , num-
b e r l number2, r e s u l t .
, .
, r e
sult:
mov ,[number1]
mov ebx,[number2] = number2
add eax,ebx - +
mov [result],eax result

number1 dd 8 ; numberl
/ 8
number2 dd 2 ; number2
; 2
result dd 0 ; r e s u l t
, :
mov e a x , [ n u m b e r l ] ; = ''numberl"
add e a x , [ n u m b e r 2 ] ;EAX - EAX + number2
mov [ r e s u l t ] , e a x ; r e s u l t

98
7.


: 32- . EDI
, .
EDX.
32- ,
EDI. 4 ,
, , 4 .
mov e d x , [ e d i ] / EDX
add e d x , [ e d i + 4 " / , EDX
EDI:
mov e d i , n u m b e r s / EDI numbers
... / -
mov e d x , [ e d i ] / EDX
add e d x , [ e d i + 4 ] /

numbers dd 1 / numbers
/ 1 2,
/ EDX 3
dd 2 /
/
, .


: 8- , ESI
. ^
8- ,
32- .
,
.
mov esi,array / ESI
mov ebx,0 /EBX =
mov eax,ebx /EAX =
again:
mov al,[esi] / AL
inc esi / .
add ebx,eax / = +
cmp a1,0 /AL ?
jnz again / again, AL
array db 1,2,3,4, ,6,7,8,0 / .
/ () 3 6

99
.

( j

. 7.7. -


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

odd: ; , --
, :
test 1 ; 1, TEST
j z even ;ZF ( ) , t e s t
; , - ,
;
odd:

even: ; ,
, AL, .
.
100
7.


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

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


: ,
.
,
10 (). AL ,
. :
, YES.
, :
mov bl,OxA ;BL = 10 -
div ; BL
ah, ; = 0 ?
jz yes ; , 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) ;
}
}

( j

^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 , ;ECX = 0
mov ;EBX = 010
.divide:
mov edx, EDX - 0
div ebx EAX EBX, EAX,
EDX
add e d x , ' 0 ' ASCII-
push edx
inc ecx
cmp eax,0 ? ( ?)
jnz .divide , .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.
( ) I
convert:
, ECX - 0
ebx,ebx EBX = 0
mov bl EBX - 010
.divide:
edx,edx EDX = 0
div ebx EBX, ,
EDX
add d l ^ O ' ASCII-
push edx
inc ecx
or eax,eax ? ( ?)
jnz .divide , .divide.
, ,

104
7.

. reverse:
pop
stosb AL ,
EDI, EDI 1
loop .reverse =-1, ,
ret ?
, .
,
:
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 ; EDI
;
c a l l coPxVert ;

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

105
.

9, .
ASCII- ,
HexDigit:
HexDigit: ; DL 0 - 1 5 ,
;
;
cmp d l / DL 10
jb .less ;,
add d l , ' A ' - I O ;10 '', 11 '' ..
ret ;
.less:
or d l , ' 0 ' ;
ret ;
convert, 10
0x10, 16.
,
. ,
N- ,
.
, , ,
. ,
PUSHAD POPAD.
, 7.3,
:

7,3,
( )

NumToASCII

= 32-
-
edi = -
:

NumToASCII:
pushad

xor esi,esi ESI - :
convert_loop:

106
7.

edx^edx EDX - 0
div ebx EAX EBX , EAX,
EDX
call HexDigit ASCII
push edx EDX
inc esi
test eax,eax ? ( - 0)
jnz convert_loop ,
eld DF:

write_loop:
pop eax /
stosb / ES:(E)DI
dec esi /
test esi,esi ;? (ESI = 0)
jnz write_loop ; ,
mov byte [edi],0 ;
popad /
/
ret /!!!

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' / ASCII-
1 0 / 10,
/
/
jb done / JB ,
/
add al,'0' /AL =
and al,0x5f /

107
.

sub al,'A'-IO 10
and al,OxOf 0-15.
F,
4 AL
done :
ret ; ;

: ,
.
, .
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 ; AL
or al,al ; ?
j z .done
call convert_char /
; AL
imul ebx,ecx ; ,
add ebx,eax ;
jmp short .next ;
.done:
xchg ebx,eax /
pop esi / ESI
cmp byte [esi] , ' - ' / ?
jz .negate /,
ret /,
.negate:
neg eax /
ret /!!!

110
8


,
,
() .
.

8.1.
1950- .
:
, ,
, .
,

.

.

(1960- ).
.
,
.

.
,

. .
,

.

. ,
, ,
, ,
1964 .
112
8.


: , , ,
, ?
.

8.2.
.
, ,
.


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

init xdm W Xfree86


<-
xdm window sshagent
maker
^- xterm

. 8.1.

, .
UNIX (Linux) init.
, .
DOS
COMMAND.COM.

. (kill),
. ,
( init), .

113
.


,
.
,
. , .

,
.
, ,
.
, - ,
,
. ,
.
.
,
, , , ,
.
. ,
,
. ,
, .


.
,
(, process control block).
UNIX
:
, ,
.
: ,
.
,
.
.
,
, : - ,
, .
, ,
, , (-).

114
8.


>
()
1
1
4


()

. 8.2.

DOS , DOS
:
.
:
.
( , ..).
.
.
.
(PID, Process ID).
.
.
.
.


, (,
) , .
(Round
Robin). .
.
,
.

. ,

. ,
115
.

->
I I
(

\ I
J

[ 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 /i
6 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,
9.1

1 OR

^
' XOR

1 ^

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 SECIN_MIN 60
%define 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 2-3
a d d %1,%2
add %1
%endmacro

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- (NetB-
SD, 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
1
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 , AL
15 8 7

+ + + + + + + + + + ++\
DI
15

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
ROM
0 x 0 0 8 0 0 : 0 x 0 0 0 0 - :
(Read Only Memory)

OxFOOO:OxOOOO ROM BIOS 1


. . 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
|
1 0x1234:0x0080 |
0x1234:0x0100 0x100 - |
0x1234:0x7??? () |
01234:0???? () |
01234:0???? (.bss) |
,
|
0x1234:0xFFFE |


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

:
AL , i
()
1 = 0x01

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 ; DX ()
int 0x21 ; 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:
mov ah,0x01 ;DOS-
int 0x21 ; DOS
mov dl,al ; DL
cmp d l , OxD ; "Enter"?
jz endprog ; ,
mov ah, 0x02 ;DOS-
int 0x21 ; DOS
jmp again ;
endprog:
mov ah,0x4C ;DOS-
int 0x21 ; DOS
;
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 , /DOS-
mov d x , s t r i n g / DX
i n t 0x21 / 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 /
/ 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 /
04,
endprog:
mov ah,0x4C /DOS-
mov al,0 /
int 0x21 / DOS

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

: :

CF = 0 ,
= 0x30 = ,
DS:DX = ,
0x0
AL = : CF = 1 ,
AL = 0 = :
AL = 1 - 0x0002
AL = 2 = 0x0003

153
.


,
:

: :

CF = 0

=
= CF = 1 ,
=

, ,
, .
( 10.5).

illli^HiiiiiiHRW ^^
SECTION-.text
org 0x100
mov ax, 0x3D00 ; DOS ,
;-
mov dx,file_name ; , DS
;
int 0x21 ; DOS
jc error ;? , error
mov bx,ax ; ,
;
mov ah, / DOS
int 0x21 ; DOS
mov al,0 ; :
;
endprog:
mov ah,4Ch ; DOS
int 0x21 ; DOS
error:
mov al,1 ; :
jmp short endprog /
;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 CF = 0
= = ,
( , EOF)
DS:DX = :
CF = 1
= =

.
, ,
DS:DX.
, :
.
( ,
, ).
CF = 1,
.
, ,
DOS 0x09. , -
; $, 0x09.
, 0x09,

,
/, .
0x0001.


0x40. ,
:

: :

:
:
CF = 0
= 0x40
= ,
=

:
DS:DX =
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 . DS

int 0x21 DOS
jc error ? , error
mov bx,ax

read_next:
mov ah,0x3F DOS
mov dx,buffer
mov cx,B._LENGTH
int 0x21 DOS
jc error ? , error
or ax,ax ?
,
jz end_reading ,
mov ex,ax

,
,

156
10. DOS

mov ah,0x40 ; DOS


xchg bp,bx ;
; ,
/
;
int 0x21 ; DOS
xchg bp,bx ;
jmp read_next ; read__next,
;
end_reading:
mov ah, / DOS
int 0x21 ; DOS
mov al,0 ; :
endprog:
mov ah,4Ch ; DOS
;
int 0x21 ; DOS.
;
error:
mov al,1 :
jmp short endprog 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 CF = 0
AL = 0x00 ( 0) =
= = (. )
= :
DX = CF=1
DS:DX = =


: ,
(
).
DX.
.
4 ( DL):
DL = , (
).
DL = 0001b , .
DL = 0010b ( ),
.
4 ( DH):
DH = , .
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 .asm ( 10.6) ,
. ( )
.
, . ,
:
mov , GCOOh DOS CO/
:
AL
mov , 0x2 0
mov d x , 0x12
mov s i , other_file_name
i n t 0x21 DOS
jc error
mov b p , a x
, MOV bp,STDOUT.
:
:
mov a h , 0x3 / DOS
mov b x , b p ;
i n t 0x21
, , :
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.

: :

:
CF = 0
= 0x42 DX:AX =
AL = =
= :
CX;DX = 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 ,0x4202
mov bx,filedes =
xor dx,dx DX =
xor ex,ex -
int 0x21 DOS

160
10. DOS

DX:AX
.


,
.
0x41:

: :

:
CF = 0
= 0x41 :
DS:DX = CF=1
=

,
, .
0x43:

: :


= 0x43
:
AL = 0x00
CF = 0
DS:DX =
=
:
:

CF=1
=
=
AL = 0x01
DS:DX =

0x56.
,
, ,
.

: :

: CF = 0
= 0x56 :
DS:DX = CF=1
ES:DI = =

: text.txt
.
. :
( 10.7).

161
.

10.7. ||:^^^^11<||1^

SECTION . t e x t
o r g 0x100
mov a h , 0x5 6 / DOS 0x5 6
mov d x , s r c ; DX
mov d i , d e s t ; DI
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, RM-
DIR, CHDIR .. ,
.

(MKDIR, RMDIR)
0x39,
. .
,
.

: :


: CF = 0
= 0x39
:

CF=1
=
=
DS:DX =

(CHDIR)

(CHDIR). DOS,
, .
, DOS .

: :

: CF = 0

:
=
CF=1
DS:DX =
=

(GETCWD)
,
. 0x47 ,
.

: :


= 0x47
DL = : : CF = 0
DL = 0x00 :
DL = 0x01 : CF = 1
=
DS:SI = ,

163
.

,
0x19:

: :

AL = :
AL = 0x00 :
= 0x19 AL = 0x01 :

10.8. pwd,
,

10.8. ,

SECTION .text
org 0x100
mov ah,0x19 DOS
int 0x21 DOS
add byte [buffer],al '' ,


xor dl,dl

mov ah,0x47 DOS

mov si,buffer+3 '':\"
int 0x21 DOS
mov ah,0x40 DOS
mov bx,0x0001
mov cx,BUFF_LEN
mov dx,buffer
int 0x21 ; DOS
mov ax,0x4c00 ; DOS
int 0x21 ; DOS
SECTION .data
buffer db "A:\"
times 64 db " " ;

db OxOD,OxOA ;
BUFF_LEN equ $-buffer / $ ,
; .
/ ,

164
10. DOS

: :


=
DL = 0x00 : AL=
DL = 0x01 :

10.8.
, DOS,
.
DOS,
. ,
04.


04.
, .
, 1,
.

: :

: CF = 0


= 04
CF=1
=
=
ES = ,
=


16 .
- 64 (
0x1000 ). 0x1000
:
mov ah, 04 ;
mov bx, 0x10 00 ;- ; ES
i n t 0x21 ;
jc e r r o r

0x48, .

165
.


0x48
. ,
. .

: :

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

, 64 , .
, ,
04.
mov ah,0x4 8 ; DOS
mov bx,0x1000 ; 4 KB
int 0x21 ; DOS
jc error ; CF = 1, ,
;
/
push es ; ES
mov es,ax ; ES


()
. 0x49:

: :

: CF = 0


= 0x49
CF=1
ES =
=

10.9.

argc ( ) argv (
). DOS ,
0x81 .
OxOD -

166
10. DOS

0x80 ( ).
.

. : ,
0x81 ( 10.9).

.. , | ^ ^ ^ ^ ^
SECTION .text ;
%define STDOUT 1 ;
org 0x100 ;-
mov ah,0x40 ; DOS
mov bx,STDOUT
mov dx,0x81
xor ex,ex
mov cl,[0x80] CL
mov di,ex DI
add di,dx
[OxOD]
ine di DI

mov byte [di],OxA 0x0,
EOL
ine ex 1
int 0x21
mov ax,0x4e00
int 0x21 DOS

10.10.
, ,
: (CF=1), .
.
=0x59.
Ralf Brown Interrupt List.
10.3 .
DOS- 10.3

1 0x0002
1 0x0003
1 0x0005

167
.

. 10.3

1 0x0008
1 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 csilOFB
1 0 F B : 0 1 0 0 74 I E jz 0120
->
(->) ,
. 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 -41 5 20-20 20 20 20-20 20 20 20 : \
10FB:0130 20 20 20 20-20 20 20 20-20 20 20 20-20 20 20 20
10FB:0140 20 20 20 20-20 20 20 20-20 20 20 20-20 20 20 20
10FB:0150 20 20 20 20-20 20 20 20-20 20 20 20-20 20 20 20
10FB:0160 20 20 20 20-20 20 20 OD-OA C7 06 8C-CD 01 00 EB

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


10FB:0180 ED OB C9 74-OF 43 53 26-8B IF E8 C6-00 5B 73 41 . . .t.CS& [sA
10FB:0190 43 43 E2 F2-2E C7 06 8C-CD 03 00 EB-34 26 8A 47 CC 4&.G
lOFBiOlAO 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 4 19 mov ah,0019
10FB:0102 CD 21 int 21
10FB:0104 00 OG 24 01 add [0124],al
10FB:0108 30 D2 xor dl, dl
lOFBrOlOA B4 47 mov ah,0047
lOFBrOlOC BE 27 01 mov si,0127
lOFBrOlOF CD 21 int 21
lOFBiOlll B4 40 mov ah,0040
10FB:0113 BB 01 00 mov bx,0001
10FB:0116 B9 45 00 mov ex,0045
10FB:0119 BA 24 01 mov dx,0124

171
.

lOFBiOllC CD 21 int 21
lOFBiOllE 8 00 4C mov ax,4C00
->
q.

grdb
grdb . 10.4.
grdb 10.4

1 <filename> [] <filename> () []
,
t

,

U
.
,
g
<addr> <addr>
?
addr.
b<num>,addr 0 F, 16

1 ^

q


,
.

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 ;DI
.find_end_of_number:
inc di ;DI=DI+1
cmp [di],'0' ; ASCIT-
;

173
.

jb .end_found ; ,
cmp [di],'9' ; ASCII-
; 9
ja .end_found ; ,
jmp .find_end_ofnumber ; ;
.end_found:
ret
A :
mov si, 0x81 / SI
;
call SkipNum /
mov [di],0 ;
mov ecx,10 ;
call ASCIIToNum ; , SI
;
mov edx,eax ; EDX
mov si,di / SI
;
inc si
call Skipspace ;
call SkipNum ;
mov [di],0 ;
;
call ASCIIToNum ; , SI
/
add e a x , e d x / ,
; EDX
.
,
.
. DI
, , .
SI,
DL
,
.
.
,
.
NumToASCII:

174
10. DOS

NumToASCII

= 3 2-
=
edi =
:

mov ebx,1 ; EBX



mov di,buffer ; DI
call NumToASCII ;
/
,
. ,
. :
.
, .
mov a h , 0 x 4 0 ; DOS
mov d x , d i ;
; 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 SI

call SkipNum
mov [di],0
mov
call ASCIIToNum , SI

mov edx,eax EDX
mov si,di SI

inc si
call Skipspace ;
call SkipNum ;
175
.

mov [di],0

call ASCIIToNum , S1

add eax,edx ,
EDX
mov

mov d i , b u f f e r DI
call NumToASCII
mov ah,0x40

mov dx,di
DX,
mov ex,2 5 DI
int 0x21
mov ax, 0x4c00 DOS
int 0x21

SkipSpace -

S1

:
si = ,
SkipSpace:
.again:
lodsb / AL DS:SI,
; SI
cmp a1,' ' ;
jz again ; ,
ret

SkipNum ~
: S1 =
: DI =

SkipNum:
mov di,si ;DI
. find_end_, of number
inc di ;DI=D1+1
cmp [di],'0' / ASCII-
jb .end_found ; ,
cmp [di],'9' ; ASCII- 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
test asm: 141 : eerroi
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 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 45 50
5
, .
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 / AL D S : S I ,
/ SI
a l , ' ' /
jz again / ,
dec s i
ret
, , ,
:
: \ 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 / SI
mov ax, OxBSOO ;
mov es,ax ;
mov ex,80*25 /
.repeat:
inc si / SI 1
mov byte [es:si],0x4 / 0x04
inc si / 1
dec ex / 1
jnz .repeat / .repeat, >
pop es /
pop si
pop ex
pop ax
ret
.
, ,
04:
SECTION . t e x t
call color
mov a x , 0x4c00
i n t 0x21
color:

IRQ 1:
new_handler:
push ax / AX
i n a l , 0x60 / -
cmp a l , 0x4 6 / 0x4 6 ( S c r o l l - L o c k )
jnz pass_on / , p a s s _ o n
call color /
pass_on:
pop ax
jmp f a r [ c s : o l d _ v e c t o r ]

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

182
10. DOS

CS,
.
old_vector,
.
setup.
setup:
cli ;
xor ax, ax ; AX
moV e s, 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
sti / old_vector
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 ; AX
in al, 0x60 ; -
cmp al, 0x4 6 / 0x4 6 (Scroll--Lock)
jnz pass_on ; , 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 / SI
mov ax, OxBSOO /
mov es,ax /
mov ex,80*25 /
.repeat:
inc si / SI 1
mov byte [es:si],0x4 / 0x04
inc si / 1
dec ex / 1
jnz .repeat / .repeat, >
pop es /
pop si
pop ex
pop ax
ret
old_vector dd 0
initialize:
call setup

mov ax,0x3100 DOS:

mov dx,initialize :

initialize
shr dx,4 16
inc dx 1
int 0x21

setup:
cli ;

184
10. DOS

, AX
mov e s , a x

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
sti old_vector
ret

:
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 MessageBoxA ;MessageBoxA
IMPORT MessageBoxA u s e r 3 2 . d l l ; u s e r 3 2 . d l l
EXTERN ExitProcess /ExitProcess
IMPORT ExitProcess kernel32.dll ; - 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. ^^^ (^ai J
EXTERN MessageBoxA ;MessageBoxA
IMPORT MessageBoxA user32.dll ; a u s e r 3 2 . d l l
EXTERN E x i t P r o c e s s ; E x i t P r o c e s s
IMPORT 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
SECTION CODE USE32 CLASS-CODE /
..start: ; ,
/
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
1
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

1 argc (dword)
argv[0]
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 NR_read 3
#define NR_write 4
#define NR_open 5
#define NR_close

#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 1
, , , 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 ; e x i t
mov e b x , 0 ;
i n t 0x80 /

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 WRITE(2)
NAME
write write to a file descriptor
SYNOPSIS
#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 12.1

-o<name> () <name>
-S

Id ,
_start.
Hello, World! 12.1.

12.1. ^ World!
SECTION .text
global _start ; .
;
_start:
mov eax,4 ;
; - write
mov ebx,1 ; STDOUT 1
mov ecx,hello ;
mov edx,len ; Hello, World!
;
int 0x80 ;
mov eax,1 ; 1 exit
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-).
12.2

0_RDONLY
0_WRONLY
0_RDWR
0_CREAT ,
0_TRUNC
,
0_APPEND
( NFS)
1 0_LARGEFILE 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 ; ?
;? EAX
;
add , ;
; ,
;
; + 1
compare_next:
dec ecx ;
cmp byte [ecx],'a' ; < '',
/,
jb no_conversion ;
cmp byte [ecx], ' ' ; > 'Z'
ja no_conversion ;
sub byte [ecx],0x20 ;
; 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:
s y s _ e x i t 2 55 ; 2 5 5
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
xor edx,edx ; EDX
.next:
inc edx EDX path
lodsb AL, ESI
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 = 0x00000000 = 0x00000000 edx = 0x00000000
esp - OxBFFFFSCC 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 = 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
step [n]
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:
# s t d o u t :
movl $len,%edx # -
movl $msg,%ecx # -
movl $l,%ebx # ~ STDOUT - 1

215
.

movl $4,% # 'write'


i n t $0x80 #
# :
movl $0,%ebx #
movl $ 1 # 'exit'
i n t $0x80 #

12.16.

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

12.17.
NASM :

-V

-
-f <f mt> <fmt> (. 9)
-fh
- <name> <name>
1 -1 <path>

216


,

-?

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 55 p u s h ebp
080483F5 895 mov dword ebp, esp
080483F7 8318 sub dword e s p , 0x18
080483FA 83C4F8 add dword esp, OxfffffffS
080483FD 68000000 p u s h Oxaa
08048402 655 p u s h 0x55
08048404 E8C7FFFFFF c a l l near +0xffffffc7
08048409 83410 add dword e s p , 0x10
0804840 890 mov dword e a x , eax
0804840 8945FC mov dword [ebp+Oxfc], eax
08048411 89 m.ov dword e s p , ebp
08048413 5D pop ebp
08048414 retn
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 55 push ebp
080483D1 895 mov d w o r d e b p , e s p
080483D3 8318 s u b dword e s p , 0x18
080483D6 84508 mov e a x , d w o r d [ e b p + 0 x 8 ]
080483D9 8550 mov e d x , d w o r d [ e b p + O x c ]
080483DC 8D0C02 l e a ecx, [eax+edx]
080483DF 894DFC mov d w o r d [ e b p + O x f c ] , e c x
0804832 8B55FC mov e d x , d w o r d [ e b p + O x f c ]
0804835 89D0 mov d w o r d e a x , e d x
0804837 07 jmp s h o r t +0x7
0804839 8DB42600000000 lea esi, [esi+OxO]
080483F0 89 mov d w o r d e s p , e b p
080483F2 5D pop ebp
080483F3 retn

221
.

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

1 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 1
|0xBFFFF868 = EBP + 0xC 1
1 0xBFFFF86C - 0xBFFFF84 main
10xBFFFF888 : main
0xBFFFF88C 0xBFFFF8C8 - LIBC
0XBFFFF890 0400392

, (dword)
,
. , d,
.
main. addit,
main main.
addit EDX
:
080483D6 84508 mov , dword [ebp+0x8]
080483D9 8550 mov e d x , dword [ebp+Oxc]
0x8?
- ,
8 ( -)
. ,
8 + 4 = 12 .
. 13.3 ( ),
.

222
13.

13,3

1 [ - 4] d
[ + 0]
[ + 4]
1 [ + 8]
[ + ]

addit 0x55. ,
, .
addit ,
EDX, LEA.
. addit -
:
080483F0 89 mov dword e s p , ebp
080483F2 5D 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 . plus
extern printf . printf
global printit printit
proc printit printit,
%$what arg what
mov eax,[ebp + %$what]
add eax,[plus] ^ plus
push eax
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 : integers-
f 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 FAR-
% include mi s/1.mac
extern plus
plus
global addit addit
proc addit addit
-
%$what arg
what
xor dx,dx DX
mov ax,[bp+%$whatj = what
add ax,[plus] = - p l u s
adc dx,0
endproc

arg
. 16- 2 .
longint ,
, 4 .
, :
C S :007 55 push bp
C S :0077 895 mov bp, sp
CS :0079 31D2 xor dx,dx DX-0
CS :007 84606 mov ax,[bp+0 6 1-

CS .007 03065200 add ax, [0052]
p l u s
CS 0082 81D20000 adc dx,0000
CS 0086 89 mov sp, bp e n d p r o c
CS 0088 5D pop bp e n d p r o c
CS 0089 02 00 retf 0002 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
.

1 643 2B f 1 086 56 12? 81 u 1 172 ^ 1 215 D7 \\


0&i. 01 Q 044 2C J" 087 57 13 82 e 173 \ 216 D3 i
002 82 Q 845 2D 08g 53 131 83 a 174 < 217 D? -*
003 ^ 846 2E , 089 59 132 04 a 175 216 DI) 1
004
04 047 2F / 1 090 5
J
133 85: a 176 219 1(3 I
005 85 ^ 848 36 0 091 SB
z[ 134 86 a 177 1
^
i 228 DC
006 06 049 31 1 1 092 5C \ 1 135 87 9 178 2 221 DD 1
007 07 050 32 2 j 093 5D ] i 13& 88 i 179 \ 222 DE 1
008 86 851 33 3 ! 094 5E *

137 89 180 4 \ 223 DF


009 09 052 34 4 ! 095 5F 138 8 181 5 \ 224 9 I
:-1 \ h 9 WA ^

W'i KH 1 1. \\ /!! K1
Oil - 854 36 6 097 61 d. 140 8C 1 183 7 1 226 E2
012 ? 55 37 7 098 2 141 8D i. 184 8 1 227
013 811 856 38 8 099 63 142 8E 185 9 ^1 228 4
014 81- ^ 857 39 9 100 G4 d 143 o r 8 106 II 229 5
015 OF * 058 f 101 &5 e 144 90 187 230 6 J.I
016 19 K 859 3B f i02 66 f 145 91 ffi 188 il 231 7 X
017 11 -i 060 3C < 103 7 9' 14b 92 ^ 189 232 5
1>! t11 61 M) = 14 hK h 147 v:^ n 19 J ''
019 13 862 3E > 105 5? i 143 94 191 BF 234
020 14 41 063 3F ? 106 & J 149 9 192 1 235
021 15; 5 Plh4 4 P 107 ISR 1< IFirt 9fi 193 .1 JL 26 F.r.
022 16 ^ 865 41 108 6C 1 151 97 u 194 2 237 E D Jtf
023 17 t 066 42 109 &D PI 152 98 195 \ 238
024 18 r 867 43 110 6E 153 99 196 4 239 E F II
025 1^ i 866 44 D 111 Gr G 154 9ft 197 5 \ 248 F9 =
026 Ifi 069 45 E 112 70 P 155 9B ^ 198 6 1= 241 F1 +
027 IB t- 878 46 F 113 71 Cr 156 9C 199 7 \\ 242 F2 >
028 1 71 4? G 114 72 r 157 9D V 200 F3 L
1 HZV 1
1-

f* nvv 4 H 11b / IfiK 9b .. >?1 Li


CV II
243
V.44 4 f
^
030 IE i. 873 4? I 116 74 I 159 9F X 202 Cfi 245 F5 J
031 IF T 074 4 J 117 75 u. i 1&9 203 11 246 F6 -f
032 28 875 4B 118 76 V 161 fil 1 204 \\ 247 F7 ~
033 21 876 4C L 119 77 %.' 1G2 2 205;
24G F9 "
034 22 " 077 4D M 120 78 X j 163 ft3 U. 206 CF 249 F9 -
X.
035 23 ^ 878 4E N 1 I2i 7 v 1 154 A4 207 CF 258 FA -
036 24 ^ 879 4P 0 122 7ft z 1&5 ftG " 20 II 251 FD NT
037 25 OSO 58 p ! 123 7B < 1&& 6 209 1 252 FC
038 26 ^ 1 881 51 Q 1 124 7Z 1 167 A7 ~ 210 2 ^ 253 FD =
f
039 27 082 52 125 7D > 1&9 8 211 L1 254 FE 1
4 y.}i s:< i;!fi 7h " IhV flV 7.1 7. D4 'J\^} KK
041 25 > 884 54 127 7F '^ 179 ftft 213 5 Y
042 2fi 085 55 128 171 Ji 214 6 1
*^_J JJ J 1
. 15.1. ASCII ( )

232

Вам также может понравиться