Sei sulla pagina 1di 190

-

(. . .) -
.

http://www.stolyarov.info


NASM UNIX, 2011 ,
, - .
, , -
, , .
-
,
, .
, ,
, , -
,
, -
.

, , -
:
1. ( ) -

, -
;
2. ,
, -
: (1)

PDF, , , ,
,
; (2)
, -
, ,
,
,
. .
-
. , : - -
, , ,
- ; -
-, , ,
, ;
, , -
; -
; - -
. , ( ) -
, , ,
, . .
, ,
.
. .

-
.
. .



NASM
Unix

2011
004.431.4
32.973.26-018.1
81
. .
81 NASM
Unix: . .  2- .  .: , 2011.  188 .: .

ISBN 978-5-317-03627-0

-
( ) -
NASM i386 -
Unix ( Linux
FreeBSD) .
, -

, i386, -
-
, . -
NASM,
; -
, , .
, -
.
004.431.4
32.973.26-018.1




NASM UNIX
-
29.03.2011 .
60x90 1/16. ... 11,5. 100 . 132.
 
00510 01.12.99 .
11992 -2, , ,
. .., 2- , 627 .
. 939-3890, 939-3891. ./ 939-3891

ISBN 978-5-317-03627-0 c . . , 2011



-
; -
, -
, -
,
( ).
-
.
, -
, -
. : ,
, , .
,
,
. , -
 ,
,

,
.
,
-
. , ,

. -

, setjmp
longjmp , -
- -
, ++ . , -
,
, ,
 -
.
,
-
, ,
-
. -
, :
. , ,
, ,
, -
, -
3
,
.

8086, -
16- , MS DOS
tasm masm.
. ,
IBM PC
- ,
; , 80x86,

, -
. , 80x86 -
DOS , ,

.
,
. , MS DOS -
1990 ;
,
, .
, 32- (. . -
80386) ,
1 -
 ,
.
32- (. . -
i386), ,
, MS DOS: -
MS Windows, Unix.
, (
)
. -
,
,
, , ;
, -
Windows, .
, Unix, -

, (at) -
Unix, -
1 , , ,
, -
; ,
, -
. .
4
(, Linux FreeBSD) -

.
. -
, x86 -
 AT&T Intel.
-
-: , , Intel -

mov eax, [a+edx]

AT&T :
movl a(%edx), %eax

Unix AT&T,
.
,
Intel, - , AT&T -
() ,
. ,
MS DOS
; Intel -
( , , )
. Unix ,
Intel: NASM (Netwide Assembler), -
, FASM (Flat Assembler),
.
.
NASM,
; -
.

,
, ( ) . -
, , ,
, -
. , ,
NASM
Unix. , -
, , :
Unix ? XXI ! ,
.
, , Unix 
5
, -
, ( ). Unix
, -
. ,
,
 ,
,
.
,
.
, -
. ,
,
( );
-
2 , , , -
. , ,
,
.
?
? ;

, -
,
. -
, ,
, . -
, ,
, . , , -
, , ++, ,
3 . -
( ),
.
, , -
, - ,
, -
,
. -,
: - , -
, -
. -
: ,
, , ,
2 , 256 -
8 .
3 ++ : ,
list<string>, ?
6
, , ,
.
,
. , -
, - ( )
; , ,
, , ,
, .
-
. i386
- . Unix,
, i386,
Unix , -
.
, , -
Unix.
-
- , ,
Unix , .
, ,
: -
, , ,
.

, -
, -
.
, -
.
, 2010 , -
, -

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

7
1.

1.1.


.
( ) ,
. -

(
), -
.
,
, .
(  ) ,
, -
.
2n , n 
; , , -
28 = 256, , , 0
255.  ,
. , -

-
. , little-endian 1 , ,
, ,
. ,
big-endian, : ,
1 big-endians little-endians

.
.
-
.
8
. ,
, little-endian, -
.

( ), -
: ,
,
. , -
( , )
 , -
,
.
, ,
. -
.
, -
41, 4E, 4E 41 (  65,
79, 79, 65). , ,
:

1095650881;

(. . ) 12.894105;

, 'ANNA';

, , ; -
, i386 ,
inc ecx, dec esi, dec esi, inc ecx.
, .

 , -
; -
, ,
,
.
,
; -

, 2 .
2 ;
, Pentium , , -
,
SPARC, Sun Microsystems,
, ,
9
,
( ), . -
. -
, , ,
; , Pentium 32 ,
.
. Pentium -
x86, -
( 80286) 16-,
16 . , -
,
, ,
(word dword).
, -
, .
.
, , -
(-
), , .
, 3 , -
,
, .
, -
. ,
4
, .
, -
; ,
, .
, -
: ,
A,
B , -
A, . . , ,
, -
, -
, (,
).

, -
.
3  instruction pointer,
;
, .
4 -
.
10

, -
( , , -
). -
. ;

, ,

. -
, -
.
, , ,
, ; -
.
, -
(). ,
,
,
. , , , -
01 D8 ( -
 1, 216) Pentium
EAX, EBX,
EAX.
01 D8 , Pentium 
, 
(01), (D8) , (
),
( , , -
), ,
, ,
. (, ,
), EAX  0, EBX  3,
: 11 011 000
( ),
216,  D8.
-
, , ,
,
, , . , ,
-
, ,
, , -
,
11
.
,
, , ., -
, ,
.
 ,

5 . ,
, , .
. , -
,
,
.
-

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

, . -
, , -
, . , -
, , ,
01 D8, 6 :

add eax, ebx



, , add -
, -
( ,
),  (
, , ), -
.
() .
-
.
5 ,  ,
;  ,
.
6 ,
NASM, .
12
( ) , -
, 
,
,
- , . . -

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

, , -
-
; ,
-
.
,
. ,
-
( ). -
, ,
. , ,
( ).
; ,
add eax,ebx , -
AT&T, addl %ebx,%eax  ,
, , -
, ,  01 D8.
,
, , -
. , -
, -
,
, ( ) -
(, , ) -
13
,
. , ,
, , .
 ,
, -
-, -
. NASM, 
, 80x86.
; , -
. , ,
.
, -
,
.

1.2.


Unix,
;
,
Unix, -
, -
, .
-
.
, 7 , -
, -
(  ).
-,
. -
( , )
- , ,
, -
. -
(- ) -
7 , , ,
, -
, , .
14
, , ,
-, .
-
:
, -
.
-, ,
, 8 .
,
, , -
, . -
,
.
, -
, -
, -
9
.
.
, , -
. , , -
,
.

,
. -
, -
;
,
, .

. -
-

. -
. -
,
. -
8 , , , -
,
.
9 i386 , ,
,
( )
( ).
15

. ,
. ,

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

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

. , ,
32- ,
0
232 1; ,
00000000 ffffffff. -
,
- . , -
,  -
, -
, .
; -
.
16
, ,
, ,
. ,

, ,
. , ,
: -
,
, -
,
 .
, ,
.
i386, -
, . ,
; , -
Unix -
,
.
,
.
,
Linux FreeBSD; -
, /

, -
. -
,
.

1.3.

,
, -
, , -
, -
(/), -
0 1. , ; , 10 ,
,
( ). ,
10 , -
.
17
. 1.1.

, -
, .
, ,
N , N +1. -
, ,
 -
( :
).

1.3.1.
-
11 ();
8 ( ), 16 ( ), 32 ( )
64 ( ).
, .
, , ,
-
: ,
, ( ),
. , -
(. . 1.1).
: 00000.
, 00001, 00002, 00009.
,
, -
,
00010, ;
11
, ; -
( ), -
,
.
18
: , ,
. 00019
00020, , 00099
, -
, ,
: 00100.
, ,
: -
99999, ;
,
,
, .
, ,
100000 ( ), ,
. .
, ,
: ;
,
,
.
-
, . -
, - -
, .
: 00000000. , -
: 00000001. , (-
) ,
, ,
, :
00000010; 2. 00000011,
00000100 ; -
, : 11111111;
255 (28 1).
, 256 ,
; .
, -

N k
, , -
N k 1; , -
, 99999 = 105 1,
, -
, 28 1 = 255.
19
1.3.2. ;
, ,
. , - -
, , -
. ,
,
, .

, , -
. , ,

, -
.
12 . -
,
-
:
-
( -
),
. , -
: -
,
, , ,
.
, , -
.
, ,
,
. -
.
99999; , 99999 ,
00000, . -
, :
,
, (, 100000 1 = 99999),
. :
(00000000) ,
: 11111111; ,
12  two's complement, ;
, , -

.
20
. -
-1
, .
, , 11111111 -
-1, 255;
, 1111111111111111 , -, -1,
65535, .

, , -2
11111110 ( 254), -
-3  11111101 ( 253), .
, -
,
() -
()
256: 255 256 = 1, 254 256 = 2 . . ( 256
28 , -
;
2n , n  ). ,
, ;
, , 00000001 ,
1, -255. :
,
, -
1, .
, -
;
10000000, -128.
, 01111111; ( , -
)
, ,
127. , -
.
011...11 100...00
13 -
, ,
: .
, - -
. -,
, () . -, -
.

13 .

21
-
, -
, .
, 5
: 00000101. -5,
, 11111010; -
11111011, -5. -
: -
-5, 00000100, ,
00000101, 5, . -
, ,
inv. +1
: 00000000 11111111 00000000.
-128 ( -
) , , -
inv. +1
: 100000000 01111111 10000000.
-
,
100...00 .

1.4. i386

1971 Intel ,
MCS-4. , Intel 4004, -

, . ., , -
.
14 .
Intel Intel 8008, 1974 -
 Intel 8080. , 8080
, , -
8008,
8080.
Intel 1978 16-
Intel 8086. Intel 8088
, -
( 8088 8 , 8086  16
). 8088 IBM PC,
15
14 , , -
.
15 IBM- -
; , -
, , IBM- ,
22
, IBM PC-
IBM- .
8086 8088 -
, -
-
.
80186, 1982 .

,
, -
. -
, -

. ,
; ,
-
. 8086, 8088 80186 20-,
1 Mb .
1982 80286,
16- . -
(protected mode),
, -
, , ;
,
, -
. -
, , ,
16 Mb.
- -
, 32-
Intel 80386, i386. ,
1986 ,
, , 32 , -
,
32 , 4 Gb -
. -
,
, . -
i386 IBM- , ,
. , i386 -

. ,
.
23
,
. , 808680286
AX, BX, CX DX 16 ; -
i386 , -
32 EAX, EBX, ECX EDX ( E
extended, . . ), 16 -
(, AX, BX,
CX DX). -
8 , 16 32 , . .
x86 2003
: , -
, -
. 2003 AMD , -
64- ,
,
32- , -
i386.

1.5.


, , -
( , -
), . ,
: -
, , , -
, ,
, 
, , , -
. .
,
; -
,
.
,
. -
; - , -
. :
16 Hello. . 17,
16 . . , , , -
;, ,
-
24
, -
,
,
. , -
,
.
,
; -
. , :
%include "stud_io.inc"
global _start

section .text
_start: mov eax, 0
again: PRINT "Hello"
PUTCHAR 10
inc eax
cmp eax, 5
jl again
FINISH

- . -
%include; -
,
 stud_io.inc. -
PRINT, PUTCHAR FINISH,
, -
, .
, %include, -
, .
, %include -
, .
.
, -
. , ,
-
,  ,
.
%include : -
.
, -
-.
25
%include global;
, .
section. -
Unix , -
, (. . ,
)  , , -
, ,
.
,
 -
, 
. .text, .data
.bss. .text,
-
.
.

_start: mov eax, 0

, mov ,
;
mov , -
, ,
, .
0 () -
EAX17 . , EAX,
, ,
Hello; ,
, .
,
EAX; _start: ?
_start (
) .
, ,
, .

17 , ,
, : xor eax, eax, -

;
. , -
.
26
mov eax,0 18 ,
-
(  ,
). ,
; ,
, , -

( , . . 11).
, -
, . ,
, , -
, -
. , ,
, . , -
, 19 , ,
18b4a0f0. ,
,
.
.  -
(), -
,  -
. _start .
(, , ,
), -

, -
, , ( )
()
. , _start
, ,
,
mov eax,0 ( b8 00 00 00 00).
, -
. -
,
.

18 ,
: b8 00 00 00 00,
, EAX.
( ) , ;
0.
19 , , .

27
. , -
. , -
, ; NASM
. , ,
. , -
( ,
), , -
(). _start
, .
, -
_start .
? , _start  -
, ,
,
; ,
_start , .
:
again: PRINT "Hello"
, again 
. again - . , -
, Hello
; .
PRINT , "Hello" 
. , ,
stud_io.inc. , -
,
Hello.
, PRINT
. -
, : PRINT 
- ,
.
, , . -
,
( , -
23 Linux
15  FreeBSD) ,
. -
, ,
( write). , -
PRINT,
, , .
28
, , stud_io.inc,
; ,
.
.
PUTCHAR 10
, PUTCHAR
.
10; ,
,
. ,
,
. ,
, , , -
, .
:
inc eax
inc,
1. EAX. -
, EAX ,
Hello.
, PRINT
PUTCHAR, Hello,
, . ,

( 40, 64).
:
cmp eax, 5
-
cmp to compare  .
EAX 5. -
, -
. , , -
,
:
jl again
jl ( Jump if Lower)  -
, , -
,
29
, , EAX ,
5. , Hello
, ,
, ( ) -
, again.
, , -
jl , , ,
. -
, Hello 5 ,
. -
, , , .
:

