Upload
sexykockar
View
220
Download
8
Embed Size (px)
DESCRIPTION
Ръководство за начинаещи за програмиране за Асемблер
Citation preview
2011
: . -
: . -
. . -
, 2008 , . , 2008 ISBN 978-954-9775-37-2
2
........................................................................... 9 ........................................................................ 10 ............................................................................................... 11 1 ............................................................................ 15 2 8086 ................................................... 33 3 8086 ........ 44 4 MS-DOS ...................................................................................... 63 5 .................................. 78 6 MS-DOS .......................................... 110 7 ........................................................................................ 135 8 ................................................ 150 ................................................................................... 168
3 4
........................................................................... 9 ........................................................................ 10 ............................................................................................... 11 1 ............................................................................ 15
1.1 .............................................................. 15 1.2 ....................................................... 16 1.3 - - a a ............................................................................ 16 1.4 .......................................................... 17
1.4.1 ................................................... 18 1.5 ............................................ 21 1.6 ....................................................... 22 1.7 ................................. 22 1.8 ...................................... 23 1.9 ................................................................. 24 1.10 ASCII/IBM .................................................. 25
1.10.1 ASCII .................................................. 25 1.10.2 IBM .................................................... 27
1.11 ..................... 27 1.11.1 ............................... 27 1.11.2 ..................... 28 1.11.3 ....................................... 30
1.12 ................................................................................. 31 2 8086 ................................................... 33
2.1 ........................................................................ 33 2.1.1 .............................................................. 34 2.1.2 ............................................................................. 35 2.1.3 / ..................................................................... 36
2.2 ................................................................ 36 2.2.1 ............................................................ 37 2.2.2 ........................... 38 2.2.3 ................................................. 38 2.2.4 ...................................................................... 38
2.3 8086 ....................................................... 40 2.3.1 ........................................................................ 41 2.3.2 ............................... 42 2.3.3 ..................................... 42 2.3.4 ............................................... 42 2.3.5 ............................................. 43
2.6 ................................................................................... 43 3 8086 ........ 44
3.1 8086 ................................................... 44 3.1.1 ....................................................... 44 3.1.2 .................................... 44 3.1.3 ...................................................... 46
3.2 80286 ................................................. 47 3.3 80386 ................................................. 48 3.4 80486/Pentium ..................................... 49 3.5 . ....................................... 49 3.6 ...................................................................................... 51 3.7 ROM-BIOS DOS .................................................................. 51
3.7.1 ROM-BIOS ...................................................................... 51 3.7.2 DOS ............................................................... 52 3.7.3 ......................................................... 52
3.8 .......................................................................... 52 3.9 ................................................................. 56
3.9.1 ................................................... 56 3.9.2 ............................................ 56 3.9.3 .......................................................... 56 3.9.4 ........................................................ 58
3.10 ............................................................................... 61 4 MS-DOS ...................................................................................... 63
4.1 ................................................................ 63 4.2 ................................................................. 66
4.2.1 ...................................................... 66 4.2.2 ......................................................................... 67 4.2.3 ............................................................................ 67 4.2.4 ..................................................... 67
4.3 MS-DOS ..................................... 68 4.3.1 TREE .............................................................................. 68 4.3.2 DIR ................................................................................ 69 4.3.3 TYPE .............................................................................. 70 4.3.4 MORE ............................................................................. 70 4.3.5 MKDIR (MD) ................................................................... 71 4.3.6 RMDIR (RD) .................................................................... 72 4.3.7 CHDIR (CD) .................................................................... 72 4.3.8 PATH .............................................................................. 73 4.3.9 COPY ............................................................................. 75 4.3.10 RENAME (REN) .............................................................. 75 4.3.11 DEL (ERASE) ................................................................. 76
4.4 ................................................................................ 77 5 .................................. 78
5.1 ................................... 78 5.1.1 ............................................................ 78
5 6
5.1.2 ...................................................................... 78 5.1.3 ...................................................................... 80 5.1.4 .......................................................... 81
5.2 ...................................... 81 5.2.1 ............................................ 82 5.2.2 ...................................................................... 89 5.2.3 ........................................................... 90 5.2.4 EXE COM ...................................................... 91 5.2.5 , , ............ 97
5.3 .......................................... 98 5.3.1 ............................................... 98 5.3.2 .................. 99 5.3.3 ...................................... 99 5.3.4 .................................... 104 5.3.5 ..................................... 106 5.3.6 ................................. 106
6 MS-DOS .......................................... 110 6.1 ........................................... 110
6.1.1 DOS ........................................................... 110 6.1.2 BIOS .......................................................... 112 6.1.3 ...................................... 115
6.2 ............................................................ 118 6.2.1 DOS ........................................................... 118 6.2.2 BIOS .......................................................... 123
6.3 ................................................ 131 6.3.1 ........... 131 6.3.2 ............................................................................ 132
6.4 ................................................................................. 134 7 ........................................................................................ 135
7.1 ....................................................... 135 7.2 .............................................. 145
8 ................................................ 150 : ................................................................................ 150 ADD ......................................................................................... 150 ......................................................................................... 151 DEC ......................................................................................... 152 DIV .......................................................................................... 152 INC .......................................................................................... 153 INT .......................................................................................... 153 INTO ........................................................................................ 153 IRET ........................................................................................ 154 JMP .......................................................................................... 154 Jxx ........................................................................................... 155
7
LODS/LODSB/LODSW/LODSD ...................................................159 LOOP/LOOPxx ...........................................................................160 MOV .........................................................................................161 POP ..........................................................................................162 POPA/POPAD ............................................................................163 POPF/POPFD .............................................................................163 PUSH ........................................................................................164 PUSHA/PUSHAD .......................................................................164 PUSHF/PUSHFD ........................................................................165 REP/REPE/REPZ/REPNE/REPNZ .................................................165 RET ..........................................................................................166 STOS/STOSB/STOSW/STOSD .....................................................167 XOR .........................................................................................167
....................................................................................168
8
.1.1 ................................................. 18 .1.2 ................................................................. 19 .1.3 ................................................. 19 .1.4 ................................................................. 19 .1.5 ................................................................ 19 .1.6 ...................................... 19 .1.7 , .............................. 20 .1.8 ............................................. 23 .1.9 8 92 ............................ 24 .1.10 8 -92 .......................... 24 .1.11 8 92 -92 .................................... 24 .1.12 8 ......................................... 25 .1.13 ASCII .. 26 .2.1 ............................................. 33 .2.2 , ..................... 36 .2.3 ................................................... 39 .2.4 8086 ....................... 41 .3.1 8086 .................................................. 45 .3.2 .............................................................. 46 .3.3 80386 .............................................................. 48 .3.4 ( + ) ....... 49 .3.5 20 ............................ 50 .4.1 Run ....................................................................... 63 .4.2 Run, cmd .......... 64 .4.3 cmd .................................... 64 .4.4 Run, command .. 65 .4.5 command ............................ 65 .4.6 tasm ............................. 73 .4.7 PATH ................ 74 .4.8 PATH .................... 74 .5.1 Turbo Debugger .............. 107 .5.2 Hello World Turbo Debugger ................ 108 .6.1 debug ............................................. 133 .8.1 ...................................... 161
.1.1 , , .... 15 .1.2 4310 ........................................... 17 .1.3 15 .............. 18 .1.4 .... 20 .1.5 ......................................................................................... 21 .1.6 ........................................................................................ 22 .1.7 .......................................................... 22 .1.8 NOT ................................................................ 23 .1.9 ASCII ................................................ 26 .1.10 ..... 28 .2.1 80x86 . 34 .2.2 80x86 .. 35 .3.1 .................................................. 52 .3.2 .................................... 53 .3.3 BIOS ...................................................... 53 .3.4 21h DOS ....... 53 .3.5 ptr ...................................................... 59 .5.1 ......................................................... 85 .5.2 .............................. 86 .5.3 ................................ 87 .5.4 , model ........................................ 88 .5.5 ..................................................... 98 .6.1 10h BIOS .....112 .6.2 .....................................114 .6.3 16h BIOS 124 .8.1 DIV ......................................152 .8.2 Jxx ........................156 .8.3 ......................................166
9 10
,
, . .
?
, . - , , , , , .
, . , .. . , , .
10110000 01100001 mov al, 61h ,
. , . , . , . .
. , . . .
Turbo Assembler (TASM), Turbo Link (TLINK) Version 5.1 Copyright 1992 Borland International Turbo Debugger (TD) Version 3.1 Copyright 1988, 1992 Borland International.
, . . .
80x86. .
80x86 . . .
. , , , , .
MS DOS, .
, . .
DOS BIOS . .
11 12
.
, , , . 2008
13 14
1
1.1
,
, .
.
, ,
. , -
. - ,
, .
B, O (Q), D H,
.
.1.1
.
.1.1 , ,
2 0 1 8 0 1 2 3 4 5 6 7 10 0 1 2 3 4 5 6 7 8 9 16 0 1 2 3 4 5 6 7 8 9 A B C D E F
A F 10 15
.
: 55h ; 55 10011b ; 10011 75 ;
;
radix ideal (.radix masm):
radix
2, 8, 10 16. :
radix 10
radix 16.
.
1.2
0, 1, 2, 3, 4, 5, 6, 7, 8 9. , , 10 . . 10. . 345 :
345 = 3*102 + 4*10
1 + 5*10
0 = 300 + 40 + 5.
345, 678 :
3456,789 = 3*10
3 + 4*10
2 + 5*10
1 + 6*10
0 + 7*10
-1 + 8*10
-2 +
9*10-3
= 3000 + 400 + 50 + 6 + 0,7 + 0,08 + 0,009.
1.3 - - a a
a - : e
a = (ak, ak-1, , a0)
15 16
- , p 2.
n10 : ak*p
k + ak-1*p
k-1 + + a0*p
0 = n10.
n10 a - : i := 0 while n > 0 do
ai := n mod p, n := n div p, i := i +
1 print ai
1.4
0 1. bit ( binary digit). , , . . :
1010112 =1*25 + 0*2
4 + 1*2
3 + 0*2
2 + 1*2
1 + 1*2
0
= 32 + 8 + 2 + 1 = 4310
. ,
, . , . . (.1.2) 4310.
.1.2 4310
43 / 2 21 1 21 / 2 10 1 10 / 2 5 0 5 / 2 2 1 2 / 2 1 0 1 / 2 0 1
, ,
1010112.
1.4.1
- . . - , , , - .
.. . .1.3 15.
.1.3 15
0 0000 8 1000 1 0001 9 1001 2 0010 10 1010 3 0011 11 1011 4 0100 12 1100 5 0101 13 1101 6 0110 14 1110 7 0111 15 1111
, 15 - ,
. - ,
(byte). . , .1.1.
7 6 5 4 3 2 1 0
.1.1
,
, . 1.2.
17 18
7 6 5 4 3 2 1 0
.1.2
,
28, 256 .
16 (word). 15, . 1.3. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
.1.3
. 0 7
, 8 15 , . 1.4.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
.1.4
-
. little endian . ,
.1.5.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3 2 1 0 .1.5
16 216,
65 536, . 32 (double word), .1.6. 31 0
.1.6
,
, .1.7. 31 16 15 0
31 24 23 16 15 8 7 0
2 1
31 28 27 24 23 20 19 16 15 12 11 8 7 4 3 0
6 5 4 3 2 1
.1.7 ,
232
, 4 294 967 296 .
: (quad word), 4 = 8 = 16 = 64 , (paragraph) 8 = 16 = 32 = 128 (page) 32 = 256 .
.1.4
SI ICE
Kilobyte (kB) (103)1 = 10
3 Kibibyte (KiB) (2
10)1 = 2
10
Megabyte (MB) (103)2 = 10
6 Mebibyte (MiB) (210)2 = 220
Gigabyte (GB) (103)3 = 10
9 Gibibyte (GiB) (210)3 = 230
Terabyte (TB) (103)4 = 10
12 Tebibyte (TiB) (210)4 = 240
Petabyte (PB) (103)5 = 10
15 Pebibyte (PiB) (2
10)5 = 2
50
Exabyte (EB) (103)6 = 10
18 Exbibyte (EiB) (2
10)6 = 2
60
Zetabyte (ZB) (103)7 = 10
21 Zebibyte (ZiB) (2
10)7 = 2
70
Yotabyte (YB) (103)8 = 10
24 Yobibyte (YiB) (210)8 = 280
19 20
(.1.4): SI
1000 = 103 (k). kB ()
1000 B. ICE
1024 = 210
- (Ki). KiB (kilo binary byte, ) 1024 B.
1.5
. . . . 0 15. 0 9 A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.
, 78C16,
, .. 0A78Ch.
. , 0BCF8Ah .1.4.
.1.5
B C F 8 A 1011 1100 1111 1000 1010
, . . ,
10110100111011b , 14 .
0010110100111011b.
: 0010 1101 0011 1011b.
2D3Bh.
1.6
. 1.5, , .
.1.6
2
-128..127 -27..(2
7-1)
-32 768..32 767 -215
..(215
-1)
-2 147 483 648..2 147 483 647 -231
..(231
-1)
1.7
, , . , .1.7:
.1.7 + 0 1
0 0 1 1 1 10
10b 2d. ,
00001110b 00000111b, .1.8:
21 22
1 1 1 0 0 0 0 1 1 1 0 (14)
+ 0 0 0 0 0 1 1 1 (7)
0 0 0 1 0 1 0 1 (21) 7 6 5 4 3 2 1 0
.1.8
0: 0 + 1 =
1. 1 1 + 1 = 10; 0, 1 . 2 , .. 1 + 1 + 1 = 11; 1 1 . 3 : 1 + 1 + 0 = 10; 0 1 . 1 + 0 + 0 = 1. 1 4, . 5, 6 7 , ; .
, .. . NOT () , .1.8:
.1.8 NOT
NOT 0 1 1 0
00010110b.
11101001b.
1.8
8086 ..
. 0, 1, . 1. , 92, .1.9:
01011100 92 10100011
+ 1 1 10100100 -92
.1.9 8 92
-
92 01011100, . 1.10:
10100100 -92 01011011 + 1 1
01011100 92
.1.10 8 -92
92 -92 :
01011100 92 +10100100 -92
(1)00000000 .1.11 8 92 -92
8- .
.
1.9
.
23 24
, 8 00001110b
(14d) 00010101b (21d)
8 00001110b (14d),
11110010b (-14). :
00010101 (21) +11110010 (-14)
(1)00000111 (7) .1.12 8
.
- .
1.10 ASCII/IBM
ASCII (American Standard Code for Information Interchange, ) ( ) 7 128 0 127. (8 ), 128 , 128 255, .. IBM ( ) ASCII .
1.10.1 ASCII
ASCII 32 . 32 , 0h 1Fh, . , , , . - , .1.9:
ASCII , 20h 3Fh, , 0 9. , . 30h ASCII .
.1.9 ASCII
(10) (16)
00 00 NUL ( )
07 07 BEL
08 08 BS
09 09 HT
10 0 LF
11 0B VT
12 0C FF
13 0D CR
24 18 CAN
27 1B ESC
32 , 40h 5Fh,
.
, 60h 7Fh,
, DEL.
20h 0FFh, .
: d D ASCII , .1.13:
7 6 5 4 3 2 1 0 D 0 1 0 0 0 1 0 0
7 6 5 4 3 2 1 0
d 0 0 1 0 0 1 0 0
.1.13 ASCII
25 26
5. .
1.10.2 IBM
80h 0FFh , , .
1.11
1.11.1
. . ?.
, dpl (duplicate)
. : [ [_dpl [_dpl...]]
_dpl :
?, , dpl (_dpl[,_dpl]),
.
?.
.
1.11.2 db
(define byte), dw, dd, dq, df, dp, dt.
.1.10
db
dw
dd
dq
df 6 (48- 80386)
dp 6 (48- 80386)
dt 10
.
DB -128 255; 8
, high low; , .
: msg db "Hello World", 0dh, 0ah, '$'
14 msg
Hello World, ;
, $. fname db 25 dup(?)
fname 25 ,
. str db 1, 2, 3, 4, 5
27 28
5 str
1, 2, 3, 4 5. radix 16 const1 db 1100010b
const2 db 62
const3 db 98d
const4 db 31+31
const5 db 'b'
radix 10 const6
db 49*2 const7
db "b" const8
db 62h const9
db 98 const10
db 142q
const1 const10
. 98.
dw -32768 65535; 16 ; , .
dd -2147483648 4294967295; , 16 16 ; 4 , .
dq -2147483648 4294967295 ( 80386) -32768 65535 ); 32 - (80386) 16 - ( );
-263
263-1
; 8 , . dp dt : -2147483648
4294967295 (80386) -32768 65535 ; 32 - (80386) 16 - ( ); 16 16 ;
-247
248-1
; 6 , .
dt -2147483648 4294967295 ( 80386) -32768 65535 ); 32 - (80386) 16 - ( ); 16 16 ;
-279
280-1
; 10 , ; 0 99999999999999999999.
' ". .
dd, dq dt : 2.040 ; 2,0 10^40 0.050 1.28+7 ; 1,28 10^+7
5.40-3 ; 5,40 10^-3
1.11.3
EQU, : EQU
equ . ,
numb EQU 7
mov al, numb
: mov al, 7
29 30
1.12
1.1 : a) 127 e) 256 i) 32767 m) 178 q) 511 u) 512 y) 278 b) 989 f) 23 j) 129 n) 15 r) 45 v) 2048 z) 19 c) 300 g) 84 k) 32768 o) 254 s) 81 w) 16 d) 128 h) 255 l) 1025 p) 4095 t) 1023 x) 1024
1.2 :
a) 1000 0001 b) 1001 0001 c) 1111 0000 d) 1111 1111 e) 0101 1010 f) 1100 0010 g) 0100 0100 h) 1101 0001 i) 0101 0101 j) 1010 1011 k) 0110 0111 l) 0011 1100 m) 0100 1110 n) 0001 0011 o) 0111 1110 p) 1110 0111 q) 1010 1100 r) 1011 0101 s) 0101 1111 t) 0110 1110 u) 0111 1101 v) 1101 1100 w) 0011 1110 x) 1000 0111 y) 1001 1100 z) 1100 0001
1.3 1.2 .
1.4 .
a) ABCD e) CD0A i) ADD m) BEBE q) BADA u) 911A y) 17C9 b) 41D9 f) 1208 j) 95A3 n) 759C r) 180D v) FACE z) FADE c) 15FA g) FFFF k) 74B3 o) 5432 s) DAA w) 359B d) BAD h) DEAF l) C0DE p) DADA t) 2D3D x) BEEF
1.5
. a) 5234 + 7976 f) FFFF-1024 k) CCCC + 4444 p) 1001 + ABD9 b) ABDC - 5B81 g) 5050 + A505 l) FA01 + D1FF q) 5214 - 1015 c) FFA0 + 617C h) 1000 - 1 m) 4321 + BCDE r) DADE + 3522 d) 8353 + 2797 i) ADB8 - 1111 n) 21AC + 21AC s) 7353 - 4ABC e) 2A9C + E564 j) 10000 - FFFF o) 1821 + E7DF t) 9919 + ABCF
1.6
? a) b) c)
1.7 8 ( , ).
a) -12 e) -83 i) -128 m) -67 q) -11 u) -1 y)-1024 b) -32 f) -15 j) -17 n) -512 r) -100 v) -14 z) -2 c) -132 g) -45 k) -29 o) -10 s) -127 w) -64 d) -129 h) -8 l) -256 p) -4 t) -23 x) -18
1.8 1.2
.
1.9 ? a) b) c)
31 32
2 8086
. , 8086, .. . (memory), (, Central Processor Unit) / (Input/Output). , .. .
2.1
. . , . .
/ . . , .
.2.1 .
.2.1
2.1.1
0 1. 0,0 V 0,8 V, 2,4 V 5 V. , .
/ . () 8, 16, 32 64 ( ). , . 8, 16, 32 64 , .2.1.
.2.1 80x86
()
8088 8 8086 8 80188 8 80186 16 80286 16 80386sx 16 80386dx 32 80486 32 80586/Pentium (Pro) 64
/ ,
. n 2n
. 8088 8086 20
, 220
. .
33 34
.2.2
80x86
()
8088 20 1
8086 20 1
80188 20 1
80186 20 1
80286 24 16
80386sx 24 16
80386dx 32 4 GB
80486 32 4 GB
80586/Pentium (Pro) 32 4 GB
,
.
2.1.2
. , . . . . . , , . , .2.2.
582 No 3 581 578 No 2 580 No 1 579 No 0 578 577 No 1 576 575 No 0 575 574 573 572 No 0 572 571
.2.2 ,
2.1.3 /
20, 24 32 , , 8086 16 / . 65 536 (216) / . .
: / . / . / .
/ .
2.2
8086 . , . , 8086 .
35 36
2.2.1
. , . . . Hertz (Hz) . 1 Hz . . : 1 kHz = 1000 Hz 1 MHz = 1000 kHz = 1 000 000 MHz. , . , 1 MHz 1 s (1 ), .. 1/1 000 000 .
- , . - , .. , 8086 . , , . 80286 .
, , . 8088 8086 12 . 80286 80386 45 , 80486 . Pentium , Pentium 4 .
( ) . 100 MHz Pentium 200Mhz 80486 400Mhz 80386 80286. , , , .
2.2.2
- . . , . 8086 . .
( ) .
2.2.3
. . ( ), . - .. (cache) .
2.2.4
-, -. , , , ( -) RAM , - .
- L1 (Level 1- 1) L2 (Level 2 2). L3 (Level 3), . , , L1 L2 . - , .
. L1 - , L2 , , .2.3.
37 38
.2.3
, L1, L2 . , CPU RAM , . , . L1 L2 , . L1 , , .
500 MHz,
L1 500 MHz. L2
-
.
.
, Pentium II
Pentium III, - L2
(backside bus). -
, .
1:2. Pentium III 500
MHz, L2 250 MHz.
,
L2 , 1:1 L2.
2.3 8086
, . , . (). - , (sockets) . - , . - : , , , -, , , , , , .
: (execution unit, EU) (bus interface unit, BIU), .2.4.
39 40
EU: Execution Unit
AH AL BH BL CH CL DH DL
SP BP SI DI
ALU: Arithmetic and Logic Unit
CU: Control Unit Flags Register
Instruction Pointer
BIU: Bus Interface
Unit
CS DS SS ES
Bus Control Unit
1 2 6
Instruction Queue
- . .
8086 16 .
. : AX
- (accumulator) , BX - (base)
, CX - (count) , DX - (data) . ,
IP - (instruction pointer), .
- Flags, . , -, - .
2.3.2
(), , , . , AX :
-- AX ; -- ; -- ; -- AX.
2.3.3
.2.4 8086
. / , .
2.3.1
, . , .
()
, , , . .
2.3.4
() , . , .
41 42
, - .
, .
2.3.5
:
1. . IP
. 2. . 3. . , ,
. 4. ,
. IP . IP .
2.6
2.1 .
2.2 .
3 8086
, .
3.1 8086
8086 14. .. (real mode). 1 MB 64 kB. , . 16 .
3.1.1
, , .. , 16 . 64 B. .
CS (code segment). .
DS (data segment). .
SS (stack segment). , .
ES (extra segment) . .
3.1.2
, .
: SI -
(source index) DI (destination index).
43 44
16 : AX, BX, CX
DX. 8 (8
): AX: AH (high, ) AL (low, ), BX: BH BL, CX: CH CL, DX: DH DL.
.3.1 8086
AX - ,
. BX - ,
.
CX - .
DX - - . .
: L, H X
. , 4 H (
) , 12 X . X
X L .
: X = H * 256 + L. , BH = 12, BL = 117, BX = BH * 256 + BL = 12 *256 + 117 = 3189.
SP BP
. SP (stack pointer).
. SS .
BP (base pointer). .
BX, BP, SI DI , . .
3.1.3 IP Flags. IP (instruction pointer).
CS.
Flags . . .
.3.2 45 46
.
. CF (carry).
. .
PF (parity). (1 0 ).
AF (auxillary) . 3 4.
ZF (zero) (1 0 ).
SF (sign) (1 - 0 - ).
TF (trace, trap). . 1 .
IF (interrupt). 1 .
DF (direction). . 1, .
OF (overflow) 1, .
-
CF, ZF, SF, OF
DF .
3.2 80286
80286 .. 16 . 16 . , - 64 kB. (machine status word). :
IOP (I/O-protection level)
/; NTF (nested task flag). .
3.3 80386 32 : EAX (extra),
EBX, ECX, EDX, ESI, EDI, EBP, ESP, EIP , EFlags 16 FS GS.
.3.3 80386
32 . ,
4 GB. : RF (resume flag) ,
; VF 8086 (virtual 8086 mode) .
47 48
3.4 80486/Pentium
: ACF
(alignment check); VIF
(virtual interrupt); VIP
(virtual interrupt pending); IF (identifiacation) CPUID.
3.5 .
20 8086 8088 20 , .. . 20 16 . 20 4 . 20 . 20 (offset) . 20 .
.3.4 ( + )
. + . < _> : . ,
: 1AC3h:2221h. ( ):
1AC30h 20
+ 2221h 1CE51h 20
.3.5 20
.
. . 0. ,
R. :
9 0000 B8 ---- R mov ax, data 10 0003 8E D8 mov ds, ax 11 0005 BA 0000 R mov dx, offset mes
, .
, ,
0, R, . .
seg: seg
. ,
: dataseg msg1 db "Hello World", 0dh, 0ah, '$'
msg1 db 'Good Morning', 0dh, 0ah, " : mov ax, seg msg1
mov ax, seg msg2
49 50
AX ,
msg1 msg2.
offset: offset
: mov ax, offset msg1
mov ax, offset msg2
3.6
(stack) LIFO (last in - first out, ) , . , , . , . .
3.7 ROM-BIOS DOS
.
, ,
ROM-BIOS (Read Only Memory-Basic Input Output System) DOS (Disc
Operating System) . ROM-BIOS DOS , . ROM-BIOS DOS. .
3.7.1 ROM-BIOS
ROM-BIOS . ROM , .
: , RAM, , , , , , .
3.7.2 DOS
BIOS DOS , . DOS . ROM-BIOS , DOS .
3.7.3
: , BIOS DOS. BIOS DOS, . .
3.8
. ( : , ) ( : ) . , , - . .3.1:
.3.1
08h 18.2 09h 0dh 0eh 0fh 70h
51 52
.3.2:
.3.2
00h 03h 04h
BIOS
.3.3:
.3.3 BIOS
10h 11h 12h RAM 13h / 14h 16h 17h 19h (Ctrl-Alt-Del) 1Ah
- DOS 21h (33d).
, AH. stdout
( ), stderr ().
.3.4 21h DOS
01h ah = 01h al = .
ah = 02h dl
02h dl = --
Ctrl+Break.
ah = 06h stdout
06h dl = Ctrl+Break. --
( ffh)
07h ah = 07h stdin al = Ctrl+Break.
08h ah = 08h stdin al = Ctrl+Break
,
09h ah = 09h -- ds:dx = . ($, 24h)
Ctrl+Break.
stdin ,
0Ah = 0Ah DS:SI
Enter. 0Ah DS:DX = --
(1-254).
0Dh.
ah = 40h CF = 0;
bx = 1 stdout 2 ax =
40h stderr;
ds:dx = ;
cx =
4Ch ah = 4Ch --
-
256 . . . , 4, , . 1024 . 4 ( ). .
, , :
53 54
. 4
. 1. Flags IP, CS
( ). . .
2. : IRET (interrupt return), CS, IP, Flags.
INT (interrupt). , . , . .
()
. 02h 21h DOS :
dl , ah 02h: mov dl, 67h ; 67h = g
mov ah, 02h
INT: int 21h
g.
3.9
3.9.1
. :
push cs
mov ah,
ch inc bx
3.9.2
: mov ax, 2
mov bx, 'q'
.
3.9.3
. (, direct) . .
:
dataseg var1 dw 3478h
var2 dw ? ds :
codeseg
start: mov ax, dgroup
55 56
mov ds, ax
var1 e ds:0000, a var2 ds:0002. :
ds:0003..00
ds:0002..00
ds:0001..34
ds:0000..78
, . IDEAL
[ ] . : mov ax, var1
var1 AX. : mov ax, [var1]
[var1]
var1. AX
3478h.
offset. :
mov ax, offset var2
AX 2h.
var2 .
mov ax, 0fffeh
mov [var2], ax
AX 0fffeh.
AX var1:
ds:0003..ff
ds:0002..fe
inc [var2] var2:
ds:0000 79
ds:0000 34
. : jmp to_code msg db "Hello World", 0dh, 0ah,
'$' to_code: mov dx, offset msg
3.9.4
(, indirect) BX, BP, SI DI.
[ ]. : dataseg var1 dw 3478h
57 58
var2 dw 4455h
codeseg
start: mov ax, dgroup
mov ds, ax mov
bx, 02h :
mov ax, [bx]
AX 4455h. [BX]
BX. :
mov [bx], 5 , , , .
ptr : ptr
. :
.3.5 ptr
byte 1 word 2 dword 4 qword 8 tbyte 10 near 0FFFFh far 0FFFEh
:
mov word ptr [bx], 5
ptr.
: [+]. , , (, ), , ) . (displacement) . : .data var1 dw 1, 2, 3
.code start:
mov ax, @data
mov ds, ax mov bx, offset var1
mov ax, 0 :
add ax, [bx+0]
add ax, [bx+2]
add ax, [bx+4] AX var1.
ptr mov word ptr [bx+4], 5
59 60
[+]. ,
, .
. () . : mov bx, 3 mov word ptr [string+bx],
5 add ax, [string+bx]
-
: [bx+si], [bx+di], [bp+si], [bp+di] ( 32 ). : mov eax, [ebx+esi]
- -
: [bx+si+], [bx+di+],
[bp+si+], [bp+di+] ( 32 ). - (, ).
3.10 3.10.1 8086 .
3.10.2 20 :
a) 28F3h:7211h b) 231h:1221h c) 0CCCh:4444h
d) 4444h:0CCCCh e) 4567h:0ABCDh f) 0FA01h:0D1FFh g) 0FF0h:617Ch h) 1000h:0001h i) 0ACDCh:0ACDh j) 8888h:22h k) 8D1h:0D12Dh l) 0ACBDh:0ACBDh m) 0E64Eh:0E21Ah n) 1010h:0FFFCh o) 55h:87h
61 62
4 MS-DOS
MS-DOS , , , .
MS-DOS DOS-BIOS, DOS . ().
4.1
command.com
cmd.exe. Windows XP : C:\WINDOWS\system32. : Start Run ( Win+R):
.4.1 Run
Run. cmd
:
.4.2 Run, cmd
OK. :
.4.3 cmd
Run command
:
63 64
.4.4 Run, command :
.4.5 command
.. C:\>_, D:\>_ . . .
16 command.com DOS (virtual DOS machine, VDM).
MS-DOS . cmd.exe 32
Windows XP . ,
. Enter. :
C:\DOCUME~1\USER> [/] , cmd.exe (
command.com) .
MS-DOS .com, .exe, .bat .cmd. . , .
exit.
4.2
4.2.1
( , , ), , MS-DOS . :
a..z A..Z, (:). :
A: , B: , C: .
: C:, D:, E:. CD DVD , :
F: CD , G: DVD .
( ). , : G:, H: .
, .
: d:, e:, a:. : C:\Documents and Settings\User>a:
A:\>_ A:.
65 66
4.2.2
, . . . . 1 8 , (), 3 . (.). , . : . / \ [ ] : | < > + = ; , ASCII - 20. : readme.txt, autoexec.bat, command.com, gpl.txt,
config.sys .
4.2.3
- .. (folders), (directories). ,
. : docs
:
guide.doc, manual.doc, info.doc .
.
4.2.4
. .. . , .
. ..,
. \ .
, MS-DOS , . .
D:\docs\post\today\manual.doc, manual doc D: , docs,
post, today. ()
, .
, ( ) .
4.3 MS-DOS
4.3.1 TREE
tree: tree [:][] [/f] [/a] , . : /f ; /a . : tree /a : Folder PATH listing for volume WIN Volume serial number is 00008102
4088:0C69 C:. +---WINDOWS | +---system32 | | +---config | | | \---systemprofile | | | +---Start Menu | | | | \---Programs | | | | +---Accessories | | | | | +---Entertainment | | | | | \---Accessibility | | | | \---Startup | | | +---My Documents | | | +---Favorites | | | \---Desktop | | +---drivers | | | +---etc | | | \---disdn | | +---ras
67 68
. :
tree c: /f : Folder PATH listing for volume WIN
Volume serial number is 4088-0C69 C:\ | CONFIG.SYS | AUTOEXEC.BAT | log.txt | DataTEST.XLS | csb.log | test.jpg | +---WINDOWS | | TDW.INI | | win.ini | | RtlRack.ini | | _default.pif | | explorer.scf | | msdfmap.ini
C:.
> . : tree > tr.txt
4.3.2 DIR
( ), , dir (directory). , . : dir [:][][] [...] [/p] [/q]
[/w] [/d] [/a[[:]]][/o[[:]]]
[/t[[:]]] [/s] [/b] [/l] [/n] [/x]
[/c] [/4]
, - : /p , . /w /d /w, .
.. : * ?. ? , . , info,
, xe: dir info.?xe
*
. dir *.asm ,
asm. *.* ????????.??? .
4.3.3 TYPE
,
type: type [:][][]
tr.txt : type tr.txt
4.3.4 MORE
| more. :
69 70
type c:\windows\system.ini | more
system
ini, C:
windows. , Enter . :
| more [/c] [/p] [/s] [/tn]
[+n] more [[/c][/p][/s][/tn][+n]] <
[:][] more [/c] [/p] [/s] [/tn] [+n] []
: more < c:\windows\system.ini
4.3.5 MKDIR (MD)
md (make directory). : mkdir [:]
md [:]
, . : md c:\tasm\examples\msdos
C: tasm, ,
examples,
msdos. C: tasm, .
help . md help
4.3.6 RMDIR (RD)
(remove directory), . : rmdir [:] [/s] [/q]
rd [:] [/s] [/q]
: /s ; /q .
\ , , ,
. , D:,
, temp : rd \temp
\ rd ,
. , names C: , : rd temp temp.
rd .
4.3.7 CHDIR (CD)
cd (change directory) . : chdir [[/d] [:][][..]] [[/d]
[:][][..]] cd [[/d] [:][][..]] [[/d]
[:][][..]]
71 72
: /d , .. .
. : chdir [:\[]]
cd [:\[]]
:
cd . cd bin
bin, .
4.3.8 PATH
MS-DOS , , , , () . a tasm :
.4.6 tasm
, MS-DOS
, () . path, : path [[%path%][:][;...]]
PATH , . ,
: .com, .exe, .bat, .cmd. path :
.4.7 PATH
Windows XP Path, My Computer/Properties/Advanced/Environment Variables. Varible Value, Path Edit.
; . ; :
.4.8 PATH 73 74
. %path%
, Path. Path. c:\tasm\bin Path: path %path%;c:\tasm\bin
cmd.exe,
command.com.
4.3.9 COPY
copy . : copy [/d] [/v] [/n] [{/y|/-y}] [/z] [{/a|/b}]
[{/a|/b}] [+ [{/a|/b}]
[+ ...]] [ [{/a|/b}]]
. , . : copy help.txt newhelp.txt
help.txt newhelp.txt.
copy , (/b), (\v) .
4.3.10 RENAME (REN)
ren .
75
rename [:][] 1 2
ren [:][] 1 2
1
, . 2 . , : ren *.cmd *.bat
. , : ren slovo duma slovo duma.
ren , 2 .
4.3.11 DEL (ERASE)
(delete) . : del [:][] [ ...] [/p] [/f]
[/s] [/q] [/a[:]]
erase [:][ ] [ ...] [/p]
[/f] [/s] [/q] [/a[:]]
/p
(y , n , Ctrl+C
), /f ,
, /s
, /q
. /a
, .
76
: r
, a , s , h , - - . , : del *.obj .obj .
4.4 4.3.1 cmd.exe. .
4.3.2 , MS-DOS. . .
4.3.3 cmd.exe. .
5
, . -. (), (), . .
5.1
5.1.1
, , , . . : A-Z, a-z, 0 9 (.) ( ), (_), (?), ($), (@). , , .
255 , 32 , .
/mv.
, , . /mu, /ml, /mx.
5.1.2
.
. :
77 78
[:] [] [;]
:
start: add ax, bx ; mov cx, 5 nop ret jmp part2
. . . , .
( ). (), . add mov.
, (, ), . . . :
-- (, ) - , , ; ;
-- - :;
-- - , , , ; ; : msg db "Hello World", 0dh, 0ah, '$'
msg 14
. .
-- - $;
, IP, :
byte_block db 50 dup(?)
byte_block_lenght equ $-byte_block
scan_table db 10 dup (0,1,2,3,4,5,6,7,8,9)
scan_table_lenght equ $-scan_table jmp $+3
-- - AX, BX, CX ..;
-- ;
-- ; -- . .
. , (;) .
5.1.3
, , , . ; .
: [;]
: include macro.inc
assume CS:code_segment code_segment segment para CODE ; code segment
insert_message db Insert symbol: ,$'
79 80
,
.
5.1.4
. TASM ASCII . "Hello World" 'Good Morning'
: 'I''m a programmer' ; I'm a programmer
. msg db "Hello World", 13d, 10d, '$' msg db "Hello "
db 'World', 13d, 10d, '$'
5.2
TASM : MASM ( ) IDEAL.
IDEAL .
, .
end []. , .
end start.
Turbo Assembler end.
5.2.1
..
. segment ends.
segment :
segment [] [] [] [] []
byte at xxxx use16 word common use32 dword memory para private page public
stack masm segment
.
: byte (), word (),
dword ( ), para ( ,
page ().
. private, ..
. public
( memory).
common , - . at
xxxx
xxxx.
xxxx. stack . SS
stack, a SP . 81 82
, , .
80386.
. use16 16 , .. 64 KB . use32 32 , .. 4 GB .
'' . :
'' . .
ends : ends [ | ]
ends cseg. ,
.
segment, struc union .
MASM
ends . segment ends
. , . assume, :
assume : [,:]
assume :nothing assume nothing
, . , . :
assume cs:code_segment, ds:data_segment
CS code_segment, DS - data_segment.
assume ds:nothing , ds.
assume nothing
.
,
. model, . :
model [] []
[,[] ] [, ]
MASM ,
.model. model
assume. ( ) ( ) .
: (near) (far). , .
83 84
model
. .5.1
.
.5.1
cs=dgroup
tiny dgroup. ds=ss=dgroup
.com
.
.
small cs=_text
ds=ss=dgroup dgroup.
.
medium cs=_text .
ds=ss=dgroup
dgroup.
.
cs=_text
compact dgroup.
ds=ss=dgroup
.
,
.
large cs=_text
ds=ss=dgroup dgroup.
.
huge cs=_text
ds=ss=dgroup large.
cs=_text
tchuge large,
ds=nothing ss=nothing .
cs=code,
tpascal ds=data, Turbo Pascal. -
ss=nothing
.
flat cs=_text small,
ds=ss=flat 32
.
.model
dgroup.
data, data?, const stack (T.5.2).
.5.2
(Ideal) (MASM)
codeseg [] .code []
.
dataseg .data
.
udataseg .data?
.
const .const
.
85 86
(Ideal) (MASM)
stack .stack []
[] .
[]
( ).
200h (1 kB).
fardata [] .fardata []
(far) .
far_data.
ufardata .fardata? []
[]
(far) .
(T. 5.3).
.5.3 nearstack
dgroup ss dgroup.
farstack , dgroup. use16 16 (
80386 80486). use32 32 (
80386 80486). dos, , MS-DOS. os_dos os2, , OS/2. os_os2
( )
.
.
:
PASCAL, C, CPP (C++), SYSCALL, BASIC, FORTRAN, PROLOG
NOLANGUAGE ( ). ,
NOLANGUAGE.
Windows NORMAL, WINDOWS, ODDNEAR
ODDFAR. ,
NORMAL. model
, T.5.2. . TASM .
model , T.5.4.
.5.4 , model
: 1 = tiny, 2 =
@model small flat, 3 = compact, 4 =
medium, 5 = large, 6 = huge, 7 = tchuge, 0
= tpascal.
@32bit 0 = 16 , 1 = 32 .
@CodeSize : 0 = (near), 1 = (far)
87 88
@DataSize : 0 = (near), 1 =
(far), 2 = (huge)
,
: 0
@Interface = nolanguage, 1 = c, 2, syscall, 4 =
pascal, 5 = fortran, 6 = basic, 7 =
prolog, 8 = cpp. 8 0 MS-DOS 1
OS/2.
@code .
@data (near) .
@fardata (far) .
@fardata? (far)
.
@curseg .
@stack .
5.2.2
, , ( ) . - . - .
: PROC ENDP:
[] PROC [[] ] []
[ARG ] [RETURNS ];
[LOCAL ] [USES ,] [] ENDP
MODEL. near far
RET, .
ARG ,
.
BP EBP.
LOCAL
. USES
. RETURNS .
TLINK.
EXTRN PUBLIC.
EXTRN
.
PUBLIC. : EXTRN :, ..., : PUBLIC
, DB, DW ., , = EQU. .
. :
, BYTE,
WORD, DWORD, PWORD, FWORD, QWORD TBYTE.;
, near
far.; , ABS.
5.2.3
, MACRO ENDM:
89 90
MACRO [[-
,...]] ENDM
, MACRO.
.
, . . , , LOCAL , ...
. , LOCAL, ??, 0000 FFFF. , , : INCLUDE
, INCLUDE. , , .
5.2.4 EXE COM
MS-DOS : EXE COM. MS-DOS 256 (program segment prefix, PSP). , , .
COM COM ,
, 64 kB. (CS, DS, SS ES) PSP. COM ( PSP) 100h.
org , .
IP . COM : org 100h
IP 100h. SP PSP ( 0FFFEh, -
, 64 kB ). COM
(near) , , .
4Ch MS-DOS, .3.4.
COM Ideal
. Hello
World ., . ;prog51.asm
ideal
model tiny
codeseg org 100h
start: mov ah, 9h mov dx, offset msg
int 21h mov ax, 4c00h
int 21h msg db "Hello World", 0dh, 0ah, '$'
91 92
end start
: tasm prog51.asm
: tlink /t prog51.obj
COM: prog51.com.
MASM COM : ;prog52.asm masm .model tiny .code
org 100h
start: mov dx, offset msg
mov ah, 9h int 21h mov ax, 4c00h
int 21h msg db "Hello World", 0dh, 0ah,
'$' end start
, jmp. : ;prog53.asm ideal
model tiny
codeseg org
100h start:
jmp to_code msg db "Hello World", 0dh, 0ah,
'$' to_code: mov dx, offset msg
mov ah, 9h int 21h mov ax, 4c00h
int 21h end start
COM : ;prog54.asm
ideal
segment code org 100h assume cs:code, ds:code, ss:code, es:code
start:
mov ah, 9h mov dx, offset msg
int 21h mov ax, 4c00h
int 21h msg db "Hello World", 0dh, 0ah,
'$' ends code end start
prog54.asm ideal.
MASM IDEAL
MASM.
93 94
MASM IDEAL .
EXE EXE ;
, , . 64 kB . , , . DS ES PSP. CS, IP, SS SP , .EXE .
EXE, ideal, . ;prog55.asm ideal segment stackseg stack
db 256 dup (?) ends stackseg segment data msg db "Hello World", 0dh, 0ah,
'$' ends data segment code assume cs:code, ds:data, ss:stackseg
start: mov ax, data mov ds, ax mov dx, offset msg mov ah, 9h int 21h mov ax, 4C00h int 21h
ends code
end start
: tasm prog55.asm
tlink prog55.obj
prog55.exe. :
stackseg, data
code.
ideal masm.
EXE
masm: ;prog56.asm .model small .stack 100H .data msg db "Hello World", 0dh, 0ah, '$'
.code start: mov ax, @data mov ds, ax mov dx, offset msg mov ah, 9h int 21h mov ax,4c00h int 21h
end start ideal EXE : ;prog57.asm ideal
95 96
model small stack 100h dataseg msg db "Hello World", 0dh, 0ah, '$'
codeseg
start: mov ax, dgroup
mov ds, ax mov dx, offset msg
mov ah, 9h int 21h
mov ax, 4c00h
int 21h end start
5.2.5 , ,
( ). .
() .
1, 2, 3
, 1, 2, 3 . , , . :
* - - AND, OR, NOT, , (, , , , ),
* - ,
( ).
* . / .
* - . (.5.1):
.5.5
(
): * * * () * () * *
5.3
ASCII , .
5.3.1
Borland Turbo Assembler 5.0
cmd.exe (command.com)
INSTALL, Enter.
Enter .
97 98
, C:
D:.
. C:\TASM,
TASM; C:\WINDOWS C:\WINXP, Windows; Yes. .
Start Installation F9.
5.3.2
ASCII ASM. Microsoft Visual Studio. File/New. New Files, Text File .
ASM File/Save As... , .
5.3.3
ASM
tasm.exe
OBJ. , , .
TASM tasm c:\tasm\bin.
tasm :
tasm [] [,] [,]
[,] ,
. ASM . ,
LST,
XRF. , , : tasm prog56 , , ,
: prog56.obj,
prog56.lst prog56.xrf.
. ,
nul .
tasm: tasm prog56 , , ,nul
prog56.obj prog56.lst.
:
/c ,
, , . .
/l, /la /l
/la ,
.model.
/x , . /z .
99 100
/zd . /zi . /zn .
TASM, . . ,
Error () Warning ().
Error, .
Warning ,
, .
. . . . . . .
, tasm .
- prog56.lst :
Turbo Assembler Version 4.1 08/11/06 11:43:12 Page
1 prog56.asm
101
1 ;prog56.asm 2 3 0000 .model small 4 0000 .stack 100H 5 0000 .data 6 0000 48 65 6C 6C 6F 20 57+ msg db "Hello
World", 0dh, 0ah, '$' 7 6F 72 6C 64 0D 0A 24 8 000E .code 9 0000 start: 10 0000 B8 0000s mov ax, @data 11 0003 8E D8 mov ds, ax 12 0005 BA 0000r mov dx, offset msg 13 0008 B4 09 mov ah, 9h 14 000A CD 21 int 21h 15 000C B8 4C00 mov ax,4c00h 16 000F CD 21 int 21h 17 end start
Turbo Assembler Version 4.1 08/11/06 11:43:12 Page 2 Symbol Table
Symbol Name Type Value Cref (defined at #)
??DATE Text "08/11/06" ??FILENAME Text "prog56 " ??TIME Text "11:43:11" ??VERSION Number 040A @32BIT Text 0 #3 @CODE Text _TEXT #3 #3 #8 @CODESIZE Text 0 #3 @CPU Text 0101H @CURSEG Text _TEXT #5 #8 @DATA Text DGROUP #3 10 @DATASIZE Text 0 #3 @FILENAME Text PROG56 @INTERFACE Text 000H #3 @MODEL Text 2 #3 @STACK Text DGROUP #3 @WORDSIZE Text 2 #5 #8 MSG Byte DGROUP:0000 #6 12
102
START Near _TEXT:0000 #9 17
Groups & Segments Bit Size Align Combine Class Cref (defined at #)
DGROUP Group #3 3 10 STACK 16 0100 Para Stack STACK #4 _DATA 16 000E Word Public DATA #3 #5
_TEXT 16 0011 Word Public CODE #3 3 #8 8
, .
.
TASM Microsoft Visual Studio
Microsoft Visual Studio.
Tools/Customize Tools
. Menu contents . . .
tasm.exe
, Build ASM.
New.
Enter. Command c:\tasm\bin\tasm.exe.
Arguments /c /l $(FileName).asm.
Initial directory $(FileDir).
Use Output Window. Close
Customize. Tools/Build ASM.
Microsoft Visual Studio. , prog56.asm
Tools/Build ASM Output
Window Build ASM :
Turbo Assembler Version 4.1 Copyright (c) 1988, 1996 Borland International Assembling file: prog56.asm Error messages: None Warning messages: None Passes: 1 Remaining memory: 412k Tool returned code: 0
, prog56.asm,
prog56.obj prog56.lst.
5.3.4
EXE
COM.
TLINK. .
TLINK TLINK :
tlink [] [,]
[,] [,]
[,] [,]
,
OBJ. + (). : tlink main + proc1
,
, . , .
103 104
, ; . . - prog56.map : Start Stop Length Name Class 00000H 00010H 00011H _TEXT CODE 00020H 0002DH 0000EH _DATA DATA 00030H 0012FH 00100H STACK STACK Program entry point at 0000:0000
LIB.
tlib.exe TASM.
DEF. Windows . ,
Windows RES. : /x
; /m
public; /t
COM ( EXE ;
/v , .
prog56.exe
prog56.map prog56.obj
tlink.exe : tlink /v prog56.obj
TLINK Microsoft Visual Studio ,
Microsoft Visual Studio,
Tools Tools/Customize
Link OBJ. Command c:\tasm\bin\tlink.exe, Arguments
$(FileName), Initial directory $(FileDir);
Use Output Window.
Close. Tools/Link OBJ
prog56.exe
prog56.map. Output Window
Link OBJ :
Turbo Link Version 7.1.30.1. Copyright (c) 1987, 1996
Borland International Tool returned code: 0
5.3.5
prog56.exe
Enter. Hello World.
Microsoft Visual Studio
Tools Tools/Customize Run. Command c:\windows\system32\cmd.exe, Arguments /c $(FileName), Initial
directory $(FileDir).
cmd.exe /c () .
, . .
5.3.6
Turbo Debugger Turbo Debugger (TD)
. 105 106
: ( ) .
TD (EXE COM) . TD. . . .
TD : td [] [ [] ]
,
TD. : td prog56
TD :
.5.1 Turbo Debugger
, : , , , .
TD: -- F10;
; --
TD Alt+F10 ( ).
.. ( ), . start ( end).
TD Run/Run F9.
F7 ( Run/Trace into)
F8 ( Run/Step over), .
F7 : cs:0005BA0000 mov dx,0000
DS
. Tab . Goto... Enter address to
position to ,
ds:0. OK.
msg.
.5.2 Hello World Turbo Debugger 107 108
Hello World
Alt+F5 ( Window/User screen). .
. .
TD Microsoft
Visual Studio. Tools Tools/Customize Debug EXE. Command c:\tasm\bin\td.exe, Arguments $(FileName), Initial directory $(FileDir). Tools/Debug EXE.
TD e Alt+X ( File/Quit).
(break points).
Run . . . Breakpoints/Toggle F2.
109
6 MS-DOS
6.1
6.1.1 DOS
prog51.asm prog57.asm
09h 21h
DOS. Ctrl+Break ,
23h,
. ,
.3.4 . - 09h
40h. ,
BX 1, stdout, bx = 2
stderr. 40h ($) : prog61.asm .model small .data message db 'Hello World $'
message_length equ $-message .code start:
mov ax, @data mov ds, ax mov dx,offset message ; DS:DX mov cx, message_length ; CX mov ah,40h ; mov bx,1 ; stdout int 21h
mov ah, 4ch int 21h
end start
110
.
09h 40h . 02h 06h.
02h 21h
DOS 'asm': ;prog62.asm .model small .code start: mov dl, 'a'
mov ah, 02h
int 21h
mov dl, 's'
mov ah, 02h
int 21h
mov dl, 'm'
mov ah, 02h
int 21h mov ah, 4ch
int 21h end
start
BEL, BS, LF, CR. 02h
06h . ASCII/IBM
LOOP INC:
;prog63.asm .model small
.code start: mov cx, 256d ; mov dl, 00h repeat: mov ah, 02h int 21h inc dx ; inc dl
loop repeat
mov ah,4ch
int 21h
end start
6.1.2 BIOS
DOS ( , , ). BIOS 10h:
.6.1 10h BIOS
ah = 00h
al = .
00 4025 - 04, 05,
01 4025 16 06
02 8025 16
00h 11. -- 03 8025 16
04 320200 4
05 320200 4
06 640200 -
07 8025
ah = 02h
bh =
02h dh = (0-24) . -- dl = (0-79 0-39)
.
111 112
ah = 03h dh, dl =
bh =
. ch cl =
03h
ah = 05h
05h al = --
.
ah = 08h al = ASCII
08h bh =
ah =
ah = 09h
bh =
09h cx = --
al = ASCII
bl =
ah = 0ah
al = ASCII (
0Ah bh = -- bl = )
cx =
AH = 0h
0Eh AL = ASCII ( -- BL =
( ) ).
ah = 13h
al = (0
BL,
, 1 BL, .
, 2
, BEL,
13h , 3 BS, LF, CR -- , .
)
bl =
cx =
dh, dl =
bh =
es:bp =
BIOS
. . X-, Y-. 8025, 79, 24, 320200, 319199.
, . 80 4 ( 0 3), 40 8 ( 0 7).
: ASCII . , , : 0, 1, 2 (), 3 () , 4, 5, 6 , 7 ( ). :
.6.2
0000 1000 - 0001 1001 0010 1010 - 0011 1011 - 0100 1100 - 0101 1101 - 0110 1110 0111 - 1111
a 10,10
- - . . ; prog64.asm .model small .code start:
113 114
xor bh, bh
mov dh, 10
mov dl, 10 mov ah, 02h int 10h ; 10,10
mov al, "a" mov bl, 10101100b mov cx, 5 ; mov ah,09h int 10h
xor bh, bh mov dh,10 mov dl,15 mov ah,02h int 10h ; 15,10 exit:
mov ah, 4ch
int 21h end start
6.1.3
( ) , A (0000h) B (0B000h). 0B800h:0000h 0B800h:7FFFh. ( ), ,
0000h. 0002h. 2 , 160 . , . , 8025 : = * 160 +
* 2 +
* 4096
(1, 0) 0B800h:0000h A - - ( (0, 0)): ;prog65.asm .model small .code start: mov ax,0B800h
mov es,ax mov
di,0
xor bh, bh
mov dh, 0
mov dl, 1
mov ah, 02h int 10h ; (1, 0)
mov ah, 10101100b ; mov al, ; mov es:[di], ax ; mov ah, 4ch int 21h end start
: LODSB MOVSW.
LOOP
CX . ;prog66.asm
115 116
.model small .data message db 'Direct video memory
access!' message_len equ $-message .code start: mov ax,@data
mov ds,ax mov si,offset message
mov ax, 0b800h
mov es,ax mov di,0h
xor bh, bh
mov dh, 0 mov dl, message_len
mov ah, 02h int 10h ;
mov cx, message_len ;
et1: lodsb mov ah, 01001110b ;
stosw loop et1 exit:
mov ah,4Ch
int 21h end start
message DS:SI
AL LODSB. AH. .
STOSW,
AX 0B800h:DI.
6.2
/ , . , , , , , .
/ 60h. ,
/ , ASCII . IRQ1, 09h. ASCII . ASCII .
6.2.1 DOS
DOS , .3.4.
stdin. . 08h
21h stdin. AL DL. 02h 21h . ;prog67.asm .model small .code start: mov ah, 08h ; stdin AL int 21h mov dl, al ; DL ASCII
117 118
mov ah, 02h ; ASCII DL int 21h
mov ah, 4ch ;
int 21h end start
LOOP CX : ;prog68.asm .model small .code start: mov cx, 10d et1: mov ah, 08h ; stdin
AL int 21h mov dl, al ; DL ASCII
mov ah, 02h ; ASCII
DL int 21h loop et1
mov ah, 4ch ;
int 21h end start
, 10 ,
Ctrl+C.
, JMP. . ;prog69.asm
.model small .code start: et1: mov ah, 08h ; stdin
AL int 21h mov dl, al ; DL ASCII
mov ah, 02h ; ASCII
DL int 21h jmp et1
mov ah, 4ch ;
int 21h end start
01h 21h .
- 08h 02h 21h , . .
Ctrl+C
Esc
(1Bh).
CMP, .
Jxx .
JE,
Esc. ;prog610.asm . Esc Ctrl+C
119 120
.model small .code start: et1: mov ah, 08h ; stdin
AL int 21h
cmp al, 1Bh ; Esc
je exit
mov dl, al ; DL ASCII
mov ah, 02h ; ASCII
DL int 21h jmp et1
exit: mov ah, 4ch ;
int 21h end start
prog610.exe TD Esc
, AL
1Bh.
CMP ZF (, 1). ,
JE. exit.
JE
JZ.
, : (, ), ( .) . . ASCII
30h 39h JB
ASCII - 30h, JA
ASCII - 39h. prog611.asm . a Esc Ctrl+C
.model small .code start: et1: mov ah, 08h ; stdin AL int 21h
Esc cmp al, 1Bh
je exit
cmp al, 30h ; '0' jb et1 cmp al, 39h ; '9'
ja et1
mov dl, al ; DL ASCII
mov ah, 02h ; ASCII
DL int 21h jmp et1
exit: mov ah, 4ch ; int 21h
end start
JB JC JNAE
JA JNBE.
121 122
prog612.asm . Esc Ctrl+C
.model small .code start: et1: mov ah, 08h ; stdin AL int 21h
Esc cmp al, 1Bh
je exit
cmp al, 30h ; '0'
jnae et1 cmp al, 39h ; '9'
jnbe et1
mov dl, al ; DL ASCII
mov ah, 02h ; ASCII
DL int 21h jmp et1
exit: mov ah, 4ch ; int 21h
end start
6.2.2 BIOS
. .. MF-II (multy functional) , :
-- (-) a z, 0 9, @, #, $, % .;
-- Insert, Delete, Home, End, Page Up, Page Down, BackSpace, Enter, F11 F12;
-- Alt, Ctrl Shift. ,
. ,
End . ASCII .
BIOS - DOS. 16h .
.6.3 16h BIOS
AH = 00h AL = 0, AH =
(83/84 .), .
10h . AL > 0, AH =
(101/102 ASCII .
00h .), , AH = 20h (122 .
.)
.
ZF = 1:
AH = 01h , ZF = 0:
(83/84 .),
11h , AL = 0, AH =
01h (101/102 .
.), . AL > 0, AH =
21h (122 ASCII .
.) AH =
.
.
123 124
AL =
.
0040h:0017h:
0: Right Shift
1: Left Shift
2: Ctrl Key
AH = 02h
3: Alt Key
4: Scroll Lock (83/84 .), 5: Num Lock 12h 6: Caps Lock
02h (101/102 7: Insert , .), 0040h:0018h:
22h (122 0: Left Ctrlt .) 1: Left Alt
2: Right Ctrl
3: Right Alt
4: Scroll Lock
5: Num Lock
6: Caps Lock
7: Sys Rq ,
AH = 05h AL = 00h:
CH = ;
. AL = 01h:
05 .
CL = ASCII
dir Enter. prog613.asm dir,
.model small .code
start: mov cl,'d' ; CL = ASCII- "d"
mov ah, 5 mov ch, 0
int 16h
mov cl,'i' ; ASCII- "i" mov ah, 5 mov ch, 0
int 16h
mov cl,'r' ; ASCII "r"
mov ah, 5 mov ch, 0
int 16h
mov cl,0Dh ; Enter
mov ah, 5 mov ch, 0
int 16h
mov ah, 4ch ;
int 21h end start
prog613.asm ,
. : prog614.asm dir,
.model small .stack 100h .code start:
125 126
mov CX, 'D'
call KBDWrite
mov CX, 'i'
call KBDWrite
mov CX, 'R'
call KBDWrite
mov CX, 0Dh
call KBDWrite
mov ah, 4ch
int 21h
KBDWrite PROC
near mov AH, 5 int 16h
RET KBDWrite ENDP
ProgExit PROC near
mov AH, 4Ch int 21h
ProgExit ENDP end start
KBDWrite , : prog615.asm dir, KBDWrite.obj
.model small
.stack 100h
.code EXTRN KBDWrite:FAR
start:
Main PROC near
mov CX, 'D'
call KBDWrite
mov CX, 'I'
call KBDWrite
mov CX, 'R'
call KBDWrite
mov CX, 0Dh
call KBDWrite
ProgExit PROC near
mov AH, 4Ch int 21h
ProgExit ENDP Main ENDP end start
: prog616.asm , CX
.model small .stack 100h .code start: PUBLIC KBDWrite
127 128
KBDWrite PROC far
mov AH, 5 int 16h
RET KBDWrite ENDP
end start
prog615.obj
prog616.obj : tlink prog615.obj + prog616.obj
prog615.exe. ProgExit
: prog617.asm
.model small .stack 100h .code start: PUBLIC ProgExit
ProgExit PROC
far mov AH, 4Ch int 21h
ProgExit ENDP
end start
prog615.asm
: prog618.asm dir, KBDWrite.obj ProgExit.obj
.model small .stack 100h .code EXTRN KBDWrite:FAR
EXTRN ProgExit:FAR
start: Main PROC near
mov CX, 'D'
call KBDWrite
mov CX, 'I'
call KBDWrite
mov CX, 'R'
call KBDWrite
mov CX, 0Dh
call KBDWrite call ProgExit
Main ENDP end start
: tlink prog618+prog617+prog616
prog618.exe. TLIB
prog616.obj prog617.obj
mylib.lib: tlib mylib+prog616+prog617
129 130
: tlink prog618+mylib
6.3
6.3.1
10 . ( ) .
DIV. PUSH.
POP. , , 77h.
77h. , .
30h. ASCII . , ASCII 0 9 , ASCII 30h 39h. prog619.asm , AX
.MODEL small .STACK 100h .CODE main:
mov ax,77h ;
push ax ; mov ax,234d ; mov CL,10d ;
labelDIV: mov
ah, 00h div CL ; AX/CL -> L, AH
push AX ;
cmp AL,0 ; jne labelDIV
mov ah, 02h
labelPRINT:
pop DX cmp DX, 77h ;
je ProgExit mov dl, dh
add dl,30h
int 21h jmp labelPRINT
ProgExit:
mov ah,4ch
int 21h END main
.
6.3.2
(dump) , , . ASCII. ASCII , LF CR.
131 132
BIOS : * Start Run; * MS-DOS - cmd.exe
command.com, . OK. .
* debug.exe debug Enter.
debug
debug:
.6.1 debug
*
ROM-BIOS:
d ffff:0005 l 8
08h
ffff:0005: FFFF:0000 30 31 2F-30 38 2F 30 34 01/08/04
BIOS 01.08.2004 . * debug q. * exit
Enter.
6.4
6.1 prog63.exe TD.
mov ah, 4ch.
Run/Run. .
6.2 inc dx inc dl. . .
6.3 prog63.asm , a) ASCII/IBM ; b) 55h
66h . 6.4 Hello World
. 13h 10h . .
6.5 prog66.asm :
message . LODSW STOSW.
6.6 prog611.asm ,
Esc ASCII - 30h - 39h.
6.7 prog611.asm , : a) ; b) ; c) .
133 134
7
7.1
7.1 XOR. TASM, . TD. .model small .code xor ax, ax
int 16h mov ah, 4ch
int 21h end code_segment segment assume cs:code_segment xor ax, ax ;ax=0 int 16h ;wait for a key mov ah, 4ch int 21h ;terminate program code_segment
ends end
7.2 01h 21h . code_segment segment assume cs:code_segment mov ah, 01h int 21h ;stdin -> stdout mov ah, 4ch int 21h ;terminate program code_segment
ends end
code_segment segment
assume cs:code_segment
repeat: mov ah, 01h int 21h ;stdin -> stdout jmp repeat
mov ah, 4ch int 21h ;terminate program
code_segment
ends end
7.3 08h 21h . code_segment segment
assume cs:code_segment
mov ah, 08h ; console input without echo int 21h ; al = input sign mov dl, al ; dl = output sign mov ah, 02h ; output sign from dl int 21h
mov ah, 4ch ; exit in DOS int 21h
code_segment
ends end
7.4 hello exit . .model small .stack 100h .data var1 dB 'hello',13,10,'$' ; 10d = new line ; 13d = carriete return var2 dB 'exit',13,10,'$' .code
135 136
mov ax, @data ; ax -> data mov ds, ax ; ds -> data mov dx, offset var2 ; dx -> var2 mov ah, 09 int 21h
mov ah,4ch ; terminate program int 21h
end
7.5 LOOP.
10 hello CX .
.model small
.stack 100h
.data var1 dB 'hello',13,10,'$' ; 10d = linefeed ; 13d = carriage return .code
mov cx, 10 ; counter repeat:
mov ax, @data ; ax -> data mov ds, ax ; ds -> data mov dx, offset var1 ; dx -> var1 ; ds:dx mov ah, 09 int 21h
loop repeat
mov ah,4ch ; terminate program int 21h
end
7.6 ASCII .
code_segment segment
assume cs:code_segment
start: mov cx, 256d ; character to output mov dl, 00h repeat:
mov ah, 02h ; 21,02h display output
int 21h ; character in DL inc dl
loop repeat
end_program: mov ah,4ch ;terminate in DOS int
21h code_segment
ends end start
7.7 1, 3, 5, 7, 9 AX
TD. ; suma of 4isla v masiv .model small .stack 50h .data masiv dw 1,3,5,7,9,'$' .code
mov ax,@data
mov ds,ax ; mov si,0 ; ds:si->data
mov ax,0 ; mov cx,5 ;
et1:
add ax,[si+masiv]
add si,2 loop et1
mov ah,4ch
int 21h
end
137 138
.model small .stack 50h .data masiv dw 1,3,5,7,9 .code
mov ax,@data
mov ds,ax
mov ax,0 mov si,0 add ax,[si+masiv]
add si,2 add ax,[si+masiv]
add si,2 add ax,[si+masiv]
add si,4 add ax,[si+masiv]
mov ah,4ch int
21h end
7.8 . code_segment SEGMENT assume cs:code_segment a1: call subroutine a2: inc ax a3: call subroutine a4: inc bx mov ah,4ch int 21h subroutine proc
near mov ax,0 mov bx,0
ret subroutine endp code_segment
ends end
7.9 , ASCII . . .model small .stack .code start: print_ascii proc
mov dl,00h mov cx,256
print_loop: call write_char inc dl
loop print_loop
mov ah,4Ch int 21h
print_ascii endp
write_char proc mov ah,02h int 21h ret
write_char endp
end start
7.10 ( a z). .
.model small .stack .code print_a_z proc
mov dl,'a'
mov cx,26 print_loop:
call write_char
inc dl loop print_loop
139 140
mov ah,4Ch int 21h
print_a_z endp write_char proc
mov ah,02h int 21h ret
write_char endp end
7.11 TASM, , , . :
abcdf
fdcba
macro.inc
pokazvane_cifra_dl macro
push ax
push dx
add dl, 30h ;ili 48d
mov ah, 02h int 21h
pop dx
pop ax endm exit macro
mov ah, 4ch
int 21h endm pokazvane_znak_dl macro nomer_znak
push ax
push dx
mov dl, nomer_znak
mov ah, 02h int 21h
pop dx
pop ax endm eho_al macro
push bx ;za da se zapazi ah mov bh, ah ; mov ah, 01h int 21h mov ah, bh pop bx
endm pokazvane_dl macro
push ax mov ah, 02h int 21h pop ax
endm
pet_but.asm
include macro.inc .model small .stack 100h .data .code start:
mov cx, 5 povtori1:
mov ah, 01h int 21h mov ah, 0h
push ax loop povtori1
141 142
pokazvane_znak_dl 10d
pokazvane_znak_dl 13d
mov cx, 5 povtori2:
pop dx mov ah, 02h int 21h
loop povtori2 exit end start
7.12 TASM, ASCII . ESC. .model small .stack 100h .data scan_table dB 10 dup (0,1,2,3,4,5,6,7,8,9) scan_table_lenght equ 10
interval dB ' ', '$' new_line dB 13,10,'$' .code cool:
mov ax, @data mov es, ax mov di, offset scan_table mov cx, scan_table_lenght
mov ah, 01h int 21h call inter push ax xor ah, ah mov bx, 000Bh push bx ; border mov bx, 000Ah ; et:
cwd div bx ; ax=quotient dx=reminder push dx ; push reminder to the stack
repne scasb ; searching 0..9 ? je stop_div ; equal ax=scan_table (0..9) mov cx, 10d
jmp et stop_div: push ax start_output:
pop dx cmp dx, 000bh
je go_loop add dl, 30h
mov ah, 02h int 21h jmp start_output
go_loop: pop dx cmp dl, 1bh je end_prog call nl jmp cool end_prog:
mov ah, 4ch int 21h inter proc near push ax push ds push dx mov ax, @data mov ds, ax mov dx, offset interval mov ah, 09h int 21h pop dx pop ds pop ax ret inter endp nl proc near push ax push ds push dx mov ax, @data mov ds, ax mov dx, offset new_line
143 144
mov ah, 09h
int 21h pop dx
pop ds
pop ax
ret nl endp end cool
7.13 TASM, . include macro.inc .model small .stack 300h .data kvadrati db 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 .code mov ax, @data
mov ds, ax mov bx, offset
kvadrati povtori: mov cl,
10 eho_al and al, 0fh
pokazvane_znak_dl
32d xlat kvadrati xor ah,
ah div cl mov dl, al
pokazvane_cifra_dl mov
dl, ah
pokazvane_cifra_dl
pokazvane_znak_dl 10d
pokazvane_znak_dl 13d
jmp povtori exit
end
7.2
7.14 TASM, 57 89
. ESC 0 9 ESC. .
7.15 TASM, 75 863, 2 . .
7.16 TASM, 3 15 75 . ESC 0 9 ESC. .
7.17 TASM, 0 9 . 0 : nula, 1 -> edno, 2 -> dve, 3 -> tri, 4 -> chetiri, 5 -> pet, 6 -> shest, 7 -> sedem, 8 -> osem, 9 -> devet. ESC 0 9 ESC. .
7.18 TASM, 7 , . ESC. .
7.19 TASM, 6 10 25 , - 17. .
7.20 TASM, 6 23 77 , - 8. .
7.21 TASM, 7 11 76
145 146
, 130 850, 9. . . 7.29 TASM,
7.22 TASM, 7 5 10 98 130 850, , - 60 . . 7.30 TASM, . 75
7.23 TASM, 863, 2 . 0B 1111B 6. TASM, . 25 ESC 740, 5. 0,1,ESC. 7.31 TASM, . 230
7.24 TASM, 750, - . 15 a, c, d ESC . 0 9 ESC. ESC 7.32 TASM, , , d. 175 750 . ,
7.25 TASM, . 10 95 ESC 0 9 ESC. da ne 7.33 TASM, 5 3 3. 10 99, 7 . . 7.34 25 10 99.
7.26 TASM, TASM, . , . ESC 0 9 ESC 0 ESC. 9 ESC.
7.27 TASM, 7.35 TASM, 57 89 ASCII ( 255 0). . 7.36 TASM, ESC 0 H 9 ESC. 0B800 h, :
7.28 TASM, 0, 12, 73 + 7 ., .. + ..
147 148
7.37 TASM, MS DOS path tree
8
: regB, regW, regDW , , varB, varW, varDW, varQW () , , , , 48- strB, strW , reg var segreg const - : U r
ADD ADD , 04 ib ADD AL, imm8 05 iw ADD AX, imm16 05 id ADD EAX, imm32 80/0
ib ADD r/m8, imm8 81/0 iw
ADD r/m16, imm16 81/0 id
ADD r/m32, imm32 83/0 ib
ADD r/m16, imm8 83 /0 ib
ADD r/m32, imm8 149 150
00/r ADD r/m8, r8
01/r ADD r/m16, r16
01/r ADD r/m32, r32
02/r ADD r8, r/m8
03/r ADD r16, r/m16
03/r ADD r32, r/m32 . : = + . CF 1. : OF=r,