Sei sulla pagina 1di 318

2.

05
:

, :
.

, , , , , ,
. - ,
,
. "
. ©
. -
2015 " . " ©
http://www.cyber.org.il
12 ....................................................................................................................................

15 ............................................................................................................ 1–

18 .............................................................................................. –2

18 .......................................................................................................... ? –

19 .........................................................................................Pearls Before Swine 3 :

20 .................................................................................................................

21 .........................................................................................

21 .................................................................................................................

22 ............................................................................................................

25 .........................................................................................................................

25 ..............................................................................................................................

28 .................................................................................................................. ) (

30 .................................................................................................................... ) (

32 .................................................................................................................. ) (

33 ........................................................................................ ‫־‬

34 ................................................................................................................

34 ...................................................................................................................

35 ..............................................................................................................

36 ...........................................................................................................

39 .............................................................................................................. ?

41 ....................................................................................................................

41 ...................................................................................................................... Bit –

41 ................................................................................................................ Nibble –

42 ...................................................................................................................... Byte –

43 ................................................................................................................... Word –

43 ............................................................................................. Double Word –

43 ....................................................................................................................... ASCII

44 .................................................................................................................................
45 ................................................................................................................... –3

45 ..................................................................................................................................

46 ................................................................................ Von Neumann Machine –

48 ........................................................................................... SYSTEM BUSES –

49 ..................................................................................................... DATA BUS –

49 ............................................................................................. ADDRESS BUS –

50 .............................................................................................. CONTROL BUS –

50 ................................................................................................................................

53 ..........................................................................................................................

56 .................................................................................................... CPU –

56 .......................................................................................................... Registers –

57 ............................................................... General Purpose Registers –

61 .............................................................................. Segment Registers –

62 ............................................................... Special Purpose Registers –

62 .................................................................... Arithmetic & Logical Unit –

63 ............................................................................................... Control Unit –

63 ............................................................................................................. ) (

65 .................................................................................................................................

66 ................................................................................................ –4

66 ..................................................................................................................................

66 .......................................................................................................... Editor – Notepad++

67 ............................................................................................................... Base.asm

69 ................................................................................................................ Command Line

74 ............................................................................................................. TASM Assembler

75 ....................................................................................................... Turbo Debugger – TD

83 .................................................................................................................................

84 ................................................................................................................. IP, FLAGS – 5

84 ..................................................................................................................................

84 ..................................................................................................... IP – Instruction Pointer


87 ................................................................................ Processor Status Register – FLAGS

88 ................................................................................................... Zero Flag –

89 ........................................................................................... Overflow Flag –

90 .................................................................................................. Carry Flag –

91 ................................................................................................... Sign Flag –

91 ............................................................................................ Direction Flag –

92 .......................................................................................... Interrupt Flag –

92 ...............................................................................................Trace Flag –

92 ................................................................................................. Parity Flag –

92 ........................................................................................... Auxiliary Flag –

93 .................................................................................................................................

94 .................................................................................................. mov –6

94 ..................................................................................................................................

94 .......................................................................................................................

95 .............................................................................................................

97 ................................................................................................. Signed, Unsigned

99 ..............................................................................................

100 ..................................................................................................................

102 ....................................................................................................................... MOV

104 .....................................................................................................

105 ......................................................................................................

105 ..............................................................................................

106 ..............................................................................................

108 ........................................................................................................

108 ...............................................................................................

109 .................................................................................................

111 ............................................................................................ Little Endian, Big Endian

111 ..................................................................................................
112 .................................................................................................................. offset

113 .................................................................................................................... LEA

113 .............................................................................................. word ptr / byte ptr

114 ...................................................................................................... type override

115 ........................................................................................... - mov

116 ............................................................................................ ) (

117 ...............................................................................................................................

118 .................................................................................. , –7

118 ................................................................................................................................

118 .................................................................................................................

119 .................................................................................................................... ADD

120 .................................................................................................................... SUB

121 .......................................................................................................... INC / DEC

121 ....................................................................................................... MUL / IMUL

124 ............................................................................................................ DIV, IDIV

126 ................................................................................................................... NEG

127 .......................................................................................................................

128 .................................................................................................................... AND

130 ...................................................................................................................... OR

131 ................................................................................................................... XOR

133 .................................................................................................................... NOT

133 .........................................................................................................................

133 .................................................................................................................... SHL

134 .................................................................................................................... SHR

135 ......................................................................................................

136 ...............................................................................................................................

137 ................................................................................................................. –8

137 ................................................................................................................................

137 ........................................................................................................................ JMP


138 ....................................................................................................... FAR‫ ־‬NEAR

139 .............................................................................................................. LABELS

141 ....................................................................................................................... CMP

142 ..................................................................................................................

144 ................................................................................................ ) (

145 ..................................................................................................................... LOOP

146 ..................................................................................................................

147 ............................................................................ ) ( Nested Loops

150 .................................................................................................................

151 ...............................................................................................................................

152 ......................................................................................................... –9

152 ................................................................................................................................

154 ................................................................................................................ STACK

154 ..................................................................................................................

156 ................................................................................................................. PUSH

158 .................................................................................................................... POP

160 ..........................................................................................................................

160 ............................................................................................................

162 ........................................................................................................ CALL, RET

165 .................................................................................................... NEAR, FAR

167 ........................................................................................

170 ..................................................................................................

173 ..................................................................................................

173 ........................................................................................................ Pass by Value

176 ................................................................................................. Pass by Reference

178 ........................................................................................................... BP

183 ........................................................... ) (

186 ...................................................................................

188 ................................................................................. ) ( Stack Overflow -


194 ............................................................................................) ( Calling Conventions

196 ...............................................................................................................

197 ...............................................................................................................................

198 .................................................................................... ) ( CodeGuru Extreme – 10

198 ................................................................................................................................

199 .........................................................................................................

201 ..................................................................................................... Reverse Engineering

202 ................................................................................................................... duck.com

203 ................................................................................................................. coffee.com

205 ............................................................................................................ codeguru.com

207 ....................................................................................... Make it – Break it – Fix it :

210 ...............................................................................................................................

211 ...................................................................................................................... – 11

211 ................................................................................................................................

213 ..................................................................................................................

214 ............................................................................................................. ) ( IVT‫ ־‬ISR

216 ...................................................................................................................... DOS

217 .............................................................................................. AH=1h –

219 ................................................................................................... AH=2h –

222 ............................................................................................. AH=9h –

223 ........................................................................................... AH=0Ah –

227 ............................................................................................... AH=4Ch –

227 ........................................................ ) ( AH=2Ch ,AH=2Dh – /

232 .................................................................................................. Exceptions –

232 .......................................................................................................... Traps –

233 ............................................................................................................. ) ( ISR

239 ...............................................................................................................................

240 .................................................................................................. ) ( – 12

240 ................................................................................................................................
240 ..................................................................................................... Interrupts –

243 ..................................................................................................... PIC –

244 ...........................................................................................................

245 ..................................................................................................I/O Ports – /

247 .............................................................................................................................

247 ..........................................................................................................................

248 ...................................................................................... Scan Codes

249 ..................................................................................... Type Ahead Buffer

250 ...................................................................................................

255 ......................................................................................................... BIOS

256 .......................................................................................................... DOS

259 ...............................................................................................................................

260 .......................................................................................................... – 13

260 ...............................................................................................................

260 ................................................................................................................

262 ....................................................................................................................

262 .....................................................................................................................

263 ...................................................................................................................

263 ....................................................................................................................

264 .....................................................................................................................

265 .......................................................................................................

265 ................................................................................................. filewrt.txt –

268 ..............................................................................................................................

269 ....................................................................................................... Text Mode‫־‬

269 .................................................................................................. ASCII

273 ................................................................................................. Graphic Mode‫־‬

274 .........................................................................................................

276 ........................................................................................

277 ..............................................................................................
278 ............................................................................................... BMP

285 .............................................................................................

286 .....................................................................................................................

290 .................................................................................................................................

290 .......................................................................................................................

293 ........................................................................ Random Numbers –

298 ......................................................................................................................

298 .......................................................................................................

298 .........................................................................................................

302 ............................................................................................................... Debug

302 ............................................................................................................................

302 ........................................................................................... –

304 .............................................................................................................

305 ...............................................................................................................

305 ..................................................................................................................

306 ...................................................................................................

307 ...............................................................................................................................

308 .............................................................................. –'

312 ................................................................. : –'

317 ..................................................................................................... –
12

. ,
.

,
, .
. , , .
." "

. - ,
- ,TASM
. ,
. , ,

1 -
.

. 2 -

, . 3 -
.

. , 4 -

. - 5 -

, , 6 -
.

. ,)' , ( 7 -

, . 8 -
.

, , 9 -
. .

, 10 -
. ," " Reverse Engineering

DOS . 11 -
.

12
13

. , 12 -
, .
.

, - 13 -
. –

2.0

, 25- , "
: . ,

. reverse engineering -

.calling conventions, stack overflow - -

. : , -

) ( -
.

,stackoverflow . " "


, . -
. . ,

. Randall Hyde Art of assembly


,) ,TASM NASM(
. .

" " 5 "


. .

.
. ,

, .
, : .
, , . , ,
.

13
14

: ,

14
15 –1

1–
. , !
. . , ?

, ,
, , .
. ?1- 0 ? .

.
. ,

,"10111000" , , ,
."10111000" ,mov ,
.

. Low Level Language


. , –
.' , , -

.High Level Languages ,


C++, Java . " "
. .Python-

15
16 –1

: .

. 

. 

. 

. ) ( 

. , . 


.

, . ,
, .
: ,

. 
, " "
. ,

Reverse , . 
.Stack Overflow Engineering
. , ,

, . 
, Apple , . ,
.

. 

– .
: , ‫־‬

Cyber SecurityResearcher
 Familiarity with programming languages (e.g. C++, Java, C#, PHP, Assembly, etc.)

 Knowledge of networking and internet protocols (e.g. TCP/IP, DNS, SMTP, HTTP)

 Reverse engineering experience – a must.

 Analysis of malicious code – Major advantage

16
17 –1

. ,

. , ,
. ,
.

. ,
. .

. ,

, ,
. – .

17
18 –2

–2
? –
, )Decimal , (
– , .
. )Hexdecimal , (
, . ,
. )Binary , (

, ,
, , , .
.

,
,
. ." "

, .
.3 8 ,10 -

9 .9 0- ,10 
.

7 .7 0- 8 
.

2 .2 0- ,3 
.

18
19 –2

? .

22 3 10 ,8 . 8 ,10
. , .

Pearls Before Swine 3 :

. , . ,

www.transience.com.au/pearl3.html :

! . ," "

! ." "

, , ...
.

19
20 –2

,
.0,1,2,3,4,5,6,7,8,9 : .
501 .105 501 , . .
. 5 105 , 5

: , , –

4710

: .

4710 ≠ 478

: ,

4710 = 7*100+4*101

37510 = 5*100+7*101+3*102

199410 = 4*100+9*101+9*102+1*103

20
21 –2

:5 19910

.4 39 .199:5

.4 ,7 .39:5

.2 ,1 7:5

.1 ,0 1:5

.1244 - ,

.19910 = 12445-

: 12445 - - ,

12445= 4*50 + 4*51 + 2*52+ 1*53 =4*1 + 4*5 + 2*25 + 1*125 = 4+20+50+125 = 19910

2 .0,1: , ,
, . -
.2 .

,20 .2 ,
,2n-1 n-
:

21
22 –2

. .100112
: ,

100112 = 1 + 2 + 16 = 1910

"
:19 .2 ,"

.100112 ,

. , , 16
B ,10 A . ,) 9 0- (
: . 11

.1A16, 2B16, 4C416 C1A16, F1516 . ,


.C0DE16 C0FFEE16, DEAD16

. o ,0

22
23 –2

C0DE16 – 16 -

. '0' , , . h -
- , ( 0C0DEh
)

0xC0DE – 0x -

: ,4F16 , ,

4F16 = F*160 + 4*161 = 15 + 64 = 7910

:19910 , :

.7 ,12 .199:16

.'C' 16 ,)10 ( 12 0 ,12:16

19910 = C716

.2 16‫־‬ , .
:

23
24 –2

, . ‫־‬
. , ‫־‬
:9B16

916=10012

B16=10112

9B16 = 100110112

. , ,
,
, – ,9B2C16 .
? ?1001101100101100

:2.1

,2 . ,10 –
. ,16 .

24
25 –2

. – , , –

.70‫ ־‬133 .

.
–carry " "( , 10‫־‬
. )

,
. , .2‫־‬

.0 0 0

.1 1 0

.1 0 1

.) ( 10 1 1

25
26 –2

:)3 10– ( 11 1010–

. ,1 1 0 .

. ,0 1 1

. ,1 ,0 0 ,1 ,

. ,1 0 1 ,

16‫־‬ ,
:12316 ABCD16 , .

.1 0 ,16 3 ) 13(D .

. ,F 2 C

. ,C 1 B

.A‫־‬ 0 A

26
27 –2

:2.2

27
‫‪28‬‬ ‫‪–2‬‬

‫)‬ ‫(‬

‫‪:‬‬ ‫‪,‬‬

‫‪.‬‬ ‫‪.6‬‬ ‫‪9‬‬ ‫‪.‬‬ ‫‪.1‬‬

‫‪.‬‬ ‫‪,‬‬ ‫‪.2‬‬

‫‪,‬‬ ‫"‪.‬‬ ‫"‬ ‫‪.9‬‬ ‫‪6‬‬ ‫‪.‬‬ ‫‪.3‬‬


‫‪.‬‬

‫‪:21‬‬ ‫‪619 .‬‬ ‫‪,‬‬

‫‪.8‬‬ ‫‪1‬‬ ‫‪9‬‬

‫‪,‬‬ ‫‪2‬‬ ‫‪1‬‬


‫‪.9‬‬ ‫‪,2‬‬ ‫‪11‬‬ ‫‪.‬‬

‫‪.5‬‬ ‫־‪,6‬‬

‫‪.)1‬‬ ‫‪10( 1‬‬ ‫‪1010‬‬ ‫‪2‬‬

‫‪,‬‬ ‫‪.‬‬ ‫‪1‬‬ ‫‪0‬‬ ‫‪.‬‬


‫‪.1‬‬ ‫‪,1‬‬ ‫‪2.‬‬ ‫‪)2‬‬ ‫(‪10‬‬

‫‪.0‬‬ ‫‪1.‬‬ ‫‪,‬‬

‫‪.‬‬ ‫‪,‬‬

‫‪.‬‬ ‫‪–1001‬‬ ‫‪2‬‬ ‫‪,9‬‬ ‫‪1‬‬ ‫‪10 :‬‬

‫‪28‬‬
29 –2

:16

.D‫־‬ A‫־‬ . E D
.)15 ,14 16 13– (F

. , .D A‫־‬ ,
.)12 ,13 16 ,1 10– (C

.D ,E‫־‬ ,

.1 C D

:2.3

29
30 –2

) (

, .
2X2 ,10X10
:

:) 3 10( 11 1010

30
31 –2

. ,1010 1010 1

. ,1010 1010 1–

.)2+4+8+16=30– ( 11110

, ,10 :2 –
.52x10=520 , .0 ,

.52x100=5200 :10 ,

, . ,2
.0

.11x10=110 : ,

.11x100=1100 :

.11x1000=11000 :

– . 256 , 16X16 16 :
, . .
. 101 1100 ,5 C

:2.4

31
‫‪32‬‬ ‫‪–2‬‬

‫)‬ ‫(‬

‫‪:‬‬

‫‪1/1 = 1‬‬

‫‪0/1 = 0‬‬

‫‪.‬‬ ‫‪,‬‬
‫‪.‬‬ ‫‪,‬‬
‫‪.‬‬

‫–‪:101102/1012‬‬

‫‪.10‬‬ ‫‪,100‬‬

‫‪.2‬‬ ‫‪,4‬‬ ‫־‪,5‬‬ ‫–‪22‬‬

‫‪:2.5‬‬

‫‪32‬‬
33 –2

‫־‬
.)Bit( )1 0( . ,
, .) Binary digit(
, .
. 64 32 ,16 ,8 .
.

, ? . N
:8‫ ־‬1 ‫־‬

‫פ‬ ‫פ‬ ‫פ‬ N-


1 1 1
3 11 2
7 111 3
15 1111 4
31 11111 5
63 111111 6
127 1111111 7
255 11111111 8

N ‫־‬ , : ,
.2N-1

, ? N ‫־‬
? 8 ,255+1

. , ! 8 ‫־‬
. ,11111111 , :
. .) ( ,
. 0=255+1 , 8 , , .00000000
. ,

33
34 –2

0001 , 0000 .
, . –unsigned . 0010 ,
?

.signed –

, . , N
: .

. –1 . –0
,unsigned ,
.

? , .0011 :

0011 , .3 ,011 . ,0
.3

.3 ,011 . ,1 ,1011
.3 1011 ,

,1111 ,7 ,0111 4 ‫־‬ ,


.7

! 1000 0000 -

34
35 –2

, .
.

:(-3) 3 .

, .(-6) ,
.

, )One's complement(
.

1 ,1‫־‬ 0 . - ,
.1110– ‫־‬ "-1" ."1" 0001 , .0‫־‬

: 7 0‫־‬

1 0 . -
.0 ,1111 , .
.

: , ,
. ,

35
‫‪36‬‬ ‫‪–2‬‬

‫‪:‬‬ ‫)‪,(-2‬‬ ‫‪5‬‬ ‫‪.2‬‬ ‫‪5‬‬ ‫‪,‬‬

‫‪.3‬‬ ‫–‬ ‫‪,‬‬ ‫‪,‬‬

‫)‬ ‫(‬ ‫‪,‬‬


‫–‬ ‫‪,‬‬ ‫‪.‬‬
‫‪,‬‬ ‫‪1111=0000 -‬‬ ‫‪.‬‬
‫‪.‬‬

‫‪.‬‬ ‫‪ 0‬־‪1‬‬
‫‪,‬‬ ‫(‬ ‫‪1‬‬ ‫–‬
‫)‪.‬‬

‫‪.‬‬ ‫‪,‬‬

‫‪:‬‬ ‫‪8‬‬ ‫־‬ ‫‪,6‬‬ ‫‪,‬‬

‫‪.00000110‬‬ ‫‪6‬‬

‫‪.11111001‬‬

‫‪.‬‬ ‫‪8‬‬ ‫־‬ ‫‪,‬‬ ‫‪6‬‬ ‫‪.11111010‬‬ ‫‪,1‬‬


‫– ‪1111 1111 1111‬‬ ‫‪,‬‬ ‫‪16‬‬ ‫־‬ ‫‪6‬‬
‫‪,‬‬ ‫‪,‬‬ ‫‪( 1010‬‬
‫‪.‬‬ ‫‪32‬‬ ‫־‬ ‫‪6‬‬ ‫)‪.‬‬

‫‪36‬‬
37 –2

. ,6 6.

.) 1‫– ־‬ ( ,

, 8‫־‬ , .
. 4‫־‬

, . .00000000– ,
!8

.(-2N-1) (2N-1-1) N ,
16 .128 127 8 ,
.32,768 32,767

, –
.

37
‫‪38‬‬ ‫‪–2‬‬

‫(‬ ‫‪,‬‬
‫‪:‬‬ ‫‪.‬‬ ‫)‬

‫‪,‬‬ ‫'‬ ‫‪64 ,32 ,16 ,8‬‬ ‫‪,‬‬ ‫‪-‬‬


‫‪.‬‬

‫‪-‬‬ ‫‪)0‬‬ ‫(‬ ‫‪-‬‬


‫‪.2‬‬ ‫‪,‬‬

‫־‪,2‬‬ ‫‪,)1‬‬ ‫(‬ ‫‪-‬‬


‫‪.‬‬ ‫‪,‬‬

‫־‪:2‬‬ ‫‪,1‬‬ ‫‪.10111111‬‬ ‫‪,‬‬

‫‪:‬‬

‫‪20+26 =65‬‬

‫‪)-65) :‬‬ ‫–‬