FINISH

FINISH ; -
,
.

global _start

global  ,
, ( -
, ; -
).
_start. , , -
, ,
-
. ,
, global.
, : , , -
again, ,
FINISH.
EAX 0, Hello,
,
EAX, 5; EAX
, , (
again),
.
, , ,
Unix, - -
, -
30
, 20 .asm 
, .
, hello5.asm.
-
.  NASM, , -
, .
 ; , -
,
, -
, .
, -
; -
, .
-
, ()
. , -
: ,
.
_start; ,
global ,
,
.
, NASM:

nasm -f elf hello5.asm

-f elf , -
ELF  -
21 . -
hello5.o, .
, ld:

ld hello5.o -o hello5
64- , -
, 32-
; , GNU ld Linux -
:
20 Windows, , , , .asm 
. Unix ,
, .asm
.asm.
21 Linux
FreeBSD.
; .
31
ld -m elf_i386 hello5.o -o hello5

-o (hello5,
). , ./hello5.
, Hello.

1.6. stud_io.inc
, stud_io.inc, -
, , ,
. stud_io.inc (
Linux FreeBSD) A,
, . ,
, PRINT,
PUTCHAR FINISH. stud_io.inc
GETCHAR, .
PRINT ; -
,
.
PUTCHAR .
, -
, ;

 AL, AH, BL, BH, CL, CH, DL DH. -
PUTCHAR ! ,
,

.
GETCHAR (
).
EAX; ,
AL, EAX . -
(
, Unix Ctrl-D), EAX
-1 ( FFFFFFFF, 32
).
.
FINISH . -
, -
, ;
0, , 1,
.

32
2. i386

2.1. i386

-
,
. ( )
,
. i386
, -
1
. , -
- .
(CS, DS, SS, ES, GS FS)
. ,

, ,
 , -
. ,
, .
i386  32- -
EAX, EBX, ECX, EDX, ESI, EDI, EBP ESP.
. 24, E extended,
, -
i386. -
x86 32-
( 16 ), , -
E, , , -
16- AX, BX, CX, DX, SI, DI, BP SP,
32-
.

1 x86 , -
.
33
31 16 15 8 7 0 15 0

EAX AH AL CS
AX
15 0
31 16 15 8 7 0
SS
EBX BH BL
15 0
BX
DS
31 16 15 8 7 0
15 0
ECX CH CL
ES
CX
15 0
31 16 15 8 7 0

EDX DH DL FS

DX 15 0

31 16 15 0
GS
ESI SI

31 16 15 0
EFLAGS31 16 15 0
EDI DI
FLAGS
31 16 15 0

EBP BP

31 16 15 0 31 16 15 0

ESP SP EIP IP

. 2.1. i386

, AX, BX, CX DX
, . , AX -
AL,  AH ( low
high). BL, BH, CL, CH, DL
DH,
BX, CX DX.
.
, , -
, , -
. , AX A
accumulator; , IAS
, , ( )
, -, ,
, -, , .
AX EAX
(. 2.3.4).
BX B base,
32- ( 16- -
).
CX C counter (). ECX, CX,
CL ,
( ) .
DX data ().
EDX ( DX, )
( -
34
, ) (
, 
).
SI DI , , source index
destination index ( ). ESI EDI
, , ESI
- (, , -
- ), EDI -
( , ).
BP base pointer ( ). ,
EBP
, .
, SP stack pointer ( ).
ESP ,
,
.
, , ,
ESP .
-
EIP FLAGS.
EIP, extended instruction
pointer, , -
, -
.
EIP -
(,
),
, . -
, EIP IP,
, 32- -
, .
FLAGS  -
, ,
.
() , -
.

; -

. ,
:
,
, , -
35
;
. :

ZF  (zero ag). -

: , ZF -
.

CF  (carry ag). -
-
, ,
,
, -
, (. 1.3.1).
.

SF  (sign ag).
,
(. . 21).

OF  (overow ag). ,

(. . 21).

DF  (direction ag). -
STD CLD;
,
, -
.

PF AF  (parity ag) (auxiliary


carry ag). .

IF TF  (interrupt ag)
(trap ag). ,
.

i386;
i386 , ,
EFLAGS,
, .
36
2.2. , mov
2.2.1. .

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

 32 ( , ,
, . 16).
, -
,
.
,
. , -
,
; , -
(
). -

, , ;
, , -
.
,
- -
.
-
, -
. ,
, , -
, , . ,
, , -
. , -
-
;
37
. :
-
,
-
. ,
.
, , -
 ()
. ,
,
 ,
.
.text (
).
,
, -
. , -
; , -
,
. -
, .data2
, ,
. -

BSS 3 .bss; ,
,
. BSS -
. -,
, ,
; BSS
. -, -
BSS ,
.
, , -
(heap).
, ,
Linux
brk, .
FreeBSD -

2 Data (.)  ; .
3 BSS Block Started by Symbol, -
.
BSS Blank Static Storage.
38
mmap, , , ,
.
 ;

. -
, , ;
-
: , -
Linux ,
.
,
( BSS, -
).
, -
,
. -
.
,
.

2.2.2.
-
i386; , -
. , , ,
.
-
 (-
), -
. -
( , , -
EIP) -
,
. ,
-
,
( .text)

, , , -
.

, , .
,
39
, , , ,
.
,
, (
.text), , -
( .data), , ,
,
, , ,
( .bss).
, -
(, ) , -
( ), -, ,
, , -, -
, , ,
. -
,
, -
section; ,

section .text

, -
,

section .bss

-
.

, , .

, -
:
. -
,
,
.

, -
, , , .
-
, , -
. -
resb, -
40
4 , -
(, )  resw, -
( ) resd;
( ) , -
, . -
, .
, :

string resb 20
count resw 256
x resd 1
, string, 20
( , ,
); count
256 (. . 512 ), ,
, - ; , x -
, ,
.
, -
, , ,
-
. -
; , . -
db,
 dw  dd.
,

fibon dw 1, 1, 2, 3, 5, 8, 13, 21
( 16
), 1,
 ,  5 . .

fibon.
, -
, . -
NASM :
h (, 2af3h), $ ($2af3),
4 , ,
,
; i386,
, . word
 ,
.
41
0x, (0x2af3).
$ , $ -
, , , -
0 (, $0f9 $f9).
h: ,
a21h , .
, 0a21h. , -
2fah ,
.
o q (, 634o, 754q). ,
b (10011011b).

. -
 . ,
, -
. -
ASCII , ,
. , -
A ASCII 65, 0
()  48, + ()  43,  32. -
,
, ; -
, 10 ,
.
ASCII 1 127,
5 .
-
, .
, -
(, . .),
, . ,

fig7 db '7'
5 , ASCII , -
 () , ,
, a  a,
ASCII-. , ASCII,
: 128 255, -
- ,
(,
, );
( ) ,
, .
42
, 55  ,
fig7.
, , :
welmsg db 'Welcome to Cyberspace!'
welmsg 16 -
( , -
). ,
(), ,
:
welmsg db "Welcome to Cyberspace!"

; -
. , So I say: "Don't panic!"
:
panic db 'So I say: "Don', "'", 't panic"'
-
, , -
, . ,
, -
, . ,
, -
, .
,
db, dw dd, -
, .
-
.data (
section .data), -
.bss.
: -
,
. .bss, ,
.data , -
; ,
.bss. , .data
db "This is a string"
16 ( -
"This is a string"), -
.bss
43
resd 16

, ,
.

( .text), , -
.
, (- , 
, ),
,
,
. , -
. ,
!
-
. , ,
, , ,
.
, -
. (resb, resw .)
.data, ,
; , , -
,
. -
:
, ,
 , -
, ?! ,
, . -
, BSS -
:
, .
, , -
, , , ,
.

2.2.3. mov
-
 .
mov ( move).
,
, , ,
, ,
. .
44
, mov , . . , -
(  mov)
, . -
, , 
, . , ,

mov eax, ebx
EBX EAX. , -
mov , -
. -
, .

2.2.4.
, , -
mov:
mov eax, ebx
mov ecx, 5
,
,
( 5).
, . -
,
; , -
.
, -
, ,
. mov -
,
,  , -
.
,
 , -
. ( -
) , -
. , -
NASM -
,
. , -
;
, , . ,
:
45
section .data
; ...
count dd 0
( ; ),
4 , count,
0.
section .text
; ...
mov [count], eax
mov EAX
, count, , ,

mov edx, [count]


, , count
EDX.
, ,

mov edx, count


, ( count),
. 27, , 
. , count -
, 40f2a008, -
 ,

mov edx, 40f2a008h


, mov
, . . EDX
40f2a008, , -
. ,
,
, -
, (
EDX).

2.2.5.

. -
, -
. , ,
46
-
: , -
( )
(
) . 

.
, -
, , ,
, , -
. ,
- -
: - (, )
,
. -
( ,
).
i386
, . -
 ,
. ,

mov ebx, [eax]

EAX, -
, , 4
4 EBX,

mov ebx, eax

, ,
EAX EBX.
. -
, ,
'@'.
,
( , . 25)6

6 .
, . , ,
,
.
-,
.
47
section .bss
array resb 256 ; 256

section .text
; ...
mov ecx, 256 ; - -> (ECX)
mov edi, array ; -> EDI
mov al, '@' ; -> AL
again: mov [edi], al ;
inc edi ;
dec ecx ;
jnz again ; ,
ECX ,
( 256,
,  ),
EDI,
array, -
, , ,
.
, -
. -, -
, , 256,
. -, , -
AL,
, .
, -
, -, , ;
, , cmp,
. ,

, .

2.2.6.
, -
;
, -
.
, -
(, ) -
.
, ,
, , -
. -
48
EAX
EAX
EBX
EBX
ECX 1
ECX
CONSTANT
+ EDX
ESI
EDI
+ EDX
ESI
EDI
*
2
4
8
EBP
EBP
ESP

. 2.2.

i386 -
,
.
,
(, , -
, ), , -
, , ,
1, 2, 4 8
. ,

mov eax, [array+ebx+2*edi]

(
array) EBX
EDI, -
, 4 -
EAX. ,
, , -
(, ,
).
, -
. ,
, 2, 4,
, 1, 2, 4 8, , , -
,
.
. 2.2; , , -
, ESP,
, ,
.
49
,
, -
. -, .
-, , :
. ,
3, 5 9: , , [eax*5], -
[eax+eax*4]. ,
[eax+ebx*5], , -
.
, -
, , -
, , 10 , 15 -
. matrix,
:
matrix dd 10*15
N - -
N - (
N , 15 4 = 60 ),
, , EAX,
(, EBX)  -
[matrix+eax+4*ebx]
, .

2.2.7.

, :
1. , ;
2. , -
/

3. , ,
/
.
, . -
, ,
mov,
, ;
,
, .
50
, , , -
. , , mov,
.
, , x
y, :

mov eax, [x]


mov [y], eax

mov [y],[x] , -
:
.
mov -
, mov :

1.

2.

3.

4. ( )

5. ( ) -
.

.
, ,
; -
, , ,
.
, mov -
(, AL
CX); , -
, , -
(
). ,
mov , , -
(
), , -
, ,
, , ,
. , ,

mov [x], 25 ; !!!


51
: ,
25, 25 -
25. , , ,
, . -
, ,
.
;
byte, word dword, , -
, , (. . 1, 2 4 ).
, , 25 -
, x,

mov [x], dword 25

mov dword [x], 25


. , -
, -
. ,

mov eax, 2
mov eax, [x]
mov [x], eax
mov [x], al
,

. ,

mov eax, 2
mov eax, x

,
(, x ,
).

2.2.8. lea

.
lea ( load eective
address). ,
( 2 4 ), 
52
. ;
, , ,
. -
 , 16
. ,
lea eax, [1000+ebx+8*ecx]
ECX, 8, -
EBX 1000, -
EAX. , . -
,
(. . 2.2 . 49).
, lea ,
,
.

2.3.

2.3.1.

add sub. -
, ,
, , ; -
( , ).
, ;
. -
mov, add sub
.
,
add eax, ebx
EAX,
EBX, EAX.
sub [x], ecx
x,
ECX,
.
add edx, 12
12 EDX,
53
add dword [x], 12

x;
,
(. 2.2.7, . 51).
, add sub -
, -
7 . add
sub OF, CF, ZF SF (. . 36),
.
ZF , -
, ; ,
, -
.
SF ,
, . -
, ;
, -
SF .
OF , , ,
-
, , , -
, . . , , ,
.
, CF , ( ) -
, -
. OF -
( -
, ).
.
, -
, .

; -
, . . , , ,
. ,
 ; -
,
.
7 1.3.1 1.3.2;
,
,
.
54
2.3.2.
-
, , ,
.
i386 adc sbb.
add sub, ,
(CF) -
. adc
, sbb, ,
. -
, , CF,
.
. 64- , -
EDX ( 32 ) EAX (
32 ), EBX ECX.

add eax, ecx ;
adc edx, ebx ; ,
, -

sub eax, ecx ;


sbb edx, ebx ; ,

2.3.3. inc, dec, neg cmp


-
, .
inc dec, -
(
) , ,
. ZF, OF SF, -
CF. ,
:
, , -
.
neg, , ,
.
; , ZF, OF SF
CF, .
, cmp ( compare  )
, sub, ,
55
.
, .

