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

..

.. , .. , ..


"
"
(- )
1


2012

004.2+004.43(075.8)
32.973-0273
89

-

..

:
C.. ,
.. ,

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

. . .
.. ( 05899 24.09.2001); , 2012.
64 .

ISBN 978-5-89407-499-3
ISBN 978-5-317-04302-5

,
, 1
1
2010-2012 . ,
, .
004.2+004.43(075.8)
32.973-0273

ISBN 978-5-89407-499-3


.. , 2012

ISBN 978-5-317-04302-5

.., .., .., 2012


............................................................................................................................. 5
1. .................................................................. 6
............................................................................................. 8
................................................................................. 9
...................................................................................................................... 10
.............................................................................................................. 11
1-1 ....................................................................... 11
...................................................................................... 12
....................................................................................................... 13
................................................................................................ 14
1-2 ........................................................... 15
1-3 ...................................................... 15
/ ............................................................................................... 17
1-4 Hello, World! ............................................................................................. 18
............................................................................................................................ 19
2. ......................................................... 23
............................................................ 23
2-1 ....................................... 23
2-2 ..................................................................................... 24
2-3 ............................................................. 25
2-4 ............................................................................................... 26
2-5 .................................................................................................... 27
............................................................................................................................ 28
3. ......................................................................... 31
................................................................................ 31
................................ 31
3-1 ................................................................... 32
3-2 ................................................... 32
3-3 .............................................................................. 34
3-4 .......................................................... 35
3

............................................................................................................................ 36
4. ...................................................................................... 39
4-1 ....................................... 40
4-2 ........................................ 41
............................................................................................................................ 43
5. ........................................................................ 48
5-1 .............................................................................. 48
5-2 .................................................................................. 49
5-3 ................................................................... 49
5-4 ...................................................... 50
5-5
............................................................................................................................ 51
............................................................................................................................ 51
6. ..................................................................................................... 54
........................................................................................................................ 62



, 1 1
. ,
: ,
, .

, .
,
,
.
,

1 ,
.

1.

() , IA-32/Linux
IA-32/Windows, ,
(. 1).
,
.
8 (1 ), 0
232-1 ( ). 32-
, ,
, .
,
, .

1 - IA-32,
.
, ,
.
,
( ) .

; , ,
(. 2).
IA-32 ,
, ,
. ,
:
,
6

. ,
, ,
.

2 - .

,
.
.
. ()
.

.

, , ( ):
, , .
, ,

.
, , ..
.

() . IA-32
: AT&T Intel.
, .
NASM (Netwide Assembler),
, Microsoft
7

Windows UNIX-.
http://www.nasm.us/.

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

, .text;
( ,
), .data;

,
;
.bss.
. ,
,
. . ,
,
:

,
, (/);

, (
), ;

.
. ,
, ,
.

:
:

( ,
). ,

.
.
, , ,
8

, ,
. ,
.
.

:
_ [:]

__

, ;

, .

DB, DW, DD DQ. :
_ DD 1[, 2, ... ]

, ,
, , _.
NASM ( MASMe
?), RESB, RESW,
RESD RESQ, :
_ RESB _

TIMES ( DUP
MASMe):
TIMES _

zerobuf ,
64 .
zerobuf times 64 db 0

TIMES , ,
, , :
9

buffer: db hello, world!


times 64-$+buffer db

buffer 64 ,
, . $
. $-buffer,
, hello, world!.

NASM : ,
, .
: ,
, , .
: d, b y, o q, h. .
, : 0d
0b , 0o , 0h .
-,
0x.

.
mov
mov
mov
mov
mov
mov
mov

ax,200
ax,0200d
ax,0c8h
ax,0xc8
ax,310q
ax,11001000b
ax,1100_1000b

;
;
;
;
;
;
;

.
,
, . NASM ,
,
, , .
.
, , ,
. ,
,

10

: 'abcd' 0x61626364, 0x64636261.


:
, . ,
, , abcd, dcba.
db/dw/dd/dq.


.
dd 'ninechars'

;
;
dd 'nine','char','s' ;
db 'ninechars',0,0,0 ;

.

(storage duration):
, , .
. ,

.data, .bss. ,

, .
1-1
.

%include 'io.inc'
section .text
global CMAIN
CMAIN:
MOV EAX, 0
RET

;
;
;
;
;
;
;

(1)
(2)
(3)
(4)
(5)
(6)
(7)


