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

1

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

, .
,
: (software)
(hardware).
,
;
.

1.
, ,
. , ,
.
, (1 0).
,
, , , ():
2 ;
, ;
.
,
. .
, Intel 80x86 :
0000 0100 0000 0110 (0406h mov al,06)
6, 8- ( AL).
-
.
(hexadecimal) . 6 "" :
=10, =11, =12, D=13, =14, F=15 ( ).
h . ,
( ),
, : 07.
,
,
. . .

, .
.
2

, - () .
L0.
, L0
,
. , ,
; L1. :
. L1,
,
L0. , , L1 ,
.
. , L1,
L0
, L0. L0
.
. ,
, ,
, . , VM1 ( )
, L1.
, VM0 , L0,
. 1.1.

1.1 -

, .
VM1
, . ,
VM1 , VM1
,
. , , VM1,
, V0.
VM1 VM0,
,
, VM1,
?
, VM2, .
, VMn,

.
Java. ,
Java, Java , - Java,
, , .

, Java (Java Virtual Machine, JVM).
JVM
, Java - .
3
.
, . 1.2.

1.2

, VM0 0, 1
VM1. ,
, 1 ,
"" , .
, , . ,
,
.
( 1).

. ,
, .
, ,
1, , .
( 2). ,
,
, , ,
, .
. ,
, , .
( 3).
,
, .
3
, ,
.. , ,
, . ,
, ,
2. , (
) ,
2, 3.
( 4).
,
. ,
4, , ADD, SUB,
MOV. ,
4
2 . ,
,
, 3.
( ) .
( 5). ,
++, , Visual Basic, Java. , ,
,
, 4. , ++
,
, . Java
,
- Java. , , 5,
, 4.
, 4
.
InteI IA-32
. ,
Intel 8086/8088,
IBM PC. ,
.

2
2.1

, : , , ..
, .
( binary digit - bit), 0 1.
. 0
1, , , 0 1.
8 () (byte). 8-
. ,
, 00000000 ( 0),
11111111 ( 1). , , 256 (0 255, 2
8). , n- 2n .
( n = 8) ( n = 16, 32 ..).
, .
. 0 (
) 7 ( ).
. .
, , .
:
.
(word).
.

2.2 86-64


.
, .
.
32- ,
: (16-) 8086,
8086 (V86), 32- .
(, task).
5
64- , ,
32- .
64- ,
64- :
64- (64-bit mode) 64-
64- .
( , ).
64 , ( ) 32 ,
(REX) 64- ;
(compatibility mode) 64- 32- 16-
. 32-
,
. 64- ,
, .
.
32- 86-64 32- .
64- ()
.

2.1 - 86-64

FPU/MMX XMM 5 .

2.2
2.2
.
6

2.2 - ,

64- R. 16
64-, 32-, 16- 8- ( ).
.
,
.

2.3 (16-)
2.3.1
(real address mode), (real mode),
8086. 1
.
( i8086), . 2.3.
. 16- -
, : , -
. ,
(. 2.4). - .

2.3 - 8086
7

, , DX.
(
) .
(, , DH) (AL, BL, CL DL) .
, - mov BL,AH,
, . ,
-, - -, . . ,
.
( , ) ,

.
, mul ,
( AL), loop .
SI DI , ,
. - ()
(. . ) .
( ).
.
,
,
- .
-, SP,
, .
SI, DI, SP, ,
.
CS, DS, ES SS
, , .
CS , ,
DS ES - ( ,
, ), SS - .
, , .
IP "" ,
, . IP (IP
- , ,
); ,
.
,
, (. 2.4).
6 3 ,
.

2.4 - .

CF (Carry Flag)
, ( !)
.
PF (Parity Flag) 1, 8
.
8
AF (Auxiliary Flag)
- .
( ) .
ZF (Zero Flag) 1, .
SF (Sign Flag) , 1
.
OF (Overflow Flag) , . .
.

. , 1,
( ) -- dec AX,
, ZF ( ).
, , 58 000 61 000, CF, 119000,
, ,
16- , ""
CF .
, ,
"" : ,
, -
.
, . ,
- jz zero, zero,
(. . ZF ), - jnc okey,
okey, CF (
).
TF (Trace Flag)
. TF=l,
1 ( 1).
IF (Interrupt Flag) (
) ( )
.
DF (Direction Flag) ,
. DF=0, ,
; DF=1, .
, ,
, (,
- ).

2.3.2
20- , 2 20 = 1048576
. 8 , . . 1 .
, ,
(16- ) . 20-
00000h FFFFFh ( h
).
, 2 16 = 64
. , , 16-
( 0000h FFFFh). 20-
( ), 0h.
, XXXX0h,
, 216.
,
( 16 XXXXh)
. : (
CS), ( DS), (
SS) ( ES). ,
9
, ,
1000h, ,
1000h.
20- 16-
. 2.5. 1A03h.
, ,
. 16-
(
FFFBh).

. 2.5 - 8086

20-
, 4 0000h
:
1A030h +FFFBh = 2A20Bh .
, ( ) 2A20Bh.

2.3.3 .
,
.
. ,
.
.
. MODEL. MODEL
, ,
, .
MODEL.

TINY , 64 .

;
SMALL , (
, ).
;
COMPACT ,
,
( );
10
MEDIUM , ,
,
;
LARGE HUGE , ;
FLAT , TINY, 32- ,
, , , , 4 GB.

:
.model small ( )
.code ( )
.stack n ( )
.data ( )
end metka ( )
:
;
.MODEL small
.STACK 512
.DATA

.CODE

start: mov ax,@data


mov ds,ax

end start

.stack n (.stack n) ,
.
.code .
cs DOS. ds .
@data .
start: - , () .

2.4 (32-)
.
.
, 2.6, ,
, 32 Intel.
11

2.6 -