2.3.4.
, -
8 ,
-
. , -, ,
i386 -
.
, -
, , -
,
. -, ,

-, .
-, .
( )
, , ,
, , -
, :
. : -
1,
( , -
n m + 1, n m 
),
, .
, ,
: . -
,
( )
.

9 , -
,
, .
, ,
8 , , , -
 .
9 , : -
imul , -
: ,
.
56

.
()
8 AL AX AX AL AH
16 AX DX:AX DX:AX AX DX
32 EAX EDX:EAX EDX:EAX EAX EDX

2.1. -

, -
AL, AX, EAX,
 DX:AX EDX:EAX (,
A ;
EAX, . 34).
mul,
 imul. ,
( )
AL ( ), AX (
), EAX ( ), -
AX ( ),
DX:AX ( ), EDX:EAX
( ).
(. . 2.1).
mul imul CF OF , -
( -
), CF OF
. -
mul imul (
,
-
).
( ) -
div ( ) idiv ( ). -
, , .
(1, 2 4 ) -
AX, DX:AX
EDX:EAX, AL, AX EAX,
 AH, DX EDX, (. . 2.1).
( -
 ,   ).
57
, imul, -
, ()
.
.
,
div idiv 0. , -
, , , ,
. -
, ;

. , -
: , EDX 10h,
EAX  , 0, ( -
1000000000, 236 ), , 2 ( , , EBX,
32-), (235 ) 32 -
, .
4.2.

2.4.

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

2.4.1.
i386
, ,
.
1. (far)
, . -
Unix -
, (-
, , -
), -
: .
2. (near)  -
;
58
EIP.
, -

.
3. (short)
, , , -
127 128 .

, .
-
, short near (-
, , far, ).
, ,
near, ,
short.

, ,
.
jmp ( jump, -
).
, ,
. jmp -
, , . -
, ( ),
. , , (
, ),
( , -
); , -
, . -
:
jmp cycle ; cycle
jmp eax ; EAX
jmp [addr] ; ,
; , addr
jmp [eax] ; ,
; , ,
; EAX
,  .
, , -
, ,
short:
59
mylabel:
; ...
;
; ...
jmp short mylabel

,
, (, GETCHAR)
, .
: ,
:

file.asm:35: error: short jump is out of range

( 35)
c short.