. .
,
io.inc, /.

11

#include <stdio.h> , .
, /.
,
io.inc, /.
, , , ,
( .text).

.
, CMAIN
. CMAIN,
io.inc .
,
6. , CMAIN
.
main -.
MOV, EAX
0. RET,
.
-.
#include <stdio.h>
int main () {
return 0;
}

6 return 0;

0.

IA-32 32- :
EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP (. 3).
AX,
BX, CX, DX, SI, DI, BP, SP. ,
16- . , AX, BX,
CX, DX
12

AL, AH, BL, BH, CL, CH, DL, DH,


8- . L
16- , H
.

3 IA-32.

IA-32
MOV. ,
:

MOV , ;

MOV -1, -2 -2 -1;

MOV , ;

MOV , ;

MOV , .
, (
), .
Intel. , ,
MOV (
AX EBX).

13

%include 'io.inc'
section .text
global
CMAIN:
MOV
MOV
MOV
MOV
RET

CMAIN
EAX, 1
EBX, EAX
CL, 040h
EAX, 0

;
;
;
;
;
;
;
;
;
;

(1)
(2)
(3)
(4)
(5)
(6) EAX := 1
(7) EBX := EAX = 1
(8) CL := 040h = 0x40 = 64
(9)
(10)

:
[ ],
(dword, word byte) .
, dword [a] a (32
).


ADD SUB. ,
.
ADD SUB . ,
( ) .
.
. (,
) ,
.
: , ,
.
ADD EBX, EAX
; EBX := EBX + EAX = 1 + 1 = 2, EAX
ADD EBX, EBX
; EBX := EBX + EBX = 2 * EBX = 4
SUB EAX, 2
; EAX := EAX - 2 = 1 - 2 = -1 = 0FFFFFFFFh
ADD AX, 1
; AX := AX + 1 = 0FFFFh + 1 = -1 + 1 = 0
; EAX 0FFFF0000h

14

1-2
A 0x CAFE BABE.
AX
.
MOV AX, WORD [A + 2]
ADD AX, 3

A ,
AX, . IA-32
, :

A+2 0xFE, 0xCA.


, AX
0xCAFE. , 3,
0xCB01.
1-3
4 a, b, c d 1
. 32-
EAX ,
a, b, c, ,
, d. , , a, b, c d
1, 2, 3, 4 . EAX
0x01020304.

,
32- a.

15

.

,
.

section .bss
tmp resd 1
section
a db
b db
c db
d db

.data
1
2
3
4

section .text
mov al, byte [a]
mov byte [tmp + 3], al
mov al, byte [b]
mov byte [tmp + 2], al
mov al, byte [c]
mov byte [tmp + 1], al
mov al, byte [d]
mov byte [tmp], al
mov eax, dword [tmp]

16

/
, ,
() , io.inc.

, . ,
,
/ .
1. /

PRINT_UDEC size,
data

data
10- . size ,
;
PRINT_DEC size, data 1, 2, 4. data
,
, ,
(
). ,
size
. PRINT_UDEC
, PRINT_DEC .
PRINT_HEX size, data , 16 .
PRINT_CHAR ch

, ch.

, , ,
(
).
8 .

PRINT_STRING data

,
0.
,
(
). ,
0 .

NEWLINE

17

GET_UDEC size, data

10-
.
size, (1, 2, 4).

. data ,
, (
).
,

GET_DEC GET_UDEC. GET_UDEC
, GET_DEC .

ESP.

GET_DEC size, data

GET_HEX size, data

, 16 0x.

GET_CHAR data

,
, Enter .
, Enter
0xD 0xA.
, 1 ,
.

GET_STRING data,
maxsz

,
( ),
maxsz . data
, (
).
maxsz .
0.

1-4 Hello, World!


Hello, World! .

1-1: .text,
.data,
( 4).
str, .
,

18

hello.asm:4: error: comma, colon or end of line expected


, IA-32
str. str

str. ,
, .
%include 'io.inc'
section .data
str: db `Hello, World!\n`, 0
section .text
global CMAIN
CMAIN:
PRINT_STRING [str]
MOV EAX, 0
RET

;
;
;
;
;
;
;
;
;
;
;
;

(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)

,
.
, - 0 .
- ,
.
1-1 10
PRINT_STRING. ,
, Hello, World!\n.

, , ,
.
1-1
.
,
, ,
.