:
- - eax/ax/ah/al, ebx/bx/bh/bl, edx/dx/dh/dl, c/cx/ch/cl, ebp/bp,
esi/si, edi/di, esp/sp. ;
cs, ds, ss, es, fs, gs.
;
eflags/flags;
eip/ip.
, 32-
. .
? , 16-
Intel, i8086. 32-
(Extended).
,
, 32- ,
. ,
.

2.5
:
- , .
, 0 1.
- , 0 3,
0

1 0 1 1
3 2 1 0
(Byte) , 0 7,
0 .
2n.

0 1 1 1 0 1 0 1
7 6 5 4 3 2 1 0
12

(Word) , .
16 ; 0 15. ,
, , , 15-
- . Intel
.
.
.
1 1 1 0 0 0 0 1 1 0 0 1 1 0 0 1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

(Double Word) (32 ),


. 0 31.
, , , , 31- , -
. .
.
.
. 12 34 56 78h, (offset) 0000,
78 56 34 12, .. 78h 0000, 12h 0003.

(Quadword) (64 ),
. 0 63.
, , , ,
64- , .
. .

.
(Tenbyte) 10 (80 ),
. 0 79. ,
, , , 79- , .
.

2.5

:
BYTE SBYTE - .
BYTE SBYTE ,
:
128...+127;
0...255;
. .
, .

value1 BYTE 'A' ; ASCII


value2 BYTE 0 ; byte
value3 BYTE 255 ; byte
13
value4 SBYTE 128 ; byte
value5 SBYTE +127 ; byte
value6 BYTE ? ;

db (define byte):

:
alfa DB 65, 72h, 75o, 11011b, 11h+22h, 0ach
DB -65, 'a', 'abc'
, alfa,
( ) :

41 72 3d 1b 33 ac bf 61 61 62 63
alfa +0 +1 +2 +3 +4
11011b alfa+3.

:
privet1 BYTE "Good afternoon",0
greeting2 BYTE 'Good night',0

DUP (duplicate):
BYTE 20 DUP(0) ; 20 bytes,
BYTE 20 DUP(?) ; 20 bytes,
BYTE 4 DUP("STACK") ; 20 bytes: "STACKSTACKSTACKSTACK"

WORD i SWORD 2 (
).
:
, :
32 768...32 767;
0...65 535;
, 16 ,
16- ;
2- , .

word1 WORD 65535 ;


word2 SWORD -32768 ;
word3 WORD ? ;

dw (define word):

beta DW 4567h, 0bc4ah, 1110111011b, 2476o


DW -7683, 7683, 'ab'

(, offset) beta :

67 45 4a bc bb 03 3e 05 fd e1 03 e1 62 61
beta +2 +4 +6 +8 +12

2476o beta +6.

DWORD SDWORD 4 (
).
:
14
, :
231...+231-1;
0...232-1;
, 16-
16- ;
.

val1 DWORD 12345678h ;


val2 SDWORD 21474836 ;
val3 DWORD 20 DUP(?) ;

dd (Define Double word):


val1 DD 12345678h, 21474836

QWORD DQ (Define Quad word) 8


.
:
, :
-263+263-1;
264-1;
.

quad1 QWORD 1234567812345678h

quad2 DQ 1234567812345678h

TBYTE DT 10 .
:
0...99 999 999 999 999 999 999.
.

intVal TBYTE 80000000000000001234h


intVal1 DT 80000000000000001234h

.
:
REAL4 32 ;
REAL8 64 ;
REAL10 80 .

rVal1 REAL4 -1.2


rVal2 REAL8 3.2E-260
rVal3 REAL10 4.6E+4096
ShortArray REAL4 20 DUP(0.0)

:
rVal1 DD -1.2
rVal2 DQ 3.2E-260
rVal3 DT 4.6E+4096

REAL4 - 1.18 x10-38 3.40 x1038


15
REAL8 - 2.23x10-308 1.79x10308
REAL10 - 3.37x10-4932 1.18x104932

:
-
0 9.
. .
-
0 9 .
. ( 47) .

BCD1 byte 03h, 06h ; -


BCD2 byte 33h, 96h ; -

.
. Intel
: .

2.7 MASM
,
. ,
Pentium IV 300 .
, , ,
.
Intel (x86).
:
;
;
;
;
;
.
x86 :

[label:] mnemonic [operands] [;comment]

;
, ;
( . r ,
, );
.

, , .
:

mnemonic
mnemonic [destination]
mnemonic [destination],[source]
mnemonic [destination],[source-1],[source-2]

, :
(immediate);
(register) ;
(memory).
16

2.7.1 , , BCD

MOV (MOVe operand) -


: mov ,
: .
:
.
: .
v . ,
:
.
(..
"-" v ).
CS, IP, I RIP.
.
v (
):
MOV reg,reg
MOV mem,reg
MOV reg,mem
MOV mem,imm
MOV reg, imm

:
.data
var1 WORD ?
var2 WORD ?
.code
mov ax,var1
mov var2,ax

.data
oneByte BYTE 78h
oneWord WORD 1234h
oneDword DWORD 12345678h
.code
mov eax,0 ; EAX = 00000000h
mov al,oneByte ; EAX = 00000078h
mov ax,oneWord ; EAX = 00001234h
mov eax,oneDword ; EAX = 12345678h
mov ax,0 ; EAX = 12340000h

.data
alfa dw 1234h
beta db 56h
.code
mov bl,al
mov bx,ds
mov ax, alfa; alfa x
mov bx, offset beta; beta bx
mov al, 75h;
mov cx, [100]; 100 cx
mov [di], bx; bx di
mov byte ptr alfa , [bx]; bx
17
; alfa
MOVZX (Move With Zero-Extend, )
.
- ( , 16 24 )
.
. ovzx:

MOVZX reg32,reg/mem8
MOVZX reg32,reg/mem16
MOVZX reg16,reg/mem8