2.4.2.
,
, -
.
, ,
: x86 -
, . i386
, ,
; ,
.
-
, ()
(). J (
jump, (, Z ZF)
, , N ( not),
.
. 2.2. ,
. 36.

(, cmp,
. . 55). ,

cmp eax, ebx


jz are_equal

EAX EBX
, are_equal.
60


jz ZF=1 jnz ZF=0
js SF=1 jns SF=0
jc CF=1 jnc CF=0
jo OF=1 jno OF=0
jp PF=1 jnp PF=0

2.2.

2.4.3.
, -
: , , ZF.
, , , a < b?
, ,

cmp a, b

( a b , ,
).
 ,
a b .
, , -
.
a b  , : -
a b a < b ,
(SF, sign ag) , -
js jns.
(, 32 , 32-
), !
SF ,
OF (overow ag). , a < b -
: SF=1, OF=0 ( ,
), SF=0, OF=1 ( -
, ,
). , ,
SF OF : SF6=OF.
i386 jl ( jump if less than), -
jnge (jump if not greater or equal).
, a b  .
2.3.1 (. . 54), -
OF SF
61
jump if... . -
. ab

je equal a=b ZF= 1 jz
jne not equal a 6= b ZF= 0 jnz

jl less a<b SF6=OF
jnge not greater or equal
jle less or equal a 6 b SF6=OF ZF= 1
jng not greater
jg greater a>b SF=OF ZF= 0
jnle not less or equal
jge greater or equal a>b SF=OF
jnl not less

jb below a<b CF= 1 jc
jnae not above or equal
jbe below or equal a 6 b CF= 1 ZF= 1
jna not above
ja above a>b CF= 0 ZF= 0
jnbe not below or equal
jae above or equal a>b CF= 0 jnc
jnb not below

2.3. -
(cmp a, b)

, CF (carry
ag), ,
( ), -
( ).
: a b a < b,
a b . ,
CF,
jc,
jb (jump if below) jnae (jump if not above or equal).
,
ZF, ( -
, )
cmp.

. 2.3.
62
2.4.4. ECX;
, -
; , ECX -
. ,
i386 , -
ECX, .
loop -
.
ECX,
. loop
: ECX
, ,
.
, loop : -
,
,
128 .
, , 1000 ,

array resd 1000


. -
:

mov ecx, 1000 ; -


mov esi, array ;
mov eax, 0 ;
lp: add eax, [esi] ;
add esi, 4 ;
loop lp ; ;
; -

ECX ESI
.
, -
, .
, EAX
lp , EAX
; :

dec eax
jnz lp
63
ECX:
dec ecx
jnz lp
loop lp, , -
.
ESI, :
mov ecx, 1000
mov eax, 0
lp: add eax, [array+4*ecx-4]
loop lp
. -, -
. -, add
. , ECX 1000
1 ( ), -
array+4*999 array+4*0,
4 ECX, (ecx-1).
4. 2.2.6
(
, ), NASM
4 array -
,
.
-
. jcxz (jump if CX iz zero) ,
CX . .
jecxz , -
ECX. loop,
. , , ,
ECX .
, loop -
,
(
), 232 ,
, , .
,
jecxz:
; ecx
jecxz lpq
lp: ;
; ...
loop lp
lpq:
64
loop.
loope, loopz, ,
ECX  ZF , loopne
(, , loopnz)  ECX ZF
.

2.5.

2.5.1.
, ,

, , ( )
.
-
.
and, or xor,
(, , )
, . .;
, ,
, , ,
, .
,
: -
, , ;

; -
,
byte, word dword. ()
not, .
; , ,
byte, word dword.
ZF, SF PF ;
ZF.
xor,
, ,
xor eax, eax
, . . ,
mov eax, 0
xor , (2
5 mov) . -
mov eax,-1
65
SHR 0 CF

SHL, SAL CF 0

SAR CF

. 2.3.

xor eax,eax not eax, (4


5), .
, -
, test,
, and (
), ,
.
,

cmp eax, 0

test eax, eax

2.5.2.
. -
 shr (shift
right) shl (shift left). ,
, ,  .
( -
).
, 1 31 ( -
, ,
), CL ;
. CL -
CL,
.
1 .
CF,
66
( 10 n , -
n 1), .
, , CF ,
( n ,
n+1), .
, n -
2n , 
2n . , -
,
,
. -
sal
(shift arithmetic left) sar (shift arithmetic right). sal -
, shl ( ,
). sar, -
shr, ,
, ; ,

, sar (
,  ). , -
2n
.
. 2.3.
, -
; ,
: ,
.

. ,
, ,
,
.
, i386
shrd shld, -
; ror rol;
CF  rcr rcl.
; -
.

10 , ,
, , , 32- 0, 
31.
67
2.5.3.


; -
, (,
- , -
, 0 N) -
:
;
;
N , , -
; . .
N  , N
(, 0 511,
512 , 64 ), -
N ,
, -
, . , N
: (1),
(0).
, 512 -
; , -
, 
0 511. , 16
(, 32 -
, , 32 ). ,
( -
) 0 15. 0
0 31, 1 
32 63, . .
,
0,  31. , -
17 17-
; 37  5- ; -
510  29- 15- . ,
X , -
, X 32 (
) .
,  .
div, ,
32 (25 ),
X, 32,
5 
68
. , X EBX,

. 255 (,
15, 32),
; BL (
) BH ( ).
BL BH EBX
, - ,
EDX, EBX , (
EBX ,  BL
; EDX
(
EDX, DL) BH:

mov edx, ebx


and ebx, 11111b ; 5
shr edx, 5 ; 32
mov bh, dl
, -
, -
EBX. X 
,  (
 ) . EBX X,
, , ,
BH,
EBX, EBX
,
BH; BL ,
:

shl ebx, 3
shr bl, 3
-
, .
:

section .bss
set512 resd 16
, -
set512. - ( ,
) , , ,
,
69
( ).
, , :
section .text

; ...

xor eax, eax ; eax := 0


mov ecx, 15
mov esi, set512
lp: mov [esi+4*ecx], eax
loop lp
EBX X,
, -
. -
 ,
( ) , -
.
,
. ,
.
. ,
CL
, CL. ,
:
; set512 ,
; EBX
mov cl, bl ;
and cl, 11111b ; CL
mov eax, 1 ;
shl eax, cl ; EAX
mov edx, ebx ; -
shr edx, 5 ; edx
or [set512+4*edx], eax ;
,
(0 ,
), and
( ), ,
:
; set512 ,
; EBX
70
mov cl, bl ;
and cl, 11111b ; CL
mov eax, 1 ;
shl eax, cl ; EAX
not eax ;
mov edx, ebx ; -
shr edx, 5 ; edx
and [set512+4*edx], eax ;
, ,
( , )
test. ZF: 
, , :
; , set512 ,
; EBX
mov cl, bl ;
and cl, 11111b ; CL
mov eax, 1 ;
shl eax, cl ; EAX
mov edx, ebx ; -
shr edx, 5 ; edx
test [set512+4*edx], eax ;
; ZF=1 ,
; , ZF=0 -
. ,
.
. -
, ,
, -
; 32 ,
, . -
, ECX:
jecxz. EBX,
EAX.
; set512
xor ebx, ebx ; EBX := 0
mov ecx, 15 ;
lp: mov eax, [set512+4*ecx] ;
lp2: test eax, 1 ; ?
jz notone ; ,
inc ebx ; ,
notone: shr eax, 1 ; EAX
71
test eax, eax ; - ?
jnz lp2 ; ,
;
jecxz quit ; ECX ,
dec ecx ;
jmp lp ;
quit:
; EBX

2.6. , ,

2.6.1.
, -
-
,

(. last in rst out, LIFO), . .

, -


-
, ,
, -

.

-
: -
, -
. 2.4.
,
(. . , -
) , -
( ), ,
; , ,
(. . 2.4).
, (
 ) -
;
, .
, , -
; ,
-
, 
,
72
, , , -
.
:
, -
-
. -
,
.

2.6.2. i386

, i386 -
.
; -
,
.
(. . 35), ESP, -
,
, -
; stack pointer.
, , ESP, -
, ,
. ,
ESP , -
 .
push,
. , -
word dword (
, ). -
pop,
; , -
.
push pop (
) , -
ESP. , , ,
,  (
, ) [esp].
,

mov eax, [esp]

EAX.
73
, -
:

push eax ; eax


; ... eax ...
pop eax ; eax
. ESI -
, ,
0 ( , -
) , -
;
, , ,
.  -
,
, ,
.
, -
,
. , ,
.
BX, -
(BL) ,
BX . -
.
ECX,
[esi+ecx] ( ) , ECX -
, ,
, .
, ECX
.
-
( ) ECX,
loop. ,
( ECX ), -
, .
ECX , -
 ECX EDI,
ESI ( ),
. , :

xor ebx, ebx ; ebx


xor ecx, ecx ; ecx
lp: mov bl, [esi+ecx] ;
74
cmp bl, 0 ; ?
je lpquit ; -
push bx ; bl , bx
inc ecx ;
jmp lp ;
lpquit: jecxz done ; -
mov edi, esi ;
lp2: pop bx ;
mov [edi], bl ;
inc edi ;
loop lp2 ; ecx
done:

2.6.3.
-
; pushad (push
all doublewords). ,
EAX, ECX, EDX, EBX, ESP, EBP, ESI EDI ( -
), ESP ,
.
popad (pop all doublewords).
,
pushad, , -
ESP, ( ,
).
(EFLAGS)
pushfd popfd, , -
, ( 
, ) -
, popfd .
16- ,
; pushaw, popaw,
pushfw popfw, , 32- -
16-. pushaw popaw -
, pushfw popfw, -
, , , ,
EFLAGS ,
.

2.6.4. :
-
,
75
( ); -
, , -
, ,
. , ,
. , , ,
. .
,
, -
, , -
, , -
. ,
, ,
. -
( ).

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

, 11 . ,

, -
, ,
,

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

11 , .

76
, ,
.

2.6.5.
, ,

( -
, ). i386
call; jmp,
call ( -
, , ),
( ) (-
,
). call ; -
, , ,
 (near), -
.
ret (
return). .
, 4
EIP,
, .
. ,
- -
. -
. ,
EDI,
,  ECX, -
,  AL.
, , :

; fill memory (edi=address, ecx=length, al=value)


fill_memory:
jecxz fm_q
fm_lp: mov [edi], al
inc edi
loop fm_lp
fm_q: ret
, , :

mov edi, my_array


mov ecx, 256
77
mov al, '@'
call fill_memory
256 , ,
my_array, '@' ( 64).

2.6.6.
, -
, , -
. , -
,
. ,
. -
,
. , -
: -, , -
, ,
,
, -
. , -
( - )
, , ,
.
( -
, )
,
. , -
,
, , , -
.
. -
, , ,
, , , , -
,
. , -
, ,
[esp], , ,
[esp+4], [esp+8] [esp+12].
,
[esp-4], [esp-8] . .
, ESP
, -
( ,
78
[EBP-16]

[EBP-12]



[EBP-8]



[EBP-4]

[EBP] . EBP

[EBP+8]

[EBP+12]

[EBP+16]

. 2.5.

). -
ESP -
( EBP)
, ESP
, ; -

( EBP),
.
, : -
EBP ?
. -
, , EBP ,
,
,
: -
EBP . -
, EBP ,
79
push ebp -
. , EBP
,
-
. mov ebp,esp.
EBP , -
, EBP, ;
[ebp+4], -
, , ,
[esp+8], [esp+12], [esp+16] . .
-
ESP; ,
16 , EBP -
ESP sub esp, 16;
( ) -
4 , [ebp-4], [ebp-8]
. . -

. 2.5.
, , -
,
push ebp
mov ebp, esp
sub esp, 16 ; 16
;
:
mov esp, ebp
pop ebp
ret
, i386
. , -
, ,

enter 16, 0
ret

leave
, , , enter leave -
, ,
; , -
, , ,
80
, ,
enter. enter
leave (, leave
1 ), -
;
.
.
Unix ,
.
, , -
:

( ),
-
. . 2.4 2.5
.

2.6.7.
, -
, -
. , ,
?
, , , ;

.

: -
, -
( )  ; -
call. ,
, -

( [ebp+8]). , , ,
( ) -

,
n-
. , -
, [ebp+16],
, [ebp+24].
-
,
81
( ,
).
, WriteLn, -
;
-
. ,
.
.
-
,
[ebp+8],
 [ebp+12] . .,
(,
, , ,
, ). ,
, ; ,
,
, printf, scanf ., -
, , , ,
( , ).
,
. -
, , -
( -
-
) , ,
. ,
, , -
-
( ). -
,
( 12 ),
, -
;
ESP , -
. , proc1 -
( a1, a2 a3),
:
push dword a3 ;
12 -
; , printf , ,
, execlp ,
,  .
82
push dword a2
push dword a1
call proc1 ;
add esp, 12 ;
-
(add) , .
i386 ret
( ret -
). ,
(), (
), . , , -
,

ret 12
, ret, -
, (
) ESP ( 12),
, , .

2.6.8.
, -
, ,
NASM  .
-
. , -
, -
, (,
) .

,
.
, -
,
, -
, - ( )
.
NASM
. , -
. -
, () -
. ,
83
, ( ) -
. , :
first_proc:
; ... ...
.cycle:
; ... ...
second_proc:
; ... ...
.cycle:
; ... ...
third_proc:
.cycle first_proc, 
second_proc, .
.cycle
first_proc second_proc, ,
.cycle, second_proc,
third_proc  ,
.cycle first_proc third_proc -
. ,
,
,
, .

, -

 , ,
. ,
.cycle,
first_proc.cycle second_proc.cycle.
, , ,
.

2.6.9.
, .
,  -
, .
. , -
, ,
. -
, . '?'
, '*' 
( ), -
. ,
'abc' 'abc'; 'a?c'
84
, 'a' -
'c' ( ). ,
'a*' , 'a',
'*a*' , 'a' -
. , () -
, 0, ,
1, .
,
, . -
;
, ,
, , ,
, -
. , 
, . ,
, . ,
(),  -
(); ,
.
, , -
( ) '*'.
, : -
; '?'
,
, , -
, (
) -
.
,
'*'. -

, , . .,
. . -
I, -
. (-
). -
(),
 , I. -
,
, , -
.  ,
, . 
, , I (
85
). -
, , .
-
I.
, , ,
-
:
int match(const char *str, const char *pat)
{
int i;
for(;; str++, pat++) {
switch(*pat) {
case 0:
return *str == 0;
case '*':
for(i=0; ; i++) {
if(match(str+i, pat+1)) return 1;
if(!str[i]) return 0;
}
case '?':
if(!*str) return 0;
break;
default:
if(*str != *pat) return 0;
}
}
}
. -
, -, , -,
, -
( ,
, ). :
function match(str, pat: string): boolean;
function do_match(str_ind, pat_ind: integer): boolean;
var i: integer;
begin
while true do begin
if pat_ind > length(pat) then begin
do_match := str_ind > length(str); exit
end;
if pat[pat_ind] = '*' then begin
for i:=0 to length(str)-str_ind+1 do
if do_match(str_ind+i, pat_ind+1) then begin
do_match := true; exit
end;
86
do_match := false; exit
end else
if (str_ind > length(str)) or
((str[str_ind] <> pat[pat_ind]) and
(pat[pat_ind] <> '?'))
then begin
do_match := false; exit
end;
str_ind := str_ind + 1;
pat_ind := pat_ind + 1;
end
end;
begin
match := do_match(1,1)
end;

match , , -
; ,
do_match, .
-
, match. ,
 ([ebp+8]) -
([ebp+12]); -
I;
, ,
[ebp-4].
ESI ( )
EDI ( ). , -
EAX.
: 0
( ) 1
( ).

: ,
string , , -
string+1 , .
,
, , , -
, , -
. ,
,
(),
. :

EBP, , , -
87
;
ESI, EDI EAX, EAX , -
,
ESI EDI. -
, :
, , -
, ,
.
match: ;
push ebp ;
mov ebp, esp
sub esp, 4 ; I
; [ebp-4]
push esi ; ESI EDI
push edi ; (EAX )
mov esi, [ebp+8] ; :
mov edi, [ebp+12] ;
.again: ; ,
;
;
cmp byte [edi], 0 ; ?
jne .not_end ; ,
cmp byte [esi], 0 ; , ?
jne near .false ; ,
jmp .true ; :
.not_end: ; ...
cmp byte [edi], '*' ; ?
jne .not_star ; ,
; !
mov dword [ebp-4], 0 ; I := 0
.star_loop:
; .
mov eax, edi ; :
inc eax ; .
push eax
mov eax, esi ; :
add eax, [ebp-4] ; I-
push eax ; (, [ebp-4] - I)
call match ; ,
;
add esp, 8 ;
test eax, eax ; ?
jnz .true ; , ..
; ,
;
; =>
88
add eax, [ebp-4] ; 0, ..
;
; ""
;
cmp byte [esi+eax], 0 ; , ,
; ?
je .false ;
inc dword [ebp-4] ; : I := I + 1
jmp .star_loop ; I

.not_star: ; , .
mov al, [edi] ; . '*'
cmp al, '?' ; , '?'
je .quest ; ,
cmp al, [esi] ; ,
;
; ;
; ,
;
jne .false ; ( . )
; =>
jmp .goon ; -
;
.quest: ; '?'
cmp byte [esi], 0 ; ,
jz .false ; ( )
.goon: inc esi ; =>
inc edi ;
jmp .again ;
.true: ; ,
mov eax, 1 ;
jmp .quit
.false: ; ,
xor eax, eax ;
.quit: ; ,
pop edi ;
pop esi ;
mov esp, ebp ;
pop ebp ; EAX
ret ;
;

, , ,
string,  , pattern,
match :

push dword pattern


89
push dword string
call match
add esp, 8
(0 1) EAX.
, -

.false ,
(near). , .false ,

. . . 60.

2.7.

(
) i386 , -
. -
ESI EDI , . 35.
, -
ESI, 
EDI, ( )
1, 2 4.
; -
,
AL, AX EAX. ,
.
( ) -
DF (, direction ag, . .
). , (
), 
(, ). -
DF std (set direction),  cld
(clear direction).
 stosb, stosw
stosd, [edi], ,
, AL, AX EAX, -
( DF) EDI
1, 2 4. ,

buf resb 1024


,
:

xor al, al ; al
mov edi, buf ;
90
mov ecx, 1024 ;
cld ;
lp: stosb ; al -> [edi], . edi
loop lp
rep.
, , ,
ECX ( stosw:
, CX; -
). rep
:

xor al, al
mov edi, buf
mov ecx, 1024
cld
rep stosb
lodsb, lodsw lodsd, , ,
, ESI, -
AL, AX EAX,
ESI 1, 2 4.
rep ,

- , , -
. lods -
. , ,

array resd 256


.
:

xor ebx, ebx ;


mov esi, array
mov ecx, 256
cld
lp: lodsd
add ebx, eax
loop lp
lods -
stos. , , -
. :
91
mov esi, array
mov edi, esi
mov ecx, 256
cld
lp: lodsd
inc eax
stosd
loop lp

, movsb, movsw
movsd. ,
[esi] [edi], (
) ESI EDI (, 1, 2
4). ,

buf1 resb 1024


buf2 resb 1024
,
:

mov ecx, 1024


mov esi, buf1
mov edi, buf2
cld
rep movsb
( DF),
-
. , , buf1
"This is a string" "string"
"long". ,
[buf1+10], ,
"long" . ,
, -
, . , "long " (
) buf2, , -
buf1, :

std
mov edi, buf1+17+5
mov esi, buf1+17
mov ecx, 8
rep movsb
92
mov esi, buf2+4
mov ecx, 5
rep movsb

, i386 cmpsb,
cmpsw cmpsd (compare string), scasb, scasw scasd (scan
string). scas (,
AL, AX EAX) , [edi],
cmp, -
/ EDI cmps ,
, [esi] [edi], -
/ .
rep, repz
repnz ( repe repne), , -
ECX ( CX, )
ZF ,
(repz/repe) (repnz/repne).
scas
cmps.

2.8.

i386
.
cbw, cwd, cwde cdq -
; , -
.
-
. cbw AL
AX, . . AH. cwd
AX DX:AX, -
DX. cwde AX EAX,
16 . , cdq
EAX EDX:EAX, -
EDX.
(div, . 2.3.4).
movsx (move signed extension) movzx (move zero extension)
. -
, -
, ,
(
-
93
). movzx ,
movsx  .
, -
nop. :
. No OPeration.

2.9.

, -
i386, ,
(, MMX-), -
.
, -
, ,
, -
:
, , .
, -
i386 ,
, -
. , , ,
, i386 ( -
8086) -
; , -
,
,
,
-
, -
.
, -
, , -
, SPARC. , ,
 -
i386 :-)

94
3. NASM

NASM, -
,
, . , 1.5 -
, -
. 2.2
, 2.2.2 -
. 2.6.9 -
, 2.6.8 .
NASM,
, , .

, -
NASM.

3.1. NASM

-
( NASM ) . -
( )
,
.
- ,
, ,
. -
( \), -
. ,
, ; -
(, )
75 , .
95
1 NASM ( )
: , , , -
, -
, , -
; , .
, . -
, ,
.
,
, , '_', '$', '#', '@', '~', '.' '?',
'_', '?' '.';
2.6.8, , , -
. ,
'$'; , ,
, 2 . -
, , ,
, 'label', 'LABEL', 'Label' 'LaBeL' 
. , ,
, . , -
,
, ,
. ,
.
, , -
(, rep, . . 91; -
),  -
( ,
), , , (
, , ,
PRINT; ). -
,
, -
, , mov, MOV, Mov mOv, , ,
. , , .
,
, -
. , .
1 ,
;
, .
2 , -
, ;
, -
, .
96
,
, -
.
, , , ,
: nasm -
,
: , , ,
, -
( byte, dword, near . .), -
,
(, ) 
.
NASM, .

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

3.2.


NASM , ,
,
. , db, dw, dd,
resb, resw resd 2.2.2. ,
, NASM resq, rest,
dq dt. q quadro  -
(8 ), t  ten -
. , -
( , -
); , dq dt -
, , (, 71.361775).
dq dt,
dd; , IEEE-7543 -
3 IEEE-754  ,
-
; Institute of Electrical
and Electronics Engineers (IEEE), -
.
97
 ,
, , , 4 ,
8 10 .
equ, -
. -
,
. equ
. :
four equ 4

four, 4. , -
,

mov eax, four

mov eax, 4

, , , -
, , (-
) (,
, ).
equ 
() , -
db, dw . -
$, , ,
4 . , :
msg db "Hello and welcome", 10, 0
msglen equ $-msg

$-msg, , -
,
. $ ,
, msg  ,
( 19).
3.4.
times - (
) . ,

stars times 4096 db '*'


4 , .

98
4096 , -
'*', , 4096 , -
db '*'.
incbin, -
, . -
;
, .

3.3.

NASM -
: , ,
.
(. . 41),
, , -
. ,
(, , ),
. -
:
h (, 2af3h), $, Borland
Pascal (, $2af3), , , -
0x, (0x2af3). $ -
, $ , ,
, 0 (, $0f9
$f9). ,
, ,
, $. -
o q (, 634o, 754q).
, b (10011011b).
-
, , , ,
, .

: ,
(
4 ) , , -
( ). ,
-
, .
: -
, (.
. 43).
99
, 4 , -
, ,
. -
,
i386 , -
. , ( ) -
. , , 'abcd'
64636261h: 64h  d, 61h  a,
61h , 64h  . -
,
. ,
, 1
db -
dw.
, ,
-
. , 1
1.0 ! -
, 1.0
( , 4 , , -
) 3f800000h (1065353216
).
, e E. , 1.0e-5
, 0.00001. ,
- .

3.4.

NASM
.
,
, -
. , -
, -
: , ,
.

3.4.1.
, , -
, , -
:
100
+ - 

*  ;

/ %  ( -
);

// %%  ( -
);

&, |, ^  , , ;

<< >>  ;

- + : -
, + ;

~ .

% %%
,
( ).
, seg, -

.
,
, , -
. (
) , &, ^, -
|, .
, .

3.4.2.
. -
, -
;
,
. -
.
, , , , -
, : ,
, jmp, , , -
,
, .
,
.
101
-
. , , , -
,
, ;
.
,
, . .,
,
.
,
 -
