Professional Documents
Culture Documents
ST Book PDF
ST Book PDF
" ) (1-5
"
"
)(
-
" -
1043311
" .2006 .
, ,' ,16-,
" ,39513- .61394
The Centre For Educational Technology, 16 Klausner St., Ramat-Aviv, P.O.Box 39513, Tel-Aviv,
61394. Printed in Israel.
, / .
, , , , , , ,
, , , . ,
, ,
) (.
1.1
1.2
11
1.3
14
1.4
42
47
2.1
47
2.2
48
2.3
64
2.4
63
2.5
74
2.6
77
84
85
3.1
85
3.2
97
3.3
103
3.4
107
4 8086
109
4.1
109
4.2
8086-
110
4.3
8086
115
4.4
123
4.5
131
4.6
EQU
138
139
141
5.1
141
5.2
141
5.3
144
5.4
163
5.5
182
5.6
188
5.7
202
6 ,
215
6.1
215
6.2
221
6.3
)(Immediate addressing
224
6.4
)(Register addressing
225
6.5
226
6.6
229
6.7
235
6.8
240
6.9
243
7 ,
249
7.1
249
7.2
250
7.3
253
7.4
264
7.5
271
7.6
277
7.7
281
7.8
284
7.9
302
311
8.1
311
8.2
313
8.3
MOVS
313
8.4
316
8.5
318
8.6
319
8.7
321
8.8
325
8.9
XLAT
327
9 -
329
9.1
329
9.2
DOS
329
9.3
8086
338
9.4
346
9.5
INOUT-
350
10
10.1
10.2
10.3
10.4
353
353
356
358
375
1.1
. ),(Software
, .
, . :
.1 , "" ,
, : , , .
.2 , :
) ,Linux ,Windows( ,,
;
, ,
"" , , , ""
)( : , , , , .
,
).(hardware
: , ,
. ,
. : ,
) ( ,
, . , ,
, : ,
.
) ;(machine language
0 1- . )
""( .
. - ,
- 0 .1- ,
. ) ,(Assembly language
"-" . "
" ) .*(Mnemonic codes ,
, . :
) ADD (ADDition
) CMP .(CoMPare ,
.
"" ) .(Assembler
. ,
" " ).(Low level languages
,
, . :
IBM .
,
) (High Level Languages Visual Basic ,C++ ,JAVA ,C ,.
,
, . :
,A=9 A ,
9 ,
"."A
,
. "" ) .(Compilation -
"" ) .(Compiler ,
.
- ""
) .(Interpretation
. ""
). (Interpreter
1.1
1.1
: .C
.
?
:
result = count1 + count2 + count3
10
, . ,
;result: = count1 + count2 + count3
, C:
;result = count1 + count2 + count3
,
. , ,
) .(Personal Computer PC
; .
.
.1.1
1.1
count1AX-
,AX+count2
101000010000000000000
00000011000001100000001000000000
add
AX, count2
AX-
,AX+count3
00000011000001100000010000000000
AX, count3
add
AX-
AXresult-
000000110000011000000000
mov result, AX
MOV , ADD AX
"" , ,
. ,1.1
.
.
)( .
,
11
- ,
.
.
) (
, .
;
,
. , ,
, ,
. Linux ,Windows :.
) (Embedded System
, , :- , , .
.
, , : , , .
,
,
, .
- ,
,
. ,
, .
1.2
,
. 1.2 .
)( ,
12
,
.
, ,L6 ) , ,
( - ,
.
, ,L5 .
,L5 , ,
, .
, ,L4
, ,
. ,
, .L3 ,
, ,L2
.
,
.L1 ;
) (
, .L0
1.2
13
: .
, .
,
. ,
; .
:
.1 Instruction Set Architecture
.ISA :
) , ,
(;
, .
, :
, ,
. ,
, " " .
1951 EDVAC " " ,
. ,
.
:
) (Processor ;
) (Memory ;
14
"" ) (buses
.
1.3
"" , .
.
1.3
.
, , .
:
,
.
. ,
15
/ .
/
.
:
) (fetch
) (execute
" -".
1.3.1 ""
""
.
.
"" - 100;
6 . ,
. :
. ;
. .
0.99-
, , ,
. , ,:
,
. , ,
) (99 ,98 ,
.0
16
.
,
-:
) (
1.4
,
. ,
, 6 ) ( .
"" ) ;(register
.
. " " ) Instruction Pointer .(IP
"
" ) Instruction Register .(IR
.
17
;
:
.1 - ) Arithmetic Logic Unit ,(ALU
, : ,
.
.2 AX .BX-
- .
.3 " " ) Flag register (FR
.
, : ?X = Y
-
.1.5
1.5
,
.
18
1.3.2
, ,
. "" ) ;(operator
,MOV : C =.
"" ) ;(operand
:
:C
;A=8
;A:=8
)=( : A .8
; :
:C
A=A+B C
;A:=A+B C
+ :
= A :
.C
, for if
; , ,
.
,
. , 0.2-
.
:
:
:
:
][
] [ ] [
] [ ] [ ][
19
, .
. ,
:
)(AX, BX
, . ,
6 .
,
. 0.99-
- -,
:
01 AX
02 BX
, :
][xx] [yy] [zz
] [xx )(;
] [yy [zz]- .
, 06 01 12 : 06 , )
( , 12 01- , 01 : ,AX12-
.
1.3.3
, : )
( , .
,
, .
20
,
" " " " . :
, , .
:
][xx] [yy] [zz
:
AX, BX .
: , AX
BX .
, :
, , ,
01 .05
.
21
1.2
01
02
03
04
05
:
27 AX
01 01 27
BX AX
02 01 02
27 99
04 99 27
, 99 ?98
, :
99 98
, :
03 01 99 " 99 "AX
05 98 01 AX 98
. 01 :
, : ;
;AX
;
;
? :
- . .
22
: 01- , 01
; 01 3 (yy) 4-
5 (zz) 6- ) 1( AX
.
, ,
. , 02
05 .,
,
. 01 01 23 :
23
01
01
AX
AX 23
.
:
+
:
AX BX
;
: .
.
,
, .1.3
06 :.07-
23
1.3
06
07
:
AX + 27 AX-
06 01 27
AX + BX AX-
07 01 02
, ,
, .
, ;00
.
1.1
:
23 A
54 B
A + B A-
A B- .
A .B-
, A- B-
AX BX- .AX
24
AX .A , 98 99- .
98 A 99 .B
:
23 98
04 98 23
54 99
04 99 54
98 AX
03 01 98
99 BX
03 02 99
AX+BX AX
07 01 02
AX 98
05 98 01
00
, ,
.0
.
.1.6
1.6
1.1
25
, IP ,0
. ,6
98 ,99- A ,B- .
,
.
.1 04 98 23 -
. :
, ) 0 IP(
.IR
IP ,1- ) (.
.
1.7.
1.7
04 98 23 :
26
23 .98
1.7.
1.7
04 98 23
.2 04 99 54
- ,
. :
. ,:
) 1 (IP
IP ,1- ) ,
.(02 , :
.
:
27
1.8
04 99 54
. 54 .99
:
1.8
04 99 54
28
.3 03 01 98
) 02
(IP IP ,1- ) (.
:
98 .AX :
1.9
- 03 01 98
.4 03 02 99
,
. 54 98
.BX .1.10
.5 07 01 02
, AX .54 + 23
.1.11
29
1.10
- 03 02 99
1.11
- 07 01 02
.6 05 98 01
77 .98
- .1.12
30
1.12
- 05 98 01
.7 00
, IP 06
. .
1.2
. 99 98
.99
. 2- .99
1.3
:
34 A
15 B
A1-
A+B B-
A 98 B .99
31
1.4
. :
15 A
32 B
A+B+19 A-
. ) ( -
.
, 0 A B-
98.99-
1.3.4
, ,
) ( . :
A = B
A=2
B=2
,
. )( )
00- ,(07 92 .
:
)(
32
.
. ,
, , FR
; , .
:
=
FR 1
FR 0
AX .BX
AX .BX
, .
1.3
= ?
08
= ?
09
1 , 0- .
A .B- ,
A=2
.B=2 .
33
, .
, .
1
; ,0 ,
.
:
FR = 1
IP
, ) 0 (99-
. , ,
.IP- , ,1 IP
, .
10 ; :
10
, 10 07 : ,1 IP
;07 .07
,
) ( .
,
. - ,
.
34
11 ; :
11
, 11 07 : IP ;07 ,
.07
1.2
1.5 :
A B-
2 A
2 B
.
. 98 A
99 .B
1.5
98 AX
03 01 98
99 BX
03 02 99
AX=BX 1
,1
IP 06
09 01 02
10 06
04 99 02
11 07
04 98 02
00
2 99
IP 07
2 98
35
) :(1.6
A B- A .B-
1.6
AB
A=B
98 AX
98 AX
99 BX
99 BX
AX = BX 1
,0 IP-
04
AX = BX 1
,1 IP-
06
2 99
2 98
IP 07
A 40
B .30
.1.13
1.13
A B
36
A B -
.1 03 01 98 03 02 99
-
. :
1.14
-
.2 09 01 02
, .
, .0
:
37
1.15
- 09 01 02
.3 10 06
. ,FR=0
) ( ) , .(04
:
1.16
- 10 06
38
.5 04 99 02
- -
. :
1.17
- 04 99 02
.6 11 07
) IP .(1-
.
; IP
,1- ) .(06 ,
:
39
1.18
11 07
IP .07
:
1.18
11 07
40
.7 00
07 , .
A = B -
A B- .40
.1 03 01 98 03 02 99
) .(B A
, :
1.19
-
.2 :09 01 02
- , -
A ,B-
"" , .1
:
1.20
- 02 01 09
.3 10 06
,FR=1- , IP :
, IP) 1- (04
, IP .06 -
:
1.21
- 10 06
41
42
.4 04 98 02
- , 02 .98
) (00 .
1.5
, , :
B5
A 10
BB+5
A = B
A A*2
.
A- 98 B- .99
1.4
.
) .(1.4 :
) (Data bus
) (Address bus
) (Control bus :
)" "(
.
, .
.
.
43
.
, , ,0
:
;0
);(read
0 ;
.
1.22
.
, 23 98 , :
98 ;
;23
).(write
1.23
44
-
. ;
,
. ,
) (3V .0V
, , ;
) .(1.24
. 8086
) ( 5 " "4
3.6 ) ( . ) (ADD
10 , 8086 500,000
4 360,000,000 .
1.24
- , , .
, .
. ,
, .
, .:
07 02 01 , . ,
) , -(
.- ,
, .
,
. .
45
, /
. /
. ,
/ .
: / .
.
,
, , .
;
, )(.
1.25
,
50- .
.
,-
)( . ,
)(.
46
.:
,
?
-
, ?
, ,
?
- .
47
2.1
, : )
- , ( ,
, ,
, . ,
. )
( : ; , , ,
, 0 .1- 100101 : .1111
:
; " : " ) (1- "
" ) .( 0-
, .
, .
.
. : 1 , 100-
1 ;100- :
, ) .(200
,
, . ,
- . ,
.
. , 0 100-
) , ( .
48
2.2
.
, -
2000 3000-"* . ,
, - . ,
b , b ,2 ,1 ,0
,b1 ) .(digits .
,b :
a0an1an2a0
- :
anbn + an1bn-1 + an2bn2 + + a0b0
ai , .0 ai < b
*
3500-"
)( . , b
b1, b2, b3 , b0,' .
- ,
. :
http://lib.cet.ac.il/pages/item.asp?item=7878
49
, ) (109457362 :
:
9 106 + 4 105 + 5 104 + 7 103 + 3 102 + 6 101 + 2 100
, :
9000000 + 400000 + 50000 + 7000 + 300 + 60 + 2
, ai , .0 ai < 9
: ,
, ) (16 ) .(8
2.2.1
) ( , b 10
:
0 1 2 3 4 5 6 7 8 9
, , ,582710
, .2.1
2.1
582710
1000 100 10 1
2 7
7 ; 1
;
2 ; 10
;
8 ; 100
;
50
;5 1000 .
, .
,5287 ) 5 (1000
) 7 .(1
) 0- ( .
:10
582710 = 5 103 + 8 102 + 2 101 + 7 100
, 10 ,5827
.10 ,
, 582710 .5827
,
.
,
0.001 .293.46
. - ' '+
' ' . " " )
""(.
: ?
,:
0.461 = 4 0.1 + 6 0.01 + 1 0.001
,10
: 4 ,101 = 0.1 6
,102 = 0.01 1 .103 = 0 001
51
: ,10
,10 .
10 , 10 .
, K 10 :
2.1
7 :
4782 .
273589 .
408.87 .
0.02357 .
2.2.2
, b = 2
0 : 1- . , 1012 10110110002-
. )
( ) bit .( binary digit , 10112 4
) 4( . ,
) (2 : , .1012
, .b = 2
2.2 .1001012
52
2.2
1001012
20
21
22
23
24
25
2.2
. :
110101102
10110112
. .
.
, ,
' '+ '' . , , 0 , ,1-
, '' ' .'+
,2.3
, .3.2
2.3
:
11010112 .
100010012 .
2.2.3
. ,
, .
, ,
) (16 , ,
.
53
,
.
,
.
- .
.
K
, :
K = an12n1 + an22n2 + + ai 2i + + a121 + a020
ai ) 0 (1 i - .
: 1001012 2
. .
1001012 = 1 25 + 0 24 + 0 23 +1 22 + 0 21 + 120 = 37
37 = 1001012
2.4
:
101112 .10012 .
2.5
:
? 8 = 10012 . ?25 < 1101012 .?15 = 11112 .
, ,
.1
54
2.3 :
2.3
1001012
1001012
32+4+1 = 37
, - 2
.2.4
2.4
2
28
29
210
20
21
22
23
24
25
26
27
16
32
64
2.6
. 2.4
:
10012 .iii 10110112 .ii 111111112 .i
. :
1011012 = (((((1 2 + 0) 2) + 1) 2 + 1) 2 + 0) 2 + 1
ii.-iii.
55
.
.
.1
.
2.4 .
.
, 82 .
. 2 .82-
2.4 26 = 64
:
1 26 = 10000002
,82 64 = 18
18 .
. 2
;8- ,4 24 = 16
1 24 = 100002
.18 16 = 2
. ; 2 .2-
,1 :21
1 21 = 102
,2 2 = 0 .
56
. , :
1000000
10000
10
10100102 = 82
,
:
2.5
10100102
4 3 2 1 0
64 32 16 8 4 2 1
1 0 0 1 0 1001012
5
0
6
1
64+16+2= 82
:
N , , .M
M- 0
N 0- :
2 N-
M
N-
.2
,2-
) (;
. .0
57
, 84 )
(:
a0 = 0
)82:2 = 41 (0
a1 = 1
)41:2 = 20 (1
a2 = 0
)20:2 = 10 (0
a3 = 0
)10:2 = 5 (0
a4 = 1
)5:2 = 2 (1
a5 = 0
)2:2 = 2 (0
a6 = 1
)1:2 = 0 (1
.10100102 :
2.7
;
:
29 .
83 .
196 .
, , ,
. , -
.
2.2.4
, , ,
) 0 1( ,
. : ) 773 3(
.11000001012- 10!
58
0- 1-
. ,
) (16 ) .(8
.
, ,
. ,
,
.
, ,8 , 8:
0 1 2 3 4 5 6 7
, 10028 , 4167418 :
.16 , 16-
. 10 ,
, :
10 11 12 13 14 15
, :
12316 , A01F16 , FF16
, ,16 , .1016
2.8
. 7 107568, A071B16 :
. , ?108
59
.
,
,
:
K8 = an1 8n1 + an2 8n2 + + a2 82 + a1 81 + a0 80
, :
K16 = an1 16n1 + an2 16n2 + + a2 162 + a1 161 + a0 160
; ,
. :
1378 = 182 + 381 + 780 = 164 + 38 + 71 = 95
1.
2.
3.
4.
+ 1116 + 21 = 42673
, ,
. , 1AF16 10- A
15- .F
.
, ,
: ,
)
(; .
.0
60
2.1
2615
8-
a0 = 7
)2615:8 = 326(7
a1 = 6
)326:8 = 40(6
a2 = 0
)5(0
40:8
a3 = 5
)0(5
5:8
.50678
2.2 2615
a0 = 7
)2615:16 = 163 (7
a1 = 3
)163:16 = 10 (3
a2 = A
10:16
)0 (10
A3716
: A .10
2.9
. 10AD16 .2 105328 .1 :
. 1956 431-
.
.
,
,:
264 = 18556744073709551616
61
, ,
" " :
18,446,744,073,709,551,616
. :
M = 010,110,000,101,111,0012
,
- . ,2.6
,M , 0 ,7-
, .7 0
0 ,7 ) 8 ( .
8 ; ,
, 23 .8
2.6
8 16
16
16
0000
1000
0001
1001
0010
1010
0011
1011
0100
1100
0101
1101
0110
1110
0111
1111
,8 3 )
(2.6 . M :
M = 010,110,000,101,111,0012 = 2605718
M 8 6 ) 17 (.
62
2.10
:
1011011012 .
100010001102 .
2.11
:
63028 .
145018 .
.
.
, ,16
- .16 4
.2.6
:
0110,1010,0011,1100,10002 -
.6A3C816
AB4816 10101011010010002
, 8 16- :
, ,
. 3 4
, .
, ) 2 9 (13 ,
.
2.12
. 1985 ,- .
63
. 1101011101012 -
. .
. 3E1B16 .
2.13
:
492
100101110101
721
2D4F
2.14
. . ,2- 4- 8-
?
. -?
2.15
.
.
2.16
-
.
.
2.17
.
.
64
2.3
2.3.1
. 1206.9023 : .0.0617
100101.011012 : .1001.11012
, .
, :
= 100101.011012
125 + 024 + 023 + 122 + 021 + 120 + 021 + 122 + 123 + 024 + 125
= 32 + 4 + 1 + 0.25 + 0.125 + 0.03125 = 37.40625
:
) (2.2- 2-
.2- :
) (;
.
, 37.40625 .
. ,37 :
32 16 8 4 2 1
0 1 0 1
. ,0.40625 :2-
0.40625 2 = 0.8125
0.8125 2 = 1.625
= 1.25
0.625 2
= 0.5
0.25 2
= 1.0
0.5 2
65
. 100101.011012 :
, . ,
0.2 ,
:
0.2 2 = 0.4
0.4 2 = 0.8
0.8 2 = 1.6
0.6 2 = 1.2
0 ,
0.2 2 = 0.4
. 0.2
.
. ,
- , .
: 1 0.333 : . 3.14159
3
, ,
) ( . :
300,000,000 . .
.
66
3.0108- . )
.(10 ,
, ,
.
2.3.2
:
? ,
) .(fixed point representation n
. :
,
.n2 ,
. , ,123502 ,
,1235.02 , , 100112 ,100.112
111002 .111.002
. .
: , ,
0.0014 0.006 .0.01 ,
0.004 0.007 ,0.00-
. , , ,
. ,
,5
.99999-
2.18
. ,:
99.99 .50 . 1000
.
.
67
2.3.3
, ,
) .(floating point representation , "" ,
.
. . 300,000,000 ,
, :
3.0 108
30.0 107
0.3 109
- .
, N2k N
k- . ,
, ,N , ,:
N 12- ) 102- , :
.(1 N < 2
) , , , (:
10122 1.0124
0.00101 .1.0123
32.
:
]8 [3023] 23 [2200
]1 [31
68
31 ;
8 ) 30 (23 ;
23 ) 22 (0 .
, 010000111001011000000000000000002 :
0 10000111 00101100000000000000000
) (31 1 0
.
,1 + f :
.0 f < 1 1.00101, 1.11101 : . , ,
.
:
,17 100012 ,
:
24
100012 = 1.00012
69
:
1100 1.1 23
10 1.0 21
11100.001 1.1100001 24
, 1
) ( ,
. 17
,1.00012- .00012 .
:
. 85.75 :
85.75 :
85.75 = 1010101.112
.1010101.112 = 1.01010111226 :
.010101112 :
,6 127 133
.100001012
, .0-
:
010000101010101110000000000000002 = 42AB800016
. 10000 :
100 10000= 10011100012
.1.0011100012 213 :
.010101112 :
,13 127 140
.100011002
.1
70
:
110001100 001110001000000000000002 = C61C400016
:
. ;
. ,1.f ;f
. ;
. .127
. ) (.
, , :
;0 0
+0 1 .0
1 .0
, 0 + 1 .
:
2126
(2223) 2127
:
~1044.85
~1038.53
,
, .
71
2.4
, , )(
, , .% ,$ , ,+ :
.
,
. )( ,
, ASCII .UNICODE
- . :
IBM ,
.ASCII ,
!
2.4.1 ASCII
,(American Standard Code for Information Interchange) ASCII
1968 . -
. 7 ,
ASCII 8 . ASCII
256 , 128 ABC- )
( , , 128
. : -
224- ) 250 .(ISO 8859-8 ASCII
- . , A
65 B .66
2.7 ASCII- 7 ) , 128( .
, 31- , , ,
, ,.
72
2.3
ASSEMBLY
ASSEMBLY ASCII :
01000001 01010011 01010011 01000101 01001101 01000010 01000110
2.7 ASCII.
2.7
ASCII
73
2.20
:ASCII
Assembly .
6 = 23 .
'Hello World!' .
. ) ( .
2.21
) (2.7 :
01000001 01010011 01010011 01000101 01001101 01000010 01001100
01011001
2.4.2 Unicode
, 256
. , 8
, . ASCII
, - . ,
,128- ,
, .128-
- ,
- , .
1991 ;
") Unicode "( 16 ) 8
.(ASCII 65,536 ,
. : AUnicode-
U+0041 U Unicode 0041
.A U+0042 B
U+0043 .C
, .
74
,
Windows , Windows NT- .3.1
, .
,
- , Unicode
.
2.22
. ASCII ?Unicode-
.
. ) Unicode- (ASCII
?
) 2.23(
Unicode :
http://www.unicode.org/
.Unicode-
, ,
. , -
.
2.5
, :,
, , . 0
1- . , -
) .(Picture Element Pixel
.
:
- ;
75
2.1 .
2.1
: . 2.2
, , ) -(
.
: 0 : 1- .
2.2 .
, 4 , : , ,
? , , 4
.:
00
01
10
11
76
, 2.2 , 2 ,
.
2.2
2.23
16 ? 256 ?
,
. ) (
, .
2.24
256 1000-
.
; -
, . ,
)
( . , , ,
,
. , ,
,
77
,
.
2.6
, ,
, , .
) ( : ,
. - .
. n
2n .
,
, . ""
) .(overflow .
.
) (
"" . .
8 ) "" ,( ,
16 . 32 ) : ,(4
64 .128 ,8086
, 16 , 16.
. .8086
. )(byte
"" ) (byte 8.
2.3
78
) (
000000002 .111111112
char .ASCII ,
0- ,255 .
:
28 = 256
, "" 8- , ,
" 7 ,
.
2.26
.
,
,
,
, , .
. )(word
16 .
:
216 = 65,536
0 .65,535
2.4
79
. ,
0 7 ) (L.O. byte Low Order byte
8- 15 ).(H.O. byte High Order byte
2.27
, ,
16 ,
.
. )(Double Words
32 . , ,
,:
= 4,294,967,296
232
0
.4,294,967,295
2.5
, ,
0 15
16- 31 .
2.28
,
, ,
.
80
2.6
2.29
. .
. .
2.30
?
2.8 .
, , :
2- .
2.8
23=8
24 = 16
25 = 32
26 = 64
256
65536
4294967296
1.84467*1019
81
: 10n10-
.10n+1
,
. ,
,
. :
; 0 ,100- .
1 1000- . ,
320? ,
.
2.31
.
?
. 65536 ?
2.32
,RGB :
) ,R ,(Red ) ,G (Green ) ,B .(Blue
256 . : -
.102,205,170
?
82
.
; : , ,
. ,
.
,
, :
;
ASCII ,UNICODE-
;
, ;
.
:
;
) ( .
. ,
. .
83
10-
.
, .10- Nb
n , ) b , , ,(1-
:
Kb = (an1an2an3 a 2 a 1 a 0 )b
= an1bn1 + an2bn2 + an3bn3 + + a2b2 + a1b1 + a0b0
b b- . ,
6 6-:
0 1 2 3 4 5
,
. :
4215 = 452 + 251 + 150 = 11110
) 111 .(10
= 3 84 + 2 83 + 4 82 + 6 81 + 1 80 = 13617
324618
= 1 25 + 0 24 + 1 23 + 1 22 + 0 21 + 1 20 = 45
1011012
84
10
,
. , :
.
.
Nb = an1 bn1 + an2 bn2 + + a2 b2 + a1 b1 + a0 b0
; -
b . :
Kb= (an1 bn2 + an2 bn3 + + a2 b1 + a1)b + a0
- Kb b- , ,
.a0 .Kb
,
+ an2 bn3 + + a2 b + a1
bn2
Kb = an1
6
a0
a1
a2
.14710 = 4036 :
.462 + 061 + 360 = 14710 :
3
0
4
24
4
0
= 147:6
= 24:6
= 4:6
85
) : (,
: , ,
. ,
, .
3.1
.
, :
35910
24510
:
9 + 5 = 14 .
14 ) ,(10 ) ,(14 10 = 4
) ,(4 ) , (1 ) ,(carry .
5 + 4 + 1 = 10 .
)(9 + 5
10 ) ,(10 ) ,(10 10 = 0
) ,(0 1 .
86
3 + 2 + 1 = 6 .
6 ) ,(10 ) (6
. ) ( .:
1 1
3 5 910
2 4 510
6 0 410
; .
,
) .(3.1 1+1 = 10 ,
) 102 (2 ,2
, .12+12 = 102 :
, .
3.1
)(carry
a+b
87
;3.1 ,
,
.
,
) 2 ( .
, .
.3.1
1
1 11
1 1 1 0 1 02
+
101011
____________2
1 1 0 0 1 0 12
:
,.
. ?
,
.
= 101012 + 110012
,
.
1
1
1 0 1 0 12
21
+
25
___
1 1 0 0 12
__________
46
1 0 1 1 1 02
88
:
1 ,1- ,2 ,102 , 0,
,"" 1 . ) ( 0
.0- ) ,(1 + 0 + 0 ;1
. ,1
.1 ,2
,0 .1
,- .
:
.
11
8
10112
10002
100112
19
15
15
11112
11112
30
111102
1 .1 3
) (1 + 1 + 1 = 3 112 , 1 "" 1 .
, = ,1012 + 112 + 11012
.
) (3.1 ,- .
1
1
+
89
3.1
, .
1000112 + 1012 .
10111002 + 110112 .
10110001012 + 110012 + 10012 .
3.2
10 ,2.16-
10F16 + C1016 .
E016 + FF16 + AF1 .
:
, .
, ,16 .
, , ,
, ,
.- , ) ( ,
. ,
, 8:
1 0 1 0 1 1 0 1
1 1 1 0 1 0 1 1
1 1 0 0 1 1 1 1 0
9 . ,
) ( 8 , ,100111102
. .
"" ) .(Overflow ) (
.
.
90
3.3
. .
.
. ?
.
a, b
) (a ) ,(b ) a b (.
, ) a < b
( .
.
,
:
329
245
:
.
:
95=4
. ,
.2 4 : , 10
.12 4 = 8 10
, .2 :
2 12 9
4 5
91
. ,
2 2 = 0 :
2 12 9
4 5
8 4
,
) .(3.2
3.2
)(borrow
ab
) (a ,
) (b .
, , .
, .
,
. 1- ,
.
: 02 12 .
02 12 - ,
.102 12:
100011012
1010102
11000112
92
, , ,02 12 ,
02- , .02 ,
:1002 12
1002
12
112
:
15
10
11112
10102
1012
*b
.
11
5
10112
1012
1102
3.4
, - :
1010112 .
1001012
100002 .
10112
11011001012 .
1011111112
10111112 .
1111112
) b (borrow ""
93
3.3 .
3.3
ab
) (:
1102
1012
110
000
110
111102
: , ,2 . ,
,2 , ,
. , ,0
, .
:
.
10
3
30
10102
112
1010
1010
111102
94
49
1112
1112
111
111
111
1100012
.
, .,
:
1092 100 = 109200
, 10-
. 2 - ,
:
- .
, , " ,
, , .:
0 0 0 0 0 1 1 0 1
1 0 0 0
1 0 1 1 0 1 0 0 0
3.5
, -
:
10112 .
1012
11012 .
1102
110101112
1011012
95
3.6
11112 : 101112-
. ;
.
) ( , ,
, . *:
0
=0
1
1
=1
1
,
.
3.1
.100001:11
1 1
0 1 1 1
0
0
0
1
0
1
1 1
1 1
0
1
0 0
1 1
1
) (;
0 1
;
0 0
0- .
96
. ) (
) " .(11 < 100 1
) , (.
. ) 1- (
) .(100 11 = 1
. ,
. .
,:
1 0 0 1
1 0 1 1 1 0 1 0 1
1 0 1
0 1 1 0
1 0 1
10012 .12
. ) ( ,
, ,
) (.
. , .
--
. , :103 = 1000 -
97
3.7
) (:
1001100:100 = ? .
1000:100 = ? .
3.8
, :
3.2
,
.
3.2.1
. ,
, ) (
0 .1- + ,
; 0 ,
, 1 ,
. .
. ,
8 ,
7 . ,
8 , 00000000 ,01111111
10000000 ) 11111111 (.
98
3.9
.
, , ,
0 ,7- 4- :
)( .
7 7- 4- . 3.4 15
, , .
3.4
0000
0
1000
1001
0001
1010
0010
1011
0011
1100
0100
1101
0101
1110
0110
1111
0111
, , .
, , . ,
0 1000 : .0000- )
,(0000- ) (
. .
99
, 1 11002 :4
11002
12
11012
11012 5
4 + 1 = 3
:
)(00012
(10012) 1
1010 !
, .
3.10
3.4 ) ,a + (a a 0.7-
, , ,
.
, ,
.
, ;
, . , ,
, , , ) b (:
)a b = a + (b
, ,b- ,
.
100
) a + (a .0
3 4:
00112
????2
00002
, ) (3 11012- 3-
0011 2
1101 2
1 0000 2
5 , 4- ,
) . (1
,a = 0 a 0
n- .2n
1 0000 0 ) (3 24 3 .11012
3 .0 3
11012 :1
) 1(
00002
00112
11012
, .
) .(two's complement 3.5
2- 4.
101
3.5
2-
0000
1000
0001
1001
0010
1010
10
0011
1011
11
0100
1100
12
0101
1101
13
0110
1110
14
0111
1111
15
n :
.1 , , 1 0-
0 .1-
.2 1 .
5 :2-
01012
10102
12
10112
25
:25
0 0 0 1 1 0 0 1
1 1 1 0 0 1 1 0
:1
: 25 -
1 1 1 0 0 1 1 1
102
000110012
111001112
1 00000000 2
,
. , ,
,1 , ,
, . .010100002
, ) ( ,101100002
.
:
n :
.1
,
1
.2
.1
:
. .25-
000110012 = 25 ,
,1 ,
, ,111001112 25 .
. .64-
010000002 = 64 ,
7- ,1
110000002 64 .
103
.
: a=111101012 ,000010112 .a = 11
3.11
, , 8- .
. :
)(1
101110012
)(2
010110112
)1101101012 (3
.
. ?000000002
. , ?111111112
?
3.3
.
3.3.1
:
; .
8 )(.
A , B- , ,B
, .
104
3.2
) A + (B
)23 + (13
8
:
23 = 000101112
13 = 000011012
13 , :
13 = 111100112
:
000101112
+ 111100112
100001010 2
. ) (end-carry
100000000 , , , .
; 0 ,
:
10102 = 10
3.3
) A + (B
)18 + (45
8
:
18 = 000100102
45 = 001011012
105
,
:
45 = 110100112
:
000100102
+ 110100112
111001012
,1
) , ( .
,:
000110112 = 27
, , 8-
. .
.
:
53 + 120 = 173
, , ,
,0 :1
53
001101012
120
011110002
101011012
; .83
.
:
106
80
101100002
64
110000002
1011100002
) 0 ( , .
3.3.2
, :
b = a + (b)a
,
, .
3.4
).13 (23
13 = 111100112
13
+
23
23 = 000101112
+10
10 =1 00001010 2
107
3.12
W = 100111102 ;V = 011000112 ;U = 101101112 :
) W ,V ,U .(2-
, :
Y = U + V + W .
Z = U + V W .
3.13
A = 2238 ,B = 10510 :
:
S = A + B .
X = A B .
3.4
,
.
, n:
0- 2n 1
) + (2n1 1.(2n1)-
:
000000002
111111112 - 0 .255
, .28 1 = 256 1 :
108
) 011111112 ( ) 111111112-
( +127 ) .(128
:
27 1 = 128 1 = 127
27 = 128
3.14
,
16 . ,
.
8086
109
8086
4.1
, ,
. ,
,8086
.
, :
, .
.
8086 .1978
,1970
. "-" .
.4000
.
110
4.2 8086-
,4.1
. 8086 14 16 )
( . ,
. ,
.
4.1
8086
8086
111
, ;
) /( .
CX ,BX ,AX :.DX-
.
: / ,
, ADD AX, BX .ADD CX, DX
,
, . ,AX
) ,(Accumulator DX ) ;(Data register
/ , AX ,
DX / .
, CX ) .(Counter register BX
) ,(Base register
)(.
, ) 16( .
,4.2 ,
) 8( . ) 0 (7
, ) L ;(Low )
8 (15 , ) H .(High LH-
X . , , AX 16
112
4.2
AX
, AX ) MOV (
:
FF00h AX
) AL (AX BL
mov bl, al
.
. SP, BP, SI, DI :.IP-
8086
113
:
(Instruction Pointer) IP
. ,
, , IP .
(Stack Pointer) SP
) .(stack .7
BP, SI, DI , ,
)( . .6
,
, .
"" , .
8086 ) ;(16 :
) (status flags
.
) (control flags .
.4.1
. ,
.
CS, DS, SS : ,ES- .
; ,
.
.
114
4.1
CF
) :(Parity Flag ' '1 8
PF
' '1
- ) :(Auxiliary Flag ' '1
AF
4- AL
) :(Zero Flag ' '1
ZF
) :(Sign Flag
SF
(
)) :(Trap Flag '
TF
'( ,'1'- -
.
.TF
):(Interrupt-enable Flag
IF
,'1'-
- ,
-
) :(Direction Flag ,'1'-
DF
10
. '0'-
.
) :(Overflow Flag ' '1
.
OF
11
8086
115
4.3 8086
4.3.1
.
, ,
. ,
, : .
.1 . 8086
) ,(byte 8 , 28 = 256
. ,
- . , 1000 .
.2 . ,
, .
. 8086
20 , 220
1,048,576- , .
000000000000000000002
.111111111111111111112 20 ,
. ,
00000h .FFFFFh
4.1
.
32.
. , ,
32.
;
. .4.2
116
4.2
Kilo
210 = 1,024 = 1 K
Mega
220 = 1,024 K = 1 M
Giga
230 = 1,024 M = 1 G
Tera
240 = 1,024 G = 1 T
Peta
250 = 1,024 T = 1 P
Exae
260 = 1,024 PB = 1 E
. :
8086 1,048,576 , .1M
B ) .(Byte ) 1MB ,(1Mega Byte
, , .
4.2
- 36;
.Giga
) ( .
.
:
. , .(Read-Only Memory) ROM
.
. , " Random Access Memory) RAM
"( . .
8086
117
8086 :
.1 0 .640K " ".
,RAM .
.2 640K .896K-
/ .
.3 896K .1M-
) (ROM ) ROM BIOS Basic Input/Output
.(System , ) (
) ( )
(.
. .
4.3
.
4.4
:
. ;
. ;
. .
4.3.2 )(
, )segmented
.(addressing ,
) ,(segments .64KB , 8086- " 16",
: 16 , 16 .
, )"" ,
118
4.5
1MB
. .1610
. .1MB 16
.
.64K 4.2 K = 210:
64K = 64 210 = 26 210 = 26+10 = 216
, , 16 .
) ,(Relative address
) ( . :
) ,(Effective addres ) (Logical address ) .(Offset
.
4.6
, ,
.64K
,
.
. , 7
8086
119
. - ) (
) (
) ( , .03-6666457
, .
,
.
8086 16 .
: 16
20? , :
. 4) 0000 : (0h
.
.16-
.- .
, ,4.3 1100h .450h
11000h- 450h
:
11000h
+ 450h
11450h
4.3
11450h 450h
120
4.7
100h
.2000h
;
, ) ( . , 4.4
.64KB
4.4
1000h
.1200h 20A9h
? .120A9h
A9h . , ,
.
8086
121
4.8
. A9h
.1000h
. A9h
.1200h
. ?
. , ,
?
;
.8086 8086
) ;(64KB :
) (Code Segment .-
;
- , .
) (Data Segment .
- .
) (Stack Segment )(
. .7
) (External Segment , -
.
, CS, DS, SS :
.ES- 4.5
.
122
4.5
,
.-
,
.
. , :
]mov al, [si
,AL , .SI
,
. ) (
8086
123
. IP ,
.CS , BP ,
,SS BX, SI DI-
, .DS ,
, . ,
,
.
. , ,8085
,8086 ;64KB
8085 .8086 8086
64KB .8085
, :
, )
( , ) ( .
.
, ,
.
)( -
, .
, - .
4.4
4.4.1
)
( .
:
124
23 a
54 b
a + b a
:
23 a
23d
54 b
mov b, 54d
b ax
mov a,
mov ax, b
a, ax
ax + a ax
add
:
) MOV (move
.ADD , )
( .
,
. , , MOV.
, MOV )
( . MOV )
(:
,
mov
: ; :
, . mov
MOV ,
. "" ) .(Assembler
, ,
.
8086
125
:8086
mov ax, a
A1h
B8h
, ADD ,
.
8086 ;
) (syntax . :
,
.
. , ,
, .
- , 01-
.
:
}{;Comment
}{Operands
Mnemonic
}{Label:
, : .
, ) (Mnemonic . ,
, .
:
.1 .
)( .
, . : , ,
, .
126
, doOper :mov
doOper: mov ax, bx
.
.2 ) (mnemonic
, .ADD, CMP ,MOV , ,
.
.3 ) (operands
; : , . 8086
.
.4 ;
. ;
. ;
) (.
4.4.2
, ;
, ,
. "
" ) ;(Assembler directives
- , . ,
,
.
; ; ,
, .
.MODEL SMALL
.STACK 100H
.DATA
8086
DW
DW
sum DW
127
.CODE
start:
1.
2.
mov ds,
3.
ax
4.
; ax = a
mov ax,
5.
; ax = a + b
add ax,
6.
mov sum, ax
7.
; sum = ax
8.
9.
int
10.
21h
END start
, :
.
- , -
.
,
. .
. :
1 .
.MODEL SMALL
MODEL , SMALL
.
128
:
2 . :
.STACK 100h
, ;
, .
3 . :
.DATA
4 . 6 . :
. a - ) DW (Define Words
.2
2
DW
. b - DW .5
5
DW
. sum - DW
- '?'.
?
sum DW
7 . )""( ,
:
CODE
8 . :
start:
start .
start .
- .
8086
129
9 . . ,
. :
';'
;
10 ;11-
:
10 . AX .
@DATA
.
.
11 . AX
.DS
; ax
; ds ax
mov ds,
';'
" .
12 . ; ) 13 . ,(15
a + b , .sum
; ax a
; ax a + b
; sum ax
mov ax, a
b
add ax,
mov sum, ax
16 . , , 17 ,18-
,
. .9
130
int
19 , ;
.
END start
, :
.MODEL SMALL
.STACK 100h
.DATA
CODE
start:
;
int
END start
: .
. ,
.
,
.
.
8086
131
4.9
) ( :
x = 10
y = x+1
4.5
.
. :
.1 , ;
.2 , .
.
, " " ,:
;var i: integer
C :
;int i
i ""
) ( . , :,
, MOD.DIV-
. ,
,
) (.
, - ,
.
:
init-value
data-type
var-name
132
31 ,
, '.'.' ,'?' ,'@' ,'-' ,'$
) D
,(Define , ,
:
Byte
DB
Word
DW
Double Word
DD
Quad Word
DQ
10
DT
, .
- '?'.
:
. ,stored ,BYTE ASCII y
; DB y
sorted
. ,response ,BYTE
? response DB
. ,value ,WORD 25159
DW 25159
:
ASCII
value
8086
133
, .
, :
d ; . 23d :
h ; .23h :
b ; .10001011b :
:
,A F 0 .
, .
, . ,
, ,a ,10:
10d
DB
, F6h
.10
8086 ,
,
.
.
:
?
, . ,
char :a
"char DB "a
char , ASCII .a
134
,
:
.DATA
, ) (,
"" ) .(link
,
. , ,
, .0
, ,
.
, :
? ,
. ,
.
.little-endian ,
, )
,little-endian : " "; ,
" " .(little Indian , 10110111100110002
: 101h ,
,101101112 100h ,
.100110002
4.6
little-endian
8086
135
, ;
.100h ,
) ( ,
.
, :
.DATA
10h
var1 DB
00h
var2 DB
var3 DW 1234h
28h
sum DB
var4 DW 51ABh
?
var5 DB
''e
char1 DB
''H
char2 DB
12345678h
var6 DD
,DS = 01000h
) (4.7 .
:4.7
.
.0
.
"
.
.
:
.
136
, , ,
, . :
var3 ) 1002h 1002h.(1003h-
1009h 1008h- char1 .char2- ,
65h ASCII H 48h ASCII .e
4.7
DS
;
.:
8086
137
var1 al
:
mov al, var1
:
mov al, 1000h
.
var3 .BX , var3
:BX
mov bx, var3
BX =1234h :
,
integer .char 0 ,1
, , ,
. , ,BYTE:
boolean DB 0
) (true 0 )
( ) .(false , , ;C .
,8086 , ,
: .
4.10
:
.DATA
10h
a DB
1234h
b DD
c DW 100h
''H
.
d DB
138
4.6 EQU
) EQUal .(EQUate
:
EQU
:
MAX ; 5
TEMP ; 6
MAX EQU 5
TEMP EQU 2*3
, , .
, , )
( .
,:
MODEL SMALL
.STACK 100h
;
MAX EQU 5
DATA
5 ;
;
a DB 1,2,3,4, 5
sum db 0
;
. ,MAX
; :
mov al, MAX
add ah, MAX
8086
139
, ,
) Windows ,DOS , .(exe
,
.
: , .
.
. )
PSP (Program Segment Prefix .
: ,
. ,
; DS ES-
.PSP- DS ,
:
mov ax, @DATA
ax
mov ds,
.DS
, )
( ,
,DS .AX @
.
,
. " "
) .(symbol table ) , ,(
140
, . ,
.4.3
4.3
WORD
DATA
WORD
DATA
WORD
DATA
sum
CODE
start
141
5.1
8086
. -
. :
, , .
) ( .
, : .
5.2
:
.
.
: MOV XCHG
. .
142
) ( ) ,
( , mov ax, al : . ,
,.
.5.1
5.1
MOV
mov ax, bx
mov var2, al
:
.
var2 var1
:
; ax var1
; var2 ax
mov var2, ax
, ,
, .
143
:
mov al, 10h
; al 10h
; bl 0F0h
xchg al, bl
; al 0F0h, bl = 10h
.5.2
5.2
XCHG
xchg ax, bx
xchg var, AL
5.1
.
mov bh, al .
mov dh, cx .
mov bh, bh .
mov cl, 4F2h .
mov cx, 002dh .
xchg var1, var2 .
5.2
:
.DATA
a db 12h
b db 34h
c db 78h
144
: a ,b
b ,c c - .a :
a = 78h
b = 12h
c = 34h
? ,
.
mov al, a
xchg al, b
xchg al, c
5.3
AX BX .XCHG
5.3
,
.
, .5.5 8086
.
5.3.1 ADDSUB-
. ADD
ADD :
, ADD
145
,
add ax, 1234
; ax ax + 1234
add ax, bx
; ax ax + bx
. (SUBtract) SUB
SUB
, SUB
,
sub bx, 1234
; bx bx 1234
sub cx, dx
; cx cx dx
5.3
ADDSUB-
sub al, ah
add al, ah
sub var, ax
add var, ax
146
5.1
:
aa+b c
b c b + 20
c 2c
.a = 38, b = 23, c = 36 :
:
.
) ( .
.6
.
. .
.
.DATA
; a =26h
DB 38
; b = 17h
DB 23
; c = 24h
DB 36
.
.
, :
. a a + b - c :
"+" : "-" -
, .
147
:
aa+b
aac
,
.
, AX "" .
16
AL . :aa+bc
al a
al al + b
al al c
a al
; al 26h
mov al, a
al, b
add
al, c
sub
; a 19h
mov a, al
. b c b + 20 :
:
bcb
b b + 20
, AL
c .b AL
.a
148
b c b + 20:
al c
al al b
al al + 20
b al
; al 24h
mov al, c
al, b
sub
al, 20
add
mov b, al
. c 2c :
,
:
c c+c
AL :
al c
c c + al
:
; al 24h
mov al, c
add c, al
149
.MODEL SMALL
.STACK 100h
.DATA
a DB 38
; a = 26h
b DB 23
; b = 17h
c DB 36
; c = 24h
.CODE
start:
;
; al 26h
add al, b
sub al, c
mov a, al
; a 19h
; a c b + 20
mov al, c
; al 24h
sub al, b
; al al b = 24h 17h = 0D
add al, 20
mov b, al
mov al, c
; al 24h
add c, al
150
5.4
.a 2b (c+12) :
.
5.3.2 ADDSUB-
5.1
) 0 .(255-
:
a = 172d
b = 201d
c = 78d
, a :
a a + b c = 39
:
172 + 201 78 = 295
: ? ?
,
0 255- .
:
; al 0Ach
mov al, a
? = ; al al + b
add al, b
:5.1
5.1
172
10101100
+ 201
+ 11001001
373
1 01110101
151
5.1 9 . ,
8 , "" AL
:
AL 011101012 = 75h = 117
, :
sub al, c
. , ,
.
, ) .(overflow -
, 2n n ,
. , ,x ,
:
x = x mod 2n
, ,5.1 , 8
28:
x = (201+172) mod 28 = 373 mod 256 = 117
,
.
5.4
.
5.5
AX :
mov ax, 0F023h
add ax, 067FAh
- .
152
5.6
. 5.1
:
a = 172d
b = 201d
c = 78d
, , .
. ' ,
.
.
- , ,
.
, ,
.
,
. , ,
MOV XCHG- .
. ADD,SUB-
.
153
.0
:
0
ZF = 1
ZF = 0
,
; al = 0 , ZF = 1
sub al, al
,
.
154
5.7
? ;
, add ZF .1
. CF
. :
) < (
) > (0
CF = 1
CF = 0
:
. , 0FFh-
.
. 0FFFFh-
.
.
5.2
. :
CF
;al = 2,
mov al, 2
CF = 1
;al = 1,
1- ,
2+0FFh = 101h > 0FFh
. :
; ah = 09h, CF
; ah = 0Fh,
CF = 1
155
1-
09h 0FAh= 240h < 0
,240h .DC0h-
. :0
'
ZF = 0
; ah = 0h, CF = 0,
sub ah, ah
,0 .1-
.
, .
5.8
:
mov cl, 0FEH .
add cl, 1
.
sub dl, dl
add dl, 0FFh
mov 10h
mov bl, 10
sub bl, al
add bl, al
156
. )(SF
:
1
SF = 1
SF = 0
.
, ,1
0 .
.
5.3
.
208- . 2003
214 ) ( , 5-' .
:
. .
. ?
.
+127 ,-128
:
.DATA
; waterLevel = 0FF2Ah
DW 214
waterLevel
; redLine = 0FF30h
DW 208
redLine
.
5.4
:
157
5.4
; ax 0FF2Ah
CF=0
SF=1
; ax ax + 5 = 0FF2Fh
add ax, 5
CF=0
SF=0
= ;redLine redLine ax
sub
redLine, ax
5.9
.SUB ax, redline :
ax .
. OF
. :
) < (
) > (
OF = 1
OF = 0
:
. , +127-
.128-
.
+32767- .32768-
158
.
5.4
, 65
75 .
.
:
; summer = 41h
summer DB 65d
; winter = 0B5h
winter
DB 75d
:
; al 41h
; al 41h B5h = 8Ch, OF =1, SF = 1
SF 1 1 OF
,:
65 (75) = 140 > 127
.
:
; summer = 0041h
summer DD 65d
; winter = 0FFB5h
winter
DD 75d
:
; ax 0041h
; ax 0041h 0FFB5h = 008Ch, OF =0, SF = 0
sub ax,
159
?
,
,
. ,5.4 ,
,CF ,
.CF = 1
. ;255- , ,
?
41h B5h < 0 :
, 0- .1
, .
.
) (CF
).(OF
5.10
:
mov al, 0A9h
add al, 010h
sub al, 0D0h
add al, 0FEh
5.3.4
ADD SUB-
. .
160
.
8086 .
. , ,
,
,
.,
1
+ 5678
D876
2EEE
+ 1234
C9BA
DBEF
8086 :
(ADd with Carry) ADC
, ADC
+ CF : +
:(SuBtract with Borrow) SBB
, SBB
CF :
,5.3 : , ,
.
ADC SBB- PF ,OF ,CF ,SF ,ZF :AF-
.
5.5
161
:
12345678h
+ C9BAB876h
12345678h AX BX-
C9BAB876h CX DX- :
5678h
; ax
1234h
; bx
; cx 0B876h
; dx 0C9BAh
CF = 1
add ax, cx
CF = 0
adc bx, dx
5.11
. 5.5
add ax, cx
:
adc ax, cx
.
. .5.5
. INCDEC-
- 1
1 , .
(INCrement) INC
inc
:
+ 1
162
(DECrement) DEC
dec
1
.5.5
5.5
INCDEC-
dec al
inc al
dec var
inc var
.
.
(NEGate) neg
NEG
163
:
0
.5.5
, 208 :redLine
redLine DW 208d
neg 208 :2-
; redLine 0FF30h
neg redLine
5.12
. ) neg (
. :
mov al, 0Ah
neg al
al
neg
5.4
,
- , IP
. ,
.
, ,
IP .
IP " " .
, ,
. :
. .
. .
164
nextSection:
, ,
jmp nextSection
: , .mov cx, 0Fh
.
.
) ( .
.
5.6 .jmp
165
5.6
al.
mov al, 3h
jmp next
add al, 12h
next: add al, 30h
next
. ) (,
.
al:
al = 3h + 30h = 33h
-
. jmp next 5.2
' IP , .0009
5.2
jmp next
IP
,next 000Ah
166
5.2
jmp next
5.4.2
. ,Jump if Zero) JZ
" "( 1 ,Jump if Above) JA
" ("- .
,
, .
:
IP label
IP
167
,
.
Jxxx
;J xxx
" " , ,x
. , , x Z .A
, ,
.
8086 :
. , 127
, 128 . -
. ,
:
.
:
. .
. .
. .
.
.5.6
168
5.6
)(Jump if Carry
JC
CF = 1
CF = 0
ZF = 1
ZF = 0
SF = 1
SF = 0
OF = 1
OF = 0
PF = 1
JP
PF = 0
JZ
JS
JO
. - .
5.7
:x-
x < 0
)x ( x
-
SF
, .
,x
.x 0,x-
.x 0 : , :
169
x , (x 0) 0 0
x (x 0) < 0 1
.
JS :
,1 JNS
.0
. 1 . JS .JNS
:1 . JS
:2 . JNS
sub x, 0
sub x, 0
js doAbs
x ;
x ;
jmp doNothing
doAbs: neg x
;
jnz doNothing
neg al
doNothing:
doNothing:
2 . .1 . 2 .
" ... "... ,
:
.1 x > 0 3 .
.2 x
.3
, 1 " ... .. "... ,
, :
.1 x < 0 3 .
) .2( 4 .
.3 x
.4
170
, .2 .
,
IP ,
, IP
)
(.
5.13
JNS doNothing
.1 . x .10
5.14
2 . sub :
jnz doNothing
neg x
;
doNothing:
.
, ,
, .5.7
: , ,,
, .
171
5.7
)JG (JNLE
)JA (JNBE
)JL (JNGE
)JB (JNAE
)JGE (JNL
)JAE (JNB
)JLE (JNG
)JBE (JNA
JE
JE
)(Jump if Equal
)(Jump if Equal
JNE
JNE
, ,
. , X ,Y- Y X
, :
172
5.15
5.7
.x
. ,
.
5.8
:
al < ah
inc ah
-
inc ah
AL < AH AH ,2-
.1-
, :
mov al, 11111110b
mov ah, 00000010b
; CF=0, SF=1, OF=1 :
sub al, ah
? ; al < ah
al < ah ; next-
jb next
; al >= ah
inc ah
inc ah
next:
:
. ,
11111110b 00000010b > 0
173
,JB next
AH .2-
. :
11111110b 00000010b < 0
JB next
,:
JL next
.next AH
.1-
.5.6 .AL AH < 0
.5.7
(compare) CMP
, .
- , .SUB
, , -,
, . CMP 8086-
. ,
.SUB
CMP:
, CMP
: ?
174
, ,
, CF, SF, OF, AF, PF : .
.- , ,CMP
.
.5.3
5.9
, ,x, y x- ,
y- .
:
x y < 0
x y-
-
x y-
:
; al x
mov al, x
?; x y
cmp al, y
x >= y ; end_if
jae end_if
al ; y
xchg al, y
; x al
mov x, al
end_if:
5.16
AX 100- .
,AX = 67 AX .134
175
5.10
"...:"...
a, b m- .
- :
a = b
mm+1
mm1
:
; al a
mov al, a
; al b
cmp al, b
jne doAction1
;mm+1
; continue
;mm1
inc m
jmp continue
doAction1: dec m
continue:
5.17
. ,JNE ?JNZ
. , JNE .JE
. JE JNE ?
.
176
. ?5.9
inc m
mov al, a
cmp al, b
je continue
dec m
dec m
continue:
5.18
,x, y, z .x> y > z :
5.19
. . '
) ( '
) ( .
, 50-
.
50 ,
, 50- ,
.
' ' ,
0 250- .
.
- ' ,Temp_Out
- ' .Temp_In ,
00 ,
FF .
177
5.11
- ) .(case-of :
5.8
Y
)F(x
x < 10
10 x < 20
20 x < 40
x 40
case :
.model small
.stack 100h
.data
;
x DB 10
? y DB
.CODE
start:
;
cmp x,
jb action1
178
cmp
action2
jb
cmp
action3
jb
jmp
action1:
jmp
action2:
jmp
action3:
jmp
action4:
jmp
endcase:
ax, 400h
mov
21h
int
END start
)(
. :
179
5.11
.1- .100-
, :
:1
:2
x1
sum 0
sum < 100:
sum sum + x
xx+1
x1
sum 0
:
sum sum + x
xx+1
sum 100 -
, :
AL
BL
: ,100-
.
:
1
mov al, 1
mov bl, 0
; BL < 100
cmp bl, 100
jae endLoop
mov al, 1
mov bl, 0
doAgain:
doAgain:
Add bl, al
inc al
jmp doAgin
add bl, al
in al
; BL >= 100
cmp bl, 100
jb doAgain
endLoop:
2 , .
180
5.20
. - 1-
.100-
.
.13 ,10 ,7 ,4 ,1 :
.50-
.100-
8086 .
LOOP
LOOP :
LOOP label
, CX
0 . CX ,
:
CX CX 1
CX 0 label
5.12
, 10 ...,3,2,1 :
;
; CX 10
; BX 0
mov cx, 10
mov bx, 0
doLoop:
; bx bx + cx
add bx, cx
;doLoop -
LOOP doLoop
181
LOOP CX
, .10- : 1
CX .
LOOP ,CX
CX . ,
10 ,1- BX:CX-
doLoop:
add bx, cx
mov cx, bx
LOOP doLoop
, CX ) (BX + CX
LOOP .1-
:CX - BX
5.10
CX
BX
)( )(
0Ah ;
0
add bx, cx
0Ah
0Ah
mov cx, bx
loop do_loop
add bx, cx
13h
13h
mov cx, bx
12h
loop do_loop
add bx, cx
25h
25h
mov cx, bx
24h
loop do_loop
182
CX ,
. , BX- ,
, .
8086 ,
CX , . .
5.21
. .
. :
mov cx, 0
mov bx, 0
doloop:
inc bx
loop doloop
5.5
.
: .
,
.
: 2 ,
4 ) .(9999 = 9801 ,
, )(0FFx0FF = 0FE01
.
183
8086 , ) AX AH
(AL-) DX- ( ,
, " " " "
). (implicit operand ,
.
5.5.1
add, sub
, ) ( , :
. :(multiplication) MUL
MUL
IMUL
AL .
) AX ( , AL
AH , :5.3
5.3
:
; al 12h
; cl 0A1h
; ax al cl = 0B52h
184
,
: DX AX
, :5.4
5.4
:
; ax 0AFh
; cx 0BF9h
; dx: ax ax cx
mul cx
82F37h:
; AX 2F37h
DX 0008h
IMUL ,
128- +127 , 32768- +32767 .
:
,
) ( ) ( , CFOF-
;1 . .
IMUL CF OF- .
, .:
; al 12h
;cl 0A1h
; ax al cl = 0F952h
185
5.5.2
, :
div .idiv
. (DIVision) DIV
DIV
. (Integer DIVison) IDIV
IDIV
.AX AX , AL
AH :
5.5
:
; ax 00FBh 251D
; cl 0Ch
, ah = 11d ; ax ax/cl , al = 20d
,
.AX : AX DX
:
186
5.6
:
; dx 0
mov dx, 0
; ax 5147fd
; cx 300d
div
IDIV .
) AL (AX
"" , .
.
5.5.3
idiv
AX 16 . ,
CBW .
187
.AX = FFFBh AL ,
,AH:
mov al, 0FBh
; al 0FBh
cbw
; ah = 0h, al = 07Bh
CWD
5.13
.
; al 95D
;ah 0FFH
cbw
; cl 12D
, alh = 11d ; al = 7d
.
; ax 5147D
; dx 0FFFFH
cwd
; cx 300D
, dx = 47d ; ax = 17d
5.22
n .
a ) 1 .(0
!:5
5! = 12345
188
5.6
)(
.
, " " ," :" "-" .
)"
"( .
" " ,
, )" :" ""(.
:/
5.7
,
)( , , . ,
4 16 0- ,15
.
i :( 0 i 15 ) i
1-
.0- 5.14 ,8 ,5 :
.14 ,10
0
10
11
12
13
14
15
5.8
189
""
. -
. , , ,
) (2290h .
.AND, OR, XOR, NOT :
, ,NOT : ,
.5.3
.
, .
.SF, ZF, PF :
, CF OF- .0 AF
.
5.9
AND
0 0
0 1
1 0
1 1
190
AND AND
:
, AND
:
AND
AND :
mov cl, 7Ah
mov bl, 5Bh
and bl, cl
,BL , , CL
,BL- .AND
:
CL
BL
AND BL, CL
5.6.2
,
.
"" ) .(Mask ,AND
0 .
AL .
0Fh :
and al, 0Fh
AL
) x- , 0 .(1
AL
191
5.10
AL 0Fh
5.23
5.5 AL
0AFh .
96h .
AND .
5.14
8 . 1
; .0
.
1- 0 ,
.
1- , 0-
.
BL 1 2 ,
.0
, 6 ,4 ,1 : 7- ) ,(1-
6 ,4 ,2 ,1 ,7-
.
192
,
. 1 0
.
:
,
.
, AND
.
0 1 .
:
10010110
11011111
AND
10010110
.
:
1 00101 1 0
01011101
AND
0 00101 0 0
1-
0- .
.
.
:
client
partner
BL
AL
193
:
BL 0
AL client
AL = partner
BL 1
AL AL AND partner
AL = partner
BL 2
-
BL .
.
.model small
.stack
.data
; :
;
client db 01101010b
partner db 01111010b
.code
start:
;
mov bl, 0
mov al, client
;
jne partial
194
; client = partner
;
mov bl, 1
jmp finish
partial:
;
;
mov bl, 2
finish:
mov ax, 4ch
21h
int
end start
5.6.3 "" OR
"" ) (OR ""
"" .
" . "+ x OR y :
x OR y x Y
5.11
OR
195
OR :
, OR
:
OR
,OR " "1 ,
. , 6
) 7- ( 1-
. 11000000 :
or cl, 11000000b
OR ,5.12
CL .0B2h
cl
1 0 1 1 0 0 1 0
1 1 0 0 0 0 0 0
cl or 11000000
1 1 1 1 0 0 1 0
5.12
OR
OR .
5.15
, 8
. ,
. 1,3,4,7 .
- 1 7-
3 .4-
.
196
operation
6 ,3 ,2 ,0 .1- ) 1,3,4,7
.(0
0
0 1 0 0 1 1 0 1
.worker1, worker2
5.13 OR :
0
worker1
0 1 0 0 1 0 0 0
worker2
0 0 0 0 0 1 0 1
0 1 0 0 1 1 0 1 OR
5.13
OR
OR- .
:
mov al, worker1
al, worker2
or
= ?;
, ? , '
1,2,3,6,7 : ' ?4 ,3
OR ' '
1,2,3,4,6 :.7-
197
1 1 0 0 1 0 1 1
0 0 0 0 0 1 0 1
1 1 0 0 1 1 1 1 OR
5.14
.
AND :
;
;
;
or
= ? ;
cmp al, operation
, .
5.24
.
8 , 1-
.0- , ' 11001000 : ' :
,00110111 .
.
1 ,AL .0
198
x XOR y
5.15
XOR
) XOR-( -
, XOR
:
XOR
XOR 35h:26h-
35h = 00110101
XOR
26h = 00100110
:
13h = 00010011
XOR .
:
mov ax, 0
:
xor ax, ax
XOR
; -
,
.
199
5.16
NOT
- NOT
.
NOT
, .
.
,
not al
AL 01001101 :
0
AL
0 1 0 0 1 1 0 1
1 0 1 1 0 0 1 0 NOT AL
5.17
NOT
5.25
,2- .NEG
200
TEST
CMP , TEST
.AND ,
,AND .
, TEST
:
AND
.5.3
.SF, ZF, PF :
, CF OF- .0 AF
.
5.16
. :
mov al, 00000101b
test al, 0Fh
. TEST AL
1 ?
:TEST
00000101 AND 00001111
.00000101 : .0
0F0h : .1Fh
0 .1
201
5.26
, AX
AX :
mov cx, 0C123h
ax, 08h
or
or
5.27
.
:
1 0 /
7:0010:00-
10:00 0 12:00 -
12:0016:00 -
16:0020:00 -
20:0024:00 -
00:007:00 -
4 )
( .
:
. .
1 .0
202
5.7
,
. :
.
. , ,
, .
: 000011002 : :
0
27 26 25 24 23 22 21 20
0
= 1210
5.18
000011002
:
) 0( , . 0
0
27 26 25 24 23 22 21 20
0
= 2410
5.19
, ) 2 .(10-
,
) (7 .0
20
203
27 26 25 24 23 22 21
1
= 610
5.20
, ) 2 ( )
.(10-,
1011000 : 100 = 1011000 = 10110
1011000 .10110
10110 100- ) (
" "
10110 : 100 = 10110 = 101
,
. , .
,
.1
.
.
, "" ,
, , .,
10110011
00101100
2 11101100
8086 2:
204
./
,1 )/(
./ n , n CL
,CL )/(
. .
.5.7.1
- : 2-
2-
.
.
SHL, SAL
:
(Shift Arithmetic Left) SAL
.
(Shift Left) SHL .
:
,1 SHL
:
2
,CL SHL
2CL
205
SHL 2- ,-
, 0 . ""
. 5.21 .
5.21
:
, 1 SAL
, CL SAL
:
5.22
, 2- .2-
SAL .SHL , ,
? SAL ,SHL-
, .
, ,
, , .
ZF ,SF ,OF , CF
206
:
; al 00101000
; al 01010000
shl al, 1
; al 10100000
sal al, 1
5.11
OF
CF
?
0
?
0
?
0
SF ZF
?
0
; AL = 01010000
shl al, 1
) 0-0 (1-
,
OF.
; AL = 10100000
sal al, 1
) 1-0 (0-
OF
.
1 ;
CF.
; AL = 01000000
sal al, 1
,:
mov al, 28h
mov cl, 3
al, cl
shl
, ,1 OF
.
, :
CF
207
OF 1
ZF ,PF ,SF
AF
,2 N- .2,
AX ,10- :
)AX 10 = AX 8 + AX 2 = AX (23) + AX (21
, :
; AX AX 2
; BX 2AX
; AX 2AX 4 = 8 AX
; AX AX + BX = 8AX + 2AX = 10AX
shl ax, 1
mov bx, ax
shl ax, 2
add ax, bx
MUL . ,
MUL
MUL .
5.29
AX.18-
5.7.2 SHRSAR-
:
,(Shift ArithmeticRight) SAR
.
,(Shift Right) SHR .
208
:
, 1 SHR
, 1 SAR
:
/ 2
, CL SHR
, CL SAR
/ 2CL
.
SHR )
( . :CF
5.23
:
; al 01100101
; al 00011011 :
shr al, cl
SAR )
( . :CF
209
5.24
.
2N-
:
; al 11100101
; al 11111001 :
sar al, cl
:
CF
OF
ZF ,SF
AF
5.30
. .
. :
mov ah, al
shl al, 4
shr ah, 4
al, ah
or
210
5.7.3
, ,
:
CF .
. (Rotate Left) ROL(Rotate Right) ROR-
5.25
5.26
211
:
CP OF-.
, OF )
,1- (.
5.17
10
. response 0 9
1 I .I
.
response
CF- ; 10.
:
CL0Ah
BL 0
AX response
AX
CF 1
BL BL + 1
CL CL 1
CL 0 -
.MODEL SMALL
.STACK 100h
.DATA
response DW 02A3h
.CODE
212
start:
;
MOV AX, @DATA
MOV DS, AX
;
; CF 0Ah
; BL 0
; AX 02A3h
; AX
CF 0 ; next-
ROL AX, 1
again:
JNC next
- ; CF1
; BL BL + 1
INC BL
; until CL 0
next:
; CL CL 1
CL > 0 ;
DEC CL
again
JNZ
;
MOV AX, 4ch
21h
INT
END start
5.30
5.17
.
.
. SHL
) .(ROR
213
. response ,
.
. response ,
.
start:
mov ax, @DATA
mov ds, ax
mov bl, 0
mov ax, x
again: shl ax, 1
jnc next
inc BL
next:
cmp ax, 0
jne again
;
int
END start
5.31
response
:
. 25-
. 2 5
. 3-
. 2 4- 10-
.
214
215
6.1
,
.
.
.
. -
- .
- ) ( ,
.:
W D a 100 ,7 ,0 ,3 ,12
a ,
0 4 6.1' ) : , .(!0
a
6.1'.
4
100 7 0 3 12
6.1'
- )
(
64h
7h
0h
3h
0Ch
0000
0001
0002
0003
0004
0005
0006
6.1'
- )
(
216
- ;
.DUP:
)?( 8 DUP
a DB
- a 8 ) (
0- .7 , :
)a DW 100 DUP (10
100 , ) 10
( 0- .99
i- -
.:
. , ,i=2 :
.21
. , ,i=2 .22 :
6.1
:
. - 100 .0-
. - 25 .
. ' -' ?
.
. SIZE - 10.
. 7- - '
- .
. -
-
- . .6.2
217
0
1
2
3
4
9
10
11
12
13
14
15
16
17
18
19
6.2
-
- :
y 4 : 4 ,
y 4 : 4 ,
218
- .
, ,
.C - :
4
32
0 2
15
1 5
28 19 23
2 34 6
3 22 35 78 12 32
6.3
-
10 11 12 13 14 15 16 17 18 19
5 6 7 8 9
0 1 2 3 4
32
12
78
35
22
23
19
28
15 34
32 5
2 7
6.3
-
- , - ,
. ) ( n m- ,
p , - nmp.
: - 5 4- , , 451 = 20.
, , 542 = 40 .
- , 4 5- , :
)?(45 DUP
a DB
:
) )?( 4 dup (5 DUP
a DB
)?(20 DUP
a DB
:
DB 2, 7, 3, 4, 32
219
5, 9, 0, 1, 15
DB
DB
DB
:
2,7,3,4,32,5,9,0,1,15,34,6,28,19,23,22,35,78,12,32
a DB
20 .
, -
-.
- ,
, . , ] [i,j :
= (i COLUMNS + j) ELEMENT_SIZE -
COLUMNS ;
ELEMENT_SIZE .
, 0 .1
, ,
] ,a[3,2 4 5- , ,
:
,i=3
,j=2
COLUMNS = 5
ELEMENT_SIZE = 1
:
(3 5 + 2) 1 = 3 5 + 2 = 17
6.3' , ] [3,2 17
.
:2-
(3 5 + 2) 2 = 17 2 = 34
220
6.2
. - ,a 5 10- ,
.
. :
]a[3, 9
]a[5,10
]a[0,0
.
) (record , ;
.
.
,
.
. . :
> STRUC <
> ENDS <
, .item :
;
;
.
:item
item STRUC
? DW
itemnum
? DB
quantity
? DW
supplier
item ENDS
221
. :
.DATA
;1111 100 ;
- ; 923
,2222 50 ;
- ; 120
6.3
. book : :
5;
, H : E , A ,
;
3;
. 3 .
. .
6.2
,
.
, . ;
) ,(Addressing modes "" "",
" :" .
:
,:
mov al, 10h
,:
mov al, ah
222
,:
mov al, var1
,
.
.
- ,
. :
mov al, ah
223
/ ,
) /( .
/
) ,
(.
, ) (IP
. , )
1- ( , 8086 IP
, . :
mov al, ah 0005 ,
0007 2 .IP
:
, ,
) ( . ,
.
.
, )( .
6.4
-
? .
, .
,
, , , .
, -
. , ,
/ .
224
. ,
:
. , .
. , .
. . " " , " "
.
:
, , ;
, , ;
.
.
, .
6.3
)(Immediate addressing
, ,:
mov al, 10h
mov cx, 0FFh
, .
, ,
.
, : ,
.
6.4
225
)(Register addressing
, . ,
:
inc ax
:
add al, ah
, ,
, .
, ,
. ,
.
, ,
, , .
6.1
:
a=4
b=3
I 1- 100
aa+2
bb+1
) 100( b ,a ,i-
.
.
:
226
mov al, a
; al = 4, bl = 3
mov bl, b
;
; al = al + 2
add al, 2
; bl = bl + 1
inc bl
; endloop
again:
loop again
;
; a = al
mov a, al
; b = bl
mov b, bl
,
100 ) ( .
,
- .
: 2 AL 1 .BL
- ,
) 1 . -( . LOOP
.
6.5
. -
. : a
;1234h \ AX
:
mov ax, a
]mov ax, ds:[1234h
227
6.4
, .
, , a:
, 1234h ,DS
.AX , ] DS:[offset
,
. ,
.
- , ,
.
, -
: ,
, )
( ,
) (.
6.2
6.1
:
;
;aa+2
again: add a, 2
228
;bb+1
; endloop
inc b
loop again
a : b- , !
, ,6.1
.
add a, 2
inc b
- , :
y ;
y , ;
y , .
, ,
, 3
) (.
6.5
ADD, MOV INC-
.6.1
6.6
:
a DB 20h
b DB 51h
. ,
229
- :
mov al, a
add bl, b
add bl, a
mov b, bl
. b.
6.6
, )
( , .
, ,
-.
, :
.BX, BP, SI, DI ,
]mov al, [bx
: AL .BX
,AL
. ] [ BX
. 6.5 .
6.5
230
, BX .100h , AL
].DS:[0100h
: BX, SI, DI, BP :
]add ax, [cx
.
,
,:
y DI ,SI ,BX ;DS
y BP ) SS (.
6.6.1
, ,
.
:
. .LEA
(Load Effective Address) LEA:
, LEA
: 16.
,
.
:
lea bx, a
BX .a
, :
a ; BX
mov bx, a
a ; BX
lea bx, a
231
. offset .PTR
offset :
mov bx, offset a
offset ,a MOV
BX , .
offset
- ,
. , :
mov [bx], al
, , ] [bx
.
mov [bx], 0FFh
] [bx
0FFh .
, PTR :
byte ptr
word ptr
:
bx ;
bx ;
PTR ; ,
:
b DW 1234
AH :b
;ah = 12
232
6.7
;
.
.MODEL SMALL
.STACK 100h
.DATA
x DD 12345678h
y DD 11A2092Ah
? z DD
? t DD
.CODE
start:
@DATA
mov ax,
ax
mov ds,
word ptr x
mov ax,
word ptr y
add ax,
ax
mov ax,
adc
ax,
mov ax,
21h
int
END start
6.6.2
, ) ( ,
, .
, .
,
.
233
6.3
,
, ) (b .1-
, .
.MODEL SMALL
.STACK 100h
.DATA
a DB 01
b DB 02
.CODE
start:
;
lea
bx, a
inc
je
inc
end_if:
int
end start
LEA a BX
b a
.a
234
6.8
b ,c- :
b DW 129h
c DW 0AFh
:
bx, c
lea
xchg [bx], b
. ?
. , .
6.6.3
) .(pointer
)( .
, 16.
6.4
. : p a
) (a q .
a db 172
b dw 120
p ; a-
p dw a
q ;
? q dw
p ,
, )
( ,
p dw a
235
p
.a a ,0000 p
.
:
. , . :
mov bx, p
; bx = p
. a :.x
; al = 0ACh
mov x, al
;x=a
. q ,p :
mov q, bx
; q = p = 0001
6.9
. :
]mov x, [p
, ?
. q
.p
236
:
SI DI- , .DS
. :
]mov al, [si+4
]mov al, 4[si
, : SI+4
.AL 6.6
. ] DS:[SI+4 disp ;AL
,displacement " :".
6.6
, SI 6.6 ,100h 104h
.AL :
AX ]; DS:[SI+4
CX ;
]; DS:[SI6
mov [di-6], cx
100h ]; DS:[SI192
, SI DI- .
237
6.7.2 -
- .
, :
. ;
. , , :
.0
:
. ,a , ,AL
:
mov si, 3
]mov al, a[si
. , AX ,a ],a[3
,:
mov si, 23
]mov ax, a[si
. ,a ,
, , a[3] a[2] :
:
SI ;
; AL
;
AL ;
mov si, 1
]mov al, a[si
inc si
mov a[si], al
238
6.10
:
q DW 1,2,3,4,5
)?(p DW 5 dup
:
]p[2]q[2
]p[4] q[3
]p[1] q[4
.0-
- ) (.
, SI , )
; (
.
. , ,
) ,(JAVA , .
6.5
,a 5 .
:
sum = 0
i = 0 i = 4
]sum sum + a[i
: ,
SI i . , 1 ,SI
. i -
239
.MODEL SMALL
.STACK 100h
.DATA
5 ;
;
start:
;
mov si, 0
mov al, 0
again:
]; al al + a[si
;si si + 1
;
;
; sum
inc
cmp si, 5
again
jne
mov sum, bl
int
end start
,
; MAX .
,
, .
240
6.11
. 6.5 : LOOP
.MAX
. -
.1 .100-
,
. , :
]add al, [si
SI .
:
, , , SI
.
6.12
, 10 ,
. .
.
6.13
, 20 , :
....,3 ,2 ,1
6.8
,
BX .BP- BX
) (DS BP ).(SS
241
:
+ =
, :
BX 3 ; AX
a BX ; AX
,
.
) ( .
,
- ) ( . , ,
, .
, ,:
. ;
. .
, ,6.1
,AL :
bx, p1
lea
, :
.MODEL SMALL
.STACK 100h
;
242
item STRUC
? itemnum DW
? DB
quantity
? DW
supplier
item ENDS
.DATA
bx, p1
BX ;
; AL AL +
lea
bx, p2
lea
int
end start
6.14
. :
;
;
;
243
.
) .(60-
, 1 ;a .0
6.15
. 6.6 .
. ;
.
, .
- .
6.9 -
:
, , :
][ + + =
.
:
][bx][si
][bx + si
][bx][di
][bx + di
][bp][si
][bp + si
][bp][di
][bp + di
, ,BX
, ,BP
.
,
]mov al, [bx][si
244
6.7
-
-
][BX+SI+disp
]disp[BX] [si
][BX+DI+disp
]disp[BX][DI
][BP+SI+disp
]disp[BP][SI
][BP+DI+disp
]disp[BP][DI
: 6.7 - ,
,BX = 1000h SI = 880h ,4
]mov al, 4[bx][si
DS:1884 ,AL .6.8
6.8
-
245
6.9.2 -
-
,
.
- . ,
-.
6.7
- 5 10- ,
, .... ,6 ,4 ,2 ,0- .
.MODEL SMALL
.STACK 100h
;
ROW EQU 5
COLUMN EQU 10
.DATA
- ; 510
mov al, 0
mov si, 0
again:
BX 0- ROWCOLUMN ;
mov a[si], al
2
add al,
246
inc si
? ;
int
END start
, - ,
.
6.8
- 3 4- .
...,10,20,30 ,
- sum 3 ) -
-(.
: , ,ROW , COLUMN
.
:
a y -;
sum y - .
:
y - a -
BX.SI-
y - .DI
247
:
.MODEL SMALL
.STACK 100h
;
ROW
EQU 3
COLUMN EQU 4
.DATA
- ;
a DB 10,20,30,40,50,60,70,80,90,100,110,120
- ;
; a
; sum
xor bx, bx
xor di, di
: BX 0- COLUMNROW ;
loop_row:
;
; a
si, si
xor
cl, cl
xor
;
SI 0- COLUMN-1 ;
loop_column:
]add cl, a[bx][si
si
inc
248
; sum
mov sum[di], cl
di
inc
int
END start
6.16
...,13 ,10 ,7 ,4 ,1 - .34
6.17
- 4
6- , -.
249
,
7.1
) (routine , .
,
.
;
. ,
.
: ) Procedures (
) (Functions .
, , .
) ;(Call
, , .
) ( .,
. , ,
,
.
) (
.
250
. ,
,
.
) (macro .
7.2
) ( .
: PROC
, ENDP :
> PROC <
RET
> ENDP <
7.1
.
.MODEL SMALL
.STACK 100h
.DATA
? a DB
? x DW
.CODE
;
start:
;
call initData
int
251
; initData
initData PROC
mov ax, @DATA
mov ds, ax
ret
initData ENDP
END start
, ,
. :
initData PROC .initData ENDP-
. ,7.1
, :
call
.initData , :
mov ax, @DATA
mov ds, ax
ret
, ,ret
, :
mov ax, 4c00h
:
. CALL
-
CALL
: ,
. )
,(JMP
252
. ,
.
. (RETurn) RET
RET n
) n(
. : ,
. ,
; ) (.
, .
:
.MODEL SMALL
.STACK 100h
.DATA
? DB
? DW
.CODE
initData PROC
mov ax, @DATA
mov ds, ax
ret
initData ENDP
;
start:
;
call initData
int
END start
253
)
( . ,
; .
)
,(start ) (
, ,RET )
( , .
7.1
terminateProg
.
7.3
7.3.1
, ,
) .(Stack , "
" ) Stack Segment .(SS
. :
.STACK
, 256:
.STACK 100h
. ,
. ,
: ---
).(LIFO Last-In-First-Out
254
7.1 .
. ;
7.1
,
. ) ( , , .
,
--- ,
. , ,
,
. ) ,(Pushing
) .(Popping ,
, - ,SP ,
SP . .SS
7.2 .
SP .
255
, '' .
SP
SP .
7.2
7.3 : .
,
.SP , SP ,
. , 3
, 1 . ,
SP ) .(7.2
7.3
256
, ,
. , , ,
,
. ) ,(Stack overflow
, .
.
, :SP
,SP .
,8086- , SS
. SP
).(SS:SP
7.2
?1MB .
7.3.2
;
.
, , ,
.
, .
8086 16
.- ) 8(.
. PUSH
PUSH
257
: ) SP
2- .(SP , ,
.
.
:
; ax
push ax
; 10
push 10
; var
push var
!!! ;
push al
. , ,
, .
7.2
. :
AX = 3F4Ch
SP = 100h
:
push ax
SP AX .
AX ,
.7.4
258
7.4
,4-1 :
.1 1:SP-
SP = 100h 1 = FFh
.2 AH = 3Fh FFh .
, - SS
.SP
.3 1:SP-
SP = FFh 1 = FEh
.4 AL = 4Ch FEh .
,2 ,
- SS .SP
. POP
POP
: , .
2- ) SP ( .
.
.
259
7.5 .
7.5
pop dx
) :(3F4Ch
.1 ,FEh :DX
DL = 4Ch
.2 1:SP-
SP = FEh + FFh
.3 ,FFh :DX
DH = 3Fh
.4 1:SP-
SP = FFh + 1 = 100h
, ,
.
.
,SP SP
.SS
260
7.3
. . SP
a b- .
.MODEL SMALL
.STACK 100h
.DATA
a DW 1234h
b DW 5678h
.CODE
start:
mov ax, @DATA
mov ds, ax
push a
push b
bx, a
lea
]pop [bx
]pop [bx+2
;
int
END start
. str1 4
.
7.4
DX ,CX ,BX ,AX
AX ,BX CX .DX .
261
7.3.3
,
IP , . IP
) (
) ( . " " )Return
.(address 8086- :
) (Near routine ).(Far routine
" " .
,CS:IP-
, , 8086- ,IP
. ,
, .IP
" " , ,
, : CS
) ( ) IP ,
( ,
IP . ,
CS IP ,
, .
.
7.3
7.1
;
.
: ,
.
262
7.6 . IP
, .
7.6
:
SP .2-
) (IP-
.SS:SP
IP .
263
.7.7
7.7
call initData :
, RET
. :
SS:SP .IP
SP .2-
, , , .7.8
264
7.8
) ret (
7.5
initData ?RET
7.4
.
: .
, ,
.
. ,
265
) ( .
- ;
, . ,
,
.
) (:
) (by value ;
) (by reference .
,
, . "
" " " . ,
, ,
, .
,swap
.
.
. , ) swap(a,b
.
a .b-
:
266
.MODEL SMALL
.STACK 100h
.DATA
a DW 12h
b DW A9h
.CODE
start:
mov ax, @DATA
mov ds, ax
; a
push a
; b
push b
); swap(a,b
call swap
int
swap PROC
) ( ;
swap ENDP
END start
7.9 , )
,(call swap :
7.9
,swap
267
7.9 , SP
) ( ,
. ?
) BP 16 ( ,
. BP ,SP
:
MOV BP, SP
;
, , :
a BP+4
b BP+2
, :swap
swap PROC
; BP = SP
mov bp, sp
; AX = 00A9h
; [BP+2] = 0012h
mov [bp+2], ax
ret 4
swap ENDP
, ,
,
.
RET 4
SP 4- ) (SP
SP .2- SP 4-
, ,0100h
.
268
7.6
. ) a( , bp+2
?bp-2
. ,RET 4 ?
?
. .RET
, ,
a b-
. ""
, "" , ,
a b- .
.
) swapByRef (a,b
. ,
a .b-
:
.MODEL SMALL
.STACK 100h
.DATA
a DW 12h
b DW A9h
.CODE
269
start:
lea
push bx
bx, b
lea
; b
push bx
); swapByRef (a,b
call swapByRef
int
swapByRef PROC
) ( ;
swapByRef ENDP
END start
7.10 , :
7.10
swapByRef
270
,
; BP , SP BP-
. :swapByRef
swapByRef PROC
; BP
mov bp, sp
BX ; b
; ax = 00A9h
SI ; a
; AX = 0012h, a = 00A9h
; b = 0012h
mov [bx], ax
ret 4
swapByRef ENDP
a ,b-
. ,
:
7.1
swapByRef
]DS:[0002
]DS:[0000
00A9
0012
ax
si
bx
00FA 00FA
0002
00A9
0000
00A9
0012
bp
sp
0012
mov bp, sp
mov [bx], ax
ret 4
271
,2 0002 bx
.b 3 ,b ,bx .ax
4 SI ,a ax
.a ,b
a .b- 7.11
.
7.11
swapByRef
7.6
. a ,b- ,6.20 ,
? , .
. swapByRef a, b, c
, .
7.5
, .
" " .
,
.
PUSH POP- .
,
272
) ( . ) BP (SP
. ) ( ],SS:[BP-2
] ,SS:[BP-4 .
7.12
: ] [bp-n
]) [bp+n .(n=0,2,4,6,
7.6
n .1,3,5,7
sumNum n .
n : sum- ;
, .sum
:
sum .
:
n
sum
sumNum
.
273
sumNum
} : { n
} : {1,3,5,7, :
AX CX .-
temp .
:
bp = sp
cx = 0
:temp cx
cx = n
ax = 1
cx0:
temp = temp + ax
ax = ax + 2
cx = cx 1
]ax = [bp2
[bp+2] = ax :sum
, sum
:
.MODEL SMALL
.STACK 100h
.DATA
DW 10
sum DW 0
.CODE
;
start:
mov ax, @DATA
274
mov ds, ax
;
push n
bx, sum
;
;
;
lea
push bx
call sumNum
mov ax, 4c00h
21h
int
sumNum PROC
; bp = sp
; cx = 0
temp ]; [BP2
; cx = n
; ax = 1
mov bp, sp
cx, cx
xor
push cx
]mov cx, [bp+4
mov ax, 1
again:
; temp = temp + ax
;ax= ax+2
; cx>0
add [bp2], ax
ax
inc
ax
inc
loop again
;
; ax = temp
; sum = ax
pop ax
mov [bp+2], ax
4
ret
ENDP sumNum
END start
7.13 sumNum
) .(push cx SP
, BP- .
275
7.13
, ] [bp-2
.
SP- ,
.100h ,
,POP ax ] .[bp+2
2- .sp=bp
,ret 4 SP
.
7.7
n .
1 . ,
, .
,
.
, "" .
276
7.7
, 7.6
ax) cx- ( .
:
start:
mov ax, @DATA
mov ds, ax
ax; cx-
push ax
push cx
;
push n
bx, sum
;
;
lea
push bx
call sumNum
ax; cx-
pop cx
pop ax
;
7.8
:
pop ax
pop cx
277
7.6
;
)
(; .
, ,
. ""
, .
) ( , ,
.
7.8
,
.
, i-
) .(n .sumarr
:
n ,
i ,
sumarr ,
sum ,a
i- . :
n i-
a sumarr
278
:
a
n
i
) sumarr (
sum
sum
,0
i- .i1
- , BX SI- .
} : n ,a , i-
{
} : i- {
BP= SP
n CX
BX a
i1 SI
AX
CX 0-:
] a[SI AX
SI1-
AX sumarr
:
.MODEL SMALL
.STACK 100h
MAX EQU 9
279
.DATA
a DB 9, 0Ah, 4, 5, 6, 8, 1, 4, 12
n DW MAX
i DW 04h
? sumarr DW
.CODE
start:
mov ax, @DATA
mov ds, ax
;
bx, a
lea
, ;
push bx
, ;
push n
push i
, ;
bx, sumarr
lea
push bx
;
;
sum
call
mov ax,
21h
int
sum PROC
mov bp, sp
;
;
; 0
sub cx, si
cx
inc
si
dec
xor
280
7.9
.
7.10
.
.
7.11
;
.
.MODEL SMALL
.STACK 100h
.DATA
.CODE
start:
;
281
int
outproc PROC
jmp endouts
inproc PROC
mov ah, 0Fh
ret
inproc ENDP
endouts:
call inproc
inc ah
mov bh, 0FFh
ret
outproc ENDP
END start
7.7
.
AL .AX
, AX DX- , DX-
.AX-
,
.
282
7.9
) greater(a,b ) (
.
greater PROC
mov bp, sp
ax ;b
a;b-
b a- ;next
ax ;
ja next
]mov ax, [bp+4
next: ret 4
greater ENDP
, AX ,
. ,
,BX :ax
push ax
; ax
) ( ; a
push a
) ( ; b
push b
call greater
mov bx, ax
;ax
pop ax
7.12
.7.9 AX
, ?
283
.MODEL SMALL
.STACK 100h
.DATA
a dw 34
b dw 87
.CODE
start:
;
push a
push b
call greater
mov bx, ax
mov ax, 4c00h
int
;
;
21h
greater PROC
mov bp, sp
push ax
; ax
; ax = b
; if b > a ?
ja next
mov ax, [bp+4]
next:
pop ax
ret
greater ENDP
END start
; ax
284
7.8
.
,
.
;
.
7.10
.
n 1- ,n 0 .1-
! , , ! ,2 , n
!. n
:
3 -
7 7! = 7654321 -
3! = 321
.
,
. n n>0 ) ""
":("factorial
factorial =1
x n- 1:
factorial = factorialx
x=x1
.
! .n -n ;
! n !):(n-1
285
0! = 1
!1! = 10
!2! = 21 = 21
!3! = 321 = 32
!4!= 4321 = 43
!)n! = n (n-1
n<0
, !:4
! 4
!4! = 4 (4-1)! = 43
! 3
!3! = 3 (3-1)! = 32
! 2
!2! = 2 (2-1)! = 21
! 1
!1! = 1 (1-1)! = 10
0!= 1
, 0! ,:
!:1
1! = 10! = 11 = 1
!:2
2! = 21! = 21 = 2
!:3
3! = 32! = 32 = 6
!:4
4! = 43! = 46 = 24
, :
. , :
0! = 1
" ".
. , ,:
!4! = 43
!) (n-1 ! n.
286
7.11
, ab .
,ab a b- , b
,a:
ab = a+a+.
+a
,
, .x0=0
:
43
a=4, b=3 43 = 42 + 4
a=4, b=2 42 = 41 + 4
a=4, b=1 41 = 40 + 4
)- ( a=4, b=0 40 = 0
41 = 0 + 4
42 = 41 + 4 = 8
43 = 42 + 4 = 12
95
a=9, b=5 95 = 94 + 9
a=9, b=4 94 = 93 + 9
a=9, b=3 93 = 92 + 9
a=9, b=2 92 = 91 + 9
a=9, b=1 91 = 90 + 9
)- ( a=9, b=0 90 = 0
91 = 90 + 9 = 9
92 = 91 + 9 = 18
93 = 92 + 9 = 27
94 = 93 + 9 = 36
93 = 94 + 9 = 45
287
, b=0 ab = 0
, b>1
)ab = a + a(b1
, :
. , : "" "" ,
, 0!=1
. , "" ,
!)n! = n(n1
" ." , "" , ,
, .
) AL .(AX
7.12
example
AL AL .0-
:example
AL > 0
AL = AL 1
CALL example :
AL = 0
:
AL=AL1 - ,
, .AL=0
288
:example
.MODEL SMALL
.STACK 100h
.DATA
.CODE
start:
;
;al
;
;
mov al, 2
call example
mov ax, 4c00h
21h
int
example PROC
cmp al, 0
al = 0 ;
; al= al 1
;
je done
al
dec
call example
done: ret
ENDP example
END start
example
, .
, example .000Fh-0018h
: example
, IP .0000
, :
CALL example
, :
289
SP . 2- 00FE
. , ,000A SP
. IP .000F
:
7.14
example
:
IP .000F
AL ,0 ,2
. je 0018 ,
- ,
290
.0013 1 AL
call 000F .0015
:
. SP 2- 00FC
. , ,0018 SP
. IP .000F
. 7.15
:
7.15
291
:
AL ,0 : AL
1- . 7.16
.
7.16
: ) (
AL 0 . ,
.0018
RET :
292
. 0018 SP
IP
. SP 2- 00FC
7.17 :
7.17
: ) (
RET :
. 0018 SP
IP
. SP 2- 00FE
293
7.18
:
:
. 000A SP
IP
. SP 2- 0100
,
.mov ax, 4C00h
294
7.19
, SP .0100h
example AL ?0
7.13
. ,,
! , n .
,
.
! ,n :
,n ! ;n , n!-
,(0!=1) 0 :
295
):factorial(n
n = 1
factorial = 1
)factorial = n factorial(n1
BX n AX . n>0
.n1 ,
n .
!:n
; bx=2
); factorial(2
mov bx, 2
call factorial
finish:
int
factorial PROC
;n=1
bx;
cmp bx, 0
BX >1 ;
jg call_fact
; AX =1
mov ax, 1
jmp done
; n>11
call_fact:
; bx
1 ; bx
); facotiral(bx1
push bx
bx
dec
call factorial
: ; bx
pop bx
bx ax- ; ax
mul bx
ret
done:
ENDP factorial
END start
296
, BX 2
001F .SP=00FEh
.
: )factorial(2
7.20
.
7.20
factorial(2)-
297
: )factorial(1
IP . BX
0- .0016 ,
BX , BX 1- factorial
).factorial(1
7.21 :
push bx
dec bx
); call factorial(1
7.21
factorial(1)-
call factorial
298
: )factorial(0
, IP
. BX 0- .0016
, BX , BX 1-
factorial ).factorial(0
7.22 .
7.22
factorial(0)-
299
: )factorial(0
, BX ,0
0010 : 1 AX
,001E .RET 7.23.
.
7.23
)factorial(0
300
: )factorial(1
001B
bx=1- AXBX = 11 = 1
RET .001E 7.24
.factorial(1)-
7.24
)factorial(1
301
:
001B
bx=2- AXBX = 12 = 2
.RET
. 7.25
7.25
302
7.9
) (macro
) ( ;
. - , ,
. ,
. , ,
.
,
) IP
( .
7.9.1
,
, :
][paramet1, paramet2,
MACRO
ENDM
. ,
. ,
. .
.
7.14
, :DS
MACRO setDS
mov ax, @DATA
mov ds, ax
ENDM
303
MACRO ENDM- .
, - .
. :
, ; -
, , .
, setDS ,DS endprogram ,
:
.MODEL SMALL
;
setDS MACRO
MOV AX, @DATA
MOV DS, AX
ENDM
endprogram MACRO
INT
ENDM
.STACK 100h
.DATA
.CODE
start:
;
setDS
;
endprogram
END start
,LST ,
.
304
7.9.2
; ,
,
)" "( )"
"( . ,
;
.
, writeChar .
writeChar MACRO char
mov dl, char
mov ah, 2
int 21h
ENDM
:
'writeChar 'A
-
- , .TAB
; ,
.
7.15
; a, b , c .a>b>c-
swap :
a<b a b-
b<c b c-
a<b a b-
305
:
swap first .second-
- . LOCAL in_order
. ,
. LOCAL
,
.
.MODEL SMALL
LOCAL in_order
mov al, first
cmp al, second
jnb in_order
a DB 29
b DB 15
c DB 23
.CODE
start:
;
swap a, b
swap b, c
306
swap a, b
;
int
END start
, .lst
.debugger-
7.15
.1 ;
.2 3;
.3 pushreg popreg
.
7.9.3
, .
, .
. ,
IP ;
. ,
-
.
"" ,
, ,
. , , ,
, ,
- .
307
:
,
. ,
, ,
: ,
.
, :
swap a, b
,a , b
first ] [0000
second ] .[0001 , 'writeChar 'A
, char ' .'A
, . ,
writeChar add
, char add
:
MOV DL, add
,
, .
, swap :
swap MACRO tempreg, first, second
;
LOCAL in_order
mov tempreg, first
cmp tempreg, second
jnb in_order
308
7.9.4
, , .
.
, .
- :(REPeaT) REPT
REPT
ENDM
,
. :
REPT 3
inc ax
ENDM
309
- :
inc ax
inc ax
inc ax
,
. ,
.
.
.
7.16
'=' . :
.DATA
x DB 0
y DB 0FFh
'asciicode = 'A
rept 10
DB asciicode
asciicode = asciicode + 1
ENDM
,
.
310
311
. , ) ASCII .(2
: , ,
, .- ,
,ASCII ) ,c ,b ,a :'( "
,
0 9-- ASCII 0
ASCII .
8086 ,
)
( .
.
.
8.1
.,
, 5,
":"abcde
"str1 DB "abcde
'str2 DB 'abcde
"str3 DB "a", "b", "c", "d", "e
312
. 5
, ASCII . ,
:
str4 DW a, b, c, d, e
ASCII
.0 8.1 5
, 5 .
8.1
.
.
.,
string1 DB 'abcde$
strLen EQU $ string1
string , strLen
$ .string1
,
.:
)?(string2 DB 80 DUP
8.1
:
. 10
. ) lenStr 100(
. "!"Hello
313
8.2
.
:
. SI ) (
;DS
. DI ) (
.ES
. DF , ,
.
. , .
,
, DS , ES- :
mov ax, @DATA
mov ds, ax
mov es, ax
-
. .
8.3
MOVS
MOVS , ,SI
.DI :
z
:
MOVSB
314
z
:
MOVSW
:
]ES:[DI] DS:[SI
, ) ( , ,SI
.DI
.
, , SIDI-
.
, ,
) Direction Flag
.(8086 :MOVS
DS[SI]-.ES[DI]-
) DF=0 (
SI = SI+1
DI = DI+1
) (
SI = SI1
DI = DI1
-
1 ,0 CLD.STD-
315
STD ; 1 DF
, .
, .
MOVS
. ,
SI :DI
SI ; a
DI ; b
"-" ;
;
cld
movsb
) offset (6 SI
a DI .b
.DS
8.2
SI DI-
.
8.3
,1 SI DI-
MOVSB ?MOVSW
316
8.4
, ,REP
,
REP MOVSB
) REP (REPeat n
. REP -
CX MOVS .
:REP MOVSB
CX 0
MOVSB
CX = CX 1
8.1
a .b
.7
.MODELL SMALL
.STACK 100h
MAX EQU 7
.DATA
"a DB "abcdefg
)?(b DB MAX dup
.CODE
start:
;
DI ; b
317
;
CX ;
;
rep
int
END start
:
. DS ES- ,
SI DI- .DS
. SI.DI-
. ,- .CLD
, STD ,CLD .
. ,MOVSB CX
) .(a
.
REP MOVSB
, , CX;
CX .
8.4
. 8.1 10
.
. b- a ?MAX-
a ?MAX-
.
, ?
. 10 .
318
STOSW
.ES:[DI] AX :
.
:STOSB
MOV byte ptr ES:[DI], AL
0=DF
DI = DI + 1
DI = DI 1
8.6
STOSW
8.2
10- .c
. model small
.STACK 100h
.data
)?(chr DB 10 dup
.code
319
start:
; DI
mov cx, 10
cld
'; al 'c
rep stosb
21h
int
END start
.AX
LODSW
:
320
:LODSB
]MOV AL , ES:[SI
0=DF
SI = SI + 1
SI = SI 1
- REP .LODS
REP ) LODSB (LODSW
: ,AL
AL , . , AL
. ,
LODS , ,STOS- ,
.
8.3
a b
) '.(' -
.model small
.stack 100h
;
MAX EQU 7
.data
;
;
"a DB "12 45 7
)?(b DB MAX dup
.code
start:
;
mov ax, @DATA
mov ds, ax
mov es, ax
321
: , ;
again:
lodsb
' ' =; al
><al
cont
jne
; b[di] = al
cont: stosb
; cx cx + 1
inc cx
cmp cx, MAX
? ;
jb again
; again
21h
int
end start
8.7
.CMPS ,
ASCII . :
:
; CMPSB
; CMPSW
: SI DI ?
, ]? ES:[DI] = DS:[ SI
322
SI = SI 1
DI = DI 1
-
,
.
8.4
a ,b
) 0 ( ) 1 (
:AX
mov si, offset a
mov di, offset b
mov ax, 1
cld
? ;
; equal
cmpsb
jz equal
;
mov ax, 0
jmp endcmp
equal:
323
, :
z
i i .
,CMPSB , .REP
REP :
i- n:
] DS:[SI] = ES:[DI
,
. CMPS REPeat while) REPZ
(Zero ,(REPeat while Equal) REPE : CMPSB
, .
REPE CMPSB
REPZ CMPSB
:REPZ
CX 0 ZF 0:
]CMP ES:[DI],DS:[ SI
DF=0
SI = SI + 1
DI = DI + 1
SI = SI 1
DI = DI 1
-
324
)REPNZ (REPeat while Not Zero
)REPENE (REPeat while Not Equal
:
CX 0 ZF 1
,
, .
8.5
, .
FFh ,al .0-
.MODEL SMALL
.STACK 100h
.DATA
" a DB "1234567
" b DB "1234567
.CODE
start:
;
mov cx, 7
cld
325
repz cmpsb
jne else_if
`mov al, 0FFh
jmp end_if
else_if:
mov al, 0
end_if:
mov ax, 4c00h
21h
int
end start
8.7
.
. : abcdef abedcx -
2 ) .(4 ,2
8.8
326
:SCASB
AL, ES:[DI] CMP
DF=0
DI := DI + 1
DI := DI 1
-
8.6
mov cx, 7
cld
repnz scasb
jne else_if
mov al, 0FFH
jmp end_if
else_if: mov al, 0
end_if:
8.8
.
8.9
. .
- ) ( .
. , .
: .
327
BX ;
AL .
XLAT
BX + AL ) AL ( .
256 )
.(AL
8.7
0- :9
.DATA
table DB 4695031872
3 :
mov bx, offset table
; AL = 3
mov al, 3
; AL = 5
xlat
328
8.10
)( :
5 ; : ""adrtx
"."fiwyc
329
-
9.1 -
.
) (debugger
. ,
, ,
. ,PC , , ,,
; , , , ,,
. /
,
/ ,
.
.
.
: -
. , ,
.
9.2
DOS
DOS
- DOS . ,Disk Operating System
, 80-
.90-
, "".
330
, .
:
.1 AH ;
.2 ;
.3 .INT 21h
mov al, 0
mov ah, 4ch
21h
int
331
9.2.2 ' :2
, .
, DL ASCII .
:
; DL
;
;
mov dl,
mov ah, 2
int 21h
9.1
; .
n n .
. : ","AFCTDR
.AaFfCcTtDdRr :
printChar
.
,
.20h ASCII
.MODEL SMALL
.STACK 100h
.DATA
; 6
"str1 db 6,"AFCTDR
.CODE
start:
332
;0-
xor cx, cx
SI ;
; AL = n, SI = SI+1
; CX = n
lodsb
mov cl, al
cld
doloop:
lodsb
];AL = str[SI
;
) ( ;
;
;
;
;
;
cbw
push ax
call printChar
add ax, 20h
push ax
call printChar
pop ax
loop doloop
int
printChar PROC
mov bp, sp
;
; 2
; DOS
int
ret 2
printChar ENDP
END start
333
9.2.3 ' :9
' 9
.
, .DX ; ' '$
. : ' ,'GOOD-BYE
' ;'GOOD-BYE$ ' '$ .
,
:str
DX ;
;
;
mov ah, 9
21h
int
9.1
.
" "$ ,printString
.
9.2.4 ' :1
. ,
AL ASCII .
. '' ) .(echo ctrl+break
.DOS
:
;
;
AL , ASCII .
mov ah, 1
21h
int
334
9.2
. getChar
printChar .
9.1
)(
.1 +
- .
.
- .
n+1
) + (0Dh
- .
335
, DX
. ,
10 .
.
10 ; inpBuffer
DB 10
DB 0
)11 dup(0
DB
mov ah, 1
21h
int
9.2
, .
:
printString ;
getString
printChar .
:
. outMessage1 ;outMessage2-
.
. ;inpBuffer .
0Dh, 0Ah .
, , ,
. " "$ .
336
.MODEL SMALL
.STACK 100h
.DATA
outMessage1 DB " input string: ", 0Dh, 0Ah, "$"
DB 10
DB 0
DB 11 dup(0)
.CODE
start:
;
mov ax, @DATA
mov ds, ax
;
mov bx, offset outMessage1
push bx
call
printString
pop
bx
;
getString
pop bx
;
mov bx, offset outMessage2
push bx
call
printString
pop bx
;
xor cx, cx
mov si, offset inpBuffer+1
337
lodsb
mov cl, al
cld
;
;
do_loop:
lodsb
cbw
push ax
;
call printChar
pop ax
loop do_loop
;
mov ax, 4c00h
21h
int
;
printChar PROC
mov bp, sp
;
int
ret 2
printChar ENDP
;
getString PROC
mov bp, sp
;
int
ret 2
getString ENDP
338
;
printString PROC
mov bp, sp
;
int
ret 2
printString ENDP
END start
9.3
) 0 ,(9-
.
9.3 8086
- , , ,
- . - MS-DOS .
,DOS
; ,
. ).(Interrupt
, ) (CPU-
) ( . ,
, ) .interrupt service routine (ISR
, ,
. .
- , .
(Universal Serial Bus) USB
339
.
- . :
.int
DOS .
) (exception ,
,0- .
) ,(trap . , 8086
(Trap Flag) TF ,1 1
. , , ,
- ) .(single step -
TF ,1 .1 .
8086 , . ,
8086- ) (Type . 8086- 256-
; 0.255-
, ,
" " ) (IVTInterrupt Vector Table .
,
. ) - (
) ( ,
, .
, ,
- ,int :
,
, - .
, CS .IP 8086-
, .
340
: 8086- CS
, ,IP ?
CS
. .
,
. : CS .
,
)
( . ,
, :
) ZF( ,- , . 9.1
,8086-
.
9.1
) 8086- (
, 8086-
, ,
.
341
342
9.2
. ;8086- .
9.3
.00BAh ,
,0622h - 0623h .5 :
SP = 004Fh
, CF.IF-
.5
343
9.2
5
00014h
78h
00015h
02h
00016h
D3h
00017h
C6h
9.3 .
,
,CS:IP = 00BAh:0624h ,0201h .SP = 004Fh- 8086-
, , CS IP
6-
) , ( . , SP
, .0049h 8086- 5
. 54 = 00014h
.54+3 = 00017h- 8086- IP
, CS .
,
.C6D3h:0278h
344
9.3
5
345
9.4 ,IF . ,
) 9.3 (9.4 .
9.4
346
9.4
) (
,C6D3h:03B2h 8086- .
.
9.4 .
) CS:IP = C6D3h:03B3h : (,
,SP = 0049h . , CS
IP ,
,CS:IP = 00BAh:0624h : . ,
, CF .IF- SP 6-
, .SP = 004Fh
9.4
, DOS
. DOS 35h
DOS .35h
AL ES
) ( BX - .AL ES
BX .
.ES:DX
, ES:BX :5 .
mov ah, 35h
mov al, 5
21h
int
347
. DOS 25h
.25h
:
AL
DS
DX
, 5 myProc
) (.
;
;
;
DS ;
DS ;
;
int
offset
. mov dx, offset myProc offset
myProc .dx
seg push seg myProc
.myProc
.
FAR .IRET
.
:
name PROC FAR
iret
name ENDP
348
9.5
, 0 .
. .
:
mov cl, 0
div cl
, .divide by zero :
0 . ,
.
. ,6
.
,
.
,
.
: 0 . myProc ,
. ,0 .
.
.MODEL SMALL
.STACK 100h
.DATA
'outMessage db 0Dh, 0Ah, ' divide by zero ', 0Dh, 0Ah, '$
.CODE
start:
mov ax, @DATA
mov ds, ax
0 ; ES:BX
mov ah, 35h
mov al, 0
21h
int
349
; myProc 0
mov ah, 25h
mov al, 0
mov dx, offset myProc
push seg myProc
pop ds
int
21h
; myProc 0-
mov cl, 0
div
cl
; 0 .
21h
exit :
mov ax, 4c00h
int
21h
;
myProc PROC
mov bp, sp
add word ptr [ bp], 2
lea
mov ah , 9
int
21h
iret
myProc ENDP
END start
350
9.5 INOUT-
, / ,
. ,4
8086 1M / 8086
.64K / ) .(port
/ ,
100h 100h .
, .
M / IO / .
,
/ . ,
Read / Write )
( ) (.
. mov ax, var .add var, 18
IN OUT-.
. IN
:
, IN
) IN ,(INput
- . :
AL AX .
.AL
. OUT
:
, OUT
351
AL AX .
:
.
0 255- )(.
. ,FFh-
DX . :
,0379h
:DX
MOV DX, 379h
IN AL, DX
: DX
, .AL
9.6
.0378h
037h .
mov dx, 0378h
in al, dx
neg al
out 037h, al
352
353
10.1
,
: , , ,
. ,
,
, , ,
.
, ,
- .-
, .
"- "
, .
1965 ) (
)" .("Moore's Law , ,
' , , .
, ' 18.
10.1 , ,
.
354
x 86
140000
)(
120000
100000
80000
60000
40000
20000
0
1979
1982
1985
1989
1993
1997
1999
2000
2004
10.1
) (
) (
, - .
, ,
.
, .
.
86 .
.10.1
; ) 1 = (.
: 0.003mm
"" 29,000; 0.0018mm 25,000,000
355
. , 200 )
4.77MHz 1000MHz (.
1) 1MHz ( .
" " . ,
.
,
ADD . ADD 8086
3 17- ) ( .
1MHz 58,000 330,000- ,
1000MHz 58,000,000 330,000,000- ADD.
, .
/ ,
. , ) ,(DQ
16 , , P5
)( 32 , .
. , 4 8086 20
.1Mbyte P5
64 ) 264= 64GB .(10.1
, ,
, .
,
. ,
- " "
).(pipelining
356
10.1
80x86
8088
)( )(
MHz
5
16
29,000
3 1979
80286
1982
1.5
134,000
16
80386
1985
1.5
275,000
32
16
80486
1989
1,200,000
32
25
Pentium
1993
0.8
3,100,000
32
60
Pentium II
1997
0.35
7,500,000
32
233
Pentium III
1999
0.25
9,500,000
32
450
Pentium 4
2000
0.18
42,000,000
32
1500
"Pentium 4 "Prescott
2004
0.09
125,000,000
32
3600
10.2
,
.
, .
16- 32- ,
64 . ,
: F ,G
)( .
.
,
.(Extended) E ;10.2
, ) EIP ( .
32 ,
, 32 . 32
, 8 16.
357
10.2
:
eax
neg
L3
je
eax
neg
L1:
sub
L2
jg
L1
jne
eax, edx
add
L2:
L3:
, 32.
. ,
, , LFS, LGS
, CDQ ) 32(
) 64( . ,(Convert Doubleword to Quadword) CDQ
358
80386 , CBW ,5
EAX .EDX
); eax 5 (FFFFFFFBh
cdq
, CMPXCHG
INS OUTS- - . ,
32.
).(debuging
359
10.3
, :
. ,
,
. 10.4
.
.
10.4
360
10.4 ,
) ( ,
. 28 .-
. ,
, , ,
, .
, )
( , ,
. , ,
.
, 8086 ,
: ) (BIU Bus Interface Unit
) (EU Execution Unit ,,
.
10.5
8086
,8086 .
;
, ,
; ,- ,
- .
,
.
, .
361
, ,
386 )( 16 . , ,
, , ,
.
. ,
.
:
: .
,
; )
.(IP , .
: )
(.
.
: .
) (MUL
,
.
:
; : ,
?
. :
; op1-
'; op2-
; op3-
; op4-
362
, 2- .
10.6 ,
.
10.6
, 14
.
, ,
. , ,
, ,
: , : ,-
,
, ..
-
, . , ,80486
5 4 20 .
. , ,
, )
( .
.
363
"
" - :
F
) (FETCH ;
(DECODE) D ;
(EXECUTE) E ;
W
) (WRITE .
, ,
.
10.7
4
, :
10.8
4
10.8 7 .
, . ; ,
364
: ;
. ,
. "" ) .(hazard
,
.
:
Structural Hazards , :
, ,
.
Data Hazards
, , .
Control Hazards ,
.
)(hazard
""
. .
. Structural Hazards-
) (to stall .,
Structural Hazards :
10.9
structural hazard
365
10.9
structural hazard
. :
,
) ,(ALU ,
.
.
. Data Hazard-
,
; .Data Hazards
:
add ax, bx
sub cx, ax
inc dx
366
,AX
, ) AX ( .
,10.10 ,
D SUB W ADD
; .AX-
,
.
10.10
data hazard
D ,
AX.
10.10
data hazard
, ,
. , .
, 2- .
367
,
- ) WB .(AX ,
, D2 . AX
) .(data forwarding 10.11
.
10.11
)(data forwarding
, , E
, W
) .(AX
10.12
) ( ,
, .
.data hazard
368
,
, .
:
add ax, bx
sub cx, ax
inc dx
, INC DX ,
SUB CX, AX .ADD AX, BX
, :
add ax, bx
inc dx
sub cx, ax
,NOP , ,
, .
10.13
NOP data hazard
) NOP (,
stall .
369
. Control Hazard-
Control
.Hazard
.
,
,
.IP ,
. : , -
, , .
- .
,10.14 , ,JMP
) next
.(MOV
10.14
,
.D ) ADD DX, AX
.(F ),(E
ADD ,D INC ) .(F
,
,MOV .next ,
370
, ,IP
,
.MOV ,
""
, .
.
, .
10.14
,
.JMP 3 .
,
CMP .Data hazard
:
10.15
371
,10.15 JNZ
CX .DX--
,
. , , 10.15
. JNZ 2 ,
.-
,ADD JNZ
. 5- . 3
, , 6 , 5 , 11
.2
10.15
,
.control hazard
) .(branch prediction ,
, "" .
.
, , ,
, .
) ,(W
. , :
mov cx, 100
mov ax, 0
next: add ax, cx
dec cx
jnz next
inc bx
372
CX ,ZF
) NEXT ( , , ,
.next
,
.JNZ , 99 ) ,(100
.
10.16 .CX = 2-
JNZ
. E ,JNZ , ,
,
; .INC BX
10.16
, , ,
. 100
, 99 100-
.
373
SUPERSCALAR
) (Pipelines )
( .- .Superscalar I
) U Pipeline (V Pipeline- .10.17 , I
. III .
Clock 1 Clock 2 Clock 3 Clock 4 Clock 5 Clock 6 Clock 7 Clock 8
I7
I5
I3
I1
PF u
I8
I6
I4
I2
PF v
I7
I5
I3
I1
D1 u
I8
I6
I4
I2
D1 u
I7
I5
I3
I1
D2 u
I8
I6
I4
I2
D2 v
I7
I5
I3
I1
EX u
I8
I6
I4
I2
EX v
I7
I5
I3
I1
WB u
I8
I6
I4
I2
WB v
10.17
, ,
- , .2
v-pipeline , ,u-pipeline-
, .
.486 ,
U , V .
,486
.
374
10.17 8 I1-I8 .
, 10.17 ,(PreFetch) PF
, ) (
. ,
. ,
.
, .
, 10.17 ,D1- .
- . -
:
.
, 10.17 ,D2-
.
) ( , .
, 10.17 ,WB- .
, .
,
- .
375
10.4
10.4.1
86 ,
, . :
) (;
- , )
( ) 80386
.(4GB-
"-" )(Virtual real mode
8086 : ""
"" 8086
.1MB
) 40385- (4GB- "".
10.2 ,
.
376
10.2
)(
CS
IP
SS
SP, BP
DS
,BX, DI, SI
8 16
ES
DI
CS
EIP
SS
ESP , EBP
DS
ES
EDI
FS
GS
MODE
REAL MODE
) 16(
PROTECTED
MODE
) 32(
10.4.2
:
-
.
, ,ALU-
, , - ) 1 109(.
) ( .
- ,
, .
, 100- ) 100
( .
, , ,
.
377
: . ; .
- .
. ,
, .
. :
, , .80GB
, .
, .
, ) 1 ( .
10000 .
10.1
, ,80GB
64.
,
, , , ,
;
.
) (cache memory , )(virtual memory
.
378
10.18
,
) ( .
, ,
, .
, - .
, .
, , ,
.
,
.
) ;(locality of reference ,
.
;
) .(Hit ratio
) ( , .
) 8KB (80386 .70-80%
32KB- .85-90%-
,32KB- ,
"" , ,
. - ,
, .DOS
379
:
.
: 1
) (; 2 ) (.
.
1 . ,
,.
,
) .(control hazard
.
, ,
, .
)structural
(hazard .
: ,
) (F ) ;(D
.
, :
, ;
.
,F D
.
10.4.4 )(
,
. , ) ,
( ) 8086 .(64KB-
380
,
. ,
.
Descriptor Tables . )(
8 : ) 32( ,
) 20( , .
,
.
, .
, . 10.19
;
.Selector
10.19
)(selector
) (:
13 . Index- .
. ) TI- (Table Indicator
) (.
2 . ) RPL- (Request Privilege Level
.
) .(10.20
. 13 ,
;
. .
381
10.20
, GDT
. , LDT
.
,
. GDT LDT- , TI
Selector- , ' ,'1
) (LDT ' '0 ).(GDT
382
,
. 4
,
.
,
. ,
,
, .
"" ) (Windows
.
, :
z
GDTR ,GDT
;GDT
LDTR LDT .
- , - ,
. , ,
) ( , ,
. ,
. -
. - ,,
,
.
.
;
, .
383
- ) ,(Dual-Core processors
, ,
384
10.21
, .
220 ) (1Mbyte .
. , 3-.
12 bit
10 bit
10 bit
210 .1 Kbyte
.
.
, , ,
. , ) (
.
,
,
. ,
, ,
.