19


()

16-

()

-67

01110110
28h
135
1-2
,
16- .
section
a dw
b dd
c db
d dw
e dd
f db

.data
-2076
-1
128
256
-15
-100

1-3
DB, , .
; a
DW 185Ah
; b
DW 90
; c
DW 9000
; d
DW 10000, -10000

; e
DW 'AB'
; f
DW "NASM"
; g
DW "Hello"
; h
DW "w", "orld", "!"

1-4
.
b
w
y
d

resb
resw
resw
resd

1
1
1
1

20

; i
DD 97
; j
DD "x"
; k
DD 'CD'
; l
DD "Hello, world!"

; a
mov
; b
mov
; c
mov
; d
mov

b, 1
byte [b], 1
word [b], 1
ax, bx

; e
mov
; f
mov
; g
mov
; h
mov

ecx, cx
bh, cl
dword [d], esi
byte [w], ch

; i
mov
; j
add
; k
sub
; l
sub

word[w], word[y]
15, bx
word [y], 8
eax, dword [d]

1-5
EAX,
. ,
- -
EAX.
section
a dw
b dw
c dw
d dw

.data
0xDEAD
0xF00D
0xCAFE
0xBABE

section .text
movsx eax,
movsx ax,
movzx eax,
movsx eax,
movzx ax,
movsx eax,
mov
bx,
movsx ax,

word
byte
word
byte
byte
word
word
bh

[a +
[b]
[c]
[b +
[d +
[b +
[a +

1] ;
;
;
2] ;
1] ;
1] ;
1] ;
;

()
()
()
()
()
()
()

21

section
a dw
b dw
c dw
d dw

.data
0x0DEC
0x4A6F
0x7921
0xFEFF

section .text
mov
ebx,
mov
eax,
movzx ax,
mov
ecx,
mov
eax,
movsx ax,
mov
eax,
bswap eax

dword
-1
bl
dword
dword
cl
dword

[b]

;
;
; ()
[a + 1] ;
[b + 1] ;
; ()
[c]
;
; ()

1-6
, ,
.

22

2.


ADD, SUB, NEG, MUL/IMUL, DIV/IDIV. ,
DEC INC.
,
ADC SBB.
LEA.

EFLAGS: CF , OF , SF , ZF , PF
.
2. .
T

M
( ,
)


3.
OF
ADC, SBB
M
ADD, SUB, NEG
M
MUL, IMUL
M
DIV, IDIV
DEC, INC
M
MOV, XCHG, MOVSX, MOVZX, LEA

.
SF
M
M
M

ZF
M
M
M

PF
M
M
M

CF
TM
M
M
-



.
.

, .
2-1
AL (
), CF, OF, ZF SF
.

23

MOV AL, 70
SUB AL, 130

AL = -60, AL = 196, CF = 1, OF = 1, ZF = 0, SF = 1.

.
70 = 46h, 130 = 82h
46h 82h = C4h, CF (Carry Flag)
1, .
(C4h) 0, , ZF (Zero Flag)
0. SF (Sign Flag)
, .. . 1, , SF
1.

:
C4h = 196 ,
.
-60 (196 = 256 60 = (-60)),
, , 60.
OF (Overflow Flag)
. ,
( 130) -126
(130 = (-126)). ,
:
70 (-126) = 196
(196)
[-128, 127], , OF
1.
2-2

.
static short int a = 10;
static int b = 20, ;
c = a + b;

24


a b
.data. c
.bss.
.
,
. a int.
section .data
a dw 10
b dd 20
section .bss
c resd 1
section .text
movsx eax, word [a] ;
add
eax, dword [b] ;
mov
dword [c],eax ;

2-3

.
unsigned char z = 0xff;
unsigned short a = 0xff00;
unsigned int b;
b = a * z;

, integer promotions,
.

:
INT_MIN < 0 < USHRT_MAX UCHAR_MAX < INT_MAX
32- IA-32 gcc, ,
.

25

4. .

INT_MIN

USHRT_MAX

UCHAR_MAX
char
INT_MAX

-2147483648
65535
255
2147483647

limits.h.
,
,
b. ,

.
section .bss
b resd 1
section .data
z db 0xff
a dw 0xff00
section .text
movzx eax, byte [z] ;
;
movzx edx, word [a] ;
;
imul eax, edx
;
mov dword [b], eax ;


8 32

16 32

2-4