. ,
,
. NASM :

times (label-$) db 0
label: db 'Where am I?'

times -
, label 
label , -
. ?!
:
, ,
. -
,
, -
( , , , -
).
, , -
, .
.
times, -
, , resb, resw
.,  -
, -
. , mov eax,[ebx], mov eax,[ebx+10]
mov eax,[ebx+10000] 2 , 3 6
, , -
, 1 ,  2, 
4;

mov eax, [ebx+label]


102
label ? , -
,
byte, word dword. ,

mov eax, [ebx + dword label]

, label , (, -
, ) .

3.4.3.
. 2.2 (. . 49)
( ) .
NASM
,
. , ,

mov eax, [5*ebx]

5, (
1, 2, 4 8), ,
[ebx+4*ebx],
.

mov eax, [ebx+4*ecx+5*x+y]

x y  , ,
5*x+y ,
.
, ,
, ,
,
(. ).

3.5.

3.5.1.
,
, ,
, , , -
, , . -
 -
, ;
103




. 3.1.

-
,
(. . 3.1).
-
(
), , - -
, .
, NASM
, -
.
:
PRINT FINISH , , ,
.
, ,
, ,
. ;
,
.
, -
, , -, ,
( -
), , -
, , -
, . , -
, .
(
, ), (, ,
, ) ,
. -
, ,  -
5 .
, -
, ,
, .
: %include,
5  -
macro expansion.
104
,
. ,

%include "stud_io.inc"

, stud_io.inc.

3.5.2.
,
, .
2.6.6, 2.6.7 2.6.9,
( , 2 + n,
n  ). ,
, . -
, -
. pcall1, pcall2 . ., ,
, , . .
-
; ,

push edx
push dword mylabel
push dword 517
call myproc
add esp, 12

pcall3 myproc, dword 517, dword mylabel, edx

, , . , -
, ,
pcall, ,
. , -
:

%macro pcall1 2 ; 2 -- -
push %2
call %1
add esp, 4
%endmacro

pcall1,
: call
105
. , -
%macro %endmacro,  -
, .
: -
%1 %2
, . -

pcall1 proc, eax

-
,
proc, eax
%1 %2. -
:

push eax
call proc
add esp, 4

pcall2 pcall3:

%macro pcall2 3
push %3
push %2
call %1
add esp, 8
%endmacro
%macro pcall3 4
push %4
push %3
push %2
call %1
add esp, 12
%endmacro

pcall0:

%macro pcall0 1
call %1
%endmacro

, , , -
,
. pcall4, pcall5 . .
106
pcall8 ; -
,
pcall8, , , pcall9 pcall12.

;
, -
, -
: ,
. -
.
, , -
, ? -
. , ,
2.6.9, , -
[ebp+12], [ebp-4] . . -
. ,
; ,
. 6
:
%define arg1 ebp+8
%define arg2 ebp+12
%define arg3 ebp+16
%define local1 ebp-4
%define local2 ebp-8
%define local3 ebp-12
:
%define arg(n) ebp+(4*n)+4
%define local(n) ebp-(4*n)
:
mov eax, [arg1]
(, , )
mov [arg(7)], edx
,
. , arg1
:
6 ,
,
4 ; , , ,
.
107
%define arg1 [ebp+8]

mov eax, arg1

. NASM
, , , -
, ,
. ,
,
, , arg1 , ,
,
. . ,
, , -
, 
, ()
.

3.5.3. ;
, -
 , ,
(
). ,
,
-
. , ,
. ,

. , -
, %undef;
, -
. , ,
, ,
, .
A -
%define -
B, ; -
B ,
A.
A, B, -
, , . ,
B, -
A.
. :
108
%define thenumber 25
%define mkvar dd thenumber

var1 mkvar
mkvar, -

var1 dd thenumber
, , thenumber

var1 dd 25
thenumber
mkvar:
%define thenumber 36
var2 mkvar
, -
36:

var2 dd 36
, mkvar : -
, , dd thenumber, thenumber
36, .
7 . NASM
, ,
%xdefine.
%define , , -
,
, , -
. ,
%define mkvar %xdefine:
%define thenumber 25
%xdefine mkvar dd thenumber
var1 mkvar
%define thenumber 36
var2 mkvar
7 lazy ,
(
thenumber), .
109
25:

var1 dd 25
var2 dd 25
thenumber -
mkvar, mkvar
thenumber: mkvar, -
thenumber (25).


, ,
. NASM , -
%assign. %define %xdefine,
,
-
. , . ,

%assign var 25

%assign var var+1


var 26,
, var -
.
, %assign, -
. ,
, -
,
.

3.5.4.


. ,
,
, ,
. , ,
:
, , , -
.
110
,
, .
. , -
, ( -
, ,
)
; -
, , ,
-
, , .
,  ,
, ,
,
, -
.
-
-
, -
, ,
. -
, , , .
, , -
, NASM.
, . ,
, , -
, , ,
. -
,
. -

, .
:

%ifdef DEBUG_PRINT
PRINT "Entering suspicious section"
PUTCHAR 10
%endif
;
; ""
;
%ifdef DEBUG_PRINT
PRINT "Leaving suspicious section"
PUTCHAR 10
111
%endif
%ifdef  , -
, -
( DEBUG_PRINT).
, :
%define DEBUG_PRINT
NASM -
, %ifdef
%endif;
, %define -
:
;%define DEBUG_PRINT
, , -
, -
, , ,
.
, , -
, , -
. NASM;
, , NASM
:
nasm -f elf -dDEBUG_PRINT prog.asm

%define, .
, -
, :
%ifdef FOR_PETROV
;
; ,
;
%elifdef FOR_SIDOROV
;
; -
;
%else
; , ,
;
%error Please define either FOR_PETROV or FOR_SIDOROV
%endif
112
( %elifdef  else
ifdef). -
-dFOR_PETROV -dFOR_SIDOROV, NASM -
, %else, ,
%error, .
,
( -
). %ifndef (if not dened ). %ifdef,
%ifndef %else,
%elifndef.
,
,
; NASM -
. %if,
- , -
. -
3.4.1; -
=, <, >, >=, <=,
, <>, ,
!=, ; -
==. , -
&& (), || () ^^ ( ).
, , %if, -
(. 3.4.2). ,
%if-, %if -
%else  %elif.
NASM -
. %ifidn %ifidni , -
, , -
, , . -
, ,
, , %ifidn -
, %ifidni , -
, foobar, FooBar FOOBAR .
%ifnidn
%ifnidni; %elif-, ,
%elifidn, %elifidni, %elifnidn %elifnidni. %ifmacro -
; -
%ifnmacro, %elifmacro %elifnmacro. %ifid, %ifstr
%ifnum , , , -
, . , NASM -
%ifnXXX, %elifXXX %elifnXXX
.
113
, NASM %ifctx -
, -
.

3.5.5.
NASM -
() . -
%rep ( repetition ) %endrep. -
%rep , -
. , %rep
%endrep, ( )
, %rep. ,
%rep %endrep %exitrep, -
.
. , -
100 ,
50,  51 . ., , -
, 149. , :

db 50
db 51
db 52
;....
db 148
db 149

, -, , -,
. -
,
:

%assign n 50
%rep 100
db n
%assign n n+1
%endrep

, -
n 50, , -
%rep %endrep,

db 50, db 51, db 52 . .; ,
n ( )
114
. , -

, , -
. , , -
.
. -
, -
8 , 100000. -
dd -
:
fibonacci
%assign i 1
%assign j 1
%rep 100000
%if j > 100000
%exitrep
%endif

dd j

%assign k j+i
%assign i j
%assign j k
%endrep
fib_count equ ($-fibonacci)/4
fibonacci -
, fib_count  ,
( -
. 98).
-
, , . ,
, 128 :
array resw 128
128 inc, -
.
:
%assign a array
8 ,  ,
, -
: 1, 1, 2, 3, 5, 8, 13, 41, 34 . .
115
%rep 128
inc word [a]
%assign a a+2
%endrep
, 128
-
, , :

mov ecx, 128


lp: inc word [array + ecx*2 - 2]
loop lp
, -
, ,
, 128 inc,
, ,
-
( )
.

3.5.6.
; -
, ,
.
, %macro %endmacro. 3.5.2
,
-
. , , zeromem, -
  -
, . -
, , ,
(!) :
%macro zeromem 2 ; ( - )
push ecx
push esi
mov ecx, %2
mov esi, %1
lp: mov byte [esi], 0
inc esi
loop lp
pop esi
pop ecx
%endmacro
116
NASM -
.
zeromem, -
 NASM ,
(lp:) . , -
-
, %1 %2 ,
. ,
lp: mov byte [esi], 0
lp, ( -
)  , , ,
zeromem.
, ,
, , ,
,
, . NASM
.
, %  , -
lp
%%lp.
( ) .
, zeromem NASM
%%lp ..@1.lp,  ..@2.lp . .
zeromem. -
(,
, , , ) (-
) ECX (
)  ESI, ,
, . , -
-
section .bss
array resb 256
arr_len equ $-array

section .text
; ...
mov ecx, array
mov esi, arr_len
zeromem ecx, esi
; ...
zeromem :
push ecx
117
push esi
mov ecx, esi
mov esi, ecx
; ...

, , ECX ESI ,
. ,
, .
,
, , ECX
ESI, 
,

mov ecx, %2
mov esi, %1

push dword %2
push dword %1
pop esi
pop ecx

%macro zeromem 2 ; ( - )
push ecx
push esi
push dword %2
push dword %1
pop esi
pop ecx
%%lp: mov byte [esi], 0
inc esi
loop %%lp
pop esi
pop ecx
%endmacro

3.5.7.
%macro
NASM .
-,
. ,

%macro mymacro 1-3


118
, ,

%macro mysecondmacro 2-*


, ,
. -
%0, -
, -
.
, -
%1, %2 . ., (
n- , n -
) NASM . -
, ?
%rotate, -
. :

%rotate 1
, -
. 1, ,
%2, %1,
%3 %2 . ., , -
%1,
, .
%0 .

(), -
. ,

%rotate -1
%1 , , %2 -
, ( -
%1) . .
, (. . 105)
pcall, -
. , -
%rotate,
. , pcall, -
( call)
,
. , , , -
4 . , -
, .
119
%rep %rotate -1,
( )
1.
, , ,

.
(
, ) call,
add . , :
%macro pcall 1-* ;
%rep %0 - 1 ;
%rotate -1 ; %1
push dword %1
%endrep
%rotate -1 ; %1
call %1
add esp, (%0 - 1) * 4
%endmacro
, , :
pcall myproc, eax, myvar, 27
:
push dword 27
push dword myvar
push dword eax
call myproc
add esp, 12
, , .

3.5.8.
NASM , -
( ) . -
, , ,
( )
.
, %strlen, .
.  , -
, , 
. ,
%strlen sl 'my string'
120
sl 9.
, %substr,
. ,
%substr var1 'abcd' 1
%substr var2 'abcd' 2
%substr var3 'abcd' 3
var1, var2 var3 'a', 'b' 'c' -
, ,
%define var1 'a'
%define var2 'b'
%define var3 'c'
, , ,
,
.
, -
( , ),
, , -
.

3.6. NASM

NASM -
. , -
nasm ,
, , , -
. :
-f, -o -d.
, -f -
. elf. , ,
,
, , ,
. -
,
, , ,
,
, .
-o ,
. elf,
NASM':
.asm .o,
. - ,
-o.
121
-d, (. . 112), -
, -
. -dSYMBOL,
, -
%define SYMBOL. -
: , -dSIZE=1024
SIZE, 1024,
%define SIZE 1024.


. ,
, -
.
-l,
.
-l; ,
prog.asm,
nasm -f elf -l prog.lst prog.asm
prog.lst;
-
, .
-g, NASM'

. NASM

, . .
,
.
, , ,
-g
(, gdb) , ,
, .
 -e; NASM'
, -
( , ) .
,
, -
; , ,
, , .
NASM ;
, .

122
4.

-
,
stud_io.inc, -
.
,
, , -
, . -
, , ,
, ,
-
, , ,
Linux FreeBSD -
.

4.1.

4.1.1.
,

,
. , , -
. -
, -
. ,
. ,
-
. , , -
?
123
1

. 4.1.


( , ):
, ,
, -
(
) -
. , ,
,
, , ,
-
(. . 4.1).

4.1.2.
-
-.
,
(, ). -
-
- (,
, , -
).
( . .) -
, ,
. ,
, -
( ) ,
( -
); , ,
1 ; -
, ,
1 , -
.
124
()

. 4.2.

1 1

. 4.3.

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

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

125
-
, ,
, -.
- -
,
(. . 4.3); ,
, ,
. ,

-, 2 , -
, , -
. -

,
,
 .

4.1.3.
( , -
)
, , ,
. -
, , ,
, , , ,
1000x1000, .

: (, -
) , -
,
-
- (
200 ). , -
,
:  -
, ,
. .
,
2 ,
batch mode; batch -
(,
, ). ,
packet,
.
126
. , -
,
. -
, -
,
; ,
. . , -
, ( ,
). , ,

, - ( -
),
.
,
, -
, . , Unix
 ; -
-
,
: -
, ,
, , -
, .
, -

.
, Windows, -
,
. -
, ,
; ,
, -
- ( -).
, , , -
,
. . -
,
; , Windows -
, , .