:
.data
byteVal BYTE 10001111b
.code
movzx ax,byteVal ; AX = 0000000010001111b

.data
byte1 BYTE 9Bh
word1 WORD 0A69Bh
.code
movzx eax,word1 ; EAX = 0000A69Bh
movzx edx,byte1 ; EDX = 0000009Bh
movzx cx,byte1 ; CX = 009Bh

MOVSX (Move With Sign-Extend, )


,
ovzx. -
( , 16 24 ) .
.
MOVSX:
MOVZX reg32,reg/mem8
MOVZX reg32,reg/mem16
MOVZX reg16,reg/mem8

mov bx,0A69Bh
movsx eax,bx ; EAX = FFFFA69Bh
movsx edx,bl ; EDX = FFFFFF9Bh
movsx cx,bl ; CX = FF9Bh

XCHG (Exchange Data, )


. XCHG:
XCHG reg, reg
XCHG reg,mem
18
XCHG mem,reg
XCHG ,
MOV, , XCHG
.

:
xchg al, ah
xchg alfa, ax
xchg sir [si], bx
xchg eax,ebx ; exchange 32-bit regs

, ,
MOV:

mov reg, op1


xchg reg, op2
mov op2, reg

:
.data
val1 WORD 1000h
val2 WORD 2000h
arrayB BYTE 10h,20h,30h,40h,50h
arrayW WORD 100h,200h,300h
arrayD DWORD 10000h,20000h
.code
main PROC
; Demonstrating MOVZX instruction:
mov bx,0A69Bh
movzx eax,bx ; EAX = 0000A69Bh
movzx edx,bl ; EDX = 0000009Bh
movzx cx,bl ; CX = 009Bh
; Demonstrating MOVSX instruction:
mov bx,0A69Bh
movsx eax,bx ; EAX = FFFFA69Bh
movsx edx,bl ; EDX = FFFFFF9Bh
mov bl,7Bh
movsx cx,bl ; CX = 007Bh
; Memory-to-memory exchange:
mov ax,val1 ; AX = 1000h
xchg ax,val2 ; AX=2000h, val2=1000h
mov val1,ax ; val1 = 2000h
; Direct-Offset Addressing (byte array):
mov al,arrayB ; AL = 10h
mov al,[arrayB+1] ; AL = 20h
mov al,[arrayB+2] ; AL = 30h
; Direct-Offset Addressing (word array):
mov ax,arrayW ; AX = 100h
mov ax,[arrayW+2] ; AX = 200h
; Direct-Offset Addressing (doubleword array):
mov eax,arrayD ; EAX = 10000h
mov eax,[arrayD+4] ; EAX = 20000h
mov eax,[arrayD+4] ; EAX = 20000h
19

XLAT (transLATe Byte from table) -


: xlat __
: al ()
.
:
, ds:bx+(al);
al .
__ xlat,
, al,
ds:bx. xlat .
:
:
xlat .
bx lea
offset mov.

:
table db 'abcdef'
int db 0 ;
...
mov al,3
lea bx,table
xlat ;(al)='c'

IN (INput operand from port) -


: in ,_
: -.
:
, , - al/ax/eax.
: .
:

.
dx. 0-255.
dx.
, , .

OUT (OUT operand to port) -


: out _,
: -.
:
, , al/ax/eax ,
.
:
:

.
dx.
0...255. dx.
,
.
out 64h,al

:
in al,3Ch ; input byte from port 3Ch
20
out 3Ch,al ; output byte to port 3Ch
mov dx, portNumber ; DX can contain a port number
in ax,dx ; input word from port named in DX
out dx,ax ; output word to the same port
in eax,dx ; input doubleword from port
out dx,eax ; output doubleword to same port

LEA (Load Effective Address) -


: lea ,
: () .
:
16- ;
:
:
offset. offset
lea ,
.
; bx mas
.data
mas db 10 dup (0)
.code
...
mov di,4
lea bx,mas[di]
;
lea bx,mas[4]
;
lea bx,mas+4

LAHF SAHF
LAHF (Load Status F!ags lnto , )
EFLAGS.
: SF ( ), ZF, ( ), AF (
), PF ( ) CF ( ).
:

.data
saveflags ?
.code
lahf ;
mov saveflags,ah ;

SAHF (Store lnto Status Flags, )


EFLAGS. ,
:

v ah,saveflags ;
;
sahf ; EFLAGS

PUSH
PUSH 16- 32-r ,
ES, , 2 4. PUSH:
21
PUSH r/m16
PUSH r/m32
PUSH imm16/ imm32


, ESP,
16- 32- , , ESP,
, 2 4. :

r/m16
r/m32

PUSHFD POPFD
PUSHFD 32-
EFLAGS, POPFD , ..
FLAGS . :

pushfd
popfd

16- FLAGS
PUSHF, - POPF.
:
.data
saveFlags DWORD ?
.code
pushfd ; EFLAGS
saveFlags ;
; EFLAGS

PUSHAD, PUSHA, POPAD


PUSHAD 32-
: , , EDX, , ESP ( ,
), , ESI EDI. POPAD , ..
. ,
PUSHA 16-
: , , , , SP ( , ), , SI DI.
, ..
.
PUSHAD ,
32- .

POPAD. :

MySub PROC
pushad ;
.
.
mov , .
mov edx, .
mov , .
.
.
popad ;
ret
MySub ENDP
22

2.7.2

EFLAGS.

ADD (ADDition) -
: add ,
: 8, 16, 32 64 .
:
;
;
.
: CF ( ), ZF ( ), SF (
), OF ( ), AF ( ), PF ( ).
:
ADD ,
V. add .
.
( ),
cf adc.
Ex.:
add ax, 5
add bl, 5
add ax, bx
add word ptr [bx], 75
add alfa, ax
add alfa, 5
add byte ptr [si], 75
add byte ptr alfa, 75