. ,
.
static int a, b = 1, c = -2, d = 3;
...
a = b + c * d;

26


a
.bss. .data.
section .bss
a resd 1
section .data
b dd 1
c dd -2
d dd 3

c d.
IMUL , ,
EAX. EDX:EAX.
, .. EDX,
,
- (
).
EAX .
section
mov
imul
add
mov

.text
eax, word [c]
dword [d]
eax, dword [b]
dword [a], eax

2-5

.
static int x, y;
...
x /= -y;

x y ,
.

27

section .bss
x dd 1
y dd 1

, ,
x y , ,
.
section .text
mov eax, dword [] ;
;
;
;
;
;
;
;
;
mov edx, eax
;
sar edx, 31
;
;
;
;
;
mov ecx, dword [y] ;
;
neg ecx
;
;
idiv ecx
;
mov dword [x], eax ;
;

eax
x.

,
edx:eax
, 32-
64 .
32
.
x edx
31

, ..
, edx:eax
.
ecx
y
, ..
(y)

eax
, x

2-1
b = a; ( ) :
// a
static unsigned char a;
static unsigned int b;
// b
static char a;
static short b;

28

2-2
AL (
), CF, OF, ZF SF
.
; a

; c
MOV AL, 199
ADD AL, -61

MOV AL, -13


ADD AL, 179

MOV AL, -35


SUB AL, 216

; d
MOV AL, 2
SUB AL, 200

; b

2-3

.
// a
static int x, y;
y = (x / y) * (x % y);
// b
static unsigned char a;
static int b;
b = (a - 500000) % 10;

2-4

static unsigned short n; // 100 <= n <= 999

n ,
n.
2-5 64 32
64- x y. .
// a
x += y;
// b
x -= y;

29

2-6
, .
X RESD 1
ADC WORD [X], WORD [EAX]
MUL AL, AH
NEG CF
SBB DWORD [X], 100
MOV EAX, X
DIV 15
SUB EAX, WORD [X]
MOVSX EBX, BL
XCHG WORD[X], 100
IDIV BYTE [EAX]
MOV AL, -150
IMUL EAX, 5

30

3.


;
,
.bss.
.
xp.
static int *xp;
static int x;
xp = &x;

section .bss
xp resd 1
x resd 1
section .text
mov
dword [xp], x


.bss.
static int *xp;
static int x, y;
x = *xp;
*xp = y;
section .bss
xp resd 1
x resd 1
y resd 1
section .text
mov edx, dword [xp]
mov eax, dword [edx]
mov dword [x], eax
mov eax, dword [y]
mov dword [edx], eax

;
;
;
;

EDX xp
EAX
, xp
x

;
;
;
;
;

EAX y
EDX
xp.
,
EAX

31

3-1
p:
static int **p;

,
**p + 1 ,
PRINT_DEC.


.
%include 'io.inc'
section .text
global CMAIN
CMAIN:
MOV EAX, DWORD [p]
MOV

EAX, DWORD [EAX]

MOV EAX, DWORD [EAX]


INC EAX
PRINT_DEC EAX
XOR EAX, EAX
RET

;
;
;
;
;
;
;
;
;
;

EAX
p

, EAX
p, *p
EAX **p

3-2
.
static short *px, *py;
...
*px++ = --*py;

,
. -
short*. , short ,
32

, IA-32 32- , ..
4 .
Nasm:
section .bss
px dd 1
py dd 1

.
,
.bss,
.data.

, , . ,
.
*px++ = --*py;
*(px++) = --(*py);
--(*py);
*px = *py;
px++;
*py = *py 1; // (1)
*px = *py;
// (2)
px = px + 1; // (3)

33

section .text
mov eax, dword [py] ; (1) eax
;
py
dec word [eax]
;
16-
;
,
;
eax, .. py
mov cx, word [eax] ; (2) cx
;

mov eax, dword [px] ;


eax
;
px
mov word [eax], cx ;
,
;
,
;
px, *py
add dword [px], 2
; (3) px.
;

;

;
1 * sizeof(short), . 2

3-3
,
void decode1(int *xp, int *yp, int *zp)

.
.
mov
mov
mov
mov
mov
mov
mov
mov
mov

edi, dword [ebp + 8]


edx, dword [ebp + 12]
ecx, dword [ebp + 16]
ebx, dword [edx]
esi, dword [ecx]
eax, dword [edi]
dword [edx], eax
dword [ecx], ebx
dword [edi], esi