4.1.4.
. -
, , , -
127
. ., -
; , ,
, -
, ,
.
, ( )
, -
. , -
,
, , -
,  ,
, .

4.1.5.
, -
( )
.
1.2  , -, , -, -
, -
-
.
,
, ,

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

, .
-
, , ,
. ,
, -
.
, -
. , -, -
128
,
,
, -
. -
, . , -
-
-,
, -

, -

. , -
, -
/ -. ,
, -
, ( -
) ,
.
-
: .
-
(. kernel mode , supervisor mode ). -
(. user
mode ) (. nonprivileged ). -
-

. -

. , -
, ; , -
,
. -
,  .
1.2,
;
. -

. -
, , -
.
-. -
(. 4.2 . 125) -
-
129
( ), -
-
 ,
, -
. ,
-,
, .
. ,
,
 . -
, . 4.3 . 125,
. -
. ,
-
; ,
, ,
(, ),
 -, -
( ,
, -
). ,
-,

.
. -
-
( ),
. ,
, -
, ,
-. -
. -
.
, -
,
. -
,
 . , -
-
, -
;
, :
,
130

, -
( ). -
,

. -
, -
.
, -
:

, -
( -
). -
,
,
.

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

4.2.


;
,
.
. -
,
.
131
4.2.1. ()
-
.
;
-
, (,
, )

. ( ) -
.
;
, ,
, -
. , ,
.
-
3 :
1. , , -
.
2. -
, , -
;
. -
.
3. ,
, ;
.
4. - ( ) -
; -
.
,
() ,
, (. .
) ; -

.
5.
, -
3 ; .

132
, , -
, .
, -
.
,

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

4.2.2. ()
, , -
: , -
? ,
:
- ,
, -
, ; ,
, , . -
-
, , . -
-
, ,
- .
.
, -
. : -
, . -
, . ,
, -
; -
( );
( -
, , ),
-
. , - -
, ;
133
, , -
. .
, , -
, -
.
( ): , -
,
4 .
a -
. ,
-
, ,
 . -
, ()
; ,
, -
 . -
, (traps), (exceptions)
- .

4.2.3.
,
, . -
, , -
. , ,

. , -

. , -

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

4
, , ,
; [1].
134
.
, -  -
. -
:
,
. -
trap
(), svc (supervisor call, ) . .
i386 -
int ( interrupt  ). -
.
, -
, :
 , 
, -
.
,
, -
, .
-
. ,
i386 (. gates) -

,
, , i386 -
, ; , -
.
, ( -
) -
, -
-
. ,
, -
-
, , -
.
.

4.3. Unix

. -
, , -
135
, ,
, . .,
. -
Unix ( Linux FreeBSD),
i386,
.

5 ; -
, ,
.
, Unix -
. , -
,
 , -
, -
.
Unix ,
- . , -

. , -
(, getppid Linux
64, FreeBSD  39). , -
, ,
Unix -
getppid,
, , , -
getppid,
. , -
. -
,
, , Linux, -
64, FreeBSD 39. ,
-
.
, .

, -
(, , ). ,
FreeBSD Linux -
, , ( -

5 , Linux i386; ,
, .
136
Unix ). , -
, .

4.3.1. Linux
Linux i386 -
80h. -
EAX; -
, , , EBX, ECX, EDX,
ESI EDI; , -
 , .
EAX,
, fffff000h ffffffffh,
( ).
write, -
-,
, -
( ). 4
: () -, -
, , ,
. ,
Unix 1 (, 1
). ,
, , PRINT, -
4 EAX, 1 EBX,
ECX  EDX, int 80h,
.
 _exit,
. 1 -
, .
, ,
: -
, 0,
, 1, 2 . .
, ,
; stud_io.inc
:

global _start

section .data
msg db "Hello world", 10
msg_len equ $-msg

137
section .text
_start: mov eax, 4 ; write
mov ebx, 1 ;
mov ecx, msg
mov edx, msg_len
int 80h

mov eax, 1 ; _exit


mov ebx, 0 ; ""
int 80h

4.3.2. FreeBSD
FreeBSD .
80h -
EAX,
, , , -
,
(. . 82). Linux, -
. -
EAX,
-
( Linux), CF.
CF ,
EAX, , EAX
.
. FreeBSD -
, -
:

kernel:
int 80h
ret

,
, ,
EAX call kernel;
call ,
, -
. FreeBSD
(  -
kernel 
), (
[esp+4], [esp+8] . .)
138
-
, int
, ,
push eax ( 32- ).
,
, ; ,
, ESP
add.
Linux,
write _exit (. . 137). -
FreeBSD :

global _start

section .data
msg db "Hello world", 10
msg_len equ $-msg

section .text
_start:
push dword msg_len
push dword msg
push dword 1 ;
mov eax, 4 ; write
push eax ;
int 80h
add esp, 16 ; 4

push dword 0 ; ""


mov eax, 1 ; _exit
push eax ;
int 80h

_exit,
.
, ,
( ,
).
,
int 80h jc jnc,
CF,
,
. Linux
, EAX,
.
139
4.3.3. Unix

_exit write; , _exit 6 1
 , write 4 -
, (1
), ,
, .
( , -
, . . ) read, 3.
write:  -
( -
0),  ,
,  , -
. , , -
,
, . -
, read! (,
EAX.)
, 
, , , -
,
(, 200 ,
15). , read
0  ,
.
, .

; , Unix
, Ctrl-D.
, , read
,
. , ,
-
, -
 , -
.
, ,
. Linux -
EAX ; FreeBSD
, , -
6 , Linux FreeBSD; ,
, , .
140
CF (carry ag): ,
, ,
. read,
write ( ,
, , ),
.
, ,
- 0 ( ), 1 ( )
2 ( ), -
read 0, 1 2  write.
, , -, -
, .
, , -
- ().
open, 5.
.  -
, ; ,
.  ,
(, .);
,
, ,
, , , . .
, Linux
FreeBSD;
4.1. , -
. 
, -
0.  ,
, , ,
( -
O_WRONLY|O_CREAT|O_TRUNC). Linux
 241h, FreeBSD  601h. open -

. , -
,
0666q.
open
, . open
,
, : , -
,
, ,
. , open -
141

Linux FreeBSD
O_RDONLY 000h 000h
O_WRONLY 001h 001h
O_RDWR 002h 002h
O_CREAT 040h 200h
O_EXCL 080h 800h
O_TRUNC , 200h 400h

O_APPEND , 400h 008h

4.1. open

, EAX (
Linux) CF ( FreeBSD). -
, EAX
( ). -
read write
. ,
.
, .
close, 6.
, . -
- ;
open -
.
Unix ( -
) getpid,
(, )
getppid. getpid -
20, getppid 64
Linux 39 FreeBSD. ;
-
EAX. ,
, .
kill ( 37) -
. , -
7 , ; ,
15 (SIGTERM) (
7
, .
142
, -
), 9 (SIGKILL) ,
, .
Unix -
; -

. , -
,

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

4.4.

Unix , , -
, -
 , . . , NASM
-
nasm -f elf prog.asm
, ,
. : -f,
elf, ,
prog.asm. , ,
nasm, . ,
,
: nasm, -f, elf prog.asm.
, ,
. -
-
, ,
. -
,
. , ,
_start, -
( [esp]) -
, (
143
), ( [esp+4]) -
, ,
, ( [esp+8]) ,
. . -
( ),
.
, -
( ).
stud_io.inc , , .
write -
,
strlen, -
EAX (,
). , -
(newline) ;
, 10,
, write,
.text8 -
newline ret, .
, -
Linux,
FreeBSD. -
, -
. ,
Linux ( NASM)
OS_LINUX, FreeBSD  OS_FREEBSD.
, Linux (
cmdl.asm)
nasm -f elf -dOS_LINUX cmdl.asm
FreeBSD 

nasm -f elf -dOS_FREEBSD cmdl.asm


, :
section .text
global _start

strlen: ; arg1 == address of the string


push ebp
mov ebp, esp
8 ,
; , .data.
144
push esi
xor eax, eax
mov esi, [ebp+8] ; arg1
.lp: cmp byte [esi], 0
jz .quit
inc esi
inc eax
jmp short .lp
.quit: pop esi
pop ebp
ret

newline:
pushad
%ifdef OS_FREEBSD
push dword 1
push dword .nwl
push dword 1 ; stdout
mov eax, 4 ; write
push eax
int 80h
add esp, 16
%elifdef OS_LINUX
mov edx, 1
mov ecx, .nwl
mov ebx, 1
mov eax, 4
int 80h
%else
%error please define either OS_FREEBSD or OS_LINUX
%endif
popad
ret
.nwl db 10

_start:
mov ecx, [esp]
mov esi, esp
add esi, 4
again: push dword [esi]
call strlen
add esp, 4
push esi
push ecx
%ifdef OS_FREEBSD
push eax
push dword [esi]
145
push dword 1 ; stdout
mov eax, 4 ; write
push eax
int 80h
add esp, 16
%else
mov edx, eax
mov ecx, [esi]
mov ebx, 1
mov eax, 4
int 80h
%endif
call newline
pop ecx
pop esi
add esi, 4
loop again

%ifdef OS_FREEBSD
push dword 0
mov eax, 1 ; _exit
push eax
int 80h
%else
mov ebx, 0
mov eax, 1
int 80h
%endif

4.5. :

, -
. -

.
: , -
,
,  , , -
4096 ,
.
.
cp, ,
, .
, -
. , , ,
146
,
Linux, FreeBSD. -
,
-
. , ,
, ,
;  ,
. -
, -
(, , ) ,
,
(, -
).
Linux FreeBSD.
,
;
,  . ,
Linux
, ; FreeBSD
.
-
( Linux) , -
(. . 118) 
.
FreeBSD
, -
. ,
( -
, - -
FreeBSD). %rotate
, pcall
(. . 120).
FreeBSD -
EAX, ; Linux
,
,
; -
,
, -
.
-
.
Linux EAX, -
147
, .
FreeBSD CF, -
EAX , ,
, .
: Linux ,
FreeBSD CF, ,
EAX neg.
, , Linux, EAX -
 ;
-
, , , 
add, -
, , ( CF)
.
:

%macro syscall 1-*


%rep %0
%rotate -1
push dword %1
%endrep
%ifdef OS_FREEBSD
mov eax, [esp]
int 80h
jnc %%sc_ok
neg eax
%%sc_ok:
add esp, (%0-1)*4
%elifdef OS_LINUX
pop eax
%if %0 > 1
pop ebx
%if %0 > 2
pop ecx
%if %0 > 3
pop edx
%if %0 > 4
pop esi
%if %0 > 5
pop edi
%if %0 > 6
%error "Too many params for Linux syscall"
%endif
%endif
%endif
%endif
148
%endif
%endif
int 80h
%else
%error Please define either OS_LINUX or OS_FREEBSD
%endif
%endmacro
, , , -
. , -
, , ,
Linux (. 137) FreeBSD (. 139).
:
section .data
msg db "Hello world", 10
msg_len equ $-msg
section .text
global _start
_start: syscall 4, 1, msg, msg_len
syscall 1, 0
,
, NASM'
-dOS_LINUX -dOS_FREEBSD.
. -
, -
, -
. ,
( ,
);
fdsrc fddest. H,


, argc argvp. -
, ,
.bss:
section .bss
buffer resb 4096
bufsize equ $-buffer
fdsrc resd 1
fddest resd 1
argc resd 1
argvp resd 1

:
,
149
-
, ,
- .
,
, 
.
.data :

section .data
helpmsg db 'Usage: copy <src> <dest>', 10
helplen equ $-helpmsg
err1msg db "Couldn't open source file for reading", 10
err1len equ $-err1msg
err2msg db "Couldn't open destination file for writing", 10
err2len equ $-err1msg

.text,
, ,
.
, ,
argc. -
argvp,
,
 . , argc
3;
: .
, ,
:

section .text
global _start
_start:
pop dword [argc]
mov [argvp], esp
cmp dword [argc], 3
je .args_count_ok
syscall 4, 2, helpmsg, helplen
syscall 1, 1
.args_count_ok:

, -
, .
, argvp (),
.
argvp ESI,
150
[esi+4]  -
, , , -
. EDI,
open. -
 ,
0 (O_RDONLY). -
; ,
syscall , EAX -
,  ;
open -
-,
, . -
fdsrc, 
.

mov esi, [argvp]


mov edi, [esi+4]
syscall 5, edi, 0 ; O_RDONLY
cmp eax, 0
jge .source_open_ok
syscall 4, 2, err1msg, err1len
syscall 1, 2
.source_open_ok:
mov [fdsrc], eax

. -
ESI EDI,
open, -
, fddest.
open . -, -
O_WRONLY, O_CREAT O_TRUNC,
, . 141, -
Linux FreeBSD. -,
, -
, , ,
666o. :

mov esi, [argvp]


mov edi, [esi+8]
%ifdef OS_LINUX
syscall 5, edi, 241h, 0666o
%else ; assume it's FreeBSD
syscall 5, edi, 601h, 0666o
%endif
cmp eax, 0
151
jge .dest_open_ok
syscall 4, 2, err2msg, err2len
syscall 1, 3
.dest_open_ok:
mov [fddest], eax

, .
, ,
( EAX 0) ( -
), , ,
( -
buffer, EAX) .

.again: syscall 3, [fdsrc], buffer, bufsize