.data
var1 DWORD 10000h
var2 DWORD 20000h
.code
mov eax,var1 ; EAX = 10000h
add eax,var2 ; EAX = 30000h

.data
sum qword 0

.code
mov rax,5
add rax,6
mov sum,rax

ADC (Addition with Carry) -


: adc ,
: .
:
;
: =++cf;
.
: OF SF ZF AF PF CF
23
:
adc .
, add. adc
add // add,
// adc,
. , adc
.
.data
sl1 dd 01fe544fh
sl2 dd 005044cdh
rez dd 0
.code
...
mov ax,sl1
add ax,sl2 ;
mov rez,ax
mov ax,sl+2
adc ax,sl2+2 ; cf
mov rez+2,ax

32- (FFFFFFFFh + FFFFFFFFh),


EDX:EAX: 00000001FFFFFFFEh:
mov edx,0
mov eax,0FFFFFFFFh
add eax,0FFFFFFFFh
adc edx,0

SUB (SUBtract) -
: sub _1,_2
: .
:
_1=_2-_1;
.
: OF SF ZF AF PF CF
:
SUB ,
V. sub
.

.data
var1DWORD30000h
var2DWORD10000h
.code
moveax,var1;EAX=30000h
subeax,var2;EAX=20000h

SBB (SuBtract with Borrow) -


: sbb _1,_2
:
sbb sub ( cf).
:
_2=_2+(cf);
_1=_1-_2;
: OF SF ZF AF PF CF
24
:
sbb

.
Ex. Op1 dword 12345678h
Op2 dword 0abcdef78h
Rez dword ?
.............
mov ax, word ptr op1
sub ax, word ptr op2
mov word ptr rez, ax
mov ax, word ptr op1 + 2
sbb ax, word ptr op2 + 2 ;
mov word ptr rez + 2, ax

.. 64 32 . EDX:EAX
0000000700000001h 2. (EAX-
00000001h), . . 1 2, Carry.

movedx,7;
moveax,1;
subeax,2;2
sbbedx,0;

INC (INCrement operand by 1) - 1


: inc
: 1.
: .
: OF SF ZF AF PF
:
, ,
. cf.
Ex:
inc alfa
inc bl
inc eax
inc rbx
inc word ptr [bx] [si]

DEC (DECrement operand by 1) -


: dec
25
: 1.
: 1 .
: OF SF ZF AF PF
:
dec , ,
. , cf.
mov al,9
...
dec al ;al=8

NEG (NEGate operand) -


: neg
: ( ) .

:
(0 ) ;
=0, .
( ):
OF SF ZF AF PF CF
r r r r r 0
( ):
OF SF ZF AF PF CF
r r r r r 1
:

.
. ,
neg .

mov al,2
neg al ;al=0feh -2

CMP (CoMPare operands) -


: cmp 1,2
: .
:
(1-2);
, 1 2 (
).
: OF SF ZF AF PF CF
:
,
. Zero
Carry :

cmp .
len equ 10
...
cmp ax,len
jne m1 ; (ax)<>len
26
jmp m2 ; (ax)=len

CBW (Convert Byte to Word) -


: cbw
: .
:
cbw al ax:
al:
al=0, ah=00h;
al=1, ah=0ffh.
:
:

. , ,
.

.:
a sbyte -75
b sword -188
c sword ?
.........
mov al, a
cbw ;
add ax, b
mov c, ax
..........

CWD (Convert Word to Double word) -


: cwd
: .
: ax dx.
:
:
cwd ax
dx. , ,
, ,
, , .

Ex. 32 (dw) 16 (sw).

dw dword 12345678h
sw word 0abcdh
rez dword ?
................
mov ax, so
cwd ; operand DX : AX
mov bx, ax
mov ax, word ptr do
27
sub ax, bx
mov word ptr rez, ax
mov ax, word ptr do + 2
sbb ax, dx ;
mov word ptr rez + 2

CDQ (convert doubleword to quadword)


EAX [EAX31] EDX,
64 EDX:EAX, ..:
[EAX31] = 0, [EDX] 00000000h,
[EAX31] = 1, [edx] 0ffffffffh.

, .

MUL (MULtiply) -
: mul _1
: .
:
.

, .
.
:
MULreg/mem8
MULreg/mem16
MULreg/mem32
MULreg/mem64

E , 8/16/32/64 ,
al/ax/eax/rax.
8/16/32/64 ax,
dx:ax, edx:eax, rdx:rax .
( ):
OF SF ZF AF PF CF
0 ? ? ? ? 0
( ):
OF SF ZF AF PF CF
1 ? ? ? ? 1

:
[AX] [AL] * [reg/mem8]

moval,5h
movbl,10h
mulbl;AX=0050h,CF=0
:

16 :
[DX:AX] [AX] * [reg/mem16]

.data
28
val1WORD2000h
val2WORD0100h
.code
movax,val1;AX=2000h
mulval2;DX:AX=00200000h,CF=1

32 :
[EDX:EAX] [EAX] * [reg/mem32]

moveax,12345h
movebx,1000h
mulebx;EDX:EAX=0000000012345000h,CF=0

32 :

[RDX:RAX] [RAX] * [reg/mem64]

movrax,0FFFF0000FFFF0000h
movrbx,2
mulrbx;RDX:RAX=0000000000000001FFFE0001FFFE0000

IMUL (Integer MULtiply) - .


: .
:
.
:

IMULreg/mem8 ;AX=AL*reg/mem8
IMULreg/mem16 ;DX:AX=AX*reg/mem16
IMULreg/mem32 ;EDX:EAX=EAX*reg/mem32

.
.
CF OF.

IMULreg16,reg/mem16
IMULreg16,imm8
IMULreg16,imm16

IMULreg32,reg/mem32
IMULreg32,imm8
IMULreg32,imm32
29
- op1=op2*op3 ( ):