;
;
;
;
;
;
;
;
;

(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)

xp, yp zp 8, 12 16
, ebp. decode1
, .


.
34

mov edi, dword [ebp + 8] ; (1) edi xp


mov edx, dword [ebp + 12] ; (2) edx yp
mov ecx, dword [ebp + 16] ; (3) ecx zp


, .. - ,
ebx, esi, eax.
mov ebx, dword [edx]
mov esi, dword [ecx]
mov eax, dword [edi]

; (4) ebx *yp


; (5) esi *zp
; (6) eax *xp

edi, edx, ecx, ,


,
, ebx, esi, eax.
mov dword [edx], eax
mov dword [ecx], ebx
mov dword [edi], esi

; (7) *yp eax *xp


; (8) *zp ebx *yp
; (9) *xp esi *zp


, -.

void decode1(int *xp, int *yp, int *zp) {


int y = *yp;
int z = *zp;
int x = *xp;
*yp = x;
*zp = y;
*xp = z;
}

y ebx, z esi, x eax.


3-4
-
.

35

static int *p[10];


static int x;
x = *p[8] + 1;

, .

p . 40
, , 4
.
8- p+32,
, .. ,
. ,
1, .
mov
mov
add
mov

eax, dword [p + 32]


eax, dword [eax]
eax, 1
dword [x], eax

3-1 -
,
void decode2(int *xp, int *yp, int *zp)

.
.

36

mov
mov
mov
mov
mov
mov
add
mov
mov
mov
mov
mov
mov

eax, dword [ebp +


edi, dword [eax]
eax, dword [ebp +
edx, dword [eax]
eax, dword [ebp +
eax, dword [eax]
edx, eax
eax, dword [ebp +
dword [eax], edx
eax, dword [ebp +
dword [eax], edx
eax, dword [ebp +
dword [eax], edi

12]
8]
16]

12]
8]
16]

xp, yp zp 8, 12 16
, ebp. decode2
, .
3-2
,
.
// a
int x, *px;
*px++ = x -10;
// b
short x, *px;
x = *(--px + 4);

3-3
100 . ,
eax
, :
a) char,
b) short,
c) int.
3-4
-
.

37

static int a[50];


static int *p, *q;
static int n;
p = &a[10];
q = &a[25];
n = q - p;

3-5 -
-
.
// a
short **p;
++*(*(p+=3) -= 2);
// b
char **q, **r, **t;
r = q++;
*t = (*r)++;
**q = (**t)++;

38

4.

IA-32
jmp Jcc, ,
EFLAGS.
4. .
Jcc

JE

ZF

JNE

~ZF

JS

SF

JNS

~SF

JG

~(SF^OF)&~ZF

( )

JGE

~(SF^OF)

( )

JL

(SF^OF)

( )

JLE

(SF^OF)|ZF

( )

JA

~CF&~ZF

( )

JB

CF

( )


: (1)
EFLAGS (2) . , ,
CMP, EFLAGS ,
SUB, , ..
.
IA-32 do-while
. ,
ECX (CX), .
, LOOP/LOOPcc
[128, 127] .
LOOP/LOOPcc ECX ,
, . ,
ECX,
232 .

39

5. .
JCXZ/JECXZ

, CX/ECX
.

LOOP

, ECX
.

LOOPZ/LOOPE

, ECX
ZF .

LOOPNZ/LOOPNE

, ECX
ZF .

4-1
, data_t
, COMP . a
EDX, b ECX.
, ,
data_t COMP ( ).
int comp(data_t a, data_t b) {
return a COMP b;
}

CMP ECX, EDX


SETL AL

SETcc L, ,
EAX EDX 32 .
int long,
signed.
>, L
1 < 2 .
1 < 2 ECX < EDX b < a
a > b

, .

40

int comp(int a, int b) {


return a > b;
}

4-2

.
SECTION .text
GLOBAL CMAIN
CMAIN:
MOV ESI, DWORD [a]
TEST ESI, ESI
JE
.1
MOV ECX, DWORD [b]
TEST ECX, ECX
JE
.1
MOV EDX, DWORD [ESI]
MOV EAX, EDX
SAR EDX, 31
IDIV ECX
SUB DWORD [ESI], EDX
.1:
XOR EAX, EAX
RET

;
;
;
;
;
;
;
;
;
;
;

(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)