cmp eax, 0
jle .end_of_file
syscall 4, [fddest], buffer, eax
jmp .again

end_of_file;
, ,
, -
close :

.end_of_file:
syscall 6, [fdsrc]
syscall 6, [fddest]
syscall 1, 0

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

152
5.

5.1.

, ,
. ,
-
, .

, . -
, , -
, . -
, . -,
,
, -
, ,
, , -
, - , ,
. -,
, -
, ,
,
. , ,
 , ,
, , ,
, , -
( )
, ,
.
,
%include , , , ,
153
, , -
,  , -
( ,
) , .
. -
, -
,
, .
,
, , , ( -
 ) 
. ,
 , -
, ,
. , -
:
, -
, ,

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

;
 , , , -
.
; -
, .o.
.
-
. , ,
,
(, NASM
global). ,
, ,
,
- .
:
, ,
154
. , -
, ,
, -
.
,

, -

, , -
. ,
,
, , -
,
 -
.
, ,
, , :
.

5.2. NASM

NASM , -
:
. :
global , , ,
-
, , .
, , , , -
.
(  ), - -
, . -
, .
, -
,
, ,
, ,
. NASM
extern. , ,
myproc, -
, , , :
extern myproc
:
myproc , , ,
, , -
.
155
5.3.

-
, ,
. -
, :
. -
, putstr
getstr, . -
putstr ,
; -
strlen,
4.4. . , -
_exit ( quit)
. ,
: putstr.asm, getstr.asm, strlen.asm quit.asm.
syscall,
. 148.
, . -
,  , ,
, : -
,
. , -
, ,
- , , ,
,
. , syscall, -
%include
( ,
 ,
). syscall.inc;
,
, . 148; -
.
strlen.asm. :

global strlen

section .text
; procedure strlen
; [ebp+8] == address of the string
strlen: push ebp
mov ebp, esp
xor eax, eax
156
mov esi, [ebp+8]
.lp: cmp byte [esi], 0
jz .quit
inc esi
inc eax
jmp short .lp
.quit: pop ebp
ret

,
strlen -
. , ,
,
, . -
, .
strlen,
putstr.asm. putstr strlen -
, write:

%include "syscall.inc" ; syscall


global putstr ; putstr
extern strlen ; strlen

section .text
; procedire putstr
; [ebp+8] = address of the string
putstr: push ebp ;
mov ebp, esp ;
push dword [ebp+8] ; strlen
call strlen ;
add esp, 4 ; EAX
syscall 4, 1, [ebp+8], eax ; write
mov esp, ebp ;
pop ebp ;
ret


getstr. getstr ,
, ( -
) , ,
,
.
; , ,
, ,
.
157
getstr , -
I , ,
, -
. I
,
. -
read.
: read -
1,
;
10, (
Enter); , , -
, I+1>buflen.
-
. ,
, ,
; -
I .
getstr.asm :
%include "syscall.inc" ; syscall
global getstr ; getstr

section .text
; procedure getstr
; [ebp+8] = address of buffer
; [ebp+12] = length of buffer
getstr: push ebp ;
mov ebp, esp ;
sub esp, 4 ; I
xor eax, eax ; eax:=0
mov [ebp-4], eax ; I:=0
.again: ;
mov eax, [ebp+8] ; EAX
add eax, [ebp-4] ; I
syscall 3, 0, eax, 1 ; read
cmp eax, 1 ; 1?
jne .eol ; -
mov eax, [ebp+8] ; EAX
add eax, [ebp-4] ; I
mov bl, [eax] ; ( BL)
cmp bl, 10 ; 10?
jne .noeol ; -
dec dword [ebp-4] ; - I
jmp .eol ;
.noeol: mov eax, [ebp-4] ; I
158
inc eax ; EAX . I+1
cmp eax, [ebp+12] ; arg2?
jae .eol ; -
inc dword [ebp-4] ; I
jmp .again ;
.eol: mov eax, [ebp+8] ; EAX
add eax, [ebp-4] ; I
inc eax ; 1
xor bl, bl ; BL
mov [eax], bl ; 0
mov esp, ebp ;
pop ebp ;
ret

 quit.asm:

%include "syscall.inc"
global quit
section .text
quit: syscall 1, 0

, -
, greet.asm. -
, -
syscall (, , syscall.inc)
. ,
, .data;
, -
.
.bss. .text, -
.

global _start ;
extern putstr ;
extern getstr ; putstr, getstr quit
extern quit

section .data ;
nmq db 'Hi, what is your name?', 10, 0
pmy db 'Pleased to meet you, dear ', 0
exc db '!', 10, 0

section .bss ;
buf resb 512
buflen equ $-buf

section .text
159
_start: push dword nmq ;
call putstr ; putstr nmq
add esp, 4
push dword buflen ; getstr
push dword buf ; buf
call getstr ; buflen
add esp, 8
push dword pmy ; putstr pmy
call putstr
add esp, 4
push dword buf ; putstr
call putstr ; ,
add esp, 4 ;
push dword exc ; putstr exc
call putstr
add esp, 4
call quit ; quit

, -
syscall.inc, strlen.asm, putstr.asm, getstr.asm, quit.asm
greet.asm. ,
NASM (,
syscall.inc ):

nasm -f elf -dOS_LINUX strlen.asm


nasm -f elf -dOS_LINUX putstr.asm
nasm -f elf -dOS_LINUX getstr.asm
nasm -f elf -dOS_LINUX quit.asm
nasm -f elf -dOS_LINUX greet.asm

, -dOS_LINUX ,
syscall.inc, strlen.asm
greet.asm . ,
, , ,
 .
NASM .o,
. -
, ld:

ld greet.o strlen.o getstr.o putstr.o quit.o -o greet

greet, ,
, ./greet.
160
5.4.

, -
, , ( -
 ), ,
, -
.
(global extern), -
; , ,
.
, , -
, -
, 
, . , ,
 -
,
,
.
,
, -
. , ,
( ) , -
,
,
, , , ( -
) ,
.
, ,
, ,
,
, , . -
, , -
,
. , ,
, -
, ()
,
.
, ,
, nm.

( ) -
.
161
5.5.

,
, -
, . ,
,
, -
; ,
, -
. ,
; , , , -
,
. , , -
, ,
.
 ,
,
,
.
:
-
. , ,
, - -
,
( ).
; , , -
, . .,
 .
-
.
Unix ar.
(
ar ),
, -
. ,
greet (, , , -
),
:

ar crs libgreet.a strlen.o getstr.o putstr.o quit.o


libgreet.a; . -
greet ,
, :
162
ld greet.o libgreet.a
:
ld greet.o -l greet -L .
, , -
, , ,
, , -
. -
.

5.6.

,
, -
,
, :
,  -l  -
,
, lib .a1 .
,
libgreet.a, -
greet. -l

(/lib, /usr/lib . .), -
-L; , -L . ,
, -
.
: -
(, resolved )
(unresolved links). -
, ( -
NASM global),
, ,
, ( NASM
, extern ),
-
.
, -
,
1 , -
.so; -
, .
163
, . ,
, -
. ,
, ;
,
. , ,
, -
. -
-
.
,
, ,
, , -
. -
; , -
.
( -
),
, -
,
; , -
, -
,
,
. -

. , , -
,
, , , , -
. , -
greet
libgreet.a getstr, putstr quit,
,
greet.o; strlen,
putstr.
-
.
, ( ) -
, ,
, -
; -
(. undened reference ).
 -
164
,
; , -
.
2 .
, -
, -
,
, ,
- -
, ,
. -
: , ,
.
,
,
, . -
,
.
, . , -
,
:
, -
, , - ,
. -
, ,
. ,
, , .
,
, .
-
( ) ,
,
++. -
, , ,
() , -
 ,
.

2
; , ,
++. , ,
.
165
6.

,
. ,
(, , -
) -
; , , -
(,
, , , ),
.
. -
, -
; -
( )
.
x86 ( 80386)
;
( -
), , -
: 8087 8086, 80287
80287, , , 80387 80386. -
386- ; -
,

- .
(486DX) -
-
. , -
- ( )
,
, ,
166
,
.

6.1.

 -
, -
M ( 1 6 M < 2)
P  , ,
. s -
: 1  , -
. , , N = (1)s M 2P .
, -
IEEE-754, -
, -
, , -
.
1, -
1 , -
.
( ,
. .); IEEE-754 -

:
, -
, .
IEEE-754 -
: ,
2 . 32
, , 
( 
127) 23  . -
64 , 11 ,
 52, 1023. -
, 80 , 15
16383, 64
,
( ).

1 , .
2  single precision, double precision
extended precision
167
, , , -
, . , -
, :
,  -
, (
,
, -
 ,
);
,  -
, ,

.
, , :
,  (-
);
, ( 80-
 ,
), , , -
, , , -
,  - QNAN (quiet not-a-
number); , -
QNAN;
, ( 80- 
, 10)
 - SNAN;
(, )
.

6.2.

80-
, R0, R1, ..., R7;
, Rn
ST0, ST1
. ., , R7 R0 (, R7
ST4, ST5
168
R0 ST(5) CR
R1 ST(6) SR
R2 ST(7) TW
R3 ST/ST(0)
top = 3
R4 ST(1)
R5 ST(2)
R6 ST(3) FIP
R7 ST(4) FDP

. 6.1.

R0, ST6 R1 . .) . 6.1 , -


R3;
Rn,
, , , -
, ,
, , , 
ST0  R2, R3 ST1,
. .
, -
, ST0, ST1, ..., ST7.
(R0, R1, ..., R7) ,
.
ST0, ST1, ..., ST7 NASM.
; , MASM
-
: ST(0), ST(1), ..., ST(7), -
. .
SR (state register) , -
, , .
, 13-, 12- 11- ( )
0 7, TOP , Rn -
. C0 ( 8), C2 (
10) C3 ( 14) -
CF, PF ZF. ST
, (SF), -
(P),
(O U), (Z) . -
CR , ,
, -
,
. , 11 10 -
: 00  , 01 
169
, 10  , 11  (
). TW -
R0R7:
00  , 01  , 10  -
- (NAN, ), 11 
. , -

,  .

, (
), , ,
. 6.7.3.
FIP FDP

-
() .
, -
, f -
oating (; -
oating point ). -
,
. -
, STn, . -
,
, IEEE-754, -
, ( -
dword),
.
NASM qword ( quadro word, -
),  tword ( ten
word ). (dq
, dt  ), -
(resq -
, rest 
). -
,
.

6.3.

fld ( oat load ), ,


, -
170
dword, qword
tword, STn. ,
fld st0
,

fld qword [matrix+ecx*8]


matrix, -
, , ECX.
SR TOP,
, ST1 . .
( )
fst fstp, .
, ,
ST6, , . -
, fst ,
(. . ST0), fstp -
, ST0
TOP. fst - 80- -
, fstp .
:

fstp st0
ST0 , -
ST0 ; ,
. ,
, ,
.

. fild -
(,
). , , -
word, dword qword (
). fist fistp : -
, ST0, -

, . fst fstp, -
fst , fstp
. fstp word, dword
qword, fst word dword.
fxch -
(ST0) STn,
171
. . fxch -
, ST0 ST1,
.
,
: fld1 ( 1.0), fldz (-
+0.0), fldpi ( ), fldl2e ( log2 e), fldl2t
( log2 10), fldln2 ( ln 2), fldlg2 ( lg 2).
;
TOP , ST0 -
. ,

.

6.4.


 fadd, fsub, fsubr, fmul, fdiv fdivr
,
dword qword. fadd fmul
ST0 ,
fsub ST0, fdiv ST0
, fsubr, , ST0 , fdivr -
ST0;
ST0. ,
ST1.
-
,
STn, ST0 ( -
, ). -

.
, ,
, , faddp, fsubp, fsubrp, fmulp, fdivp
fdivrp; - STn,
ST0;

ST0, TOP
; .
-
, ST1 ST0; -
,
,
172
. , -
. , -
,
( ).
-
(), ,
; -
, . , (x+y)(1z)
: x y + 1 z - *. x, y z -
() qword
.
,
:
fld qword [x] ; x
fld qword [y] ; y
faddp ; +
fld1 ; 1
fld qword [z] ; z
fsubp ; -
fmulp ; *
ST0. ,
-
; , -
:
fld qword [x]
fadd qword [y]
fld1
fsub qword [z]
fmulp
fiadd,
fisub, fisubr, fimul, fidiv fidivr,
ST0 ,
word dword -
.

. fabs ST0, fchs (
change sign  ) ST0 , -
frndint ST0
. ST0 -
 .
fprem, fprem1, fscale, fxtract -
.
173
6.5.

fsin, fcos fsqrt , , , -


, ST0,
ST0. fsincos :
, ,
ST1, ST0,
, .
fptan, -
. ST0, , -
ST0, 1,
, ,
ST0 , -
ST1.
:
fdivr; , , -
, fdivp,
fstp st0.
fpatan arctg xy , x  ST0, y  -
ST1. ,
, , .
y , .
, f2xm1, fyl2x fyl2xp.
f2xm1 2x 1, x  ST0, -
ST0. 1,
. fyl2x fyl2xp y log2 x y log2 (x + 1),
x  ST0, y  ST1; ,
,
, , .

-
fyl2xp1 x 1 + 22 ,
. -
, .
.

6.6.

-
, :
, ,
-
. ,
,
174
. -