IMULreg16,reg/mem16,imm8
IMULreg16,reg/mem16,imm16
IMULreg32,reg/mem32,imm8
IMULreg32,reg/mem32,imm32

.data
word1SWORD4
dword1SDWORD4
.code
movax,16 ;AX=16
movbx,2 ;BX=2
imulbx,ax ;BX=32
imulbx,2 ;BX=64
imulbx,word1;BX=256
moveax,16 ;EAX=16
movebx,2 ;EBX=2
imulebx,eax;EBX=32
imulebx,2 ;EBX=64
imulebx,dword1;EBX=256

:
.data
word1SWORD4
dword1SDWORD4
.code
imulbx,word1,16 ;BX=word1*16
imulebx,dword1,16 ;EBX=dword1*16
imulebx,dword1,2000000000;signedoverflow!

DIV (DIVide unsigned) -


: div
: .
DIV 8-, 16-, 32-, 64- ,
,
, DX:, D: RAX:RDX.
DIV r/m8
DIV r/m16
DIV r/m32
DIV r/m64

DIV , .
, , ,
.


AX r/m8 AL AH
DX: r/m16 AX DX
D: r/m32 D
RDX:RAX r/m64 RAX RDX

, ax.
30
al, ah:
[al] [ax]/ []
[ah] [ax]/ []

movax,0083h ;
movbl,2 ;
divbl ;AL=41h,AH=01h

, dx:ax,
ax.
ax, dx;

[ax] [dx:ax]/[]
[dx] [dx:ax]/[ ]

movdx,0;clear,high
movax,8003h;,low
movcx,100h;
divcx;AX=0080h,DX=0003h

32 , EDX EAX (64


), EAX EDX.

.data
dividendQWORD0000000800300020h
divisorDWORD00000100h
.code
movedx,DWORDPTRdividend+4;highdoubleword
moveax,DWORDPTRdividend;lowdoubleword
divdivisor;EAX=08003000h,EDX=00000020h

64 , RDX RAX (128


), RAX RDX.

.data
dividend_hiQWORD0000000000000108h
dividend_loQWORD0000000033300020h
divisorQWORD0000000000010000h
.code
movrdx,dividend_hi
movrax,dividend_lo
divdivisor ;RAX=0108000000003330
;RDX=0000000000000020
31
: 0
. : 0
al/ax/eax/rax.

IDIV (Integer DIVide) -


: idiv
: .
IDIV .
, DIV. 8-, 16-, 32-, 64- ,
IDIV CBW,
CWD, CDQ .
-48 5. IDIV AX ,
-9, DX -, -3:

;
mov ax,-48 ;
mov bx,5 ;
cwd ; dx:ax
idiv bx ; ax, dx

. (
) .
: 0 . :
0 rax/eax/ax/al.

2.7.3 BCD

AAA (Ascii Adjust after Addition) - ASCII-


: aaa
:
BCD-.
:
al af;
( al >9) (AF=1),
:
al 6;
al;
ah 1;
: af = 1, cf = 1,
af = 0 cf = 0.
:
OF SF ZF AF PF CF
? ? ? r ? r
:
aaa BCD-
add. BCD- .
BCD- 9,
BCD-. aaa.
BCD-
ah 1.

, BCD-: 48 + 29:
mov ax, 408h
mov dx, 209h
add ax, dx ; [ax]=0611h BCD-
32
AAA ; [ax]=0707h

AAS (Ascii Adjust after Substraction) - ASCII-


: aas
: BCD-
.
:
[AL0:3] > 9 [AF] = 1, {
[AL] [AL] - 6
[AH] [AH] - 1
[AF] 1
[CF] 1
[AL] [AL] AND 0FH
}
:
OF SF ZF AF PF CF
? ? ? r ? r
. 29 48.
mov ax, 408h
mov dx, 209h
sub ax, dx ; [ax]=01ffh
AAS ; [ax]=0109h

DAS (Decimal Adjust for Subtraction) -


: das
: BCD-
.
: das al.
[AL0:3] > 9 [AF] = 1, {
[AL] [AL] - 6
[AF] 1
}
[AL4:7] > 9 CF = 1, {
[AL] [AL] - 60H
[CF] 1
}

. MOV AL, 52H


SUB AL, 24H ; AL = 2EH BCD-
DAS ; AL = 28H

AAM (Ascii Adjust after Multiply) - ASCII-


: aam
:
BCD-;
63h (9910) BCD-
.
:
[AH] [AL] / 10
[AL] [AL] MOD 10
:
OF SF ZF AF PF CF
? r r r r ?
33

1. 8 9.

mov ah,08h ;ah=08h


mov al,09h ;al= 09h
mul ah ;al=48h
aam ;ah=07h,al=02h

2. 60h .

; 60h ax
mov ax,60h ;ax=60h
am ;ax=0906h (96) 60h

AAD (Ascii Adjust before Division) - ASCII- .


: aad
:
a BCD- .
:
[AL] [AH] * 10 + [AL]
[AH] 0
:
OF SF ZF AF PF CF
? r r r r ?

. 35 2.
mov ax, 305h
mov bl, 2
AAD ; [ax]=35h
div bl ; [al]=12h [ah]=1

2.7.4
1.
86:
INCLUDE Irvine32.inc
.data
alfa WORD 3 DUP(?)
.code
main proc
mov ax,17 ; -
mov ax,10101b
mov ax,11b
mov ax,0bch
mov alfa,ax
mov cx,ax ;
mov ax,bx ; ax bx
mov ax,cx ;
xchg ax,bx ;
mov si,2
mov alfa[si],ax ;
mov esi,2
mov ebx,offset alfa ; ebx
lea ebx,alfa ;
mov ecx,[ebx][esi] ;
mov cx,alfa[2] ;
mov cx,[alfa+2] ;
34
mov di,4
mov byte ptr [ebx][edi],55h ;
mov esi,2
mov ebx,3
mov alfa[ebx][esi],33h ;
;
mov alfa[ebx+esi],33h ;
mov [alfa+ebx+esi],33h ;
mov [ebx][esi]+alfa,33h ;