; (12)
; (13)


a b.
32-. ,
, .
.
static int a, b;

:
3 6 12.

( TEST JE) .
1-6 :

41

if ((0 != a) && (0 != b)) {


...
}

if (0 != a) {
if (0 != b) {
...
}
}

,
&& , 4-6
,
3.
7-11.
ESI, a
, .. a ,
.
static int *a, b;

, , eax edx,
edx
, , , 10 .
,
signed,
int, .
idiv *a b,
ecx
b.
. ,
,
. edx ( 11),
.
, ,
a.
.
*a = *a *a % b;

,
if, .
42

static int *a, b;


if ((0 != a) && (0 != b)) {
*a -= *a % b;
}

4-1

int comp(data_t a) {
return a COMP 0;
}

data_t , COMP -
. a EX.
, ,
data_t COMP ( ).
(A)
TEST EX, EX
SETNE AL

()
TEST L, L
SETG AL

()
TEST X, X
SETE AL

()
TEST X, X
SETA AL

4-2
, data_t
, COMP . a
EDX, b ECX.
, ,
data_t COMP ( ).
int comp(data_t a, data_t b) {
return a COMP b;
}

43

()
CMP DX, CX
SETGE AL

()
CMP EDX, ECX
SETNE AL

()
CMP DL, CL
SETB AL

4-3
-.
int x, y = 100, a = 0, b = 1;
x = y <= 0 ? a : b;

x = (y <= 0? a : b) ,
CMOVcc.
4-4
,
.
4-5
-.
void cond(int a, int *p) {
if (p && a > 0) {
*p += a;
}
}

.
:
mov edx, dword [ebp + 8] ; int a
mov eax, dword [ebp + 12] ; int *p

4-6
,
.

44

4-7
,
. : ,
, , .
4-8 :
, -.
mov eax, dword [ebp + 8] ; x
mov edx, dword [ebp + 12] ; y
cmp eax, -3
jge l2
cmp eax, edx
gle l3
imull eax, edx
jmp l4
l3:
lea eax, dword [eax + edx]
jmp l4
l2:
cmp eax, 2
jg l5
xor eax, edx
jmp l4
l5:
sub eax, edx
l4:

- .
int test(int x, int y) {
int val = __________;
if (______) {
if (______) {
val = __________;
} else {
val = __________;
}
} else if (______) {
val = __________;
}
return val;
}

45

4-9 :
, .
mov
mov
cmp
jg
jl
xor
jmp

ebx,
ecx,
ebx,
.L7
.L8
eax,
.L9

dword [a]
dword [b]
ecx

mov
mov
sar
sub
idiv
jmp

edx,
eax,
edx,
ecx,
ecx
.L9

dword [c]
edx
31
ebx

mov
mov
sar
sub
idiv

edx,
eax,
edx,
ebx,
ebx

dword [c]
edx
31
ecx

mov

dword [d], eax

...
static _____________________
static _____________________
static _____________________
static _____________________
if (__________) {
d = ________________;
} else if (__________) {
d = ________________;
} else {
d = ________________;
}

eax

.L8:

...

.L7:

.L9:

-.
4-10
, .
4-11 :

int dw_loop(int x, int y, int n) { //1
{
//2
do {
//3
x += n;
//4
y *= n;
//5
n--;
//6
} while ((n > 0) && (y < n));
//7
return x;
//8
}

46

a;
b;
c;
d;

mov eax, dword [ebp + 8] ;


mov ecx, dword [ebp + 12] ;
mov edx, dword [ebp + 16] ;
L2:
;
add eax, edx
;
imul ecx, edx
;
sub edx, 1
;
test edx, edx
;
jle L5
;
cmp ecx, edx
;
jl L2
;
L5:
;

(1) x
(2) y
(3) n
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)

, -,
, , -.
4-12
,
:
mov al, -80
cmp al, 150
jb M1
jl M2
jmp M3

4-13
(unsigned int).
4-14
,
N (N > 1). : F0
= F1 = 1, Fk = Fk-1 + Fk-2, k 2.
4-15
30 . :
a) ,
b) (,
),
c) .

47

5.

5-1
a, b, c, d.
0 1.
a.
static int a, b, c, d;
a = ~(a & b) | ((~c & d) | (c & ~d));

a, b, c d , .bss.
,
.
, (~c & d) | (c & ~d)
, ..