.
fcom, fcomp fcompp. -
fcom fcomp 
dword qword, STn; ,
ST1. ST0 (
ST1, . fcomp fcom ,
ST0. fcompp, ,
ST0 ST1 .

C3 C0 SR (. . 169) :
C3 , C0 
; C3 , -
( , ST0)
( ST1), C0 -
,  . C3 ,
, ZF, C0  CF (
).
C2,
 , (,
 ,  -)
, 
C2 .

, CR FLAGS
.

fstsw ax
sahf

SR AX,
( !) FLAGS AH. ,
C3 ZF, C0  CF3 ,
: -
,
: ja, jb, jae, jbe, jna . . (. . 2.3
. 62). ,
, CF
ZF, -
, , .

3 , C2 PF.

175
, , a, b m qword, -
, m
a b. :

fld qword [b] ; b ( ST0)


fld qword [a] ; ST0, b ST1
fcom ;
fstsw ax ; AX
sahf ; - FLAGS
ja lpa ; a>b -
fxcn ;
lpa: ; ST0, ST1
fstp st0 ;
fstp qword [m] ;
. -
: ffree st0,
ST0 , fincstp,
TOP . 6.7.3.
ficom
ficomp, word
dword .
fcom fcomp:
ST0, C3, C2
C0. ficomp, ficom, ST0 . -
, ftst, ,
.

6.7.

6.7.1.
-
, -
,
.
:

1. (Invalid Operation, #I)  -


- , -
. .
:
( ), -
,
176
, , -
.
2. (Denormalized, #D)  -
, -
,
.
3. (Zero divider, #Z)  .
4. (Overow, #O) 
,
( -

- , -
, fst ,
).
5. (Underow, #U)  -
,
(
fst, . ).
6. (Precision, #P) 
;
.
CR SR
, :
0 , 1  -
, . .; 5 . , -
SR 6 .
CR ,
. ,

(. 4.2.2). , -
-
; ,
, (,
;
-
, . .)

() SR. -
, -
, , ;
177
,
, -
. ,
, ,
fclex. CR SR
6.7.3.

6.7.2. wait
, ,
, ,
; -
, . -
,
(, -
F), . ,
; -
, -
: -, F-
- , -
; , -,
-

, - F-
,
.

fwait wait (
). -
, -
; , -
, wait
, , ,
( Unix , -
, SIGFPE ,
).
, -
:
wait,  , .
fstsw: -
,  wait fnstsw;
fnstsw , ,
,  .
178
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

CR IC RC PC IEM PM UM OM ZM DM IM

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

SR B C3 TOP C2 C1 C0 IR SF PE UE OE ZE DE IE

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

TW tag7 tag6 tag5 tag4 tag3 tag2 tag1 tag0

. 6.2. CR, SR TW

fclex : -
wait fnclex.

6.7.3. CR, SR TW
, -
CR (Control Register ),

SR (Status Register ), . ,
, ,
TW (Tag Word ). , -
CR, SR TW, . 6.2.
 ; ,
CR SR -
(. 6.7.1). IC
IEM CR . RC
(Rounding Control ) : 00 
, 01  , 10  , 11 
. PC (Precision Control )
: 00  32- , 10  64- , 11  80-
( ,
).
SR C3, C2 C0
(. 6.6); C1 -
; SF . IR (Interrupt
Request ) -
, ;
-
, . -
179
TOP, ,
(. 6.2). , B (Busy ) ,
. ,
-
.
TW . 170.
CR fstcw, fnstcw
fldcw. fstcw, ,
wait fnstcw. ,
word.
CR
, , ,
CR . ,

:

sub esp, 2 ;
fstcw [esp] ; CR
or word [esp], 0000110000000000b
; 11 10
fldcw [esp] ; CR
add esp, 2 ;
SR -
fstsw, AX (
), word. -
fnstsw, fstsw -
wait fnstsw. ,
( ) SR , :
, . , -
. ,
TOP fincstp
fdecstp ( ). -
,
; , fdecstp ,
ST0 , fincstp -
, ST7 ( ST0).
SR, -
 .
fclex (Clear Exceptions ) fnclex,
.
fldcw fclex,
, CR - -
, , .
180
TW , ,
, , . -
ffree,  STn, 
( ). , -
:

ffree st0
fincstp

6.7.4. ,
( -
) ,
,
,
finit fninit (finit
wait fninit, . 6.7.2). CR 037Fh
( , ,
); SR , TOP=0,
, ; -
FIP, FDP, TW ; , ,
, TW ,
( ).
fsave -
, , ,
. ,
, -
, .
108 ;
fsave , , -
. fsave
 wait fnsave.
,
finit (. ), fsave
finit . -
frstor; fsave,
, , -
108 .
-
. fsetenv, fnsetenv
fldenv 28 ;
.
fnop.
, : .
181
: stud_io.inc
Linux
;; system dependend part ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; generic 3-param syscall
%macro _syscall_3 4
push edx
push ecx
push ebx
push %1
push %2
push %3
push %4
pop edx
pop ecx
pop ebx
pop eax
int 0x80
pop ebx
pop ecx
pop edx
%endmacro
; syscall_exit is the only syscall we use that has 1 parameter
%macro _syscall_exit 1
mov ebx, %1 ; exit code
mov eax, 1 ; 1 = sys_exit
int 0x80
%endmacro
;; system dependent part ends here ;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; %1: descriptor %2: buffer addr %3: buffer length


; output: eax: read bytes
%macro _syscall_read 3
_syscall_3 3,%1,%2,%3
%endmacro

; %1: descriptor %2: buffer addr %3: buffer length


; output: eax: written bytes
%macro _syscall_write 3
_syscall_3 4,%1,%2,%3
%endmacro

%macro PRINT 1
pusha
pushf
jmp %%astr
182
%%str db %1, 0
%%strln equ $-%%str
%%astr: _syscall_write 1, %%str, %%strln
popf
popa
%endmacro

%macro PUTCHAR 1
pusha
pushf
%ifstr %1
mov al, %1
%elifnum %1
mov al, %1
%elifidni %1,al
nop
%elifidni %1,ah
mov al, ah
%elifidni %1,bl
mov al, bl
%elifidni %1,bh
mov al, bh
%elifidni %1,cl
mov al, cl
%elifidni %1,ch
mov al, ch
%elifidni %1,dl
mov al, dl
%elifidni %1,dh
mov al, dh
%else
mov al, %1 ; memory location such as [var]
%endif
sub esp, 2 ; reserve memory for buffer
mov edi, esp
mov [edi], al
_syscall_write 1, edi, 1
add esp, 2
popf
popa
%endmacro

%macro GETCHAR 0
pushf
push edi
sub esp, 2
mov edi, esp
183
_syscall_read 0, edi, 1
cmp eax, 1
jne %%eof_reached
xor eax,eax
mov al, [edi]
jmp %%gcquit
%%eof_reached:
xor eax, eax
not eax ; eax := -1
%%gcquit:
add esp, 2
pop edi
popf
%endmacro

%macro FINISH 0-1 0


_syscall_exit %1
%endmacro

FreeBSD

_syscall3 _syscall_exit, . -
FreeBSD,
Linux :
;; freebsd-specific things ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%macro _syscall_3 4
push %4
push %3
push %2
mov eax, %1
push eax
int 0x80
jnc %%ok
neg eax
%%ok: add esp, 16
%endmacro

%macro _syscall_exit 1
push %1 ; exit code
mov eax, 1 ; 1 = sys_exit
push eax
int 0x80
; no cleanup - this will never return anyway
%endmacro
;; system dependent part ends here ;;;;;;;;;;;;;;;;;;;;;;;;;;;;

184

[1] . . . 4- . .: ,
2003.

[2] . . Assembler DOS, Windows UNIX. .:, 2006.

[3] . UNIX. .:, 1986.

[4] . . UNIX. - BHV,


-, 1997.

[5] The Netwide Assembler: NASM. http://www.nasm.us/doc/ -


, AsmOs group; . ,
http://opslab.org.ru/nasm
[6] Raymond Filiatreault. Simply FPU (an FPU tutorial). 2003.
http://www.ray.masmcode.com/fpu.html

http://www.stolyarov.info/books/asm_unix

, ,

185

. . . . . . . . . . . . . . . . . . 3
. . . . . . . . . . . . . . . . . . . . . 5
. . . . . . . . . . . . . . . . . . . . 7

1. 8
1.1. . . . . . . . . . . . . . . . . . . 8
1.2. -
. . . . . . . . . . . . . . . . 14
1.3. . . . . . . . . . . . . 17
1.3.1. . . . . . . . . . . . . . . . . . . . 18
1.3.2. ; . . . . . . . . 20
1.4. i386 . . . . . . . . . . . . . . . . . . . 22
1.5. . . . . . . . . . . . . . . . . . 24
1.6. stud_io.inc . . . . . . . . . . . . . . . 32

2. i386 33
2.1. i386 . . . . . . . . . . . . . . . . . . . . . 33
2.2. , mov . . . . . . . . . . . . . . . 37
2.2.1. . . . . . . 37
2.2.2. . . . . . . . . . . 39
2.2.3. mov . . . . . . . . . . . . . . . . . . . . . . 44
2.2.4. . . . . . . . . . . . . . . . . . . . . 45
2.2.5. . . . . . . . . . . . . 46
2.2.6. . . . . . . . . . 48
2.2.7. . 50
2.2.8. lea . . . . . . . . . . . . . . . . . . . . . . 52
2.3. . . . . . . . . . . . . . . . . . . 53
2.3.1. . . . . . 53
2.3.2. . . . . . . . . . 55
2.3.3. inc, dec, neg cmp . . . . . . . . . . . . . 55
2.3.4. . . . . . . . . 56
2.4. . . . . . . . . . . . . . . 58
186
2.4.1. . . . . . . . 58
2.4.2. . . . . . 60
2.4.3. . . . . . . . . 61
2.4.4. ECX; . . . . . 63
2.5. . . . . . . . . . . . . . . . . . . . . . . 65
2.5.1. . . . . . . . . . . . . . . . . . 65
2.5.2. . . . . . . . . . . . . . . . . . . . . 66
2.5.3. . . . . . . . . . . . . . . . . . . . . . . . . . 68
2.6. , , . . . . . . . . . . . . . . . . 72
2.6.1. . . . . . . . . 72
2.6.2. i386 . . . . . . . . 73
2.6.3. . . . . 75
2.6.4. : . . . . . . . . . . 75
2.6.5. . . . . . . . . 77
2.6.6. . . . . . . . . . . . 78
2.6.7. . . . . 81
2.6.8. . . . . . . . . . . . . . . . . . . . . 83
2.6.9. . . . . . . . . . . . . . . . . . . . . . . . . . 84
2.7. . . . . . . . . . . . . . . . . . . . . . . 90
2.8. . . . . . . . . . . . . . . 93
2.9. . . . . . . . . . . . . . . . . . . 94

3. NASM 95
3.1. NASM . . . . . . . . . . . . . 95
3.2. . . . . . . . . . . . . . . . . . . . . . . . . . 97
3.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
3.4. . . . . 100
3.4.1. . . . . 100
3.4.2. . . . . . . . . . . . . . . . 101
3.4.3. . . . 103
3.5. . . . . . . . . . . . . . . 103
3.5.1. . . . . . . . . . . . . . . . . . . . 103
3.5.2. . . . . . . . . . . . 105
3.5.3. ; . . . . . 108
3.5.4. . . . . . . . . . . . . . . . . . 110
3.5.5. . . . . . . . . . . . . . . . . . . . 114
3.5.6. . . . 116
3.5.7. . . . . 118
3.5.8. . . . . . 120
3.6. NASM . . . . . . . . . . . . . . . . . . . 121
187
4. 123
4.1. . . . . . . . . . . . 123
4.1.1. . . . . . . . 123
4.1.2. . . . . . . . . . . . . . . . . . . . . 124
4.1.3. . . . . . . . . . . . . . . 126
4.1.4. . . . . . . . . . . . . . . 127
4.1.5. . . . . . 128
4.2. . . . . . . . . . . . . . . . . . . . . . . . . 131
4.2.1. () . . . . . . . . . 132
4.2.2. () . . . . . . . . . 133
4.2.3. . . . . . . . . . . . . . . 134
4.3. Unix . . . . . . . . . . . . . . . . . 135
4.3.1. Linux . . . . . . . . . . . . . . . . . . 137
4.3.2. FreeBSD . . . . . . . . . . . . . . . . 138
4.3.3. Unix . . . . . . . . . 140
4.4. . . . . . . . . . . . . . . . . 143
4.5. : . . . . . . . . . . . . . . . . . 146

5. 153
5.1. . . . . . . . . . . . . 153
5.2. NASM . . . . . . . . . . . . . . . . . 155
5.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
5.4. . . . . . . . . . . . . . . . 161
5.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
5.6. . . . . . . . . . . . . . 163

6. 166
6.1. . . . . . . . . . . . . . . 167
6.2. . . . . . . . . . 168
6.3. . . . . . . . . . . . . . . . 170
6.4. . . . . . . . . . . . . . 172
6.5. . . . . . . 174
6.6. . . . . . . . . . . . 174
6.7. . . . . . . . . . . . . . . . . . . 176
6.7.1. . . . . . 176
6.7.2. wait . . . . . 178
6.7.3. CR, SR TW . . . . . . . . . . . . . . . . . . 179
6.7.4. , . . . 181

: stud_io.inc . . . . . . . . . . . . . 182
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185