‫‪:2.6‬‬

‫‪.‬‬ ‫‪2-‬‬
‫‪:‬‬ ‫‪. 2-‬‬ ‫‪8‬‬
‫‪2‬‬ ‫‪00001100‬‬ ‫‪12‬‬ ‫‪.‬‬

‫‪-9‬‬ ‫‪247‬‬

‫‪-128‬‬ ‫‪128‬‬

‫‪-94‬‬ ‫‪162‬‬

‫‪-102‬‬ ‫‪154‬‬

‫‪-1‬‬ ‫‪255‬‬

‫?‬ ‫‪,‬‬

‫‪38‬‬
39 –2

?
? Pearls3 ...

( ,Nim Pearls3
. Nim-Sum .)https://en.wikipedia.org/wiki/Nim

, . ,
. , ,

: ,

.) 111 - 7 ( " " .1

0 1+1 ( - .2
)10

, . , - )Nim Sum ( .3
.

. 5- 4 ,3 : 3

:) ,15 , (

)3( 0011

)4( 0100

)5( 0101

: , .

0010

? – - .

: , 2 ...

39
40 –2

)1( 0001

)4( 0100

)5( 0101

0000

:2.7

. - !

40
41 –2

, ,
, – .
. , .

Bit –

. .
340 , , , , , .
, ‫־‬ .519
.

, . , ? , ,
, , . , ‫־‬
.

2 1100 . ,
.) ( 4 ,12‫־‬
.unsigned signed –

. , ,
, . , ,2
. –

, . ,
.

Nibble–

. 16 , 4 2 Nibble . Nibble
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F Nibble ,
. ,2 , –
. . 16

: –

11011110101011011100000011011110

41
42 –2

11011110101011011100000011011110

D E A D C 0 D E

? ...

Byte–

– , )Byte(
. ,
‫־‬ , , , .
. ) , (

:Nibbles

42
43 –2

Word –

,16 . , 16 )Word(
, , 16 . ,65,536
.

Double Word – ‫פ‬

, , 32 )DWORD Double Word(


,32 2 Double Word- .
-2,147,482,648 Signed ,4,294,967,295 0 Unsigned
.2,147,482,647

ASCII

,ASCII ,American Standard Code for Information Interchange


, 128 7- .
.127- 0

43
44 –2

: - ASCII ”HELLO WORLD!“ ,

726976767932877982766833

H E L L O W O R L D!

48454C 4C 4F 20 57 4F 52 4C 44 21

: ) ( "

.
, , , :
.

. - , , -

, : .
. ,

. ,
,
.

44
‫‪45‬‬ ‫‪–3‬‬

‫‪–3‬‬

‫‪.‬‬
‫‪,‬‬ ‫‪.‬‬ ‫‪,‬‬
‫‪.‬‬ ‫‪,‬‬ ‫בס ס ת‪,‬‬
‫‪.‬‬ ‫‪,‬‬
‫‪.‬‬ ‫ב‬

‫‪.‬‬ ‫‪80x86‬‬
‫‪,‬‬ ‫‪.‬‬ ‫־‪X‬‬ ‫‪,‬‬ ‫־‪80x86‬‬
‫‪.‬‬ ‫‪,‬‬ ‫‪.1978‬‬ ‫‪,8086‬‬

‫?‬ ‫–‬ ‫‪,‬‬


‫?‬ ‫‪.‬‬

‫‪:‬‬ ‫‪,‬‬

‫‪.‬‬ ‫‪,‬‬ ‫־‪80x86‬‬ ‫‪‬‬


‫)‪.‬‬ ‫(‬

‫‪,‬‬ ‫־‪.80x86‬‬ ‫‪‬‬


‫‪.‬‬ ‫‪,8086‬‬

‫‪.‬‬ ‫־‪80x86‬‬ ‫‪,‬‬ ‫‪‬‬

‫‪45‬‬
46 –3

Von Neumann Machine–


, ' .
Von ( .
: )Neumann Architecture – VNA
.)I/O( / )Memory( ,)Central Processing Unit – CPU(

)1957–1903( John Von Neumann '

. ,80x86‫־‬ ,VNA
, . ‫־‬ ) (
. , /
.) ( / , /

46
47 –3

,Bus /
. , .

,VNA‫־‬

47
48 –3

. .) " 33 , ( 8086‫־‬
I/O- .
.

SYSTEM BUSES –
80x86- .VNA )System Buses(
.CONTROL BUS– ADDRESS BUS– ,DATA BUS– :
– .
.1‫־‬ 0‫־‬ , .1 0

? , , ?

? , ." 1000h"
.

48
49 –3

DATA BUS–

.
. 64 32 ,16 .

16 . 16 16
16‫־‬ . 16
.

, – , 64 32 ,16‫־‬
, , .
, .
.

ADDRESS BUS–

, – . )I/O (
, . ?
.) , (

, I/O ,
.
. " ,

, .
, n .00,01,10,11 :
( 1,048,576 . 20 , ,8086‫־‬ . n
. , .)
, . ' – 4,294,976,296‫־‬ , 32
Windows 7 , '
. 64

49
50 –3

CONTROL BUS–

.
, ,
?

. ,)write( )read( ,
, read‫־‬ . ,1 write‫־‬ read‫־‬
. , write‫־‬ .

. , 64 32 ,16
. byte enable

.I/O- 80x86‫־‬
I/O‫־‬ ,80x86‫־‬
. 65,536‫־‬ . 16
.I/O‫־‬ ,

. n ,2n ,
.

.)2n-1) ,0 .
. 1,048,576 , 20 ,

: ,"0" 1976‫־‬ ,

. "0" .1

. 1976 .2