, .
section .bss
a resd 1
b resd 1
c resd 1
d resd 1
section
mov
mov
mov
not
and
not
and
or
mov
and
not
or
mov

48

.text
eax, dword
edx, dword
ecx, eax
eax
eax, edx
edx
ecx, edx
eax, ecx
edx, dword
edx, dword
edx
eax, edx
dword [a],

[c]
[d]

[a]
[b]

eax

;
;
;
;
;
;
;
;
;
;
;
;
;

eax c
edx d
c ecx
eax ~c
eax ~c & d
edx ~d
ecx c & ~d
eax (~c & d) | (c & ~d)
edx a
edx a & b
edx ~(a & b)
eax ~(a & b) | ((~c & d) | (c & ~d))
a ~(a & b) | ((~c & d) | (c & ~d))

5-2
a b 2 . EAX
: 2
a, 2 b,
5 .

section .bss
a resw 1
b resw 1
section .text
mov ax, word [a]

shl eax, 16
mov ax, word [b]
ror ax, 5

;
;
;
;
;
;
;

a
16
EAX

b
EAX

5-3
, ,
.
int arith(int x, int y, int z) {
int t1 = _________;
int t2 = _________;
int t3 = _________;
int t4 = _________;
return t4;
}

, .
: x [ebp + 8], y
[ebp + 12], z [ebp + 16].
mov
xor
shr
not
sub

eax,
eax,
eax,
eax
eax,

dword [ebp + 12]


dword [ebp + 8]
3
dword [ebp + 16]

;
;
;
;
;

(1)
(2)
(3)
(4)
(5)

49


y eax. ,
-.
xor y x.
eax. ( 3)
3 . , ,
.
, , 5,
z.
, ,
eax.
,

.
int arith(int x, int y, int z) {
int t1 = y ^ x;
int t2 = t1 >> 3;
int t3 = ~t2;
int t4 = t3 - z;
return t4;
}

t4,
5 EAX,
.
5-4
32- a.
3 1, 17 0, 23
.

bts, btr, btc.

50

section .bss
a resd 1
section .text
bts
dword [a], 3
btr
dword [a], 17
btc
dword [a], 23

5-5

, eax 6,
. eax.

mov ebx, eax


shl ebx, 2 ;
;
shl eax,1 ;
;
add eax, ebx
;

; ebx
ebx 2
( 4)
eax 1
( 2)
; ,
eax

5-1
x .
, eax 1, x < 0, 0
.
// a
static int x;

// b
static short x;

// c
static char x;

5-2
eax 5 5 .
5-3
.

51

// a
static unsigned x, y;
y = 32 * x x / 8 + x % 16;
// b
static long long x;
x *= 2;
// c
static int x, y;
y = x / 64 + x * 4;

5-4
eax ( ) edx (
) .
section .data
x dd 0xfeff0201
section .text
mov cx, word [x + 1]
mov ax, cx
imul ah
shl ax, cl
movsx eax, ax
movzx edx, ax

5-5

.
5-6
eax 1,
5 , 0 .
5-7
,
L , a .
5-8
"" .
5-9 sign
sign, MOV, SHR, NEG OR.

52

5-10
, ,
1 0 .
: 10100111 00001000
5-11
ROL, ROR, RCL, RCR SAR,
, .
) sar eax, 2 b) rol bx, 1

c) rcr ecx, 1 d) rol ax, 8

e) rcl ebx, 31

53

6.

1-1

()
10111101
01110110
00101000
10000111

16-

()
BDh
76h
28h
87h

189
118
40
135

1-5
() EAX = 0xdde

() EAX = 0xfffffef0

() EAX = 0xd

() EAX = 0xffff000d

() EAX = 0xcafe

() EAX = 0xffff006f

() EAX = 0xfffffffe

() EAX = 0xff79000d

() EAX = 0xffff00ba

() EAX = 0x2179fffe

1-6
section .bss
a resd 1
b resd 1
section .text
mov al, byte [a]
mov byte [b], al
mov al, byte [a +
mov byte [b + 3],
mov ax, word [a +
mov word [b + 1],

2-1
; a
movzx eax, byte [a]
mov dword [b], eax
; b
movsx eax, byte [a]
mov word [b], eax

54

-67
118
40
-121

1]
al
2]
ax

2-2
(a) AL = -118,
(b) AL = 5,
(c) AL = -90,
(d) AL = 58,