exit
main ENDP
END main

Ex. 2. :
e=((a+b*c-d)/f+g*h)/i
a, d, f (16 ), - b, c, g, h, i .
f (32 ). ,
. 1 .

INCLUDE Irvine32.inc
.data
a dw 5
b db 6
cd db 10
d dw 5
f dw 6
g db 10
h db 11
i db 10
interm dw ?
rez db ?

.code
main proc
mov eax,0
mov al, b
imul cd ; in ax avem b*c
add ax, a ; ax=b*c+a
sub ax, d ; ax=b*c+a-d
cwd ; ax, , dx:ax
idiv f ; ax si dx, ax=(a+b*c-d)/f
mov interm, ax ; interm=(a+b*c-d)/f
mov al, g
imul h ; ax=g*h
add ax, interm ; ax=(a+b*c-d)/f+g*h
idiv i ; al ah
mov rez, al

exit
main ENDP
END main

2.7.5
PTR. PTR . .
35
, ,
(..
).
,
.data
myDoubleDWORD12345678h
.code
movax,WORDPTRmyDouble;5678h
movax,WORDPTR[myDouble+2];1234h
movbl,BYTEPTRmyDouble;78h

.data
wordListWORD5678h,1234h
.code
moveax,DWORDPTRwordList;EAX=12345678h

equ "=" ()
. ,
,
.
, ,
.
, .
equ = :
, equ
, , =
;
, =,
, equ .
equ = :
+, -, *, / ();
- SHR SHL;
- NOT, AND, OR, XOR;
TYPE ;
SIZEOF ();
LENGTHOF , ( TYPE)
();
HIGH, LOW .
:

.DATA
intgr = 14*3 ;=42
intgr = intgr/4 ;10
intgr = intgr+4 ;14
intgr = intgr-3 ;11
m1 EQU 5
m2 EQU -5
const EQU m1+m2 ;const=0
vect DW 60 DUP(?)
s_vect EQU SIZEOF vect ;60 * 2 = 120
l_vect EQU LENGTHOF vect ;60
t_vect EQU TYPE vect ;2
verif EQU t_vect*l_vect ;=2*60
mov ax, SIZEOF vect
36
, ,
INCLUDE.

2.7.7 ,

JMP (JuMP)
JMP
, .
,
.
: jmp _
:
, .
:
(short) ( -128... + 127 );
(near) ( );
(far) ( );
(
).

.
18 . :
( );
( );
zf, sf, cf, of, pf ( af).
:
jcc _
, j jump (), cc
, .
_,
, .

2.1 - jcc

Ee equal
Nn not
Gg greater
Ll less
Aa above ,
Bb below ,

2.2 - cmp





je _1 = _2 zf = 1
jne _1<>_2 zf = 0
jl/jnge _1 < _2 sf <> of
jle/jng _1 <= _2 sf <> of or zf = 1
jg/jnle _1 > _2 sf = of and zf = 0
37
jge/jnl _1 => _2 sf = of
jb/jnae _1 < _2 cf = 1
jbe/jna _1 <= _2 cf = 1 or zf=1
ja/jnbe _1 > _2 cf = 0 and zf = 0
jae/jnb _1 => _2 cf = 0
,
( . 2.2).

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

: mov ax,0FFFEh
mov bx, 2
cmp ax, bx
ja alfa;jumpistaken


mov ax, 0FFFEh
mov bx, 2
cmp ax, bx
jg alfa;jumpnottaken
alfa , .

JCXZ (Jump if CX=Zero)


, CX
JCXZ _
JECXZ _
:
JRCXZ _

: ( -128 127)
.
\ jcxz:
cx/ecx/rcx:
, cx/ecx/rcx 0,
, ;
, cx/ecx/rcx 0,
jxxz .
:
,
cx/ecx/rcx , .
:
movedx,0A523h
cmpedx,0A523h
jneL5;jumpnottaken
jeL1;jumpistaken

movbx,1234h
subbx,1234h
jneL5;jumpnottaken
jeL1;jumpistaken
38
movcx,0FFFFh
inccx
jcxzL2;jumpistaken

xorecx,ecx
jecxzL2;jumpistaken

:
movedx,1
cmpedx,0
jnlL5;jumpnottaken(1>=0 isfalse)
jnleL5;jumpnottaken(1>0isfalse)
jlL1;jumpistaken(1<0istrue)

movbx,+32
cmpbx,35
jngL5;jumpnottaken(+32<=35isfalse)
jngeL5;jumpnottaken(+32<35isfalse)
jgeL1;jumpistaken(+32>=35istrue)

movecx,0
cmpecx,0
jgL5;jumpnottaken(0>0isfalse)
jnlL1;jumpistaken(0>=0istrue)

movecx,0
cmpecx,0
jlL5;jumpnottaken(0<0isfalse)
jngL1;jumpistaken(0<=0istrue)

LOOP (LOOP control by register cx) - cx.


LOOP .
,
LOOP. :
loop
: cx.
:
cx;
cx:
cx=0, loop ;
cx=1, , loop.
:
LOOPD ECX ;
LOOPW CX .

: loop .
cx ,
. :
cx jcxz.
, cx 65 536 ;
39
, loop,
-128...+127 . ,
,
loop .
Ex: n string.

.data
string byte 7, 9, 15, 25, -18, 33, 11
n equ LENGTHOF string
suma byte ?
.code

xor eax, eax


mov ecx, n
xor esi, esi
m1:
add al, string[esi]
inc esi
LOOP m1
mov suma, al
LOOPE/LOOPZ LOOPNE/LOOPNZ
LOOPZ (Loop if Zero, ) ,
, ZF , ,
. , , -128 ...
+127 . LOOPZ
:
LOOPZ _