.)" " ( 0- write‫־‬ .3

50
51 –3

: , 1976‫־‬

. 1976 .1

.)" " ( 0- read‫־‬ .4

. .2

51
52 –3

.0ABh ,Byte , 8 1970 .1

.0EEFFh ,word , 16 1974 .2

.12345678h ,double word , 32 1976 .3

. , –
. h
. ah‫־‬ ,ah‫־‬ ,

: ,

.0ABh 1970 .1

.0EEh 1975 ,0FFh 1974 .2

1979 34h 1978 ,56h 1977 ,78h 1976 .3


.12h

. H.O‫־‬ , L.O‫־‬ –

L.O. Byte, H.O. Byte–

52
53 –3

: , .

.0EEh ,1975 Byte .1

.1234h ,1978 Word .2

.05678EEFFh ,1974 Double word .3

–1971 1970 byte‫־‬ ,1970 Word .4


! ,

, , . 220 8086‫־‬ ,
16 .)registers( , 16
, .) 0FFFFh ( 65,535 0‫־‬
. , ,
)offset( , segment- - .)segments( –
: .

Segment:offset

53
54 –3

: 16 ‫־‬ ,80x86‫־‬
. .

, 16 , .
0000h , 216 ,64K
,64K 8086- .0FFFFh
.

.16‫־‬ ,
. )2*16( 32 0002h , . 16
.) 272 16 17 ,17 16 11( 272 0011h

3DD6h , . ,
3DD6h:0012h– .12h

3DD60h +0012h = 3DD72h

,16‫־‬ 0‫־‬
.

. ,
codeview- . , .dosbox codeview
.

, . ,
.0627:000C , 13 .0627:0000
.0627:000D :

54
55 –3

.' 0,1,2– , .
.
ASCII .ASCII
.

. ‫־‬ .
.0627:0001

. ASCII-

. ,
: 16 .0628:0000

. 16 ,

55
56 –3

CPU–
.
,)Registers( . –
, ,)Arithmetic & Logical Unit(
, ,)Control Unit( , ,
. ,

Registers–

, . , ,
– ‫־‬ .
.zero wait . ,
. , ,

" " " "

.80x86‫־‬
. 16 .

56
57 –3

General Purpose Registers–

: ,

.
Accumulator
, ax
register
. ax‫־‬

. Base address bx
. register

. cx
Count register
. ,

, :
, . Data register dx
. dx ,I/O

Source Index si
(
.)bx
.
Destination di
Index

.Stack " " Base Pointer bp

. sp
sp‫־‬ , sp
Stack Pointer
.
. sp

57
58 –3

. 8‫־‬ . , 16 "
– , ax . ,
,
.

–dx‫ ־‬cx ,bx ,ax– ,


,ax 8 ,high H .al‫־‬ ah‫־‬ , ax .
.ax 8 ,low L

)move ( mov –
: .

mov ax, 0FF00h

: ax ,

: ‫־‬

mov ah, 0FFh


mov al, 0

. ,ax al . 8

. dl, dh ,cl ,ch ,bl ,bh ,

: 8

58
59 –3

:21 17 .

mov ax, 1234h

mov bx, 0

mov bl, 34h

mov cx, 0

mov ch, 12h

. "

59
60 –3

.bx 8 bl :bx bl -

.cx 8 ch :cx ch -

16 ,di‫ ־‬sp, bp, si ,


.dx‫ ־‬cx ,bx ,ax

. ,
. .
.

60
61 –3

Segment Registers –

:)FS - 80x86- ( 8086-

CS- Code Segment

DS- Data Segment

SS- Stack Segment

ES- Extra Segment

SegmentRegister . . 16
.

. ‫־‬ . cs
, ?64K .64K ,
( , cs- ,
model small base.asm .
. .)

ds- ,ds
small , . ,DATA‫־‬
ds DATA , 64K‫־‬
.

61
62 –3

. )STACK( ss
ss – sp .
.

, – ,es, Extra Segment


. , , .

Special Purpose Registers –

IP- Instruction Pointer-

FLAGS -

)pointer( 16 . IP‫־‬
.code segment-

, 16 8 .
. .
. ,

- , . ,
, . ‫־‬ IP
. ,

, . ,
.

Arithmetic & Logical Unit –

)ALU – ,Arithmetic & Logical Unit(


, ALU‫ ־‬.
ALU‫־‬ , ALU‫־‬
.

:ax 3 ,

.ALU‫־‬ ax‫־‬ 

.3 ALU‫־‬ 

. " ALU- 

.ax ALU- 

62
63 –3

Control Unit–

? –

. ,
, .
.
.
.

Operational , ,Control Unit ,


. ,OpCodes Codes

. 8 OpCode , OpCodes‫־‬
IP‫ ־‬. ,IP ,Instruction register ,
IP‫־‬ , OpCode- .
.

) (

. ,

:bx+5 ax ,

mov ax, bx

add ax, 5

. ,ax bx
.bx+5 ,5 bx ax , ,

, ,) ( ,OpCode‫־‬ .
. – , .

0 ,Control Bus
. 1‫ ־‬0 .1‫־‬
, . clock cycle .clock cycle 0‫־‬ 1‫ ־‬0‫־‬
.) 1/1,000,000( 1 clock cycle 1MHz

63
64 –3

0‫־‬ ( ,
.)0‫ ־‬1‫־‬ ( )1‫־‬
, clock cycle‫־‬ ,
, .
. clock cycle‫־‬ ,

. .
clock . ‫־‬
:clock cycles , , cycle

64
65 –3

:80x86‫־‬ ,
, 80x86‫־‬
. VNA .VNA
. , ,

, 80x86‫־‬ ,
. , .
, , , .
– .
. ,

. ,
– ,
. .

65
66 –4

–4

, .

–) ( . ,

, . ,

. ,

,base.asm ,

, .)Debugger(

.Opcodes– .

.Opcodes‫־‬

Editor– Notepad++
. . -

. ,Word , " "

Notepad++‫־‬ . Editor Visual Studio ,Notepad‫־‬ .

.' , , – -
.

, Notepad++ :

.Notepad++ installer http://notepad-plus-plus.org/download/v6.5.5.html

66
67 –4

hello .hello hello.doc, doc . -


.

Notepad++‫־‬ , .asm -
.

.DOS . 8 asm‫־‬ -

Base.asm

. .
, . – ,
, ( Notepad++‫־‬
.base.asm , .)

http://data.cyber.org.il/assembly/TASM/BIN/base.asm

base.asm

, .base.asm
, . ,
– –
.

67
68 –4

.DATASEG :
DATASEG .
. DATASEG . .

: ,DATASEG‫־‬

var1 db 5

, ) ( .5 var1
.5 var1

.CODESEG
– – , .
.

: ,CODESEG‫־‬

mov al, [var1]

.al , 5 ,var1‫־‬

.base.asm

IDEAL . , Turbo Assembler –IDEAL -


.

Data, Code, – ,small –MODEL small -


. 64K Stack

. . –STACK 100h -

. –DATASEG -

. –CODESEG -

- . –start -
. end‫־‬ -“main”

@data . ds‫־‬ , –mov ax, @data -


- ds mov ds, ax .data‫־‬
.data

68
69 –4

exit . –exit -
. ,
.DOS

, end –end start -


end .
, .exe‫־‬ , .
.start

Command Line
Command ‫ ־‬.Command Line ‫־‬ , ,
, , , Line
.

. Command Line
, asm , .
, .Command Line‫־‬
64 ‫־‬ , ( 64‫־‬ .
.80x86 20 - )
. – , ,
Command - .Command Line‫־‬ ,
Command >---- >---- ,7 . Line
.Start menu- cmd .Prompt

69
70 –4

.Command Line-
. http://www.computerhope.com/msdos.htm

Change Directory –CD 

CD DirectoryName
: ."DirectoryName"

CD Games
:
CD..

:
CD\
:

70
71 –4

DIR 

.
: page‫־‬ p .dir /p ,

EXIT 

.enter exit , Command Line‫־‬

DOSBOX 

. 80x86‫־‬
, . . )Emulator(
. , ,

,Commodore 64
,1982
Commodore 64‫־‬

71
72 –4

,DOS . ,
DOS .1994 ,Disk Operating System
"Dosboxdownload" .0.74 Dosbox
.

data.cyber.org.il/assembly/dosbox : 0.74

.Z .
, . Intro
,C .mount
: ,Assembly

Mount c: c:\

: .Drive c:\ is mounted as local directory c-

C:

.C

. Command Line-

- ,
.

,
.) ( 3000cycles
:DosBox-

Cycles = max

72
73 –4

(
CPU .)3000cycles‫־‬ ,
.speed: max 100% cycles

:Dosbox
, - .
. ,

- dosbox-0.74.conf
: DOSBox 0.74 Options-

: , .

[autoexec[

# Lines in this section will be run at startup.

# You can put your MOUNT lines here.

: .

mount c: c:\

c:

cd tasm

cd bin

cycles = max

73
74 –4

TASM Assembler
. Assembler
.Turbo Assembler ,TASM ,
.1996 5.0 TASM

. Linker
. ,
, , .
. .

, . TASM
.

data.cyber.org.il/assembly/TASM.rar : tasm.rar

.c:\tasm\bin -

"rar download" ,rar ( rar‫־‬ -


.)

.bin base.asm -

.) ,cd ( bin‫־‬ Dosbox‫־‬ -

: base.asm -

tasm /zi base.asm

.base.obj .debug zi -

: - tilnk – -

tlink /v base.obj

.base.exe .debug v

74
75 –4

td base ,enter base ,


.

.Turbo Debugger base.exe ,

Turbo Debugger–TD
: ,

td base

view ,)F10 ( ,
:CPU ,

75
76 –4

Turbo Debugger‫־‬

.Help‫־‬ ,TD ,Turbo Debugger


:TD

,F8 ‫־‬ ,Step . –


. , .
, , .F7 ‫־‬ ,Trace ,
.

. TD base.exe –

. –Go to cursor ,F4


. F9

: Views‫ ־‬. –View

:DATA‫־‬ , ,CPU‫־‬ -

76
77 –4

: -

‫־‬

: , –STACK‫־‬ ,STACK‫־‬ -

.watch –Watches -

:DATASEG‫־‬ , .

77
78 –4

Var1 dw 1234h

Var2 dw 2345h

: var1+var2 Watch

: . – Variables -

78
79 –4

: CTRL+G . ,

:) ,ds:0 , (

: " "

,DATASEG ds
F8 , . ds
.

79
80 –4

breakpoints‫־‬

, . breakpoint ,
. .Toggle- Breakpoints
.

Run (F9) ,breakpoint

. .breakpoint

mov ax, 4C00 breakpoint . base.exe

. F9-

TD

ALT+X :Dosbox- -

CTRL+F2 : -

. F10 :) ( -

. Tab

ALT+Enter :) ( -

)Ctrl - Alt (" " : , -

80
81 –4

Opcodes‫־‬ –) (

. mov

. mov , ,

. . mov

, " mov ax, dx Opcode‫־‬

"Your code . base.asm .1


.here"

mov ds, ax

.TurboDebugger , .2

mov ax, 2 , . .3
.B80200

81
82 –4

. mov ?mov .1
:

mov ax,5

mov ax, bx

mov [120], ax

: ,Opcode‫־‬ .2

mov ax, 5

mov ax, 6

:Opcode‫־‬ .3

mov ax, 2

mov bx, 2

mov cx, 2

mov dx, 2

: Opcode- .4

mov bx, ax

mov ax, cx

. .mov ax, dx :
‫־‬ ,
.

82
83 –4

. ,
: . ,

Editor Notepad++ -

DOS ,Command line -

DOSBOX -

Turbo Assembly -

Tlink -

Turbo Debugger -

. .
.

83
84 IP, FLAGS – 5

IP, FLAGS – 5

. – IP –

,) (
.
,Special Purpose Registers , ,
.FLAGS IP
IP- Instruction Pointer -

FLAGS–

IP‫־‬ :
. FLAGS‫־‬ .

,
.

IP– Instruction Pointer


)pointer( 16 . IP‫־‬
.code segment‫־‬

:) ( base.asm ,
IDEAL
MODEL small
STACK 100h
DATASEG
CODESEG
start:
mov ax, @data
mov ds, ax
mov ax, 1234h
mov bx, 0
mov bl, 34h
mov cx, 0
mov ch, 12h
exit:

84
85 IP, FLAGS – 5

mov ax, 4c00h


int 21h
END start

IP ,mov ax, 1234h, IP=0005 : IP‫־‬


.

: ,F8

.) mov bx, 0 ( , , .1

85
86 IP, FLAGS – 5

. ax , mov ax, 1234h .2

. ,0008‫ ־‬0005‫־‬ IP .3

.IP‫־‬

. mov ax, 1234h ,


, . IP –
. , IP

5.1

. IP .
? IP

, IP .IP=0005h , .
. )enter (
:

. IP‫־‬ enter ,0010h‫ ־‬IP .


0010 0005 ? .F8 ‫־‬
?) cx ,bx ,ax (

86
87 IP, FLAGS – 5

Processor Status Register – FLAGS


FLAGS .FLAGS ,Processor Status Register
, , 16 8 .
. , 16 FLAGS- .

:FLAGS-

:Condition Codes– ,

Zero Flag -

Overflow Flag -

Carry Flag -

Sign Flag -

,
. .)" ... " (

87
88 IP, FLAGS – 5

Zero Flag–

: ( .0 , . 1
.mov :
.) mov ,

. - Destination Operand -
.

: ( ah al 4Bh :
. )subtract( .)?
:

mov al, 4Bh ; 75 decimal

mov ah, 4Bh ; 75 decimal

sub al, ah ; subtract al minus ah, result is 0

'z'

mov al, 0FFh ; 255 decimal

mov ah, 01h ; 1 decimal

add al, ah ; add al and ah, result is 256

,al‫־‬ ,100h .
.1 ,00h " " ,

88
89 IP, FLAGS – 5

" " .
‫־‬ .1 , .
. ,

5.2

, . 16 ,
. 16 ,

Overflow Flag–
." " ,1 , " "
.signed ,
, 8 ,
)-128( ,+127 (-128) signed
.0 , .+127

: 8 ,al

mov al, 64h ; 100 decimal

mov ah, 28h ; 40 decimal

add al, ah ; result is 140, out of 8 bit signed range

'o'

? , 16 ) ( –

.)-32,768( ,)-215( 16 signed :


.+32,767 ,(2 -1( 15

89
90 IP, FLAGS – 5

5.3

. ,base.asm

Carry Flag–
, ,Carry Flag , ,
, .
, 8 , .unsigned
,+255 0- unsigned
.+65,535 0‫־‬ 16 .+255 0

mov al, 0C8h ; 200 decimal

mov ah, 64h ; 100 decimal

add al, ah ; result is 300, out of 8 bit unsigned range

’ c‘

) ( ) (
. ) (

:
mov al, 1h

mov bl, 2h

sub al, bl

255 .255 unsigned ,0FFh al ,


. )256 " "(

90
91 IP, FLAGS – 5

: , ,

mov al, -128d

mov ah, 40d

add al, ah ; result is out of 8 bit unsigned range?

, . ,unsigned ,-88
.unsigned signed al -
,al -128 :al .unsigned ,
.unsigned al ,+128 .80h al
. ,+168 ,40 +128

5.4

. ,base.asm

Sign Flag–

.1 ) ( 1
:Signed , .0

.1 – -

: .F‫ ־‬8 nibble‫– ־‬ -


0F100h, ,) ( 0F1h, 0A3h, 088h
.) ( 0A300h, 08800h

5.5

. . base.asm

Direction Flag–

,0 .
. ,1 .

91
92 IP, FLAGS – 5

Interrupt Flag–

.)Interrupts( " "


, .
‫־‬ , . ,0‫־‬
.1‫־‬

Trace Flag–

, .Trace
.turbo debugger debugger .
. ,0‫־‬

Parity Flag–

. .
.1 ,)0,2,4,8( '1' ,
.

Auxiliary Flag–

.AL 4- 1 .
.0

5.6

mov ax, 0

mov bx, 8888h

sub ax, bx

92
93 IP, FLAGS – 5

5.7

? 5.6

) ( 5.8

, ,
.

) ( 5.9

, ,
.

. IP‫־‬ ,)Special Purpose Registers(


. IP

: ,

, IP‫־‬ ,
.

93
94 mov –6

mov –6

, , , -
. mov

. ,) ( -

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

, , .
. ,

. ,

: . - ,

mov al, [ds: 1h]

.)DS ( 1h ds al
,5 ds:1h .
. , . al 5
, .1h
. ,1h ,

.
.ds:1h age- ,age ds:1h ,
:

mov al, [age]

94
95 mov –6

– ,

. –

. )Variables(

. ,

,6 1

.12 2 ,

"start" –base.asm .DATA‫־‬


:ds

mov ax, @data

mov ds, ax

, , .DATA‫־‬ ds‫־‬
, .ds‫־‬
, , age , .ds
." " age

, .
.

: ,DATA‫־‬

ByteVarName db ?

95
96 mov –6

–DB( db . ByteVarName
,ByteVarName .)Define Byte
. .
: ,

mov al, [ByteVarName[

–" " , , al ;
.

: ,

DATASEG

ByteVarName db ?

ByteVar2 db ?

ByteVar3 db ?

.ds:0 ,ByteVarName , )0 (
,ByteVar2 , ByteVarName .
. ds:1

: ,

DATASEG

ByteVarName db ? ; allocate byte (8 bit) - DB: Define Byte

WordVarName dw ? ; allocate word (16 bit) - DW: Define Word

DoubleWordVarName dd ? ; allocate double word (32 bit) - DD:

; DefineDouble

96
97 mov –6

6.1

.byte ) ,variable ( var DATASEG‫־‬ .


.var '5' .CODESEG
var- . Turbo Debugger (TD)‫־‬ ,
.'5'

CODESEG
start:
mov ax, @data
mov ds, ax
mov [var], 5
exit:
mov ax, 4c00h
int 21h
END start

mov .DATASEG .
Turbo ‫־‬ , . ds, ax
? . Debugger (TD)

Signed, Unsigned

–unsigned signed
Bytes , .
.

: Byte .

DATASEG

Var1 db ?

Var2 db ?

: CODESEG

mov [Var1], -120

mov [Var2], 136

97
98 mov –6

! . DATASEG‫־‬
.88h

? ?136 120

.10001000: , , +136 )-120( ,


!

. ?
136 120 ,+120 ,
:

. ,120 120 -

, .100h ,256 ,136 120 -


. ,00h– ,

-unsigned signed ,
. .

98
99 mov –6

, . CODESEG‫־‬
.

,
. ,

DATASEG

ByteVarName1 db 200 ; store the value 200 (C8h)

ByteVarName2 db 10010011b ; store the bits 10010011 (93h)

ByteVarName3 db 10h ; store the value 16 (10h)

ByteVarName4 db 'B' ; store the ASCII code of the letter B (42h(

ByteVarName5 db -5 ; store the value -5 (0FBh(

WordVarName dw 1234h ; 34h in low address, 12h in high address

DoubleWordVarName dd -5 ; store -5 as 32 bit format (0FFFFFFFBh(

, .5
. – .) (

: ,

DATASEG

ByteVarName db 'HELLO'

. ,ASCII , ,
: ,

99
100 mov –6

DATASEG

ByteVarName1 db 'H'

ByteVarName2 db 'E'

ByteVarName3 db 'L'

ByteVarName4 db 'L'

ByteVarName5 db 'O'

.)ARRAY( – .
.)STRING( ASCII ,
.

:6.2

, – DATASEG‫־‬ ,base.asm
.ASCII , , ,
. DATASEG‫־‬ ,

, , , .
, .
, , , .
.

. ,
, , ,
:

ElementAddress = ArrayBaseAddress + Index * ElementSize

100
101 mov –6

0 , 0200h ,)words( ,
1 ,) ,0201h ( 0200h
. 020Ah 5 ,0202h

: DATA‫־‬

ArrayName SizeOfElement N dup)?(

. , ArrayName

– dd ,dw ,db , SizeOfElement


. ,

. N. N

. ,duplicate Dup

. N ,
. ,

ArrayOfTenFives db 10 dup (5(

:5 , ,

. ,

ArrayOf1234 db 8 dup (1,2,3,4(

101
102 mov –6

. ,1,2,3,4 – 32

:6.3

,TD‫־‬ ,
.

.)Bytes( 7‫ ־‬5 ,3 : DATASEG‫־‬ .

.'5' 10 .'5' 10 .
!

. ,4,5,6 20 .

MOV
. .

, ."move" ,mov
. .

: . mov‫־‬

mov Destination, Source

Source .Destination Source


mov , .)Operands( Destination- Source- .
.

: ) ( 22 ax ,

mov ax, 22

: 22

mov ax, 16h

mov ax, 00010110b

102
103 mov –6

mov bx, 199

mov cx, 2321

mov dx, 10

mov ax, bx

. bx- .bx‫־‬ ax‫־‬


: .ax‫־‬

mov ax, cx

mov ax, dx

mov ax, ax

.ax ax . ,

:mov ‫־‬

mov register,register

mov register, constant

mov register, memory

mov memory, register

mov memory, constant

, .memory memory ,
: .
mov opcode‫־‬ . , ,
.

103
104 mov –6

mov register, register

, ) , ( 16 , 8
: .

mov ax, bx ; 16 bit registers

mov cl, dh ; 8 bit registers

mov si, bp ; The mov instruction works with ALL general purpose registers

: ,

mov ax, bl
.

.mov ds, cs ,
ds cs . mov cs,ax : . cs .
.

6.4

.bx ax .

.ax bx .

.ch ah .

.dl al .

104
105 mov –6

mov register, constant

16‫־‬ –
. , 8 )257 (

mov cl, 10h

mov ah, 10 ; Note the difference from last command! 10 decimal, not 10h (=16(

mov ax, 555

6.5

, : ,) ( 100 al
al‫־‬ TD‫־‬ .)00001111b ,b : (
.

mov memory, register

. ‫־‬
( ,)Direct addressing ( ,
.)Indexed addressing ( )Indirect addressing

mov [1], ax ; Direct addressing

mov [Var], ax ; Another form of direct addressing, using a variable

mov [bx], ax ; Indirect addressing

mov [bx+1], ax ; Indexed addressing

.1 ax ,

.Var ax ,

: .bx‫־‬ ax ,

105
106 mov –6

mov [1], ax

:
mov bx, 1

mov [bx], ax

,1 bx‫־‬ ax ,
.bx‫־‬

6.6

al .0 , var DATASEG .
: .2 bx ) ( 100

mov [Var], al
mov [1], al
mov [bx], al
mov [bx+1], al
. DATASEG‫־‬ TD‫־‬ .
.) , ( 100 DATASEG‫־‬

– ,
:

mov register, memory

: , ,
mov ax, [1]

mov ax, [Var]

mov ax, [bx]

mov ax, [bx+2]

106
107 mov –6

6.7

:) ( ,

mov [Var], al
mov [1], al
mov [bx], al
mov [bx+1], al
; -----1-----
mov al, 0
mov al, [var[
; -----2-----
mov al, 0
mov al, [1[
; -----3-----
mov al, 0
mov al, [bx[
; -----4-----
mov al, 0
mov al, [bx+1[
al . al‫־‬ TD‫־‬
.) , ( 100

107
108 mov –6

mov memory, constant

:bx ‫־‬ 5

mov [bx], 5

: :

mov [byte ptr bx], 5

mov [word ptr bx], 5

dx cx ,ax‫־‬ .di si ,bx‫־‬ , .


: .

mov cx, [ax]

: , ,

: . bx‫־‬ -

mov ax, [bx[

mov [bx], ax

: . bx‫־‬ ,bx ‫־‬ -

mov ax, [bx+2[

mov [bx+2], ax

108
109 mov –6

,si di ,bx ‫־‬ -


: .

mov ax, [bx+si[

mov ax, [bx+di[

mov [bx+si], ax

mov [bx+di], ax

: . ,di si bx‫־‬ , -

mov ax, [bx+si+2[

mov ax, [bx+di+2[

mov [bx+si+2], ax

mov [bx+di+2], ax

. .mov
:

mov [1], ax

mov [Var], ax

mov [bx], ax

,"1" . 20 :
? 20 ,) 16 , ( bx var

– 16 , .
. ]1[ .
, – – , ?
.DATASEG 1 ax .DATASEG

, ? , 16 ,ax :
.mov‫־‬ .
: , DATASEG , DATASEG‫־‬

109
110 mov –6

DATASEG:

ZeroArray db 8 dup (0(

mov ax, 0AABBh

mov [1], ax

:DATASEG

? ,DATASEG-

: ,DATASEG‫־‬

mov [1], ax

mov ]es:1], ax ; as you recall, ES is the pointer to the Extended Segment

Extended Segment , 'AABBh' ,


: ES

110
111 mov –6

Little Endian, Big Endian

,ax –" " ,


? . ah
. ah .

,)' bx ,ax ( ,
: ,

Big . )' ah, bh ( High Order‫־‬ -


.Endian

Little . )' al, bl ( Low Order‫־‬ -


.Endian

8086‫־‬ , .
: .Little Endian

http://en.wikipedia.org/wiki/Endianness

" " Big Endian- Little Endian

. mov
: .

DATASEG:

Array db 0AAh, 0BBh, 0CCh, 0DDh, 0EEh, 0FFh

111
112 mov –6

.) –ax al , ( al 2
:

mov al, [Array+2[

,DATASEG Array .
: DATASEG 0002 Array

. 2 ,0CCh al‫־‬
.1 ,0 ,

: . ,

mov [Array+1], al

, 2 , al
.

offset

:offset , bx

mov bx, offset Array

mov al, [bx]

Array . Array- , ,al


. ax ,words

, . ?
bx ,
. bx

112
113 mov –6

LEA

, .Load Effective Address ,lea offset


.

mov bx, offset Array

lea bx, [Array]

:BB0000 , –

word ptr / byte ptr

Array db 0AAh, 0BBh, 0CCh, 0DDh, 0EEh, 0FFh

mov ax, [Array+2]

? – ? :

)db , (
.) 16 ( ax

. – ,
:

mov ax, [word ptr Array+2]

.) ( word , byte ?
? ax –

113
114 mov –6

: .Array+2 .word ax :

. , –

byte word byte ptr word ptr :


.byte ptr word ptr: , TD‫־‬ , .

type override

.type override , mov


: ,

mov memory, constant

: ,bx ‫־‬ 5

mov [bx], 5

5 ,5 ?
.5 ... , ,

: .

mov [byte ptr bx], 5

mov [word ptr bx], 5

. ,
.) 5 ( 00000101 ,bx :
. ,
,bx+1 ,00000101 ,bx :
.00000000

, .
.word ptr byte ptr ,

114
115 mov –6

-mov

:mov

: . .1

mov al, bx

mov ax, bl

. .
. ,

: . .2

mov ds, 1234h

( .
:)base.asm

mov ax, 1234h

mov ds, ax

: . .3

mov [var1], [var2]

,var1 var2 , .
:

mov ax, [var2]

mov [var1], ax

: . .4

mov [ax], 5

( di‫ ־‬si ,bx .


.) ,

: . .5

mov [bx], 5

115
116 mov –6

( 5
.) 64 32

: . .6

mov 5, ax

) (
?

– – ,
: . .

,[1]

mov [1], al

? 20 [1]

.
,ds , ds- .
.) (1 ) ( 16-

mov [ds:1], al

,
.

mov [cs:1], al

. ,cs al- ?
, .
? ?

. , ,

116
117 mov –6

- :6.8

: .

xor ax, ax

xor bx, bx

add ax, 2

add ax, 2

.4 ax , .
: .3 ax ,

) ( o

mov o

.3 bx 3 ax , .
. '

:mov , , 

, :
.

117
118 , –7

, –7

, .mov –
:

, , : -

and, or, xor, not : -

shr, shl : -

. – .

.) ( , , : 80x86‫־‬
.NEG‫ ־‬DEC ,INC ,IDIV ,DIV ,IMUL ,MUL ,SUB ,ADD :
:

add dest, src ; dest = dest + src

sub dest, src ; dest = dest – sub

inc dest ; dest = dest + 1

dec dest ; dest = dest - 1

mul src ; ax = al * src

imul src ; ax = al * src

div src ; al = ax / src (ah stores the remainder(

idiv src ; al = ax / src (ah stores the remainder(

neg dest ; dest = 0 – dest

118
119 , –7

ADD

,)destination( )source( add


, . , .
:

‫פ‬

ax = ax + bx add ax, bx add register, register

ax = ax + var1 add ax, [var1] add register, memory

ax = ax + 2 add ax, 2 add register, constant

var1 = var1 + ax add [var1], ax add memory, register

var1 = var1 + 2 add [var1], 2 add memory, constant

,ax -
! .

. 16 8 -

add :7.1

. al . . 6 .
.al‫־‬ ,TD‫־‬

)? ( al , .
.ax ,

: .

var1 -

var2 -

sum -

)?sum ( sum

119
120 , –7

SUB

,destination source )subtract ( sub


.add , .
: , . ,

‫פ‬

ax = ax - bx sub ax, bx sub register, register

ax = ax - var1 sub ax, [var1] sub register, memory

ax = ax - 2 sub ax, 2 sub register, constant

var1 = var1 - ax sub [var1], ax sub memory, register

var1 = var1 - 2 sub [var1], 2 sub memory, constant

sub :7.2

: .

var1 -

var2 -

diff -

)?diff ( diff

. . 4 .
.6,7,8,9 9,8,7,6 (
)3,1,-1,-3(

120
121 , –7

INC / DEC

)decrease ( dec .1‫־‬ )increase ( inc


add , , . 1‫־‬
: . , 1 1 ,sub

‫פ‬

ax = ax + 1 inc ax inc register

var1 = var1 + 1 inc [var1] inc memory

ax = ax - 1 dec ax dec register

var1 = var1 - 1 dec [var1] dec memory

MUL / IMUL

8 , . )multiply ( mul
. 32 16 16
, 16 .ax , 8
.dx 16 ax 16

.0AB0h .al=0ABh, bl=10h : 8 ,


.ah=0Ah, al=0B0h .ax

.0ABAB00h .ax=0AB0h, bx=1010h : 16 ,


dx ax=0AB00h ax .dx‫ ־‬ax
.dx=0ABh

‫פ‬

ax = al * bl mul bl mul register (8 bit)

dx:ax = ax * bx mul bx mul register (16 bit)

ax = al * ByteVar mul [ByteVar] mul memory (8 bit)

dx:ax = ax * WordVar mul [WordVar] mul memory (16 bit)

121
122 , –7

. ,unsigned signed
. 2‫־‬ 11111011 .
. , , .251

imul ,) ( unsigned mul


. .) ( signed

.00000010 bl ,)+251 -5 ( 11111011 al


signed mul - unsigned .
: ax .)ax ( imul

CODESEG:

mov ax, 0

mov bl, 00000010b

mov al, 11111011b

mul bl

mov ax, 0

mov al, 11111011b

imul bl

ax, bx

122
123 , –7

502‫־‬ ,01F6h 251 al ,mul ‫־‬

,0FFF6 , al ,imul -

mul :7.3

,255-0 ,byte .
)? : (

.-128 +127 ,byte .


)? : (

. .signed , 4 , .
: .word , .sum
,b‫ ־‬a

sum = a[0]*b[0]+a[1]*b[1]+…

123
124 , –7

DIV, IDIV

ax div , 8 . ) divide ( div


div , 16 .ah‫־‬ al‫־‬ ,
.dx‫־‬ ax‫־‬ ,dx:ax ‫־‬ 32

.1 3 .al=7h, bl=2h : 8
.ah=1, al=3 ,

.1 3 .ax=7h, bx=2h : 16
.dx=1, ax=3 ,

( 8 8 , div –
.) 16 16

,al . 16 ) ( , 8 ) (
. . mov ah, 0 : ah‫־‬
, 16 ax , . 32 , 16 ,
, .mov dx, 0 : dx‫־‬
!

: ?

)? ( -

‫פ‬

al = ax div bl
div bl div register (8 bit)
ah = ax mod bl
ax = dx:ax div bx
div bx div register (16 bit)
dx = dx:ax mod bx
al = ax div ByteVar
div [ByteVar] div memory (8 bit)
ah = ax mod ByteVar
ax = dx:ax div WordVar
div [WordVar] div memory (16 bit)
dx = dx:ax mod WordVar

124
125 , –7

:div‫־‬

IDEAL

MODEL small

STACK 100h

DATASEG

CODESEG

start:

mov ax, @data

mov ds, ax

mov al, 7

mov bl, 2

mov ah, 0

div bl

mov ax, 7

mov dx, 0

mov bx, 2

div bx

quit:

mov ax, 4c00h

int 21h

END start

div- ,)signed( ,div idiv


.)unsigned(

125
126 , –7

div :7.4

,unsigned‫־‬ ,byte .
.

,signed‫־‬ ,byte .
.

,unsigned‫־‬ ,word .
.

NEG

, , ) negative ( neg‫־‬
: .

neg dest

dest = 0 - dest

‫פ‬

al = 0 - al neg al neg register (8 bit)

ax = 0 - ax neg ax neg register (16 bit)

ByteVar = 0 - ByteVar neg [ByteVar] neg memory (8 bit)

WordVar = 0 - WordVar neg [WordVar] neg memory (16 bit)

126
127 , –7

.
, . , ,
. ,MASKING

, . , ?
: 0 , .Packed data–" "

0000000000000001000000010000000100000000000000000000000100000000

01110010

,
.

, , .
:

00100101

. ,
. . " " ,

. .not ,xor ,or ,and–

and dest, src ; dest = dest and src

or dest, src ; dest = dest or src

xor dest, src ; dest = dest xor src

not dest ; dest = not dest

: . ,

and register, register

and memory, register

127
128 , –7

and register, memory

and register, constant

and memory, constant

.and xor‫ ־‬or

: not

not register

not memory

AND

.0 , .1 ,1 . and ,
, ." "
. and‫־‬ – .

and

– and , 16 8 , and
1 , 0 0
. 2 2 , 1

00000111and

10010110

---------------

00000110

128
129 , –7

. –and

–0 . ,
. –1 .

, .)MASK( ,
.00000001 : , .

, . and
, . , . 0 and‫־‬
and‫ ־‬,1 – and‫־‬
.0 , .1‫־‬ ,1 ,

and :7.5

. - ?4‫־‬ and

129
130 , –7

OR

: or

.1 ,1‫־‬ ,

. " " or
, , ( , –
0, 1– ‫־‬ .)
. .

. .)7 0‫־‬ (4 ,
:

11000100

. , . ,4 ,4

: ) 4 ( 0001 0000 or ,

11010100

. , , 4

.4

or :7.6

4‫ ־‬2 ?2 , .
?) or (

?4 .

130
131 , –7

XOR

: ,)exclusive or ( ,xor

.) , (2 xor ,

: . , xor

!0 , xor

: .

10010011

:) ( . ,

01010100

: .xor

10010011xor

01010100

---------------

11000111

: xor ,

11000111xor

01010100

---------------

10010011

131
132 , –7

, ,
. .

xor ‫־‬ ,mov xor opcode‫ ־‬,


: .
mov ax, 0

:
xor ax, ax

, ,0 ax‫־‬
.

xor :7.7

. – xor .

.'I LIKE ASSEMBLY$' .ASCII msg .


$‫־‬ ( . , 8
DATASEG‫־‬ .) ,
. ASCII
. DATASEG‫־‬ .

: ,

print:
mov dx, offset msg
mov ah, 9h
int 21h
mov ah, 2 ; new line
mov dl, 10
int 21h
mov dl, 13
int 21h

132
133 , –7

NOT

:not . not

. not . not

1 ,1 0 , . " "
. . 2

. ,
.shl(shift left)‫ ־‬shr(shift right):

SHL

, . ,shl ,
: .cl

shl register, const

shl register, cl

shl memory, const

shl memory, cl

: shl‫־‬

. , -

, n . 0 , ( 0 -
.) n

.CF -

. . 1 OF -

. 1 ZF -

133
134 , –7

. -

. 8‫־‬ 1 -

16 . 8 shl
:8 16

shl

SHR

. .shl‫־‬ ,shr ,
.cl ,
:

shr register, const

shr register, cl

shr memory, const

shr memory, cl

: shr‫־‬

. , -

n . 0 , ( 0 -
.) n ,

.CF -

. OF -
.

. 1 ZF -

. , -

. 8‫־‬ 1 -

16 . 8 shr
:8 16

134
135 , –7

shr

) ( -

, ( , , :
. , , .)
, .
, ,
.' , – ,
. ,
– , ,1
. ,

.LFSR . :
, xor .
, LFSR . xor
:

.http://en.wikipedia.org/wiki/Linear_feedback_shift_register

. ,

, . :
:

http://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch

. , :
.

135
136 , –7

:7.8

.4‫ ־‬al , .3 al‫־‬ .

.8‫ ־‬al , .) ( 120 al‫־‬ .

: .20‫ ־‬al , .) ( 10 al‫־‬ .


. .4‫ ־‬16 20‫־‬

:unsigned‫ ־‬signed , 

o
o
o
o

: 

And o

Or o
Xor o
Not o

, , 
.

: , 

Shr o

Shl o

. ,

136
137 –8

–8

.)"... , "(

: ,

.) jmp ( -

.)cmp– ( -

.)loop– ( -

, , , . ,
.CODESEG ,IP
, , .
– . ,1‫־‬ "-
"– , ."
." ,

)"... , "( :
, . .)"... "(
.

JMP
, jmp . , , jmp
IP‫־‬ ,jmp- .CODESEG
.

DATASEG

address dw 000Ah

CODESEG

mov ax, @data

mov ds, ax

137
138 –8

mov ax, 1

jmp [address]

:jmp‫־‬

:ip ,jmp‫־‬

.CODESEG‫־‬

FAR‫ ־‬NEAR

– ,jmp‫־‬
, jmp‫־‬ .address –000Ah
( , . jmp-
jmp‫־‬ ,near .near ,)
. –

, . far
, . ,
: .

jmp cs:offset ; for example cs:000A

. cs-

138
139 –8

, . –
. far‫ ־‬near‫־‬

jmp :8.1

:)base.asm CODESEG- (

xor ax, ax
add ax, 5
add ax, 4
.ax=4 , jmp

LABELS

, . jmp‫־‬
. CODESEG‫־‬
jmp- , )label( , .
, label‫־‬ . label‫־‬ ,
: .label‫־‬
:LoopIncAx
inc ax
jmp LoopIncAx
. ax ,

label- . –
. ,

. ,label
( , .startloop- StartLoop -
. - )

Check, Back, PrintResult, Wait4Key, Next_Level, : labels " "


. , .Not_Positive

139
140 –8

. .' Label1, MyLabel, Shooki:


.) ( , ,
label‫־‬ . ,label‫־‬
– ,)MOV : ( )ax : (
.

.label :
.

label :8.2

. label‫־‬ .label jmp‫־‬ ,jmp‫־‬

140
141 –8

CMP
" " . )compare ( cmp
, . ,
) , , ( . – .
. – ,sub cmp .

:cmp

‫פ‬

cmp al, bl cmp register, register

cmp ax, [WordVar] cmp register, memory

cmp [WordVar], cx cmp memory, register

cmp ax, 5 cmp register, constant

cmp [ByteVar], 5 cmp memory, constant

. – cmp

: , ,

Code CF ZF SF

mov al, 3h ? ? ?

cmp al, 3h 0 1 0

cmp al, 2h 0 0 0

cmp al, 5h 1 0 1

, mov .3 al -
.

. cmp , .3‫ ־‬al , -


. 0 . )3 al (

141
142 –8

. 2 al .2‫ ־‬al -

'1' 5 al .5‫ ־‬al -


,
.

) (
– .)"... ... "(
. ,

.cmp , -
. , cmp‫־‬

.1‫־‬ , . , , -

.label . ‫־‬ , -

, ( , -
.)

. ,cmp , :
. cmp , .

.cmp‫־‬ ,
signed ,
.unsigned

?10000001b 1b–

,unsigned . – –
10000001b, signed , .1b‫־‬ ) 129 ( 10000001b‫־‬
.1b‫־‬ ,127

142
143 –8

, ,cmp , ,
: , . –

unsigned cmp .Jump ,J


cmp , .Above Below –A B
, J .Greater Less –G L signed
.Equal E‫ ־‬Not N

: .

cmp Operand1, Operand2

Unsigned ‫פ‬ Signed ‫פ‬ ‫פ‬

JA- Jump if Above JG - Jump if Greater

JB-Jump Below JL -Jump if Less

JE - Jump Equal

JNE - Jump Not Equal

JAE- Jump if Above or JGE - Jump if Greater or


Equal Equal
JBE-Jump if Below or
JLE - Jump if Less or Equal
Equal

143
144 –8

) (

, .
: cmp‫־‬

mov al, 10000001b

mov bl, 1b

cmp al, bl

: cmp

.)1‫־‬ 129( ,unsigned ,JA


,JG . , . ZF CF
ZF‫־‬ SF=OF .)1‫־‬ 127 ( ,signed
. .

) ( :8.3

. , :

,) signed- ax ( ax .
.

.ax bx – ,ax=bx .

- – (Var2- Var1 .
.ax=0 ,ax=1 - .)unsigned

.var2- var1 , : .
ax – .var1+var2 ax –
.var1-var2

:'x' .

144
145 –8

mov dl, 'x'


mov ah, 2h
int 21h
.) ( .TimesToPrintX ,
: .TimesToPrintX 'x'
. 1‫־‬ x label . 'x'‫־‬
.label‫־‬ – ,TimesToPrintX‫־‬

. ? ? TimesToPrintX

LOOP
, .
, .TimesToPrintX 'x'
,TimesToPrintX‫־‬ , 'x'‫־‬
– – . 'x' –
. , .)Loop(

: loop

.cx 1 -

. cx -

. label- jmp –) cx , (

loop SomeLabel

dec cx

cmp cx, 0

jne SomeLabel

: 'x' loop

xor cx, cx ; cx=0

mov cl, TimesToPrintX ; we use cl, not cx, since TimesToPrintX is byte long

145
146 –8

mov dl, 'x'

PrintX:

mov ah, 2h

int 21h

loop PrintX

.TimesToPrintX , :
? ,

. TimesToPrintX

loop‫־‬ , . cx loop ,
- 1 (65,535 cx , , cx
cx 65,536 – 65,534 cx .)unsigned
.

:) ( ,

xor cx, cx

mov cl, TimesToPrintX ; we use cl, not cx, since TimesToPrintX is byte long

cmp cx, 0

je ExitLoop

PrintX:

... ; Some code for printing 'x'

Loop PrintX

ExitLoop:

146
147 –8

) ( Nested Loops

. , –

: ‫־‬

mov cx, 10

LoopA:

mov cx, 5

LoopB:

… ;Some code for LoopB

loop LoopB

… ;Some code for LoopA

loop LoopA

5 LoopA ,LoopA 10 –

? . 50 LoopB , .LoopB

. cx LoopB .cx loop‫־‬

, ... cx . 65,536 LoopA‫־‬ ,cx 1 LoopA

( ,
: , .) - cx

mov bx, 10

LoopA:

mov cx, 5

147
148 –8

LoopB:

… ;Some code for LoopB

loop LoopB

… ;Some code for LoopA

dec bx

cmp bx, 0

jne LoopA

:8.4

,0 – ' :' .
,1=0+1 ( ,1
' .)' 2=1+1
: . 10

0,1,1,2,3,5,8,13,21,34
: .

mov ah, 1h
int 21h ; al stores now the ASCII code of the digit
‫־‬ . 5
. HELLO

,unsigned‫־‬ ,Var1*Var2 .
sum=Var1+sum : . ‫־‬
. loopVar2 ‫־‬

'x' : .
. –
: ,4 5

xxxx
xxxx
xxxx
xxxx
xxxx

148
149 –8

9‫ ־‬0 , –
:al‫־‬

mov ah, 1h
int 21h ; al stores now the ASCII code of the digit
sub al, '0' ; now al stores the digit itself
:'x' –

mov dl, 'x'


mov ah, 2h
int 21h
: –

mov dl, 0ah


mov ah, 2h
int 21h

149
150 –8

jmp -

' ja, jg, jb -

loop -

: loop ,
. .Error- Relative jump out of range

( 16 jmp .opcode-
8 ,) 32767 32768
, .) 128 127 ( loop
relative jump out of , 8-
.range

: -

. .1

, - .2
.

. , .3

ja , .
:

cmp ax, bx

ja my_label

…. ; more than 127 bytes in code memory

my_label:

…. ; some code here

:jmp- ja-

cmp ax, bx

jbe help_label

jmp my_label

150
151 –8

help_label:

…. ; more than 127 bytes in code memory

my_label:

…. ; some code here

. .
," " ja ( bx>ax my_label-
, jbe bx>ax , .)" " ,jbe-
. jmp my_label

labels‫־‬ . ,jmp 
.

. ,cmp 

" ,"... " ,"... "( 


.)"...

unsigned signed 
.

.)cl ( cx loop 

. ,
. ,
. . –

151
152 –9

–9

– , .
? ?

, , –
)" " ( )Procedure( .
.)Function(

– –

: . ,

, . –

, – , .
, .

. ,

. .

,ReadPassword– , ,label ,

? , , ? jmp

? jmp . ,

, ? – .

, .

? , .

: label‫־‬

OpenComputer:

jmp ReadPassword

… ; Code for signing into computer

152
153 –9

OpenEmail:

jmp ReadPassword

… ; Code for signing into email

ReadPassword:

… ;Code for reading password from user

jmp ??? ; Where should we jump back to???

–cmp ‫־‬ –

. ...

. –
. . . , . ReadPassword
,ReadPassword . ,
. –
.

call , . –
: , .

call ReadPassword

, . –
, . ,
" " .
. ,

. , ? ,

, . –
. ,
. ,

. –
. ,

153
154 –9

. , . –
" " . ,
.

. , , ,
.

STACK

, . , )STACK(
.

, . ,
. ,

Stack Segment‫־‬

. ,
: . ‫־‬

STACK number of bytes

:)base.asm ( , 256 ,

STACK 100h

: , .

. . stack segment–ss 

154
155 –9

. . stack pointer–sp 

.bx . ,sp ,
- STACK‫־‬ ,ds:bx DATASEG‫־‬
.ss- sp

,100h , . sp ,
.100h sp

0 100h , 256 ,100h


. ,100h ,sp , .0FFh‫־‬

ss .100h
sp .
. ,100h

, sp‫־‬
,LIFO– Last In First Out .
sp , .
, . sp
.

155
156 –9

PUSH

: . push

push operand
?

.sp=sp-2 : sp -

.ss:sp , -

,push sp :
.word– .
. – push

:push
push ax

push 10

push var
.ax

.)byte‫־‬ ,word- ( 10

.word – var

push al

. push al . :

? .100h ,

mov ax, 0AABBh

push ax

156
157 –9

2‫ ־‬sp push

. ,ah‫־‬ ,ax

: .sp 2 .

push 0CCDDh

157
158 –9

POP

) ( .push‫־‬ pop
:

pop operand

. -

.2‫־‬ sp -

:pop

pop ax

pop ]var[

. ax

var , ( var
.)

pop al

. pop –

158
159 –9

?bx ? sp :

pop bx

.0FEh sp ,0CCDDh bx :

: pop‫־‬ , :

pop var

? var sp

.100h sp ,0AABBh var :

? ?

sp‫־‬ , !
: .

sub sp, 4

. ,sp " "

pop‫ ־‬push , :9.1

. sp .10h, 20h . .

?sp .ax push .ax 1234h .


.

1234h – ?sp .ax pop .


?

?1234h .5678h push .

.mov bx ax .

159
160 –9

. ,) ( ,
: " "

.call -

– , . -
.num1, num2–

, num1+num2 , . -
.

) ( -
.

: .CODESEG ,CODESEG

proc ProcedureName

… ;Code for something that the procedure does

ret ; Return to the code that called the procedure

endp ProcedureName

10 ,ZeroMemory –
,CODESEG .)0‫־‬ , ( DATASEG‫־‬
:

160
161 –9

IDEAL

MODEL small

Stack 100h

DATASEG

digit db 10 dup(1) ; if we do not allocate some memory we may run over


; important memory locations

CODESEG

proc ZeroMemory ;Copy value 0 to 10 bytes in memory, starting at location bx

xor al, al

mov cx, 10

ZeroLoop:

mov [bx], al

inc bx

loop ZeroLoop

ret

endp ZeroMemory

start:

mov ax, @data

mov ds, ax

mov bx, offset digit

call ZeroMemory

exit:

mov ax, 4C00h

int 21h

END start

161
162 –9

) ( 8 DATASEG

9.2

ip‫־‬ TD‫־‬ .ZeroMemory


sp‫־‬

CALL, RET

: . call

call ZeroMemory

,CODESEG‫ ־‬12h ,ip=12 , call‫־‬

.)... : ( call‫־‬

162
163 –9

:call‫־‬ ip

offset‫־‬ , ( ZeroMemory .0000- ip


call , .)CODESEG‫־‬ ,CODESEG
.jmp . ip
call‫־‬ .sp – , . call
. .0FEh 100h call‫־‬ .

163
164 –9

.0015h ) ( ss:00FEh .ss ,


. ? .0000h call‫־‬

:ip=0Ah .ret –

ip ,ret‫־‬ –
:

.15h‫ ־‬0Ah‫־‬ ip ,

.call‫־‬ , ?15h ...

: ret‫ ־‬call‫־‬

164
165 –9

–call

– sp ( sp .1
.) –FAR

. ,ss:sp .2

jump ( ip .3
)

-ret

. ,ss:sp .1

.) ; ( sp .2

,ss:sp- ip‫־‬ .3
.

ret‫־‬ call‫־‬ . ip , ,
! ip

NEAR, FAR

. call .CODESEG‫־‬
:

. ,CODESEG‫־‬ .1

.) ( CODESEG‫־‬ .2

, ,model small ,
, – ,CODESEG‫־‬
.CODESEG‫־‬ ,

ZeroMemory .near ,CODESEG‫־‬


, – call , .near
.2‫־‬ sp , .CODESEG–

call , .far ,CODESEG‫־‬ ,


. ,
.4‫־‬ sp ,

165
166 –9

?far near call

: .far near ‫־‬ ,

proc ProcedureName near

proc ProcedureName far

.near ,far near

:"far" –ZeroMemory .

proc ZeroMemory far

: . . ,

."push cs" : , cs:0012 -


cs‫־‬ ,0879h . cs
.) (

, 15h ( 16h ,ip -


.)"push cs"

.) ,00FEh ( 00FCh sp , -

. cs .near ,
.far‫ ־‬near ,

166
167 –9

– .'X' , ,
. X ,
:

CODESEG

proc Print10X

mov cx, 4 ; 4 'X' in each line

PrintXLoop:

mov dl, 'X'

mov ah, 2h

int 21h ; Print the value stored in dl ('X')

loop PrintXLoop

ret

endp Print10X

start:

mov ax, @data

mov ds, ax

mov cx, 3 ; 3 lines of 'X'

Row:

call Print10X

mov dl, 0ah

mov ah, 2h

int 21h ; New line

loop Row

exit: mov ax, 4c00h

int 21h

END start

167
168 –9

. , . , ,
.cx .TD‫־‬ , ?

,0 .4‫־‬ .3‫־‬ cx :
unsigned- , ( 65,535- loop Row

,) cx( row‫־‬ .)
... 0 cx ,

, ,
. ) (

. Print10X pop‫ ־‬push


, :
.

CODESEG

proc Print10X

push cx

mov cx, 4 ; 4 'X' in each line

PrintXLoop:

mov dl, 'X'

mov ah, 2h

int 21h ; Print the value stored in dl ('X')

loop PrintXLoop

pop cx

ret

endp Print10X

start:

168
169 –9

mov ax, @data

mov ds, ax

mov cx, 3 ; 3 lines of 'X'

Row:

call Print10X

mov dl, 0ah

mov ah, 2h

int 21h ; New line

loop Row

exit: mov ax, 4c00h

int 21h

END start

, .) ( pop push
cx , PrintXLoop .4‫ ־‬cx . cx
, cx pop‫־‬ .
.

! cx TD‫־‬ ,

169
170 –9

:9.3

:
CODESEG
proc ChangeRegistersValues
; ???
mov ax, 1
mov bx, 2
mov cx, 3
mov dx, 4
; ???
ret
endp ChangeRegistersValues

start:
mov ax, @data
mov ds, ax
xor ax, ax
xor bx, bx
xor cx, cx
xor dx, dx
call ChangeRegistersValues
exit: mov ax, 4c00h
int 21h
END start
( . ,ChangeRegistersValues
. )'???'

.
? – .
. ? ,
,

170
171 –9

( , ?
. ,)

, , .
, ? ? .
. ,

: ,

DATASEG‫־‬ -

bx , ,ZeroMemory . , ,
.
.ax

proc ZeroMemory

mov cx, ax ; ax holds the number of bytes that should become zero

xor al, al

ZeroLoop:

mov [bx], al

inc bx

loop ZeroLoop

ret

endp ZeroMemory

. ax‫־‬

,cx ‫־‬ :
.ax‫־‬

, . ,
, ? – ax, bx, cx, dx .
.

171
172 –9

. ,DATASEG‫־‬ ,
.) (
?

proc ZeroMemory

mov cx, [NumOfZeroBytes] ; NumOfZeroBytes is defined in DATASEG

xor al, al

ZeroLoop:

mov bx, [MemoryStart] ; MemoryStart is defined in DATASEG

mov [bx], al

inc [MemoryStart]

loop ZeroLoop

ret

endp ZeroMemory

,ax NumOfZeroBytes .
,bx MemoryStart .
.
, , bx‫־‬ al‫־‬ , (
.)

172
173 –9

, –
. , DATASEG‫־‬
, – ,
?) ( ?

, .
( :
,) ,64K‫־‬ ,
– , ‫־‬ ,
.

Pass by Value– .1

Pass by Reference– .2

Pass by Value

. , .
, . – ,
.

.2 ,SimpleAdd .
, . ,
DATASEG‫־‬ SimpleAdd
. SimpleAdd .

SimpleAdd‫־‬ .parameter ,
.parameter

173
174 –9

: ,

push [parameter]

call SimpleAdd

. , . parameter
, .DATASEG‫־‬ parameter‫־‬
" " parameter , 2 .
.

. , Pass by Value
.Pass by Value
.i+j-k ax k– j ,i : ,SimpleProc
: )Pass by Value (

push [i]

push [j]

push [k]

call SimpleProc

174
175 –9

:) ( ,

i, j, k

.pop SimpleProc
pop‫־‬ .call‫־‬ ,ip , –
.

.
, . ,
.

. pop‫־‬

proc SimpleProc

pop ReturnAddress

pop ax ;k

pop bx ;j

sub bx, ax ; bx = j-k

pop ax ;i

add ax, bx ; ax= i+j-k

push ReturnAddress

175
176 –9

ret

endp SimpleProc

. , .DATASEG‫־‬ ReturnAddress
- .
.

Pass by Value :9.4

'X' pass by value .


! : .
: – .

mov dl, 'X'


mov ah, 2h
int 21h
'A' pass by value .
. 'C'‫־‬ 'B' ,
.

.min‫ ־‬max , .
max pass by value
. min

Pass by Reference
, .
.
. – ,
. ,

,SimpleAdd . :
:Pass by Reference

push offset parameter ; Copy the OFFSET of "parameter" into the stack

call SimpleAdd

- –
, . parameter-
:

176
177 –9

push seg parameter ; Copy the SEGMENT of "parameter" into the stack

: .

:parameter

proc SimpleAdd

;Takes as input the address of a parameter, adds 2 to the parameter

pop ReturnAddress ; Save the return address

pop bx ; bx holds the offset of "parameter"

pop es ; es holds the segment of "parameter"

add [byte ptr es:bx], 2 ; This actually changes the value of "parameter"

push ReturnAddress

ret

endp SimpleAdd

Pass by Reference :9.5

.1‫־‬ pass by reference .

. pass by reference .

177
178 –9

– ( ,pass by reference .
.)var1=5, var2=4 .var1=4, var2=5

BP

. pop [ReturnAddress]-
.ip , ,

,Base Pointer ,bp


:) ( .ip

proc SimpleProc

push bp

mov bp, sp

… ;Code of the stuff the procedure does

pop bp

ret 6

endp SimpleProc
.

? .bp sp bp

sp , .sp ,
. bp ,
.bp‫־‬

178
179 –9

k‫ ־‬i .bp 8 , ,i
. bp‫־‬ .bp , , 4‫ ־‬6

.ax=i+j-k ,SimpleProc .
:
proc SimpleProc
push bp
mov bp, sp
; Compute I+J-K
xor ax, ax
add ax, [bp+8] ; [bp+8] = I
add ax, [bp+6] ; [bp+6] = J
sub ax, [bp+4] ; [bp+4] = K
pop bp
ret 6
endp SimpleProc
equ . ,
: . ,

iParm equ [bp+8]


jParm equ [bp+6]
kParm equ [bp+4]

179
180 –9

: ,
proc SimpleProc
push bp
mov bp, sp
; Compute I+J-K
xor ax, ax
add ax, iParm
add ax, jParm
sub ax, kParm
pop bp
ret 6
endp SimpleProc

, .pass by value equ- :


- ,pass by reference .
, pass by reference ,equ
.bx- - (
.)bx

mov bp, sp bp , :
.

) ( pop push ,
. ,bp‫־‬ , ,
:bp‫־‬

proc WrongBP

push ax

push bp

mov bp, sp

180
181 –9

WrongBP

. i j bp+8 . bp‫־‬ ,bp ax

. ret 6‫־‬

, ret :call ret


ret‫־‬ (ret . ,2‫ ־‬sp
:)bx

pop bx ; pop increments sp by 2

jmp bx

.ret‫־‬ sp- ,pop- ,ret‫־‬


: ret 6‫־‬

pop bx ; pop increments sp by 2

add sp, 6 ; sp is incremented by a total of 8

jmp bx

. push ret‫־‬
. 6 , 2 push
. " " sp ret 6

181
182 –9

, ret 6‫־‬ ,pop , ret


.

:bp‫־‬

. pop .1

.bp . pop .2

. ‫־‬ .3

. bp- , ,bp

bp‫־‬ :9.6

– ( ,pass by reference , .
.)var1=5, var2=4 .var1=4, var2=5
.bp

pass by max .var1, var2, max : .


max , .pass by value reference
.var1, var2

182
183 –9

) (

. ,
, . ,
.

, .sp , ?
, 6 , , .sp
:

sub sp, 6

sp ( ,
:)ip –

add sp, 6

SubXY‫ ־‬AddXY .x,y–


. , y‫ ־‬x

varX equ [bp+6]

varY equ [bp+4]

AddXY equ [bp-2]

SubXY equ [bp-4]

proc XY

push bp

mov bp, sp

sub sp, 4 ; Allocate 4 bytes for local variables

push ax ; Save ax value before we change it

mov ax, varX

add ax, vary

183
184 –9

mov AddXY, ax

mov ax, varX

sub ax, vary

mov SubXY, ax

pop ax ; Restore ax original value

add sp, 4 ; De-allocate local variables

pop bp

ret 4

endp XY

:push ax

184
185 –9

: XY‫־‬

push 5

push 4

call XY

.SubXY‫ ־‬AddXY ,
:bp

. .4 5
,)0001( )0009( ,
. ,SubXY‫ ־‬AddXY

,]bp-2[ ,ss:00F6 :
. ]bp-4[ ,ss:00F4 .

:9.7

.XYZ ,
. .LocalX, LocalY, LocalZ–
.

185
186 –9

. ,
: .pass by value ,

. , -

. -

. -

: , ,pass by reference

:DATASEG .

DATASEG

num_elements equ 15

Array db num_elements dup)?(

, ,CODESEG
:

push num_elements

push offset Array

call SomeProcedure

, , -
.

:9.8

. , -
.

, . sum sum .
.sum=16 2,2,3,4,5

186
187 –9

, SortArray .
.1,2,3,5,6 : 3,6,5,2,1 .
:

, FindMin -
.

pass by reference Swap -


.

Swap‫־‬ .FindMin‫־‬ SortArray -


. FindMin :

, SortArray -
- SortArray , . FindMin‫־‬
. FindMin : Swap

. -

, ,Sort2Arrays .
, sorted
: .

Array1 = 4,9,5,3,2
Array2 = 3,6,4,1
:

Sorted = 1,2,3,4,5,6,9
:

, Merge -
.

.Merge SortArray‫־‬ -

Filter -
.

187
188 –9

) ( Stack Overflow -
. Stack Overflow
.) (
: , .

. , .1

, .2
.

, .Buffer Overflow
. 12 , . ," "
? 13 . 12- ,
, 257 ,100h – – , .Buffer Overflow
.Buffer Overflow 257-

. Buffer Overflow
.Stack Overflow ,

.Stack Overflow

? , .

; ------------------------------------------------------------------------------
; Program StackOF – demonstration of stack overflow
; Author: Barak Gonen 2015
; ------------------------------------------------------------------------------
IDEAL

MODEL small

STACK 100h

DATASEG

msg1 db 'Please enter your name, press enter to finish',13,10,'$'

188
189 –9

msg2 db 13,10,'Program finished$'

msg3 db 13,10,'Here be dragons$'

CODESEG

proc GetName

; Get user input and store it on the stack


push bp

mov bp, sp

sub sp, 10; Allocate a buffer of 10 bytes on the stack


mov di, sp

mov ah, 1

xor bx, bx

get_char:

int 21h

cmp al, 13 ; Is it the 'enter' key?

je quit_proc

mov [ss:di+bx], al ; Copy user input to the buffer on the stack

inc bx

jmp get_char

quit_proc:

add sp, 10 ; De-allocate buffer

pop bp

ret

endp GetName

start:

mov ax, @data

mov ds, ax

mov ah, 9

mov dx, offset msg1

int 21h

189
190 –9

call GetName

mov ah, 9

mov dx, offset msg2

int 21h

exit:

mov ax, 4c00h

int 21h

; This code should not be reached at all, as the program should have

; already exited

nops db 20E8h dup (90h) ; Fill a part of the memory with NOP (90h)-

; NOP - a command which does nothing (No Operation)

mov ah, 9

mov dx, offset msg3

int 21h

jmp exit

END start

.enter . :
: .Program finished

. 10 , ,

, push
.

. , , ,
. ip . ,

? ,

190
191 –9

. ,

: , . 10 GetName

. 10 - .GetName

ASCII- .) ( 8 ,Jon Snow,


:

.ss:00F2 –:ASCII(4Ah (J – .
8 10 . ,ss:00F9
bp " " . , ," " ,
. ,ip

5 ,'Jon Snow' .
.Stack Overflow , 10 14 .
:

191
192 –9

ASCII- , bp ,
.21h - 20h ASCII- :
, . , ,
? - .

. enter
bp , .bp- pop .
ip 2120h .ret- .2020h -
.)little endian – ?2021h ( 2120h

.'Here be dragons' ip 2120h


."... . , "

, .1265- Psalter
. ,

192
193 –9

Stack Overflow- ,
( , .
. )

Stack Overflow :9.8

: .Stack Overflow ,
Stack - 'Here be dragons' .
. , Overflow

193
194 –9

) ( Calling Conventions
. . ,
– -
, .
( endians- . ? .
-big endian little endian .)mov
. ,
.

. . –Convention

Calling , ,
?Calling Conventions- .Conventions

:C

int MyProc (int a, int b);

integer ?MyProc
. .integer

int c = MyProc(1,2);

,)" " ( , Caller )" " (


.Callee

,C MyProc
?Callee- Caller- - .

: .1

:' .

push 1

push 2

call MyProc

:'

push 2

194
195 –9

push 1

call MyProc

Pass ' Pass Left to Right '


. .Right to Left

callee- caller-
callee- caller- , , . ,
.

: .2

, . , MyProc
callee- , MyProc
.c - ,

. callee- caller-
. callee- caller- ,

: .3

. , ,
. sp
: 4 , . ret- , .

ret 4

:sp

add sp, 4

, callee- . callee- caller-


. , callee- . ret
:

call MyProc

add sp, 4

. callee- caller- ,

? ? callee- caller- :

195
196 –9

, . ,
, )https://en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions(
.STDCALL- CDECL

:CDECL

.Right to Left -

) 16- , ax ( ax -

. caller- -

:STDCALL

.Right to Left -

) 16- , ax ( ax -

. callee- -

? . , STDCALL CDECL
caller- .caller - sp , caller-
, ,callee- .
.

print , ?
,print . ,
. print

, ?CDECL STDCALL
caller- ret . ret
, . . sp
.

http://www.codeproject.com/Articles/1388/Calling-Conventions-Demystified :

196
197 –9

. ,
, . ,

. ,

.push‫ ־‬pop , ,

, , –
. .ret‫ ־‬call .
:

Pass by value– -

Pass by reference– -

,
.equ bp

,Stack Overflow , .
.
,Calling Conventions , .
.

, – .
. ,
.

197
) ( -10

) ( CodeGuru Extreme– 10

, CodeGuru Extreme
." " .
. 2-5

CodeGuru Extreme

/http://www.codeguru.co.il/xtreme :

. , .
, .Reverse Engineering ,
- .
. ,

, . ,
, . . 64
- , .
. , .
.

. , ." " ,
. ,

198
) ( -10

,
: ,

http://data.cyber.org.il/assembly/codeguru-guide.pdf :

http://data.cyber.org.il/assembly/codeguru-slides.pdf :

: ,

http://www.codeguru.co.il/wp/?forum=%D7%90%D7%A7%D7%A1%D7%98%D7%A8%D7%99%D7%9D

. ,
Reverse Engineering .
.

,
.
.

, , XCHG :XCHG .1

xchg ax, bx

.mov .bx- ax

LUT .LUT ,Look Up Table XLAT :XLAT .2


LUT- - . ,
' .'

0, 1, 1, 2, 3, 5, 8, 13 …

- ( 13 7 ,8 6
.)

199
) ( -10

- . ?
b ,m- a . (
,m a ( .)' f-
- LUT- .)... f b
97 a ASCII- , . LUT
,98 ASCII- ,b .109 ASCII- ,m
: .102 98 109 97 .102 ,f

Cipher db 97 dup (0), 'mf'

: . mf

mov bx, offset Cipher

mov al, 'a'

xlat

. ,m ASCII- al-

." " ,No Operation :NOP .3


. ,

? . CLD STD :STD / CLD .4


.MOVSW

, . : MOVSW .5
. , 200
mov ,
, . ,
. MOVSW

, .di, si ,es:si ds:di


di, si .

200
) ( -10

CLD STD .
.

,REP MOVSW :REP .6


cx .Repeat

rep movsw

my_label:

movsw

dec cx

jnz my_label

.'

Reverse Engineering
. ,RE ,Reverse Engineering-
RE , . RE
. , .

data.cyber.org.il/assembly/zombies.zip

. -
, .
. . ,

201
) ( -10

duck.com

. ? - .duck.com .

... .enter duck cmd- .


. ,
. . , ?

jmp .cs:100h .
. . .100h

. .f7
: , .100h jmp 100h

start:

jmp start

end start

?cs:102h ,
. , ." " ,

202
) ( -10

coffee.com

RE .coffee.com , ,RE
.

. .
ret- .ret- push mov-cmp-jne
)int 21h ,ax=4C00h( dos- .
push 0000 , . ret- .
. ,

: (properties) ,

203
) ( -10

,cs:0100h .40h , 64 ,
. . cs:0140h-

?coffee –

. int 87 -
.

, 43h- dl .dl 0000h cs:0104-


- ( 43h 0000h , .
30h - , .)c ASCII
?ret- - .)0 ASCII- (

? . ,
. cs:013Fh ret- . ip-
.ret- , .
ret- , .ret- " " ,
bx bx 0006h . ip-
0006h .
. . ,

RE .coffee.com-
.

204
) ( -10

codeguru.com

, .CodeGuru Extreme 2015


.

.cs:011Ch ret- , .

. .

ax ,ax cx )cs:010Ah cs:0104h( .


.1Dh bx

2015h ax .

al :xlat .xlat cx- ax xor .


, .ds:bx+al
?ds:bx , . cs- ds-

205
) ( -10

.1Dh bx
.cs:011Dh- ds:bx .11Dh bx ,100h

add .ret- .cs:011Dh .


.al=0 ? xlat .add .
xlat ,al=1 .0 .cs:011Dh xlat
" " .1 cs:011Eh
.

. ? . 0,1,1,2,1,2,2,3 .
. - ,
. -

2015 .
. ,LUT- xor ,

.ah al , xlat- .

?ip .ret ,6 - .

- ,ax ? .
.2015h word

,ret- ,
" " . ip- .2015h word-
.

206
) ( -10

Make it – Break it – Fix it:

, ." "
. , ."Accessgranted":
. "Access granted" ,
.

Make it -'

,
, . , : .
.

,int 21h ,1 ah :
: .al

mov ah, 1

int 21h

ISR- ( '$' ,
: . 13,10 .)... –

message db 'Hello World’,13,10,'$'

: dx

mov dx, offset message

:int 21h ah=9h

mov ah, 9h

int 21h

.DOS ,

. ,
. . ,
.

207
) ( -10

; ----------------------------------------------------------------------------------------------
; Simple get password program- a very basic code just to help you start
; Author: Barak Gonen 2015
; ----------------------------------------------------------------------------------------------
IDEAL

MODEL small

STACK 100h

DATASEG

Save db (?)

Welcome db 'Please enter password, press enter to finish',13,10,'$'

Access db 13, 10, 'Access granted$'

Wrong db 13, 10, 'Login failed$'

CODESEG

start:

mov ax, @data

mov ds, ax

mov ah, 9

mov dx, offset Welcome

int 21h

xor cx, cx

getChar:

mov ah, 1

int 21h

cmp al, 13

je check

mov [Save], al

208
) ( -10

inc cx

jmp getChar

check:

cmp [Save], 'X'

jne fail

cmp cx, 3

jne fail

success:

mov ah, 9

mov dx, offset Access

int 21h

jmp exit

fail:

mov ah, 9

mov dx, offset Wrong

int 21h

exit:

mov ax, 4c00h

int 21h

END start

, . .1
?

. ,'X' , RE .2
.

209
) ( -10

. .3

! . .4

Break it -'

.) - , (
. .

Fix it -'

,
.

.
, .
. - .

210
-11

–11

- )Interrupt(
.)jmp‫ ־‬cmp – (

( .
? .)

– , –
. , .
. –
, . –
. ,

:" " 80x86‫־‬

. ‫־‬ , .Traps , -

, .Exceptions , -
. , .

( . Interrupts , -
. .)
. ,

int- . , .int
. ,

int operand

:1 ,
int 1h

, , .
:

int 21h

, , .
DOS .Disk Operating Systems –DOS

211
-11

. , ,
BIOS‫ ־‬.Basic Input Output System –BIOS ,80x86‫־‬ ,
BIOS‫־‬ . ,
, BIOS‫־‬ .
. ,

, . ,
:

.9h -

" " .BIOS ,16h -


. 9h

.BIOS " " DOS‫־‬ .DOS ,21h -

:) ( ,

. 8h -

8h " " . , BIOS 1Ch -


.

.BIOS " "DOS‫־‬ . ,DOS 21h -

, :
. ,

. ,
. ,DOS

. , ,
.ISR Interrupt Service Routine .

ISR , ,
. .

212
-11

mov ax,5 – . .1
. 5 ax ,

.2
.ip‫ ־‬cs , .)ISR‫( ־‬

Trap ( )Interrupt Flag( ) (" " ISR‫־‬ .3


. – .)Flag

ISR‫־‬ .ip‫ ־‬cs ISR‫־‬ .4


.

.ISR‫־‬ .5

)' ( , ISR‫־‬ .6
.ip‫ ־‬cs ,

.cs:ip .7

) 100h (

213
-11

:Trap Flag‫ ־‬Interrupt Flag

.
, 55 . , .
– , ISR‫־‬ . ,
... 55 . F7‫־‬
... , ISR F7‫־‬

.
, " " .disable interrupts
.

,if , , ISR‫־‬
.enable interrupts .

) ( IVT‫ ־‬ISR

: ISR

proc ISRname far

iret

endp ISRname

– ret iret‫־‬
, . , pop iret‫־‬ ret‫ ־‬iret .ISR‫־‬
. , ISR

. :
? .ISR‫־‬ ,

, 255‫ ־‬0 . ,
: , .

int 21h

.21h

214
-11

, , .
.IVT ,Interrupt Vector Table .

0 0 IVT‫־‬ . 1024 ,256Double Words IVT‫־‬


3FFh) 1023‫ ־‬0 IVT‫– ־‬ ,
.)

256 IVT‫? ־‬ IVT‫־‬


ISR‫־‬ . . 'ISR' ,
.0h ISR IVT‫־‬ , .ISR‫־‬
. 1h ISR

4‫־‬ , , ISR ,
. 84h 21h : . ISR‫־‬
.21h ISR‫־‬ , ,IVT‫־‬ ,84h

Interrupt Vector Table‫־‬

215
-11

DOS
. ‫־‬ ,Disk Operating System ,DOS
.Windows ‫־‬ ,

– .
, .
. ,
, 'ISR' , .
.

.2Fh‫ ־‬20h – IVT‫־‬ DOS 'ISR'‫־‬

IVT‫־‬ DOS‫־‬

, . ,21h
.int 21h ‫־‬
int 21h‫־‬ ?
.

. ah ,

216
-11

http://spike.scu.edu.au/~barry/interrupts.html
. – ah– int 21h
.

AH=1h–

: ."1" ah , int 21h

mov ah, 1

int 21h

.al

( 32h ,2 al ,"2" , . ASCII‫־‬ al‫־‬


."2" ASCII‫־‬ ,)

ASCIIĄ , ,2 al‫־‬
.)30h ( "0"

sub al, 30h

217
-11

:al ASCII‫־‬
IDEAL

MODEL small

STACK 100h

DATASEG

CODESEG

start:

mov ax, @data

mov ds, ax

mov ah, 1

int 21h

exit:

mov ax, 4C00h

int 21h

END start

)ASCII 37h (7

al

218
-11

:11.1

. ASCII .

ASCII‫־‬ ( .
.)9- 0 ASCII‫־‬

, .
.) : ( DL

. 4 : .
4‫־‬ , .
.)0250‫־‬ 250 (
. 30‫־‬

AH=2h–

dl "2" ah , int 21h


:58h ASCII‫־‬ ,'X' . ASCII‫־‬ ,

mov dl, 'X' ;same as: mov dl, 58h

mov ah, 2

int 21h

. al

: , ASCII

. , – Line Feed0Ah – ,10 .1

. – Carriage Return 0Dh – ,13 .2

:'Y' ,'X'

219
-11

IDEAL

MODEL small

STACK 100h

DATASEG

CODESEG

start:

mov ax, @data

mov ds, ax

;print x

mov dl, 'X'

mov ah, 2

int 21h

;newline

mov dl, 10

mov ah, 2

int 21h

;carriage return

mov dl, 13

mov ah, 2

int 21h

;print y

mov dl, 'Y'

mov ah, 2

int 21h

exit:

mov ax, 4C00h

int 21h

END start

220
-11

:11.2

.'A' .

.'a' .

. ,'HELLO' .

, ,'WORLD' , , ,'HELLO' .
.

. .

– . .
, : .
.7 5 – .
. ,

221
-11

AH=9h–

ISR- ( '$' ,
: .)... –

message db 'Hello World$’

: dx

mov dx, offset message

mov ah, 9h

int 21h

: ,

message db 'Hello World', 10, 13,'$'

IDEAL

MODEL small

STACK 100h

DATASEG

message db 'Hello World',10,13,'$'

CODESEG

start:

mov ax, @data

mov ds, ax

push seg message

pop ds

mov dx, offset message

222
-11

mov ah, 9h

int 21h

exit:

mov ax, 4C00h

int 21h

END start

:11.3

. 'Enter a digit' : .

,9‫ ־‬0 , .
.''Wrong input" :

: . , .

I like to write assembly code


Barak

AH=0Ah–

.)Buffer( " - ,
.
.0Ah ah Int 21h )ds- ( dx

223
-11

, , Enter-
.

. Enter ASCII-

: 20

- , 23 - (
:)Enter ASCII- - ,
IDEAL

MODEL small

STACK 100h

DATASEG

message db 23 dup)?(

CODESEG

start:

mov ax, @data

mov ds, ax

mov dx, offset message

mov bx, dx

mov [byte ptr bx], 21 ;21 not 20, the last input is ENTER

mov ah, 0Ah

int 21h

exit:

mov ax, 4C00h

int 21h

END start

224
-11

,14h , .ds ,message


, .enter‫־‬ , ,13h , .
.enter ASCII‫־‬ 0Dh ,

:11.4

)abc( , 10
.)ABC(

225
-11

) : ( -

. :11.5

. 10 :11.6

)! – ( :11.7

***** . ***** . ***** . ***** . ***** .

**** **** * * ***** *****

*** *** * * ***** *****

** ** * * ***** *****

* * ***** ***** *****

: .11.8

ABCDEFGE….Z

: :11.9

. . .

. .

. .

. .

: :11.10

Small letter, Capital letter, Number, Other

."*" ," " 5 :11.11


. 2 , . - "12345" :

226
-11

AH=4Ch–

‫־‬ ‫־‬ 4Ch int 21h‫־‬


al . al .
,
:base.asm‫־‬
mov ax, 4C00h
int 21h

) ( AH=2Ch ,AH=2Dh– /

55 . –
." 1/18 " . 18.2 ,) 0.055(

,int 21h - ,DOS


:2Ch

mov ah, 2Ch


int 21h

.cl - .ch -

.dl - .dh -

, . 55
. , 55‫־‬

.ah=2Dh int 21h‫־‬ , DOS


:

. cl - . ch -

. dl - . dh -

: –

227
-11

. int 21h-
. ASCII

; -----------------------------------------------------------------------------------
; Print time to screen
; Author: Barak Gonen 2014
; Credit: www.stackoverflow.com (printing-an-int, by Brendan(
; -----------------------------------------------------------------------------------
IDEAL

MODEL small

STACK 100h

DATASEG

hourtxt db 'Hour: ','$'

mintxt db 13,10,'Mins: ','$'

sectxt db 13,10,'Sec: ','$'

mstxt db 13,10,'1/100sec: ','$'

savetime dw ?

divisorTable db 10,1,0

CODESEG

proc printNumber

push ax

push bx

push dx

mov bx,offset divisorTable

nextDigit:

xor ah,ah

div [byte ptr bx] ;al = quotient, ah = remainder

228
-11

add al,'0'

call printCharacter ;Display the quotient

mov al,ah ;ah = remainder

add bx,1 ;bx = address of next divisor

cmp [byte ptr bx],0 ;Have all divisors been done?

jne nextDigit

pop dx

pop bx

pop ax

ret

endp printNumber

proc printCharacter

push ax

push dx

mov ah,2

mov dl, al

int 21h

pop dx

pop ax

ret

endp printCharacter

start:

mov ax, @data

229
-11

mov ds, ax

mov ah, 2ch

int 21h ;ch- hour, cl- minutes, dh- seconds, dl- hundreths secs

mov [savetime], dx

;print hours
mov dx, offset hourtxt

mov ah, 9

int 21h

xor ax, ax

mov al, ch

call printNumber;

;print minutes
mov dx, offset mintxt

mov ah, 9

int 21h

xor ax, ax

mov al, cl

call printNumber

;print seconds
mov dx, offset sectxt

mov ah, 9

int 21h

xor ax, ax

mov dx, [savetime]

mov al, dh

call printNumber

230
-11

;print 1/100 seconds


mov dx, offset mstxt

mov ah, 9

int 21h

xor ax, ax

mov dx, [savetime]

mov al, dl

call printNumber

quit:

mov ax, 4c00h

int 21h

END start

:11.12

.1 ,0 .
, :
55 ,960 0
.

. . , .
.

231
-11

Exceptions–
exception .IVT‫־‬
. . , –

. ,

:
mov cl, 0

div cl ah= al / cl al= al % cl

. ,
.

.int 0h , ,

. debugger‫־‬
, .
." " , .

.int 1h , ,

breakpoint .debugger‫־‬ breakpoints‫־‬


." " breakpoint

.int 3h ,breakpoint ,

Traps–
. , , exceptions‫־‬
: ,

: , int

int operand

int 80h

, –
.ISR‫־‬ IVT‫־‬ ISR‫־‬

, ?
. -

232
-11

. ,

, .
.
. ,
IVT‫־‬ . IVT‫־‬
. " "

.Trap

) ( ISR
Hello ' ,Trap ISR ISR‫־‬
.'World

.ISR- –'

: ISR‫־‬
proc SimpleISR far

iret
endp SimpleISR
. ISR‫־‬

,iret- ,
. , opcodes-

proc SimpleISR far



iret
message db 'Hello World$'
endp SimpleISR
ds‫־‬ ,ah=9h .int 21h‫־‬
. dx‫־‬

proc SimpleISR far

mov dx, offset message

push seg message

233
-11

pop ds

mov ah, 9h

int 21h

iret

message db 'Hello World$'

endp SimpleISR

ISR‫־‬ . ISR‫־‬
. ,

proc SimpleISR far

push dx

push ds

mov dx, offset message

push seg message

pop ds

mov ah, 9h

int 21h

pop ds

pop dx

iret

message db 'Hello World$'

endp SimpleISR

.ISR‫־‬ ,

IVT- ISR- ‫'־‬

.IVT- ISR- , .IVT- - , ISR-


, .)0FFh (255 ,IVT- ISR-
IVT- , ISR - ISR-
)?21h ISR- - (

234
-11

,int 21h , ISR- IVT-


ISR- ,al .IVT- AH=25h .
: .ds:dx

mov al, 0FFh ; The ISR will be placed as number 255 in the IVT

mov ah, 25h ; Code for int 21h

mov dx, offset SimpleISR ; dx should hold the offset of the ISR

push seg SimpleISR

pop ds ; ds should hold the segment of the ISR

int 21h

: . int 0FFh- .

IDEAL

MODEL small

STACK 100h

DATASEG

CODESEG

proc SimpleISR far

push dx

push ds

mov dx, offset message

push seg message

pop ds

mov ah, 9h

int 21h

pop ds

pop dx

235
-11

iret

message db 'Hello World$'

endp SimpleISR

start:

mov ax, @data

mov ds, ax

; Plant SimpleISR into IVT, int 0FFh

mov al, 0FFh

mov ah, 25h

mov dx, offset SimpleISR

push seg SimpleISR

pop ds

int 21h

; Call SimpleISR

int 0FFh

exit: mov ax, 4c00h

int 21h

END start

ISR :11.13

. , ASCII al ISR .
.int 0FEh ‫־‬

: ax, bx ISR .

.bx- ax 'ax' -

.ax- bx 'bx' -

236
-11

. 'SAME' -

.int 0F0h ‫־‬

-
) DOS (
.

, .
' c b ,b a -1 .) (
.fdw cat ,3 , .a ,z

. , .1
.

. Anna Karenina .2
. .
. .'$'
.

all happy families resemble one another every unhappy family is unhappy in its own way
all was confusion in the house of the oblonskys
the wife had discovered that her husband was having an intrigue with a french governess who had been in
their employ and she declared that she could not live in the same house with him
this condition of things had lasted now three days and was causing deep discomfort not only to the husband
and wife but also to all the members of the family and the domestics
all the members of the family and the domestics felt that there was no sense in their living together and
that in any hotel people meeting casually had more mutual interests than they the members of the family
and the domestics of the house of oblonsky
the wife did not come out of her own rooms
the husband had not been at home for two days
the children were running over the whole house as if they were crazy
the english maid was angry with the housekeeper and wrote to a friend begging her to find her a new place
the head cook had departed the evening before just at dinner time
the kitchen maid and the coachman demanded their wages$

. , .3

. , .4
. .

, .5
, ( .
.)' 9.1% T . 12.02% E
:

237
-11
Letter Frequency (%)
E 12.02
T 9.10
A 8.12
O 7.68
I 7.31
N 6.95
S 6.28
R 6.02
H 5.92
D 4.32
L 3.98
U 2.88
C 2.71
M 2.61
F 2.30
Y 2.11
W 2.09
G 2.03
P 1.82
B 1.49
V 1.11
K 0.69
X 0.17
Q 0.11
J 0.10
Z 0.07

238
-11

. ,
,IVT‫־‬ , ,ISR‫־‬
. 'ISR'-

.DOS 21h
. ,

.IVT‫־‬ ISR

)Traps( -

)Exceptions( -

. ,)Interrupts( ,

239
) ( -12

) ( – 12

, DOS
, . DOS .
? , , ?
.

. – -

. / –I/O Ports -

, ,
:

BIOS -

DOS -

Interrupts–
. ) ( , ,
. , .
, , –
– , .
.
, .
?

, , .Polling–
, , .

240
) ( -12

– , – ? .
. .

( , Polling

. Polling .) ,

, , .

, .

– .

. ,

, 10 .

. 1000

, , .

. ,

, . , –Polling
.

" " .)Interrupt( ,


, .
,ISR ,
Interrupt‫־‬ Interrupt . ISR- .
– ) (
. ,

241
) ( -12

, . –Interrupt
.

–" " ," " . ,


.
.

8086-

,"INTR" ,18

242
) ( -12

PIC–

. ,8086‫־‬
? .

Programmable Interrupt , .
.PIC ,Controller

.8086‫־‬ ,1976 8259A PIC

, 8 ,IR7 IR0 . PIC‫ ־‬,


8 8259A PIC .
IR2 , IR6 , IR1 , IR0 – .
.)IR2- , (

.INTR .INT PIC‫־‬


IR1 . .
.8086- INTR ,INT PIC‫ ־‬, .PIC‫־‬

243
) ( -12

, PIC‫־‬ . PIC‫־‬
. , : .
? ...

, . PIC‫ ־‬.
, .
.

: PIC‫־‬

. -

. – I/O Port , -

. -

, end of interrupt -
.

. -

. ‫־‬ disable interrupts ,


( , .
. ,)
– ?
. .
?" "

, . PIC‫ ־‬, ,
PIC‫־‬ . PIC‫ ־‬,

244
) ( -12

PIC‫־‬ , , .
. PIC‫־‬

55‫־‬ , . 55 , , –
.
.

I/O Ports– /
, )buses(
–address bus– , . /
.

, . ,
. , 16 80x86‫־‬
.)Input/Output ( I/O , /

: , I/O

.)Port( I/O -

.) 64K ( 16 - I/O -

.out‫ ־‬in ,mov -

,mov ( I/O‫־‬ out‫ ־‬in -


.)

in . out , in :in, out


. al ax‫־‬ out .al ax

:in, out -

. 255‫ ־‬0 , : o

.dx ,255 : o

in ax/al, port

in ax/al, dx

out port, ax/al

245
) ( -12

out dx, ax/al

:255‫־‬ in / out

in al, 61h ; read the status of the port

or al, 00000011b ; change some bits

out 61h, al ; copy the value in al back to the port

– .) , (,
, in
.out ‫־‬

:255‫־‬

mov dx, 300h

in al, dx

. dx- ,255‫־‬

.)Device manager>- ( : –

, .)Properties( " " – ,


.64 60 ." "

246
) ( -12

247
) ( -12

: , , .

.Scan Codes . – .1

.60h , I/O- .2

60h scan codes‫־‬ , , .3


Type Ahead 9h .
.Buffer

. ,
:

. .1

, BIOS– (BIOS .2
16h BIOS‫ ־‬.) .
. – –

,21h ,DOS .3
.

20 ,Art of Assembly . ,
.) (

248
) ( -12

Scan Codes

. PIC‫־‬ ,
alt, . , –
. control, delete
.

– .scan codes , ‫־‬


,80h scan code , .up down
, 1‫־‬ 0 scan code‫־‬ 7
.) (

scan codes‫־‬

, .81h ESC‫־‬ ,1h ESC ,


. scan code
:

. / scan code‫־‬ 60h .1

. IR1 PIC‫־‬ .2

int . , PIC‫־‬ .3
.9h

.9 ISR , .4

249
) ( -12

.) Type Ahead Buffer‫( ־‬ scan code‫־‬ ISR‫־‬ .5

. end of interrupt PIC‫־‬ ,ISR‫־‬ .6

Type Ahead Buffer

, ,scan code‫־‬ 9h ‫־‬ ISR‫־‬


'a' . ASCII‫־‬ .ASCII
, .'a' ASCII ,'A' ,shift
ISR‫ ־‬. ,ASCII code scan code . ASCII‫־‬
.Type Ahead Buffer ,
:

–0040:001A -

–0040:001C -

)words( 16–0040:001E -

( 16 ,
– ? 16 .)scan code, ASCII code–
, .2- , .
. .

,BIOS . ,
.2‫־‬ ,

. 16 " "
? " " . BIOS
. ,
, ,
15 , ?
. BIOS‫־‬ , ) (

,
, . –
.

250
) ( -12

, ASCII ,
:

BIOS , –
.DOS

, .microcontrollers , ,
: .

. microcontroller‫־‬ 60h .1
. scan codes- ,

. microcontroller‫־‬ 64h .2
.60h scan code ,

, . ,64h 61h .3
.) (

:) ( 64h

in al, 64h ;Read keyboard status port

cmp al, 10b ;Data in buffer?

: – – ,

WaitForData:

in al, 64h

cmp al, 10b

je WaitForData

251
) ( -12

:60h – ,

in al, 60h

.2- ‫־‬ , ,

. ESC‫־‬ –
. – 15 .
" 16‫־‬ . 15‫־‬ ,ESC‫־‬
60h , . "
. ,

252
) ( -12

; --------------------------------------------------------------------
; Use keyboard ports to read data, until ESC pressed
; Author: Barak Gonen 2014
; --------------------------------------------------------------------
IDEAL

MODEL small

STACK 100h

DATASEG

message db 'ESC key pressed',13,10,'$'

CODESEG

start:

mov ax, @data

mov ds, ax

WaitForData :

in al, 64h ;Read keyboard status port

cmp al, 10b ;Data in buffer ?

je WaitForData ;Wait until data available

in al, 60h ;Get keyboard data

cmp al, 1h ;Is it the ESC key ?

jne WaitForData

ESCPressed:

mov dx, offset message

mov ah, 9

int 21h

exit:

mov ax, 4C00h

int 21h

END start

253
) ( -12

15 , 15‫־‬

, . ,
. -

80h scan code- -


,80h .) (

and al, 80h

. .

; -----------------------------------------------------
;Identify key press and key release
; Print "Start" when a key is pressed
; Print "Stop" when the key is released
; Exit program if ESC is pressed
; Barak Gonen 2015
; -----------------------------------------------------
IDEAL

MODEL small

STACK 100h

DATASEG

msg1 db 'Start'$

msg2 db 'Stop'$

saveKey db 0

CODESEG

start:

mov ax, @data

254
) ( -12

mov ds, ax

WaitForKey:

;check if there is a a new key in buffer


in al, 64h

cmp al, 10b

je WaitForKey

in al, 60h

;check if ESC key


cmp al, 1

je exit

;check if the key is same as already pressed


cmp al, [saveKey[

je WaitForKey

;new key- store it

mov [saveKey[, al

;check if the key was pressed or released


and al, 80h

jnz KeyReleased

KeyPressed:

;print "Start"
mov dx, offset msg1

jmp print

KeyReleased:

;print "Stop"
mov dx, offset msg2

print:

mov ah, 9h

int 21h

jmp WaitForKey

255
) ( -12

exit:

mov ax, 4c00h

int 21h

END start

BIOS

" " ) ( , 16h BIOS


.) , (

al .ah=0h 16h ,
" " , . scan code‫־‬ ah ASCII‫־‬
.2‫־‬ ‫־‬

, . – ,
. ,

0– , . ah=1 16h
. scan code‫ ־‬ASCII‫־‬ ah- al , . 1,

.) ( -

. -

. -

:
WaitForData:

mov ah, 1

int 16h

jz WaitForData

mov ah, 0 ; there is a key in the buffer, read it and clear the buffer

int 16h

: -ESC
; -------------------------------------------------------------------------------------
; Use BIOS int 16h ports to read keyboard data, until ESC pressed
; Author: Barak Gonen 2014

256
) ( -12

; -------------------------------------------------------------------------------------
IDEAL
MODEL small
STACK 100h
DATASEG
message db 'ESC key pressed',13,10,'$'
CODESEG
start:
mov ax, @data
mov ds, ax
WaitForData :
mov ah, 1
Int 16h
jz WaitForData
mov ah, 0
int 16h
cmp ah, 1h
jne WaitForData
ESCPressed:
mov dx, offset message
mov ah, 9
int 21h
exit:
mov ax, 4C00h
int 21h
END start

DOS

al‫־‬ – , ah=0Ch 21h


, – ,al=7h . int 21h ,
: . , 7h

257
) ( -12

. ASCII‫־‬ al

: "

; Clear keyboard buffer and read key without echo


mov ah,0Ch

mov al,07h

int 21h

: .

. , ,
.'

ASCII , ASCII‫־‬ ,
, . , .
. – ASCII‫־‬

:12.1

Type Ahead Buffer‫־‬ scan code‫־‬ ISR‫־‬ .


. 0040:001Eh

TD‫־‬ ,) int 21h‫־‬ (


, .type ahead buffer‫־‬ ,step by step
. scan codes‫־‬ type ahead buffer‫־‬ 'a'

: wasd , .

W = up -

A= left -

S = down -

D= right -

258
) ( -12

,"Move up" ,wasd .


. – . ,Esc‫־‬ .' "Movedown"
. ,

: .

in al, 61h
or al, 00000011b
out 61h, al
mov al, 0b6h
out 43h, al
mov ax, 2394h
out 42h, al
mov al, ah
out 42h, al
:

in al, 61h
and al, 11111100b
out 61h, al
. ,
, . 16h :
scan code- 60h
.

259
) ( -12

,PIC‫־‬ :
. ,

. ,

scan code . –
.Type Ahead Buffer‫ ־‬,
:

64h‫ ־‬60h , -

int 16h ,BIOS -

,21h ,DOS -

260
‫‪261‬‬ ‫‪– 13‬‬

‫‪– 13‬‬

‫‪.‬‬ ‫‪,‬‬
‫‪,‬‬ ‫‪.‬‬ ‫‪,‬‬
‫"‬ ‫"‬ ‫‪.‬‬
‫‪.‬‬ ‫‪.‬‬ ‫‪,‬‬
‫‪.‬‬
‫‪,‬‬ ‫‪:‬‬ ‫‪,‬‬ ‫‪.‬‬
‫–‬ ‫‪.‬‬
‫‪.‬‬ ‫‪,‬‬ ‫‪,‬‬

‫‪,‬‬ ‫?‬
‫‪,‬‬ ‫‪.‬‬
‫‪.‬‬ ‫–‬

‫‪.‬‬ ‫‪2000‬‬ ‫‪1000‬‬ ‫‪.‬‬


‫‪.‬‬ ‫‪,‬‬ ‫־‪1000‬‬
‫‪.‬‬ ‫‪,‬‬ ‫־‪2000‬‬
‫‪:‬‬

‫‪,‬‬ ‫‪.‬‬ ‫‪,‬‬ ‫–‬ ‫‪-‬‬


‫‪.‬‬ ‫‪,‬‬

‫‪,‬‬ ‫(‬ ‫–‬ ‫‪-‬‬


‫(‬ ‫)‪,‬‬ ‫(‬ ‫)‪,‬‬
‫‪.‬‬ ‫)‬
‫‪.‬‬

‫‪,‬‬ ‫–‬ ‫‪-‬‬


‫‪,‬‬ ‫–‬ ‫‪.‬‬
‫‪,‬‬ ‫‪.‬‬

‫‪261‬‬
262 – 13

.
.

: -

, o

: -

ASCII o

BMP o

: -

: -

) ( o

: -

262
263 – 13

– ( ,
,DOS .)... ,
: .ah=3Dh

–AL

:0 -

:1 -

:2 -

. –DS:dx

: .0‫־‬

Filename db ‘file.txt’,0

,CF , .DOS ‫־‬ filehandle‫־‬ ax ,


: ax‫־‬

. :2 -

. :5 -

. :12 -

. – ,
. ,

proc OpenFile
; Open file
mov ah, 3Dh
xor al, al
lea dx, [filename]
int 21h
jc openerror
mov [filehandle], ax
ret
openerror:

263
264 – 13

mov dx, offset ErrorMsg


mov ah, 9h
int 21h
ret
endp OpenFile

: .ah=3Fh ‫־‬

. DOS‫־‬ filehandle–bx

. –cx

. ) ( –dx

, dx :
.

proc ReadFile

; Read file
mov ah,3Fh

mov bx, [filehandle[

mov cx,NumOfBytes

mov dx,offset Buffer

int 21h

ret

endp ReadHeader

. , ax ,

: .ah=40h . –

. DOS- filehandle–bx

. ,filehandle cx=0 : . –cx

264
265 – 13

. ) ( –dx

. , ax ,

ax=5 )cx=1, cx=2( :


.access denied

proc WriteToFile

mov ah,40h

mov bx, [filehandle[

mov cx,12

mov dx,offset Message

int 21h

ret

endp WriteToFile

,)ah=4Ch 21h (
? , .
. , , . ,
, , , ( :
. , .)

: .ah=3Eh ‫־‬

. DOS‫־‬ filehandle–bx
proc CloseFile

mov ah,3Eh

mov bx, [filehandle[

int 21h

ret

endp CloseFile

265
266 – 13

. ,
, ,
assembly int 21h ' ‫־‬ . ,
: '…=ah

–AH=3Ch

–AH=41h

–AH=42h

filewrt.txt–

. '!Hello world' ,testfile.txt

; ----------------------------------------------------------------------------
; Write to file
; Author: Barak Gonen, 2014
; ----------------------------------------------------------------------------
IDEAL

MODEL small

STACK 100h

DATASEG

filename db 'testfile.txt',0

filehandle dw ?

Message db 'Hello world!’

ErrorMsg db 'Error', 10, 13,'$'

CODESEG

proc OpenFile

; Open file for reading and writing

266
267 – 13

mov ah, 3Dh

mov al, 2

mov dx, offset filename

int 21h

jc openerror

mov [filehandle], ax

ret

openerror:

mov dx, offset ErrorMsg

mov ah, 9h

int 21h

ret

endp OpenFile

proc WriteToFile

; Write message to file


mov ah,40h

mov bx, [filehandle]

mov cx,12

mov dx,offset Message

int 21h

ret

endp WriteToFile

proc CloseFile

; Close file
mov ah,3Eh

267
268 – 13

mov bx, [filehandle]

int 21h

ret

endp CloseFile

start:

mov ax, @data

mov ds, ax

; Process file
call OpenFile

call WriteToFile

call CloseFile

quit:

mov ax, 4c00h

int 21h

END start

268
269 – 13

. , .

. I/O-

A000:0000 video memory‫ ־‬. video memory

.B000:FFFF

:modes ,

video memory‫־‬ ,text mode , -

80 25 , .) 4,096( 4K B800:0000‫־‬

.ASCII

video memory‫־‬ ,graphic mode , -

200 , .) 65,536(64K ,A000:0000‫־‬

. 320

,DOSBOX ( –

.)

.) ( –

, .video memory‫־‬ –

, .video memory‫־‬ -

BIOS , . BIOS .BIOS -

. BIOS .

. ,DOS -

269
270 – 13

.graphic mode‫־‬ text mode‫־‬ ,

Text Mode‫־‬

,
. ASCII , .ASCII

.DOS , .text mode –


. 80‫־‬ 25‫־‬

: .BIOS, int 10h ,text mode‫־‬

mov ah, 0

mov al, 2

int 10h

. , – .

ASCII

, . DATASEG‫־‬

include , ,DATASEG‫־‬

.DATASEG

:monalisa.asm

270
271 – 13

$‫־‬ . ,
. –

.ah=9h int 21h ,

IDEAL ( monalisa.asm
:) include‫־‬ ,

; ---------------------------------------------------------
; Display ASCII graphics
; Author: Barak Gonen 2014
; ---------------------------------------------------------
model small

stack 100h

DATASEG

include monalisa.asm

CODESEG

start:

271
272 – 13

mov ax,@data

mov ds,ax

; Print string
mov ah,9h

mov dx,offset monalisa

int 21h

; Wait for key press


mov ah,0h

int 16h

exit:

mov ax,4C00h

int 21h

end start

272
273 – 13

ASCII Generator ASCII


: .http://sourceforge.net/projects/ascgen2‫־‬

: , ,

" how to generate ascii art"

For beginners:

www.en.wikipedia.org/wiki/ASCII_art_converter

ASCII Art Galleries:

http://www.afn.org/~afn39695/collect.htm

http://chris.com/ascii/

273
274 – 13

Graphic Mode‫־‬

. ,
,VGA . . , .
, .Video Graphics Array
:

http://en.wikipedia.org/wiki/Video_Graphics_Array

320 200 ,320X200 , VGA


.

: BIOS, int 10h

mov ax, 13h

int 10h

. 200x320 ,

. ,A000:0000- ,video memory‫־‬


. , . , :

274
275 – 13

, . ‫־‬ : ‫־‬
,Y . 0:1 A000:0001 ,0:0 A000:0000
. X ,320‫־‬

, ‫־‬ , VGA , .
, . 256=28 . 8
,RGB 256‫־‬ .RGB– ,Red, Green, Blue
video memory‫־‬ .) ( 256
–0,0,0 RGB‫־‬ – – ,0 .0
.

standard ‫־‬ .standard palette ,


:palette

standard palette‫־‬ 256 . ,


,standard palette‫־‬ 256‫־‬ , .
palette‫־‬ . ,
. palette bmp ,

. ,
.out video memory‫־‬
. x,y ,

.ah=0Ch ,BIOS ,
:

–al

)0 ( –bl

X –cx

Y –dx

275
276 – 13

; -------------------------------------------------------------
; Paint a red pixel in the center of the screen
; Author: Barak Gonen 2014
; -------------------------------------------------------------
IDEAL

MODEL small

STACK 100h

DATASEG

x dw 160

y dw 100

color db 4

CODESEG

start:

mov ax, @data

mov ds, ax

; Graphic mode
mov ax, 13h

int 10h

; Print red dot


mov bh,0h

mov cx,[x]

mov dx,[y]

mov al,[color]

mov ah,0ch

int 10h

; Wait for key press


mov ah,00h

276
277 – 13

int 16h

; Return to text mode


mov ah, 0

mov al, 2

int 10h

exit:

mov ax, 4c00h

int 21h

END start

.ah=0Dh , BIOS‫־‬
, – . ,
.
. – .

; Set graphics mode 320x200x256


mov ax,13h

int 10h

; Read dot
mov bh,0h

mov cx,[x]

mov dx,[y]

277
278 – 13

mov ah,0Dh

int 10h ; return al the pixel value read

) : (

, ( ,
.)

1‫־‬ X , ,
X‫־‬ .) ( 1‫־‬ Y ) (
–“Bresenham algorithm” , , . Y‫־‬
. ,

Y‫־‬ ,
. , .1‫־‬

) : ( ,

278
279 – 13

BMP

( . , ,
. ,Bit Map ,BMP , )
BMP . ,
, JPG , ,video memory‫־‬
.

‫־‬ BMP‫־‬ , .BMP


: .“read bmp file inassembly” .

www.brackeen.com/vga/bitmaps/html

www.ragestorm.net/tutorial?id=7

: BMP

'BM' header‫־‬ . 54 –Header .1


.BMP

, .) 1,024 ( , 256–Palette .2
BMP‫־‬ palette‫־‬ ,) – – , ( RGB
palette‫־‬ ,) – – ( BGR
.

,255–0 . –Data .3
data‫־‬ , . , .palette‫־‬
BMP . ,video memory‫־‬
320 . VGA video memory‫־‬
. 64,000 , 200

320x200 .
BMP‫־‬ .) ( .
1079‫־‬ , .data‫־‬ ,BMP‫־‬ ,
.BMP(1079=54+256x4)‫־‬

, .'0' 1079‫־‬
0 BGR‫־‬ ,palette‫־‬ .palette‫ ־‬0
.54:57 ,palette‫־‬

279
280 – 13

, .200 320 BMP


. Microsoft Paint‫־‬
. ,

. .test.bmp -
.tasm/bin

. -

.header‫־‬ -

.palette‫־‬ -

BGR‫־‬ .3C8h, 3C9h , palette‫־‬ -


.RGB‫־‬

video memory‫־‬ , data‫־‬ -


.

. , -

280
281 – 13

; -------------------------------------------------------------------------------------------------------------

; Read a BMP file 320x200 and print it to screen

; Author: Barak Gonen, 2014

; Credit: Diego Escala, www.ece.msstate.edu/~reese/EE3724/labs/lab9/bitmap.asm

; -------------------------------------------------------------------------------------------------------------

IDEAL

MODEL small

STACK 100h

DATASEG

filename db 'test.bmp',0

filehandle dw ?

Header db 54 dup (0)

Palette db 256*4 dup (0)

ScrLine db 320 dup (0)

ErrorMsg db 'Error', 13, 10,'$'

CODESEG

proc OpenFile

; Open file
mov ah, 3Dh

xor al, al

mov dx, offset filename

int 21h

jc openerror

mov [filehandle], ax

281
282 – 13

ret

openerror:

mov dx, offset ErrorMsg

mov ah, 9h

int 21h

ret

endp OpenFile

proc ReadHeader

; Read BMP file header, 54 bytes


mov ah,3fh

mov bx, [filehandle]

mov cx,54

mov dx,offset Header

int 21h

ret

endp ReadHeader

proc ReadPalette

; Read BMP file color palette, 256 colors * 4 bytes (400h)


mov ah,3fh

mov cx,400h

mov dx,offset Palette

int 21h

ret

endp ReadPalette

282
283 – 13

proc CopyPal

; Copy the colors palette to the video memory


; The number of the first color should be sent to port 3C8h
; The palette is sent to port 3C9h
mov si,offset Palette

mov cx,256

mov dx,3C8h

mov al,0

; Copy starting color to port 3C8h


out dx,al

; Copy palette itself to port 3C9h


inc dx

PalLoop:

; Note: Colors in a BMP file are saved as BGR values rather than RGB.
mov al,[si+2] ; Get red value.

shr al,2 ; Max. is 255, but video palette maximal

; value is 63. Therefore dividing by 4.

out dx,al ; Send it.

moval,[si+1] ; Get green value.

shr al,2

out dx,al ; Send it.

moval,[si] ; Get blue value.

shr al,2

out dx,al ; Send it.

add si,4 ; Point to next color.

; (There is a null chr. after every color.)

283
284 – 13

loop PalLoop

ret

endp CopyPal

proc CopyBitmap

; BMP graphics are saved upside-down.


; Read the graphic line by line (200 lines in VGA format),
; displaying the lines from bottom to top.
mov ax, 0A000h

mov es, ax

movcx,200

PrintBMPLoop:

pushcx

; di = cx*320, point to the correct screen line


movdi,cx

shl cx,6

shl di,8

add di,cx

; Read one line


mov ah,3fh

mov cx,320

mov dx,offset ScrLine

int 21h

; Copy one line into video memory


cld ; Clear direction flag, for movsb

mov cx,320

movsi,offset ScrLine

284
285 – 13

rep movsb ; Copy line to the screen

;rep movsb is same as the following code:

;mov es:di, ds:si

;inc si

;inc di

;dec cx

... ;loop until cx=0

pop cx

loop PrintBMPLoop

ret

endp CopyBitmap

start:

mov ax, @data

mov ds, ax

; Graphic mode
mov ax, 13h

int 10h

; Process BMP file


call OpenFile

call ReadHeader

call ReadPalette

call CopyPal

call CopyBitmap

; Wait for key press


mov ah,1

285
286 – 13

int 21h

; Back to text mode


mov ah, 0

mov al, 2

int 10h

exit:

mov ax, 4c00h

int 21h

END start

. ,
: .graphics bmp .

https://piazza.com/class/i98gbkdp1mg15m?cid=20

: . "Graphics- advanced"

"
. ,
,
.
:
10x10 BMP- , ? BMP .1
80,100
. , ? .2
, ? .3
.
( , ? .4
.)
+ , ,
):

"

, -1

286
287 – 13

, -2,3

, -4

. , ,
Art of Assembly pdf‫־‬ .
.)The Physics of Sound(

. , , –
, " " ." " ,
. " " .
. . –" "
, . . 12- –
, 110 ,)1.06 (2 12
2*110 , 117 ,1.06*110
. 220 ,

,61h ‫־‬ .)speaker(


: .61h ,00‫־‬ , .

in al, 61h

or al, 00000011b

out 61h, al

in al, 61h

and al, 11111100b

out 61h, al

43h . , ,
.42h

287
288 – 13

:43h 0B6h . ,

mov al, 0B6h

out 43h, al

. , 16 )divisor( " " port 42h‫־‬


.

: . 1193180

1193180
Divisor = ---------------
Frequency

, – , 8 port 42h
.

,2712 , 440 , " " ,


: .0A98h

mov al, 98h

out 42h, al ; Sending lower byte

mov al, 0Ah

out 42h, al ; Sending upper byte

. 131 , ,

; ----------------------------------------------------------------

; Play a note from the speaker

; Author: Barak Gonen 2014

; ----------------------------------------------------------------

IDEAL

MODEL small

288
289 – 13

STACK 100h

DATASEG

note dw 2394h ;1193180 / 131 -> (hex)

message db 'Press any key to exit',13,10,'$'

CODESEG

start:

mov ax, @data

mov ds, ax

; open speaker

in al, 61h

or al, 00000011b

out 61h, al

; send control word to change frequency

mov al, 0B6h

out 43h, al

; play frequency 131Hz

mov ax, [note]

out 42h, al ; Sending lower byte

mov al, ah

out 42h, al ; Sending upper byte

; wait for any key

mov dx, offset message

mov ah, 9h

int 21h

289
290 – 13

mov ah, 1h

int 21h

; close the speaker

in al, 61h

and al, 11111100b

out 61h, al

exit:

mov ax, 4C00h

int 21h

END start

290
291 – 13

.ah=2Ch int 21h , .

. –

. , -

. , -

. , -

, .

, , .

: .

. , -

. , , -

. , -

, – ‫־‬

–dl , (

. .)

55 , 55 ,

55‫־‬ .

. .

55‫־‬

, . 0040:006Ch
( . . 55

291
292 – 13

, .) ,

: .) (
. – .0040:006Ch

. )182x0.055sec=10.01 sec( 182

---------------------------------------------------------------;

; Produce a delay of 10 seconds (182 clock ticks(

; Author: Barak Gonen 2014

---------------------------------------------------------------;

IDEAL

MODEL small

STACK 100h

DATASEG

Clock equ es:6Ch

StartMessage db 'Counting 10 seconds. Start...',13,10,'$'

EndMessage db '...Stop.',13,10,'$'

CODESEG

start:

mov ax, @data

mov ds, ax

; wait for first change in timer

mov ax, 40h

mov es, ax

mov ax, [Clock]

FirstTick:

cmp ax, [Clock]

292
293 – 13

je FirstTick

; print start message

mov dx, offset StartMessage

mov ah, 9h

int 21h

; count 10 sec

mov cx, 182 ; 182x0.055sec = ~10sec

DelayLoop:

mov ax, [Clock]

Tick:

cmp ax, [Clock]

je Tick

loop DelayLoop

; print end message

mov dx, offset EndMessage

mov ah, 9h

int 21h

quit:

mov ax, 4c00h

int 21h

END start

293
294 – 13

Random Numbers–

, .

, ( –

. –)

. , .

.)Pseudo-random( ‫־‬

( ,

, .' ,

" " " , .)

."

. .

. ,

.0040:006Ch –

:al )1 0( .1 0: ,

mov ax, 40h

mov es, ax

mov ax, es:6Ch

and al, 00000001b

.0–3 , .11 ,10 ,01 ,00 :


:

and al, 00000011b

.' 0–15 ,7–0 .

. ,2 ,9–0

: - 9–0

.1–0 -

294
295 – 13

.1–0 -

.7–0 -

0 , .

1,5,1 1,1,5 1,0,6 0,0,7( 7 )0,0,0 (

.)... 6,1,0

–0 ,9–0 , . –

.9‫־‬ 15

. ,

, , . 55 ,0040:006Ch
, . ,

, , . ,

, , . – xor

. , .xor

xor‫־‬

. CODESEG‫־‬

. ,

; ---------------------------------------------------------------------------------------------

; Generate 10 random numbers between 0–15

; The method is by doing xor between the timer counter and some bits in CODESEG

; Author: Barak Gonen 2014

; ---------------------------------------------------------------------------------------------

IDEAL

MODEL small

295
296 – 13

STACK 100h

DATASEG

Clock equ es:6Ch

EndMessage db 'Done',13,10,'$'

divisorTable db 10,1,0

CODESEG

proc printNumber

push ax

push bx

push dx

mov bx,offset divisorTable

nextDigit:

xor ah,ah ;dx:ax = number

div [byte ptr bx] ;al = quotient, ah = remainder

add al,'0'

call printCharacter;Display the quotient

mov al,ah ;ah = remainder

add bx,1 ;bx = address of next divisor

cmp [byte ptr bx],0 ;Have all divisors been done?

jne nextDigit

mov ah,2

mov dl,13

int 21h

mov dl,10

int 21h

296
297 – 13

pop dx

pop bx

pop ax

ret

endp printNumber

proc printCharacter

push ax

push dx

mov ah,2

mov dl, al

int 21h

pop dx

pop ax

ret

endp printCharacter

start:

mov ax, @data

mov ds, ax

; initialize

mov ax, 40h

mov es, ax

mov cx, 10

mov bx, 0

297
298 – 13

RandLoop:

; generate random number, cx number of times

mov ax, [Clock] ; read timer counter

mov ah, [byte cs:bx] ; read one byte from memory

xor al, ah ; xor memory and counter

and al, 00001111b ; leave result between 0-15

inc bx

call printNumber

loop RandLoop

; print exit message

mov dx, offset EndMessage

mov ah, 9h

int 21h

exit:

mov ax, 4c00h

int 21h

END start

298
299 – 13

. / –

Scan Codes -

BIOS -

DOS -

. ,
:

, .1
BIOS , .)BIOS/ DOS / (
, . DOS ,
.

– . ASCII‫־‬ DOS int 21h‫־‬ .2


. ASCII –) (
,scan code‫־‬ ASCII‫־‬ , ASCII
, . ? . ASCII‫־‬
: ,DOS .scan code‫־‬

in al,60h

.int 33h ‫־‬


. "int 33h mouse function calls" ‫־‬ ,DOS
. ,

: , ,

mov ax,13h

299
300 – 13

int 10h

ah ax , ( ax .33h
ax=0h . , .)int 21h
:

mov ax,0h

int 33h

:ax=1h ,

mov ax,1h

int 33h

mov ax,3h

int 33h

–bx -

–'1' 0 –xxxx xxx1 o

–'1' 1 –xxxx xx1x o

. – '00' bx ,

320 , ( 639–0 , –cx -


.) cx ,

199– 0 , –dx -

.
. , .

300
301 – 13

; ----------------------------------------------------------------------------------------

; PURPOSE : Paint a point on mouse location, upon left mouse click

; AUTHOR : Barak Gonen 2014

; ----------------------------------------------------------------------------------------

IDEAL

MODEL small

STACK 100h

DATASEG

color db 12

CODESEG

start:

mov ax,@data

mov ds,ax

; Graphics mode

mov ax,13h

int 10h

;Initializes the mouse

mov ax,0h

int 33h

;Show mouse

mov ax,1h

int 33h

;Loop until mouse click

301
302 – 13

MouseLP:

mov ax,3h

int 33h

cmp bx, 01h ; check left mouse click

jne MouseLP

;Print dot near mouse location

shr cx,1 ; adjust cx to range 0-319, to fit screen

sub dx, 1 ; move one pixel, so the pixel will not be hidden by mouse

mov bh,0h

mov al,[color]

mov ah,0Ch

int 10h

;Press any key to continue

mov ah,00h

int 16h

; Text mode

mov ax,3h

int 10h

exit:

mov ax,4C00h

int 21h

END start

302
303 – 13

Debug

. , . ,
,
, .
, , ,
.

, – ,
.

, ,
.

. ,
. .
: .

. : , -

. , -

, . , , -
? 2‫ ־‬cx ,

shr cx,1 ; adjust cx to range 0-319, to fit screen

, , . ,
: .

mov ax, 5 ; copy ‘5’ into ax

, . .
... , ,
.

, , .
.)" " ( .'

303
304 – 13

) (

304
305 – 13

. ,
, .
, - .
: .

, , –" " -
. ,
:bmp

; Process BMP file


call OpenFile

call ReadHeader

call ReadPalette

call CopyPal

call CopyBitmap

? . . .
, " " , " "
: , .

; Process BMP file


call OpenFile

;call ReadHeader

;call ReadPalette

;call CopyPal

;call CopyBitmap

( . ,
. ? .)

. TD‫ ־‬. -
. , ReadHeader‫־‬
header .header‫־‬ DATASEG

305
306 – 13

, – 'BM' .
.

. , -
, , ‫־‬
, .
, - , .
, .
.

, ,

. , ,

, . , ,

.)cx , (

. .

, ,

, .

– ,DATASEG‫־‬ . ,

. CODESEG‫־‬ –

. ,

. ,

306
307 – 13

: , ,

. – ? .A2034: must be in segment block

)StackOverflow ( ,

“Key pressed” , .
- . ESC .“Key released”
scan code- , .
.

. . , .

. 5X5 , .
: .

25 , .a

" " .b

, , .

307
308 – 13

– .

. bmp ASCII

. , ,

. ,

. , , :

, . , .

! .

308
309 – 13

–'
- .

. )" " (

. ,

ADD JNA NEG


AND JNAE NOP
CALL JNB NOT
CLC JNBE OR
CLI JNGE OUT
CMP JNL POP
DEC JNLE POPF
DIV JNO PUSH
IDIV JNP PUSHF
IMUL JNS RCL
IN JNZ, JNE RCR
INC JO RET
INT JP ROL
IRET JPE ROR
JA JPO SAL
JAE JS SAR
JB JZ SBB
JBE LAHF SHL
JC LEA
JCXZ LOOP
JE LOOPE
JG LOOPNE
JGE LOOPNZ
JL LOOPZ
JLE MOV
JMP MUL

309
310 – 13

-CLC

-STC

-CLI

Signed Unsigned

JG (JNLE) JA (JNBE)

JL (JNGE) JB (JNAE)

JGE (JNL) JAE (JNB)

JLE (JNG) JBE (JNA)

JE JE

JNE JNE

cx=0 -JCXZ

JC

JNC

310
311 – 13

JZ

JNZ

JS

JNS

JO

JNO

JP / JPO

JNP / JPE

ah- 8 -LAHF

-PUSHF

-POPF

- , LOOP-

- . cx ,LOOP -LOOPE (LOOPZ)

.LOOPZ LOOPE cmp .

- . cx ,LOOP -LOOPNE (LOOPNZ)

311
312 – 13

- , SHL, SHR

- , , SHL -ROL

. , ROL ah .

- , ,SHR -ROR

. ,SHL -RCL

. ,SHR -RCR

SHL- -SAL

SHR- -SAR

. ,SUB -SBB

312
313 – 13

: –'
, . Piazza

, " " , .

! , .

:Piazza-

"students get started" www.piazza.com .1

"gvahim" .2

313
314 – 13

"other" .3

assembly .4

314
315 – 13

"assembly" student- .5

submit email , , .6

315
316 – 13

submit .7

I am not pursuing a degree . .8

. . , -Continue .

316
317 – 13

.new post ,

- Post type = Question

- Post to = Entire class

- Folder = Other

Post my question ,

317
318 – 13


http://edjudo.com/wordpress_livedec10/wp-content/uploads/slider/digital.jpg
http://visual6502.org/images/pages/Intel_8086_die_shots.html
http://www.ousob.com
http://en.wikipedia.org/wiki/MS-DOS_API
http://iitestudent.blogspot.co.il/

318

Potrebbero piacerti anche