AL = 138,
AL = 5,
AL = 166,
AL = 58,

CF = 1, OF = 0, ZF = 0, SF = 1.
CF = 0, OF = 0, ZF = 0, SF = 0.
CF = 1, OF = 0, ZF = 0, SF = 1.
CF = 1, OF = 0, ZF = 0, SF = 0.

3-1
void decode2(int *xp, int *yp, int *zp) {
int y = *yp;
*yp = *xp + *zp;
*xp = *yp;
*zp = y;
}

4-1
()
data_t int, unsigned, ,
long
COMP - !=
()
data_t short unsigned short
COMP - ==
()
data_t char
COMP - >
()
data_t unsigned short
COMP - >

55

4-2
()
data_t short
COMP - >=
()
data_t unsigned char
COMP - <
()
data_t int, unsigned, ;
long
COMP - !=

4-3
section .bss
x resd 1
section .data
y dd -1
a dd 0
b dd 1
section .text
global CMAIN
CMAIN:
cmp dword [y], 0
mov eax, dword [b]
cmovle eax, dword [a]
mov dword [x], eax

56

4-4
%include 'io.inc'
section .text
global CMAIN
CMAIN:
GET_DEC 4, eax
cmp eax, 0
jge label
neg eax
label:
PRINT_DEC 4, eax
NEWLINE
xor eax, eax
ret

4-5
test eax, eax
jz label
test edx, edx
jle label
add dword [eax], edx
label:

4-6
%include 'io.inc'
section .text
global CMAIN
CMAIN:
GET_DEC 4, ecx
mov eax, 1
jecxz end_label
loop_body:
imul eax, ecx
loop loop_body
end_label:
PRINT_DEC 4, eax
mov eax, 0
ret

57

4-7
%include 'io.inc'
section .text
global CMAIN
CMAIN:
GET_DEC 4, ebx ;
GET_DEC 4, edx ;
GET_DEC 4, ecx ;
xor eax
jexcz end_label
loop_body:
add eax, ebx
add ebx, edx
loop loop_body
end_label:
PRINT_DEC 4, eax
mov eax, 0
ret

4-8
int test(int x, int y) {
int val = x^y;
if (x < -3) {
if (y < x) {
val = x * y;
} else {
val = x + y;
}
} else if (x > 2) {
val = x - y;
}
return val;
}

.
. .
.
CMP Jcc,
if . ,
.

58

int test(int x, int y) {


int val;
if (x < -3) {
if (y < x) {
val = x * y;
} else {
val = x + y;
}
} else if (x > 2) {
val = x - y;
} else {
val = x^y;
}
return val;
}

4-9
...
static int a;
static int b;
static int c;
static int d;
if (a > b) {
d = c / (a - b);
} else if (a < b) {
d = c / (b - a);
} else {
d = 0;
}
...

59

4-10
%include 'io.inc'
section .text
global CMAIN
CMAIN:
mov eax, 1
.loop1:
cmp eax, 10
jge .exit1
xor ebx, ebx
.loop2:
cmp ebx, 10
jge .exit2
cmp eax, ebx
je .lc
PRINT_DEC 4, eax
PRINT_DEC 4, ebx
NEWLINE
.lc:
inc ebx
jmp .loop2
.exit2:
inc eax
jmp .loop1
.exit1:
xor eax, eax
ret

4-11
mov eax, dword [ebp + 8] ; (1) x
mov ecx, dword [ebp + 12] ; (2) y
mov edx, dword [ebp + 16] ; (3) n
L2:
; (4)
add eax, edx
; (5) x += n;
imul ecx, edx
; (6) y *= n;
sub edx, 1
; (7) n--;
test edx, edx
; (8) n v 0
jle L5
; (9) n <= 0
cmp ecx, edx
; (10) y v n
jl L2
; (11) y < n
;

L5:
; (12)

4-12
M3

60

:
mov al, -80; (-80) = 176
cmp al, 150; 150 = (-106)
jb M1
jl M2
jmp M3

M1 , 176 > 150;


M2 , .. -80 > -106.
M3.
5-4
eax = -8
edx = 65528

61

62

1.

. , . :
(Computer Systems: A Programmers Perspective).
: -, 2005 . 1186 .

2.

Henry S. Warren. Hacker's Delight (2nd Edition). / Addison-Wesley Professional; 2


edition (October 5, 2012) . 512

3.

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

4.

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

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

5.

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