LOOPE (Loop if Equal, ) LOOPZ,


.
LOOPZ LOOPE:

= - 1
( > ZF = 1),
; .

LOOPNZ (Loop if Not Zero, )


LOOPZ . LOOPNZ
, , ,
ZF. LOOPNZ :

LOOPNZ _

LOOPNE (Loop if Not Equal, )


LOOPNZ, .
LNZ LOOPNE:

= - 1
( > ZF = } ,
; .

2.7.8
,
:
40
cf;
, , 0;
cf,
!
:
shl ,_ (Shift Logical Left) - .
,
_. _ CL (5 ). (
) .

SHL ( SHR, SAL, SAR, ROR,


ROL, RCR, RCL):
SHLreg,imm8
SHLmem,imm8
SHLreg,CL
SHLmem,CL
Ex.:
movbl,8Fh;BL=10001111b
shlbl,1;CF=1,BL=00011110b

moval,10000000b
shlal,2;CF=0,AL=00000000b

movdl,10;before:00001010
shldl,2;after:00101000

shr ,_ (Shift Logical Right) .


,
_. ( , ) .

Ex.:
moval,0D0h;AL=11010000b
shral,1;AL=01101000b,CF=0

moval,00000010b
shral,2;AL=00000000b,CF=1

sal ,_ (Shift Arithmetic Left) .


,
_. ( ) . sal
, cf .
sal shl.

sar ,_ (Shift Arithmetic Right) .


,
41
_. . sar ,
.

Ex.:
moval,0F0h;AL=11110000b(16)
saral,1;AL=11111000b(8),CF=0

movdl,128;DL=10000000b
sardl,3;DL=11110000b

movax,128;EAX=????FF80h
shleax,16;EAX=FF800000h
sareax,16;EAX=FFFFFF80h


, .
rol ,_ (Rotate Left) .
,
_. .

moval,40h;AL=01000000b
rolal,1;AL=10000000b,CF=0
rolal,1;AL=00000001b,CF=1
rolal,1;AL=00000010b,CF=0

ror ,_ (Rotate Right) .


,
_ (0-255 CL).
.

moval,01h;AL=00000001b
roral,1;AL=10000000b,CF=1
roral,1;AL=01000000b,CF=0

moval,00000100b
roral,3;AL=10000000b,CF=1

:
,
cf.

cf
, ,
42
cf.
( , )
.

cf :
rcl ,_ (Rotate through Carry Left)
. ,
_ (0-255 CL).
cf.

clc;CF=0
movbl,88h;CF,BL=010001000b
rclbl,1;CF,BL=100010000b
rclbl,1;CF,BL=000100001b

rcr ,_ (Rotate through Carry Right)


. ,
_(0-255 CL).
cf.

stc;CF=1
movah,10h;AH,CF=000100001
rcrah,1;AH,CF=100010000

SHLD SHRD
SHLD SHRD , . SHLD (SHift Left Doue,
)
, .
(.. ) .
, SF,
ZF, AF, PF CF. SHLD
:
SHLD , ,

SHRD (SHift Right Doue, )


, .
43

(.. ) . SHLD :

SHRD , ,

SHLD SHRD , . -
, .
. CL, 8-
:

SHLDreg16,reg16,CL/imm8
SHLDmem16,reg16,CL/imm8
SHLDreg32,reg32,CL/imm8
SHLDmem32,reg32,CL/imm8

Ex.:
.data
wvalWORD9BA6h
.code
movax,0AC36h
shldwval,ax,4;wval=BA6Ah

movax,234Bh
movdx,7654h
shrdax,dx,4

2.7.9
,
:
and _1,_2 .
()
_1 _2. _1.
:
44
ANDreg,reg
ANDreg,mem
ANDreg,imm
ANDmem,reg
ANDmem,imm

.
moval,10101110b
andal,11110110b;resultinAL=10100110

or _1,_2 .
()
_1 _2. _1.
moval,11100011b
oral,00000100b;resultinAL=11100111

xor _1,_2 .

_1 _2. _1.
:

test _1,_2 ( ).
_1
_2. , zf, sf, pf,
.

testal,00100000b;testbit5

not .
( )
. .
moval,11110000b
notal;AL=00001111b


.

, , .
_2 .
1
_1. , :
() 1 :

or _1,_2

_2, ,
, 1
_1.
or ax,10b ; 1- ax
45
() 0 :

and _1,_2

_2, ,
, 0
_1.
and ax,0fffdh ; 0 1- eax
xor _1,_2 :
, _1 _2 ;
_1.
xor ax,10b ; 1- ax
jz mes ; , 1- al
(_2) xor
, .
:

test _1,_2 ( _1)

_1 (_2) .
test and,
_1.
zf:
zf = 0, ,
,
_1;
zf = 1, ,

_1.

test ax,0010h
jz m1 ; , 4- 1
, test
jnz (Jump if Not Zero) , zf ,
jz (Jump if Zero) , zf = 0.


BT, BTC, BTR BTS .


(Bit Test, ) , n
, CF:

_, n
:
BTr/m16,imm8
BTr/m16,r16
BTr/m32,imm8
BTr/m32,r32

(Bit Test and Complement, )


, n , CF,
.

BTR (Bit Test and Reset, )


46
, n , CF, (..
) .

BTS (Bit Test d Set, )


, n , CF,
.
CF 6-ro
perem, :

Ex.
.data
perem WORD 10001000b

.code
bts perem,6 ; CF=0 perem=11001000b

.
str. , ax 0,
. long -
.

.data
str DB 'Poisc pervogo probela!'
long EQU sizeof str

.code
mov ecx,long
mov esi,-1
mov al,' '
next: inc esi
cmp al,str[esi]
loopne next
jne nol
mov eax,esi
jmp exit
nol: mov eax,0
exit: nop

2.7.10
:
CMC (CoMplement Carry flag); cf
CLC (CLear Carry flag); 0 CF
STC (SeT Carry flag); 1 CF
NOP ; , 3 .
CLD (CLear Direction flag) ; 0 DF
STD (SeT Direction flag); 1 DF
CLI (CLear Interrupt flag); 0 IF,
STI (SeT Interrupt flag); 1 IF

2.7.11
.
, ,
.
, ,
,
.
47

MOVSB (Move (copy) bytes)


MOVSW (Move (copy) words)
MOVSD (Move (copy) doublewords)

,
ESI, , EDI. ,
, ESI EDI ,
.
: () .
:
,
, :
esi
edi
df esi edi:
df=0,
;
df=1,
;
, .
:
: .
.
VS, OVSW OVSD .
:

REP - , >
REPZ,REPE - , > (ZF =1)
REPNZ,REPNE - , > (ZF = 0)

Ex. 20 source
target:
.data
sourceDWORD20DUP(0FFFFFFFFh)
targetDWORD20DUP(?)
.code
cld ;DF
;
movecx,LENGTHOFsource; REP
movesi,OFFSETsource;
movedi,OFFSETtarget;
repmovsd ; 20

CMPSB (Compare bytes)


CMPSW (Compare words)
CMPSD (Compare doublewords)

,
ESI, , EDI.
CMPS .
CMPSB -
CMPSW -
CMPSD - .
48
.data
sourceDWORD1234h
targetDWORD5678h
.code
movesi,OFFSETsource
movedi,OFFSETtarget
cmpsd ;comparedoublewords
jaL1 ;jumpifsource>target
2.:
movesi,OFFSETsource
movedi,OFFSETtarget
cld;direction=forward
movecx,LENGTHOFsource;repetitioncounter
repecmpsd;repeatwhileequal

SCASB (SCAS- Scans a string)


SCASW
SCASD

, AL/AX/EAX ,
, DI.
-
. SCAS REPE ( REP),
, ,
, ,
AL/ / (.. ZF).
REPNE, ,
, ,
, AL/AX/EAX (.. ZF).

. F
alpha. , EDI
. ,
JNZ:

.data
alphaBYTE"ABCDEFGH",0
.code
movedi,OFFSETalpha;EDIalph
moval,'F'
movecx,LENGTHOFalpha;alph
cld ;direction=forward
repnescasb;"F"
jnzquit;,

REPNE SCASB JNZ,


, "F" (..
REPNE SCASB = 0, ZF = 1).

STOSB (STOS- Store string data)


STOSW
STOSD
49
AL/AX/EAX ,
EDI. sos EDI
DF
. REP, SOS
.

.data
Count=100
string1BYTECountDUP(?)
.code
moval,0FFh;
movedi,OFFSETstring1; EDI
movecx,Count;
cld; -
repstosb; AL

LODSB (LODS- Load Accumulator from String)


LODSW
LODSD
AL/ / ,
, ESI. LODS
ESI DF
. REP
LODS, , .
, .

. array
.
LODSD, - STOSD.

INCLUDEIrvine32.inc
.data
arrayDWORD1,2,3,4,5,6,7,8,9,10;testdata
multiplierDWORD10
.code
mainPROC
cld ;direction=forward
movesi,OFFSETarray;
movedi,esi ;ESIEDI
movecx,LENGTHOFarray;
L1:lodsd ;[ESI]EAX
mulmultiplier
stosd;
;([EDI])
loopL1
exit
mainENDP
ENDmain

2.8
, , .
PROC ENDP.
, .
, , RET.
50
, ,
:
sample PROC

ret
sample ENDP

SumOf, 32- .
,
, , :

SumOf PROC
add ,b
add ,
ret
SumOf ENDP

CALL RET
CALL ,
. ,
. ,
CALL, RET. CALL
, CALL
, .
(.. RET),
. , ,
EIP, .. . 16-
I.

. , main
00000020h CALL. 32- 5
. ( MOV)
00000025h:

main PROC
00000020 call MySub
00000025 mov ,b

, , ySub
00000040h:
MySub PROC
00000040 mov eax,edx

ret
MySub ENDP

CALL (
00000025h), EIP SuB,

51

SuB,
RET. RET ,
ESP, EIP. RET
, , 00000025h.
, CALL,
,

, SumOf. ,
, . main
, :
INCLUDE Irvine32.inc
.data
theSum DWORD ?

.code
main PROC
mov eax,10000h ;
mov ebx,20000h ;
mov ,30000h ;
call SumOf ; = ( + EBX + ECX)
mov theSum,eax ;
exit
main ENDP
SumOf PROC
add ,b
add ,
ret
SumOf ENDP
END main

(macro procedure) .
,
. ,
. ,
CALL .
. , ,
( , ),
,
INCLUDE.
. .
.
,
, .
:
R -1, -2 ...

-
52

ENDM

, R ENDM,
. ,
.
,
. REQ.
, . :
mPutchar R char:REQ
push
mov al,char call WriteChar
ENDM

,
REQ.

, (;;).
, .

:

_ -1, -2,


,
. ,
.
, ,
.

: 3 ax.

INCLUDE Irvine32.inc
addup MACRO ad1,ad2,ad3
mov ax,ad1
add ax,ad2
add ax,ad3
ENDM
.data
a DW 1
b DW 2
cd DW 3
d DW ?

.code
main PROC
addup a,b,c
mov dx,ax
addup dx,dx,dx
mov d,ax
addup d,dx,c
exit
main ENDP
END main
53
LOCAL (
). LOCAL.

LOCAL {,} ...

LOCAL, ,
MACRO.
: .

power MACRO factor,exponent


LOCAL again, gotzero
xor dx,dx
mov ax,factor
mov cx,exponent
again: jcxz gotzero
mul bx
loop again
gotzero:
ENDM