You are on page 1of 317

2.

04
:

: ,
.

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

12 ....................................................................................................................................

1 15 ........................................................................................................

2 18 .........................................................................................

? 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 .................................................................................................................................

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

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

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

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

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

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

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 ...............................................................................................................................

8 137 ...............................................................................................................

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

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


NEAR138 ..................................................................................................... FAR

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

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

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

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

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

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

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

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

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

9 152 ........................................................................................................

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 ...............................................................................

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


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

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

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

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

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

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

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

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

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

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

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

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

11 211 .....................................................................................................................

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

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

ISR( IVT) 214 ............................................................................................................

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

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

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

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

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

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

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

232 ................................................................................................. Exceptions

232 ..........................................................................................................Traps

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

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

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

240 ................................................................................................................................
240 ................................................................................................... Interrupts

243 ..................................................................................................... PIC

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

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

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

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

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

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

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

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

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

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

13 260 .........................................................................................................

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 "" , .

11 . DOS -
.

12
13

12 , . -
. ,
.

13 - , -
.

2.0

" , 25- ,
, . :

reverse engineering . -

.calling conventions, stack overflow - -

, : . -

( ) -
.

"" . ,stackoverflow
- . ,
, . .

Art of assembly Randall Hyde .


( NASM ,TASM ) ,
. .

" 5" "


. .

.
, .

. ,
. : ,
, , . , ,
.

13
14

, :

14
15 1

1
! , .
? , . .

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

.
, .

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

. Low Level Language


, .
- , , '.

, .High Level Languages


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

15
16 1

. :

( ) .

. , .

, . ,
. ,
, :

.
" " ,
, .

. , Reverse
Engineering .Stack Overflow
, , .

. ,
, . , Apple,
.

.
, :

Cyber Security Researcher


)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 , .

, ,
. , , ,
.

,
,
" " . .

. ,
- ,10 8 .3

,10 0- .9 9
.

8 0- .7 7
.

,3 0- .2 2
.

18
19 2

. ?

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

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

19910 :5

.199:5 39 .4

.39:5 ,7 .4

7:5 ,1 .2

1:5 ,0 .1

, .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

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

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

21
22 2

.100112 .
, :

100112 = 1 + 2 + 16 = 1910

"
" , .2 :19

, .100112

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

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


C0FFEE16, DEAD16 .C0DE16

,0 o .

22
23 2

16 C0DE16 -

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

0x 0xC0DE -

, ,4F16 , :

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

: , :19910

.199:16 ,12 .7

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

19910 = C716

. , 16 .2
:

23
24 2

. ,
, .
:9B16

916=10012

B16=10112

9B16 = 100110112

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

:2.1

,10 . ,2
. ,16.

24
25 2

, , .

. 133.70

.
, 10 ("" carry
) .

,
.2 , .

0 0 .0

0 1 .1

1 0 .1

1 1 ( 10 ).

25
26 2

1010 ( 11 10 :)3

. 0 1 ,1 .

1 1 ,0 .

, ,1 0 ,0 ,1 .

, 1 0 ,1 .

, 16
. , ABCD16 :12316

. 13( D ) 3 ,16 0 .1

C 2 ,F .

B 1 ,C .

A 0 .A

26
27 2

:2.2

27
28 2

()

, :

.1 . 9 .6 .

.2 , .

.3 . 6 .9 " " . ,
.

, 619 . :21

9 1 .8

1 2 ,
. 11 ,2 .9

,6 .5

2 1010 10( 1 .)1

. 0 1 . ,
( 10 )2 2 . ,1 .1

, 1 . .0

, .

10 : 1 ,9 2 1001 .

28
29 2

:16

D E . A .D
( F 13 16 ,14 .)15

, A .D , .
( C 10 ,1 16 ,13 .)12

, ,E .D

D C .1

:2.3

29
30 2

()

. ,
,10X10 2X2
:

1010 10( 11 3):

30
31 2

1 1010 ,1010 .

1 1010 ,1010
.

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

:2 ,10 ,
, .0.52x10=520 ,

, .52x100=5200 :10

, 2 . ,
.0

, .11x10=110 :

.11x100=1100 :

.11x1000=11000 :

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

:2.4

31
32 2

()

1/1 = 1

0/1 = 0

, .
, .
.

:101102/1012

,100 .10

22 ,5 ,4 .2

:2.5

32
33 2


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

N . ? ,
1:8

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? ,
,255+1 8?

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

33
34 2


. 0000 0001 ,
0010 , . unsigned .,
?

.signed

N , . ,
.:

0 . 1 .
, ,unsigned
.

.0011 : , ?

,0 . ,011 .3 0011 ,
.3

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

, 4 ,0111 ,7 ,1111
.7

- 0000 1000 !

34
35 2

. ,
.

. 3 ):(-3

, ) .(-6 ,
.

( )One's complement ,
.

, - . 0 ,1 1
.0 0001 , " ."1 " "-1 .1110

0 7 :

- . 0 1
. , ,1111 .0
.

, , :
, .

35
36 2

, 5 .2 5 ) ,(-2:

, , .3

, ( )
. ,
. 1111=0000 - ,
.

0 1 .
1 ( ,
).

, .

, ,6 8:

6 .00000110

.11111001

,1 .11111010 6 , 8 .
1111 1111 1111 1010 6 16 ,
( , , ) .
6 32 .

36
37 2

6 . ,6 .

, ( 1 ).

. , 8,
4.

, .0000 0000 .
, !8

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

,
.

37
38 2

, (
) . :

, 64 ,32 ,16 ,8' , -


.

( )0 - -
, .2

( ,)1 ,2 -
, .

, .10111111 ,1 :2

20+26 =65

)-65) :

:2.6

2- .
8 . 2- :
. 12 00001100 2

-9 247

-128 128

-94 162

-102 154

-1 255

, ?

38
39 2

?
... Pearls3 ?

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

, . ,
, , .

, :

.1 "" ( 7 111 -).

.2 - ( 1+1 0
)10

.3 ( )Nim Sum - , . ,
.

3 4 ,3 : 5- .

( , ,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 ,
.

, , ? , . ,
, . , ,
.

, . 1100 2
,12 ( 4 ) .
signed .unsigned

, , .
, 2 , . ,
.

, . ,
.

Nibble

Nibble Nibble . 2 4 , 16.


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

1101 1110 1010 1101 1100 0000 1101 1110

41
42 2

1101 1110 1010 1101 1100 0000 1101 1110

D E A D C 0 D E

... ?

Byte

( )Byte ,
, .
. , , ,
( , )
.

:Nibbles

42
43 2

Word

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

Double Word

( Double Word )DWORD 32 , ,


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

ASCII

,American Standard Code for Information Interchange ,ASCII


. 7- 128 ,
0.127-

43
44 2

, ! HELLO WORLD ASCII- :

72 69 76 76 79 32 87 79 82 76 68 33

H E L L O W O R L D !

48 45 4C 4C 4F 20 57 4F 52 4C 44 21

" ( ) :


.
: , , ,
.

- , , - .

. : ,
, .

, .
,
.

44
45 3

3

.
, . ,
, , .
, .
.

80x86 . 80x86
, X . ,
,8086 .1978 , .

, ?
. ?

, :

80x86 , .
( ).

.80x86 ,
,8086 .

, 80x86 .

45
46 3

Von Neumann Machine


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

' )19571903( John Von Neumann

,VNA ,80x86 .
( ) . ,
/ , .
/ , / ( ).

46
47 3

/ ,Bus
. , .

,VNA

47
48 3

( 8086 , 33" ) .
. . -
I/O .

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

? , , ?

" ." 1000h , ?


.

48
49 3

DATA BUS

.
. 32 ,16 64.

16 16 . 16
16 . 16
.

32 ,16 64 , ,
. , ,
. ,
.

ADDRESS BUS

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

, I/O ,
.
, " .

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

49
50 3

CONTROL BUS

.
, ,
?

, ( )read ( ,)write .
read write ,1 . read ,
. write , .

32 ,16 64 , .
byte enable .

80x86 .I/O-
,80x86 I/O
16 . 65,536 .
, .I/O

, ,2n n .
.

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

, 1976 " ,"0 :

.1 " "0 .

.2 1976 .

.3 write ( 0- "").

50
51 3

1976 , :

.1 1976 .

.4 read ( 0- "").

.2 .

51
52 3

.1 1970 8 , ,Byte .0ABh

.2 1974 16 , ,word .0EEFFh

.3 1976 32 , ,double word .12345678h

, .
h .
, ,ah ah .

, :

.1 1970 .0ABh

.2 1974 ,0FFh 1975 .0EEh

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


.12h

L.O , H.O .

L.O. Byte, H.O. Byte

52
53 3

. , :

Byte .1 ,1975 .0EEh

Word .2 ,1978 .1234h

Double word .3 ,1974 .05678EEFFh

Word .4 ,1970 byte 1970 1971


, !

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

Segment:offset

53
54 3

,80x86 16 :
. .

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

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

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

3DD60h + 0012h = 3DD72h

0 ,16
.

, .
codeview .dosbox , . codeview-
.

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

54
55 3

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

. .
.0627:0001

ASCII- .

, .
.0628:0000 16 :

, 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
,I/O dx .

si
Source Index

(
.)bx
.
Destination di
Index

bp
"" .Stack Base Pointer

sp .
, sp sp
Stack Pointer
.
sp .

57
58 3

" 16 , . 8.
, . ax ,
,
.

, cx ,bx ,ax dx
ax . , ah H .al ,high 8
,ax L ,low 8 .ax

( mov )move
.:

mov ax, 0FF00h

, ax :

mov ah, 0FFh


mov al, 0

8 . al ,ax.

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

8:

58
59 3

. 17 :21

mov ax, 1234h

mov bx, 0

mov bl, 34h

mov cx, 0

mov ch, 12h

" .

59
60 3

bl :bx bl 8 .bx -

ch :cx ch 8 .cx -

sp, bp, si , ,di 16


cx ,bx ,ax.dx

, .
. .
.

60
61 3

Segment Registers

8086- ( 80x86- :)FS -

CS - Code Segment

DS - Data Segment

SS - Stack Segment

ES - Extra Segment

16 . . Segment Register
.

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

,ds ds-
,DATA . , small
64K , DATA ds
.

61
62 3

ss ( )STACK .
. sp ss
.

,es, Extra Segment ,


. , , .

Special Purpose Registers

IP - Instruction Pointer-

FLAGS -

IP . 16 ()pointer
.code segment-

. 8 16 ,
. .
, .

, . , -
IP . ,
, .

, . ,
.

Arithmetic & Logical Unit

( ,Arithmetic & Logical Unit )ALU


. ALU ,
ALU , ALU
.

, 3 :ax

ax .ALU

ALU .3

ALU- ".

ALU- .ax

62
63 3

Control Unit

, .
. ,
.
.
.

,Control Unit , , Operational


Codes ,OpCodes .

OpCodes , OpCode 8 .
,Instruction register , ,IP . IP
. OpCode- , IP
.

()

, .

, ax :bx+5

mov ax, bx

add ax, 5

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

. ,OpCode ( ) , ,
. , .

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

63
64 3

, ( 0
)1 ( 1 .) 0
, clock cycle ,
. ,
, clock cycle.

. .
. clock
cycle , , :clock cycles

64
65 3


, :80x86
80x86 ,
.VNA VNA .
, , .

, 80x86 ,
. , .
. , , ,
.
, .

, .
,
. .

65
66 4

4

. ,

, . ( )

, . ,

, .

, ,base.asm

( .)Debugger ,

. .Opcodes

.Opcodes

Editor Notepad++
- . .

"" , ,Word .

. Visual Studio ,Notepad Editor . Notepad++

, , ' . -
.

: Notepad++ ,

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

66
67 4

. hello.doc, doc .hello hello -


.

.asm , Notepad++ -
.

asm 8 . .DOS -

Base.asm

. .
, . ,
Notepad++ ( ,
) . , .base.asm

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

base.asm

.base.asm ,
, . ,

.

67
68 4

: .DATASEG
. DATASEG
. . DATASEG .

,DATASEG:

var1 db 5

var1 .5 ( ) ,
var1 .5

.CODESEG
. ,
.

,CODESEG:

mov ]al, [var1

, 5 ,var1 .al

.base.asm

IDEAL Turbo Assembler , IDEAL . -


.

MODEL small ,small Data, Code, -


Stack 64K .

STACK 100h . . -

DATASEG . -

CODESEG . -

start . - -
-main end .

mov ax, @data , ds . @data -


.data mov ds, ax ds -
.data

68
69 4

exit . exit -
, .
.DOS

end start end , -


. end
. , .exe ,
.start

Command Line
, , .Command Line Command
Line , , ,
.

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

69
70 4

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

CD Change Directory

CD DirectoryName
" ."DirectoryName:

CD Games
:
CD ..
:
\ CD
:

70
71 4

DIR

.
, .dir /p p page :

EXIT

Command Line , exit .enter

DOSBOX

80x86 .
( . )Emulator . ,
, , .

,Commodore 64
,1982
Commodore 64

71
72 4

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

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

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

\Mount c: c:

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

C:

.C

Command Line- .

, -
.

,
cycles 3000 ( ) .
:DosBox-

Cycles = max

72
73 4

(
, .)cycles 3000 CPU
.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 .
, ,TASM .Turbo Assembler
TASM 5.0 .1996

Linker .
, .
. , ,
. .

TASM . ,
.

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

.c:\tasm\bin -

rar ( ,rar ""rar download -


).

base.asm .bin -

Dosbox ( bin ,cd ). -

base.asm : -

tasm /zi base.asm

zi .debug .base.obj -

- tilnk : -

tlink /v base.obj

v .debug .base.exe

74
75 4

, base ,enter td base


.

, base.exe .Turbo Debugger

Turbo Debugger TD
, :

td base

, ( ,)F10 view
, :CPU

75
76 4

Turbo Debugger

,Turbo Debugger ,TD .Help


:TD

. ,Step ,F8
. , .
, ,Trace .F7 , ,
.

base.exe TD .

,F4 Go to cursor .
F9 .

View . Views :

,CPU , :DATA -

76
77 4

: -

,STACK STACK , : -

Watches .watch -

. , :DATASEG

77
78 4

Var1 dw 1234h

Var2 dw 2345h

Watch var1+var2:

Variables .: -

78
79 4

, . CTRL+G :

( ,ds:0 , ):

"" :

ds ,DATASEG
ds . , F8
.

79
80 4

breakpoints

, breakpoint . ,
Breakpoints .Toggle- .
.

,breakpoint ) Run (F9

.breakpoint .

base.exe . breakpoint mov ax, 4C00

. F9-

TD

:Dosbox- ALT+X -

CTRL+F2 : -

( ) : F10 . -

Tab .

( ) : ALT+Enter -

, : "" ( Alt)Ctrl - -

80
81 4

() Opcodes

mov .

, , mov .

mov . .

Opcode mov ax, dx " ,

.1 base.asm . "Your code


" .here

mov ds, ax

.2 , .TurboDebugger

.3 . , mov ax, 2
.B80200

81
82 4

.1 ?mov mov .
:

mov ax,5

mov ax, bx

mov [120], ax

.2 ,Opcode:

mov ax, 5

mov ax, 6

.3 :Opcode

mov ax, 2

mov bx, 2

mov cx, 2

mov dx, 2

.4 Opcode- :

mov bx, ax

mov ax, cx

: .mov ax, dx .
,
.

82
83 4


, .
, . :

Editor Notepad++ -

,Command line DOS -

DOSBOX -

Turbo Assembly -

Tlink -

Turbo Debugger -

. .
.

83
84 IP, FLAGS 5

IP, FLAGS 5

IP .

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

FLAGS

: IP
. FLAGS .

,
.

IP Instruction Pointer
IP . 16 ()pointer
.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 :

. 1 , , ( .) mov bx, 0

85
86 IP, FLAGS 5

.2 mov ax, 1234h , ax .

.3 IP 0005 ,0008 .

.IP

, mov ax, 1234h .


IP .,
IP , .

5.1

IP .
. IP ?

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

IP ,0010h enter IP . .
.F8 ? 0005 0010
( ?) cx ,bx ,ax

86
87 IP, FLAGS 5

Processor Status Register FLAGS


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

:FLAGS-

, :Condition Codes

Zero Flag -

Overflow Flag -

Carry Flag -

Sign Flag -

,
( " .)" ... .

87
88 IP, FLAGS 5

Zero Flag

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

- Destination Operand - .
.

: 4Bh al ( ah :
?) . ( )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

. ,100h ,al
, "" ,00h .1

88
89 IP, FLAGS 5

. " "
. , .1
, .

5.2

, 16 . ,
, 16.

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

,al 8:

mov al, 64h ; 100 decimal

mov ah, 28h ; 40 decimal

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

''o

( ) 16 , ?

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


( ,(215-1 .+32,767

89
90 IP, FLAGS 5

5.3

,base.asm .

Carry Flag
, ,Carry Flag , ,
. ,
.unsigned , 8 ,
unsigned 0- ,+255
0 .+255 16 0 .+65,535

mov al, 0C8h ; 200 decimal

mov ah, 64h ; 100 decimal

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

( ) ( )
( ) .

:
mov al, 1h

mov bl, 2h

sub al, bl

, al ,0FFh unsigned .255 255


("" )256 .

90
91 IP, FLAGS 5

, , :

mov al, -128d

mov ah, 40d

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

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

5.4

,base.asm .

Sign Flag

1 ( ) .1
.0 , :Signed

.1 -

nibble 8 .F : -
( 0F1h, 0A3h, 088h ) , 0F100h,
( 0A300h, 08800h ).

5.5

base.asm . .

Direction Flag

. ,0
. ,1 .

91
92 IP, FLAGS 5

Interrupt Flag

"" ( .)Interrupts
. ,
, 0 . ,
.1

Trace Flag

.Trace ,
. debugger .turbo debugger
,0 .

Parity Flag

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

Auxiliary Flag

. 1 4- .AL
.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)

, ,
.


( ,)Special Purpose Registers IP .
IP .

, :

, IP ,
.

93
94 6 mov

6 mov

:

, , , -
mov .

( ) , . -

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

. , ,
, .

, .


, - .:

mov ]al, [ds: 1h

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

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

mov ]al, [age

94
95 6 mov

,
.

(. )Variables

, .

1 ,6

, 2 .12

.DATA base.asm " "start


:ds

mov ax, @data

mov ds, ax

ds .DATA , ,
.ds ,
.ds , age , ,
age "".

. ,
.

,DATA :

ByteVarName db ?

95
96 6 mov

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

mov [al, [ByteVarName

; al , , ""
.

, :

DATASEG

ByteVarName db ?

ByteVar2 db ?

ByteVar3 db ?

( )0 ,ByteVarName , .ds:0
. ByteVarName ,ByteVar2 ,
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:

; Define Double

96
97 6 mov

6.1

DATASEG ( var ,variable) .byte .


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

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

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

Signed, Unsigned

signed unsigned
. , Bytes
.

. Byte:

DATASEG

Var1 db ?

Var2 db ?

CODESEG :

mov [Var1], -120

mov [Var2], 136

97
98 6 mov

DATASEG . !
.88h

120 ?136 ?

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

? .
,+120 , 120 136
:

120 ,120 . -

120 ,136 ,256 .100h , -


, ,00h .

, signed - unsigned
. .

98
99 6 mov

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 6 mov

DATASEG

ByteVarName1 db ''H

ByteVarName2 db ''E

ByteVarName3 db ''L

ByteVarName4 db ''L

ByteVarName5 db ''O

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

:6.2

,base.asm DATASEG ,
, , , .ASCII
, DATASEG .

. , , ,
. ,
. , , ,
.

, .
, , ,
:

ElementAddress = ArrayBaseAddress + Index * ElementSize

100
101 6 mov

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

DATA :

ArrayName SizeOfElement (?) N dup

ArrayName , .

SizeOfElement , dd ,dw ,db


, .

N N . .

Dup ,duplicate.

, N .
, .

ArrayOfTenFives db (10 dup (5

, , :5

, .

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

101
102 6 mov

32 ,1,2,3,4 .

:6.3

, ,TD
.

DATASEG 5 ,3 : 7 (.)Bytes .

10 ' .'5 10 ' .'5 .


!

20 ,4,5,6 . .

MOV
. .

,mov " ."move ,


. .

mov . :

mov Destination, Source

Source .Destination Source


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

, ax ( 22) :

mov ax, 22

22 :

mov ax, 16h

mov ax, 00010110b

102
103 6 mov

mov bx, 199

mov cx, 2321

mov dx, 10

mov ax, bx

ax .bx bx- .
.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 ,
. :
, , . opcode mov
.

103
104 6 mov

mov register, register

8 16 , ( , ) ,
. :

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
. cs . mov cs,ax : . cs ds
.

6.4

ax .bx .

bx .ax .

ah .ch .

al .dl .

104
105 6 mov

mov register, constant

16
( )257 8 , .

mov cl, 10h

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

mov ax, 555

6.5

al ( 100) , : ,
( : ,b .)00001111b TD al
.

mov memory, register

.
, ( ,)Direct addressing (
)Indirect addressing (.)Indexed 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

, ax .1

, ax .Var

, ax .bx :

105
106 6 mov

mov [1], ax

:
mov bx, 1

mov [bx], ax

, ax bx ,1
.bx

6.6

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

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

,
:

mov register, memory

, , :
mov ]ax, [1

mov ]ax, [Var

mov ]ax, [bx

mov ]ax, [bx+2

106
107 6 mov

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
TD al . al
( 100 ,).

107
108 6 mov

mov memory, constant

5 :bx

mov [bx], 5

: :

mov [byte ptr bx], 5

mov [word ptr bx], 5

. , si ,bx .di cx ,ax dx


. :

mov ]cx, [ax

, , :

bx . : -

mov [ax, [bx

mov [bx], ax

,bx bx .: -

mov [ax, [bx+2

mov [bx+2], ax

108
109 6 mov

,bx di ,si -
.:

mov [ax, [bx+si

mov [ax, [bx+di

mov [bx+si], ax

mov [bx+di], ax

, bx si ,di .: -

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

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

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

: ,ax 16 , ? ,
. .mov
DATASEG , DATASEG , :

109
110 6 mov

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

, ' 'AABBh , Extended Segment


ES :

110
111 6 mov

Little Endian, Big Endian

, "" ,ax
ah . ?
. ah .

, ( bx ,ax ') ,
, :

( High Order ah, bh') . Big -


.Endian

( Low Order al, bl') . Little -


.Endian

. , 8086
.Little Endian :

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

Little Endian Big Endian- " "

mov .
. :

DATASEG:

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

111
112 6 mov

2 ( al al , ax ) .
:

mov [al, [Array+2

. Array ,DATASEG
Array 0002 DATASEG :

al ,0CCh 2 .
, ,0 .1

, .:

mov [Array+1], al

, al 2 ,
.

offset

bx , :offset

mov bx, offset Array

mov ]al, [bx

,al , Array- . Array


,words ax .

? . ,
, bx
bx .

112
113 6 mov

LEA

offset ,lea .Load Effective Address ,


.

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
ax ( 16).

, .
:

mov ]ax, [word ptr Array+2

? byte , ( word ).
ax ?

113
114 6 mov

: ax .word .Array+2 :

, .

: word ptr byte ptr word byte


. , , TD word ptr : .byte ptr

type override

mov , .type override


, :

mov memory, constant

5 ,bx:

mov [bx], 5

? ,5 5
, , ... .5

. :

mov [byte ptr bx], 5

mov [word ptr bx], 5

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

. ,
, byte ptr .word ptr

114
115 6 mov

- 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

. , var2 ,var1
:

mov ]ax, [var2

mov [var1], ax

.4 .:

mov [ax], 5

. si ,bx di (
, ).

.5 .:

mov [bx], 5

115
116 6 mov

5 (
32 64).

.6 .:

mov 5, ax

()
?

,
. . :

] ,[1

mov [1], al

] [1 20?

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

mov [ds:1], al

,
.

mov [cs:1], al

? al- ,cs .
. ,
? ?

, , .

116
117 6 mov

:6.8 -

. :

xor ax, ax

xor bx, bx

add ax, 2

add ax, 2

. , ax .4
, ax .3 :

( ) o

mov o

. , ax 3 bx .3
' .

, , :mov

: ,
.

117
118 7 ,

7 ,

.mov ,
:

: , , -

and, or, xor, not : -

shr, shl : -

. .


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

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

add ( )source ( ,)destination


. , . ,
:

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 -
. !

8 16. -

:7.1 add

6 . . al . .
,TD .al

, al ( ?) .
, .ax

: .

var1 -

var2 -

sum -

sum ( )?sum

119
120 7 ,

SUB

( sub )subtract source ,destination


. , .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

:7.2 sub

: .

var1 -

var2 -

diff -

diff ( )?diff

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

120
121 7 ,

INC / DEC

( inc )increase .1 ( dec )decrease


1 . , , add
,sub 1 1 , . :

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

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

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


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

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


ax ax .dx ax=0AB00h dx
.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 ,

signed ,unsigned .
. 11111011 .2
.251 , , .

mul ( unsigned ) , imul


( signed) . .

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


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

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 ,

,mul al 251 ,01F6h 502

- ,imul al , ,0FFF6

:7.3 mul

,byte ,255-0 .
( : ?)

,byte +127 .-128 .


( : ?)

, 4 .signed , . .
.sum , .word:
a ,b

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

123
124 7 ,

DIV, IDIV

( div ) divide . 8 div , ax


, al .ah 16 div ,
32 ,dx:ax ax .dx

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

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

div , 8 8 (
16 16).

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

? :

( ?) -

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

idiv ,div ( ,)signed div-


(.)unsigned

125
126 7 ,

:7.4 div

,byte ,unsigned .
.

,byte ,signed .
.

,word ,unsigned .
.

NEG

( neg ) negative , ,
.:

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 .

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

00000000 00000001 00000001 00000001 00000000 00000000 00000001 00000000

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

or xor .and

not :

not register

not memory

AND

and , . ,1 .1 , .0
" " . ,
. and .

and

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

0000 0111 and

1001 0110

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

0000 0110

128
129 7 ,

and .

, . 0
. 1 .

, ( .)MASK ,
. , .00000001 :

and . ,
and 0 . , . ,
and ,1 and
, ,1 .1 , .0

:7.5 and

and ?4 - .

129
130 7 ,

OR

or :

, ,1 .1

or "" .
, ( , ,
) . 1 0 ,
. .

, ( 4 0 .)7 .
:

1100 0100

,4 ,4 . , .

, or ( 0001 0000 4) :

1101 0100

4 , , .

.4

:7.6 or

, ?2 2 4 .
( or)?

?4 .

130
131 7 ,

XOR

( ,xor ,)exclusive or :

xor , ( 2 , ).

xor , . :

xor , !0

. :

1001 0011

, . ():

0101 0100

.xor :

1001 0011 xor

0101 0100

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

1100 0111

, xor :

1100 0111 xor

0101 0100

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

1001 0011

131
132 7 ,

, ,
. .

, opcode xor ,mov xor


. :
mov ax, 0

:
xor ax, ax

ax ,0 ,
.

:7.7 xor

xor . .

msg .ASCII ' .'I LIKE ASSEMBLY$ .


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 .


"" . , 0 ,1 1
2 . .

, . shr :
) (shift right.(shift left) shl

SHL

, shl , . ,
.cl :

shl register, const

shl register, cl

shl memory, const

shl memory, cl

shl :

, . -

0 ( , 0 . n, -
n).

.CF -

OF 1 . . -

ZF 1 . -

133
134 7 ,

. -

1 8 . -

shl 8 . 16
16 :8

shl

SHR

,shr , .shl .
, .cl
:

shr register, const

shr register, cl

shr memory, const

shr memory, cl

shr :

, . -

0 ( , 0 . n -
, n).

.CF -

OF . -
.

ZF 1 . -

, . -

1 8 . -

shr 8 . 16
16 :8

134
135 7 ,

shr

( ) -

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

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

.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

al .3 , al.4 .

al ( 120) . , al.8 .

al ( 10) . , al .20 : .
20 16 .4 .

signed ,:unsigned

o
o
o
o

And o

Or o
Xor o
Not o

, ,
.

, :

Shr o

Shl o

, .

136
137 8

8

(" , .)"...

, :

( jmp ). -

( .)cmp -

( .)loop -

, . , , ,
,IP .CODESEG
. , ,
" - ,1 .
" . , "
, ".

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

JMP
jmp , , . jmp ,
.CODESEG ,jmp- IP
.

DATASEG

address dw 000Ah

CODESEG

mov ax, @data

mov ds, ax

137
138 8

mov ax, 1

jmp ][address

:jmp

,jmp :ip

.CODESEG

NEARFAR

,jmp
000Ah .address jmp ,
jmp- . , (
) , .near ,near jmp
.

far . ,
, . ,
. :

jmp cs:offset ; for example cs:000A

cs- .

138
139 8

. ,
near far .

:8.1 jmp

( CODESEG- :)base.asm

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

LABELS

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

. label -
, .

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

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


.Not_Positive , .

139
140 8

Label1, MyLabel, Shooki :' . .


, , ( ).
,label . label
( )ax : ( ,)MOV :
.

: .label
.

label :8.2

,jmp jmp .label label .

140
141 8

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

: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

al .3 mov , -
.

, al .3 cmp , . -
( al )3 . 0 .

141
142 8

al .2 al 2 . -

al .5 al 5 ' '1 -
,
.

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

, .cmp -
cmp , .

, , . , .1 -

, . .label -

, ( , -
).

: , ,cmp .
. , cmp .

, .cmp
, signed
.unsigned

1b ?10000001b

. ,unsigned
( 10000001b 129 ) .1b , 10000001b , signed
,127 .1b

142
143 8

, , ,cmp ,
. , :

,J .Jump cmp unsigned


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

. :

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 :

,JA ,unsigned ( 129 .)1


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

:8.3 ( )

: , .

ax ( ax signed-) , .
.

,ax=bx bx .ax .

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

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

':'x .

144
145 8

mov 'dl, 'x


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

TimesToPrintX ? ? .

LOOP
. ,
' 'x .TimesToPrintX ,
' 'x , ,TimesToPrintX
' 'x .
( .)Loop , .

loop :

1 .cx -

cx. -

( , cx ) jmp label-.

loop SomeLabel

dec cx

cmp cx, 0

jne SomeLabel

loop ' 'x:

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
cx , , cx ( 65,535 1-
.)unsigned cx 65,534 65,536 cx
.

, ( ):

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

10 ,LoopA LoopA 5

.LoopB LoopB , 50 . ?

loop .cx LoopB cx .

LoopA 1 ,cx LoopA 65,536 . cx ... ,

, (
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 ( ,1=0+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 .

,Var1*Var2 ,unsigned .
. : sum=Var1+sum
Var2 loop.

: ''x .
.
5 ,4 :

xxxx
xxxx
xxxx
xxxx
xxxx

148
149 8

, 0 9
: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 .

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

- :

.1 .

.2 - ,
.

.3 , .

. , ja
:

cmp ax, bx

ja my_label

. ; more than 127 bytes in code memory

my_label:

. ; some code here

ja-:jmp-

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

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

,jmp . labels
.

,cmp .

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


.)"...

signed unsigned
.

loop ( cx .)cl

, .
, .
. .

151
152 9

9

. ,
? ?

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


, . :

. ,

. , ,

, , .
. .

, ,label , ,ReadPassword

? 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

, 256 , ( :)base.asm

STACK 100h

. , :

stack segment ss . .

154
155 9

stack pointer sp . .

,sp , . .bx
DATASEG ,ds:bx STACK -
sp .ss-

, sp . , ,100h
sp .100h

,100h 256 , 100h 0


.0FFh , ,100h ,sp .

.100h ss
. sp
,100h .

sp ,
. ,LIFO Last In First Out
. , sp
sp . ,
.

155
156 9

PUSH

push . :

push operand
?

sp .sp=sp-2 : -

, .ss:sp -

: sp ,push
. .word
push .

:push
push ax

push 10

push var
.ax

( 10 ,word- .)byte

var .word

push al

: al . push .

, .100h ?

mov ax, 0AABBh

push ax

156
157 9

push sp2

,ax ,ah .

. 2 .sp:

push 0CCDDh

157
158 9

POP

pop .push ( )
:

pop operand

. -

sp .2 -

:pop

pop ax

pop []var

ax .

var ( var ,
).

pop al

pop .

158
159 9

: sp ? ?bx

pop bx

: bx ,0CCDDh sp .0FEh

: , pop:

pop var

sp var ?

: var ,0AABBh sp .100h

? ?

! , sp
. :

sub sp, 4

"" ,sp .

:9.1 push ,pop

.10h, 20h . sp . .

1234h .ax push .ax ?sp .


.

pop .ax ?sp 1234h .


?

push .5678h ?1234h .

ax bx .mov .

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

,ZeroMemory 10
( DATASEG , .)0 ,CODESEG
:

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

DATASEG 8 ( )

9.2

.ZeroMemory TD ip
sp

CALL, RET

call . :

call ZeroMemory

call , ,ip=12 12h ,CODESEG

( call .)... :

162
163 9

ip :call

ip .0000- ZeroMemory ( , offset


,CODESEG .)CODESEG , call
ip . .jmp
call . , .sp call
. call 100h .0FEh .

163
164 9

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


call .0000h ? .

.ret :ip=0Ah

,ret ip
:

ip , 0Ah.15h

?15h ... , .call

call ret:

164
165 9

call

.1 sp ( sp
FAR ).

.2 ,ss:sp .

.3 ip ( jump
)

- ret

.1 ,ss:sp .

.2 sp ( ; ).

.3 ip ,ss:sp-
.

, , ip . callret
ip !

NEAR, FAR

.CODESEG call .
:

.1 ,CODESEG .

.2 ( CODESEG ).

, ,model small ,
,CODESEG ,
, .CODESEG

,CODESEG .near ZeroMemory


.near , call ,
.CODESEG , sp .2

, ,CODESEG .far , call


, .
, sp .4

165
166 9

call near ?far

, near .far:

proc ProcedureName near

proc ProcedureName far

near ,far .near

. :"far" ZeroMemory

proc ZeroMemory far

, . . :

cs:0012 , ."push cs" : -


cs . ,0879h cs
( ).

,ip ( 16h 15h , -


".)"push cs

, sp ( 00FCh ,00FEh ). -

, .near cs.
, near.far

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

, , . , .
? , .TD .cx

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

) . row ( cx ) ,
, cx 0 ...

, ,
( ) .

push pop Print10X .


: ,
.

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

push pop ( ) . ,
cx . cx .4 PrintXLoop , cx
. pop cx ,
.

, TD cx !

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 -

, , . ,ZeroMemory , bx
.
.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

. NumOfZeroBytes ,ax
. MemoryStart ,bx
.
( , al bx , ,
).

172
173 9

,
DATASEG , .
, ,
? ()?

. ,
: (
, ,64K ) ,
, ,
.

.1 Pass by Value

.2 Pass by Reference

Pass by Value

. , .
, . ,
.

. ,SimpleAdd .2
, . ,
SimpleAdd DATASEG
. SimpleAdd .

, .parameter SimpleAdd
.parameter

173
174 9

, :

push ][parameter

call SimpleAdd

parameter . , .
parameter .DATASEG ,
. 2 , " parameter"
.

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

push ][i

push ][j

push ][k

call SimpleProc

174
175 9

, ( ):

i, j, k

SimpleProc .pop
,ip , .call pop
.

.
, . ,
.

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

ReturnAddress .DATASEG , .
.-
.

Pass by Value :9.4

pass by value ''X .


. : !
. :

mov 'dl, 'X


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

, max .min .
pass by value max
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=4, var2=5 .)var1=5, var2=4

BP

pop [ReturnAddress]- .
, , .ip

,bp ,Base Pointer


.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

,i , 8 .bp i k
6 4 , , .bp bp .

. ,SimpleProc .ax=i+j-k
:
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

: equ- .pass by value ,


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

: , bp mov bp, sp
.

, push pop ( )
, , ,bp .
:bp

proc WrongBP

push ax

push bp

mov bp, sp

180
181 9

WrongBP

ax ,bp bp . bp+8 j i.

ret 6 .

ret :call ret ,


sp ,2 . ( ret ret
:)bx

pop bx ; pop increments sp by 2

jmp bx

,ret ,pop- sp- .ret


ret 6:

pop bx ; pop increments sp by 2

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

jmp bx

ret push .
push 2 , 6 .
ret 6 sp "" .

181
182 9

ret , ,pop ret 6 ,


.

:bp

.1 pop .

.2 pop . .bp

.3 .

,bp , bp- .

:9.6 bp

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

.var1, var2, max : max pass by .


reference .pass by value max ,
.var1, var2

182
183 9

()

, .
, . ,
.

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

sub sp, 6

, ( sp
:)ip

add sp, 6

.x,y AddXY SubXY


x ,y.

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

, AddXY .SubXY
:bp

5 .4 .
, ( )0009 ( ,)0001
AddXY ,SubXY.

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

: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 ., .
2,2,3,4,5 .sum=16

186
187 9

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

FindMin , -
.

Swap pass by reference -


.

SortArray .FindMin Swap -


: FindMin .

SortArray , -
FindMin . SortArray , -
Swap : FindMin .

. -

,Sort2Arrays , .
sorted ,
.:

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

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

Merge , -
.

SortArray .Merge -

Filter -
.

187
188 9

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

.1 , .

.2 ,
.

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

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

, .

GetName 10 . , :

.GetName - 10.

,Jon Snow , 8 ( ) . ASCII-


:

. ( J (4Ah :ASCII .ss:00F2


,ss:00F9 . 10 8
, "" , , . "" bp
,ip .

. ' ,'Jon Snow 5


. 14 10 , .Stack Overflow
:

191
192 9

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

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

2120h ip ' .'Here be dragons


" , . ."...

Psalter .1265- ,
,.

192
193 9

, Stack Overflow-
. , (
) .

:9.8 Stack Overflow

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

193
194 9

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

Convention . .

, , Calling
.Conventions ?Calling Conventions-

:C

;)int MyProc (int a, int b

?MyProc integer
.integer .

;)int c = MyProc(1,2

("") Caller , ("") ,


.Callee

MyProc ,C
. - Caller-?Callee-

.1 :

. ':

push 1

push 2

call MyProc

':

push 2

194
195 9

push 1

call MyProc

' Pass Left to Right ' Pass


.Right to Left .

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

.2 :

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

caller- callee- .
, caller- callee- .

.3 :

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

ret 4

:sp

add sp, 4

caller- callee- . callee- ,


ret . callee- , .
:

call MyProc

add sp, 4

, caller- callee- .

: caller- callee- ? ?

195
196 9

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

:CDECL

.Right to Left -

( ax ax , 16-) -

caller- . -

:STDCALL

.Right to Left -

( ax ax , 16-) -

callee- . -

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

? , print
, . ,print
print .

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

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

196
197 9


, .
, . ,

, .

, , pop.push

, ,
. call .ret .
:

Pass by value -

Pass by reference -

,
bp .equ

. ,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://www.cyber.org.il/assembly/codeguru-guide.pdf :

http://www.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 .1 XCHG , ,

xchg ax, bx

ax .bx- .mov

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


, . - LUT-
' . '

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

6 ,8 7 13 ( -
).

199
-10 ()

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

'Cipher db 97 dup (0), 'mf

mf . :

mov bx, offset Cipher

'mov al, 'a

xlat

al- ASCII- ,m .

:NOP .3 ,No Operation " " .


, .

:STD / CLD .4 STD CLD . ?


.MOVSW

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

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


. di, si

200
-10 ()

. STD CLD
.

:REP .6 MOVSW ,REP


.Repeat cx

rep movsw

my_label:

movsw

dec cx

jnz my_label

'.

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

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

- .
. ,
, . .

201
-10 ()

duck.com

. .duck.com - ? .

. cmd- duck .enter ...


, .
? , . .

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

.f7 .
jmp 100h .100h , :

start:

jmp start

end start

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

202
-10 ()

coffee.com

,RE , .coffee.com RE
.

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

, ) (properties :

203
-10 ()

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

?coffee

- int 87 .
.

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


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

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

.coffee.com- RE
.

204
-10 ()

codeguru.com

.CodeGuru Extreme 2015 ,


.

. , ret- .cs:011Ch

. .

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


bx .1Dh

. ax 2015h

. xor ax cx- .xlat :xlat al


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

205
-10 ()

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

.cs:011Dh .ret- add .


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

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

. 2015
, xor ,LUT- .

. xlat- , al .ah

- ,6 .ret ?ip .

. ? ,ax -
word .2015h

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

206
-10 ()

Make it Break it Fix it :

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

'Make it -

,
. : , . ,
.

: ah ,1 ,int 21h
.al:

mov ah, 1

int 21h

, '( '$ ISR-


.)... 13,10 .:

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

dx :

mov dx, offset message

ah=9h :int 21h

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 . ,
?

.2 RE , ' ,'X .
.

209
-10 ()

.3 .

.4 . !

'Break it -

( , - ) .
. .

'Fix it -

,
.


.
. ,
. - .

210
-11

11

( )Interrupt -
( cmp.)jmp

. (
) . ?

,
. , .
.
. ,
, .

80x86 "":

, .Traps , . -

, .Exceptions , -
. , .

, .Interrupts ( -
) . .
, .

.int , . int-
, .

int operand

, :1
int 1h

. ,,
:

int 21h

. , ,
DOS .Disk Operating Systems DOS

211
-11

, , .
, ,80x86 BIOS .Basic Input Output System
BIOS , . BIOS
. BIOS ,
, .

, .,
:

.9h -

,16h .BIOS "" -


9h .

,21h .DOS " DOS" .BIOS -

, ():

8h . -

1Ch , BIOS . "" 8h -


.

21h ,DOS . " DOS" .BIOS -

: ,
, .

, .
,DOS .

, , .
. Interrupt Service Routine .ISR

, , ISR
. .

212
-11

mov ax,5 .1 .
, ax 5 .

.2
( .)ISR , cs.ip

.3 ISR "" () ( )Interrupt Flag ( Trap


.)Flag .

.4 ISR cs .ip ISR


.

.5 .ISR

.6 , ISR ( ')
cs ,.ip

.7 .cs:ip

( 100h )

213
-11

Interrupt Flag:Trap Flag

.
. , . 55 ,
, . ISR ,
F7 . 55 ...
F7 ISR , ...

.
. disable interrupts "" ,
.

, ISR , ,if
. .enable interrupts

ISR( IVT)

ISR :

proc ISRname far

iret

endp ISRname

iret ret
.ISR iret ret iret pop , . ,
ISR , .

: .
, .ISR ?

, . 0 255,
. , :

int 21h

.21h

214
-11

. , ,
. ,Interrupt Vector Table .IVT

IVT ,Double Words 256 1024 . IVT 0 0


, IVT 0 3FFh) 1023
).

IVT ? IVT 256


'ISR' , . . ISR
.ISR , IVT ISR .0h
ISR 1h .

, ISR , , 4
ISR . : 21h 84h .
,84h ,IVT , ISR .21h

Interrupt Vector Table

215
-11

DOS
,DOS ,Disk Operating System .
, .Windows

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

''ISR DOS IVT 20h.2Fh

DOS IVT

,21h . ,
.int 21h
? int 21h
.

, ah .

216
-11

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

AH=1h

int 21h , ah " ."1:

mov ah, 1

int 21h

.al

al ASCII . , " al ,"2 ,2 ( 32h


) , ASCII "."2

al , 2 , ASCII
"( "0 .)30h

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 .
ASCII 0.)9-

, .
( DL : ).

: 4 . .
. , 4
( 250 .)0250
30 .

AH=2h

int 21h , ah " "2 dl


, ASCII . ' ,'X ASCII :58h

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

mov ah, 2

int 21h

al .

ASCII , :

.1 ,10 0Ah Line Feed ,


.

.2 ,13 0Dh Carriage Return .

219
-11

:'Y' ,'X'
IDEAL

MODEL small

STACK 100h

DATASEG

CODESEG

start:

mov ax, @data

mov ds, ax

;print x

mov dl, 'X'

mov ah, 2

int 21h

;new line

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 . .

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


.

. .

. .
. : ,
. 5 .7
, .

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' : . .

, 0 ,9 .
.''Wrong input" :

, .: .

I like to write assembly code


Barak

AH=0Ah

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

223
-11

, Enter- ,
.

ASCII- Enter .

20 :

( - 23 , -
, - ASCII- :)Enter
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

,message .ds ,14h ,


. ,13h , , .enter ,
, 0Dh ASCII .enter

:11.4

10 , ( )abc
(.)ABC

225
-11

- ( : )

:11.5 .

:11.6 10 .

:11.7 ( !)

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

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

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

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

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

.11.8 :

ABCDEFGE.Z

:11.9 :

. . .

. .

. .

. .

:11.10 :

Small letter, Capital letter, Number, Other

:11.11 5 " " , "*" .


"12345" : - . , 2.

226
-11

AH=4Ch

int 21h 4Ch


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

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

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

,DOS ,int 21h -


:2Ch

mov ah, 2Ch


int 21h

.cl - .ch -

.dl - .dh -

55 . ,
55 , .

DOS , int 21h .ah=2Dh


:

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

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

. , . .
.

231
-11

Exceptions
.IVT exception
, . .

, .

:
mov cl, 0

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

, .
.

, , .int 0h

debugger .
. ,
. , "".

, , .int 1h

breakpoints .debugger breakpoint


breakpoint "".

, ,breakpoint .int 3h

Traps
, exceptions , .
, :

int , :

int operand

int 80h

,
ISR IVT .ISR

? ,
- .

232
-11

, .

. ,
.
, .
IVT . IVT
"" .

.Trap

( ISR)
ISR ISR ,Trap ' Hello
.'World

' .ISR-

ISR:
proc SimpleISR far

iret
endp SimpleISR
ISR .

, ,iret-
opcodes- , .

proc SimpleISR far



iret
'message db 'Hello World$
endp SimpleISR
.int 21h ,ah=9h ds
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

' ISR-IVT-

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


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

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 )
.

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

.1 , .
.

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

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 ,
. (,
E 12.02% . T 9.1%') .
:

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


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

21h .DOS
, .

ISR .IVT

()Traps -

()Exceptions -

, ( ,)Interrupts .

239
-12 ()

12 ()

DOS ,
. DOS . ,
? , , ?
.

. -

I/O Ports /. -

, ,
:

BIOS -

DOS -

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

.Polling , ,
. , ,

240
-12 ()

. ? ,
. .

Polling , (

, ) . Polling .

. , ,

. ,

, .

. 10 ,

1000 .

. , ,

, .

Polling , . ,
.

, ( .)Interrupt ""
. ,
,ISR ,
. ISR- . Interrupt Interrupt
( )
, .

241
-12 ()

Interrupt . ,
.

, . "" , ""
.
.

8086-

,18 " ,"INTR

242
-12 ()

PIC

,8086 .
. ?

. Programmable Interrupt ,
,Controller .PIC

PIC 8259A ,1976 .8086

, PIC . IR0 ,IR7 8 ,


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

PIC .INT .INTR


. . IR1
.PIC , PIC ,INT INTR .8086-

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 .

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

I/O , :

I/O (.)Port -

I/O - 16 ( 64K). -

,mov in.out -

in out ( I/O ,mov -


).

:in, out in , out . in


ax .al out ax al .

:in, out -

: , 0 255. o

: ,255 .dx o

in ax/al, port

in ax/al, dx

out port, ax/al

out dx, ax/al

245
-12 ()

in / out :255

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

,255 dx- .

: ( .)Device manager >-

, "" ( .)Properties ,
"" . 60 .64

246
-12 ()


. , , :

.1 . .Scan Codes

.2 I/O- , .60h

.3 , , scan codes 60h


. 9h Type Ahead
.Buffer

, .
:

.1 .

.2 ( BIOS BIOS ,
. ) . BIOS 16h
.

.3 ,DOS ,21h
.

, . ,Art of Assembly 20
().

247
-12 ()

Scan Codes

, PIC .
, . alt,
control, delete .
.

, .scan codes
down .up , scan code ,80h
7 scan code 0 1 ,
( ).

scan codes

, ESC ,1h ESC .81h ,


scan code .
:

.1 60h scan code / .

.2 PIC IR1 .

.3 PIC , . int
.9h

.4 , ISR .9

248
-12 ()

.5 ISR scan code ( Type Ahead Buffer ).

.6 ,ISR PIC end of interrupt .

Type Ahead Buffer

ISR 9h ,scan code ,


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

0040:001A -

0040:001C -

16 0040:001E ()words -

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

, . ,BIOS
, .2

"" 16 .
BIOS . "" ?
, .
, ,
? , 15
( ) , BIOS .

,
. ,
.

249
-12 ()

, ASCII ,
:

, BIOS
.DOS

, .microcontrollers , ,
. :

.1 60h microcontroller .
, scan codes- .

.2 64h microcontroller .
, scan code .60h

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

64h ( ):

in al, 64h ; Read keyboard status port

cmp al, 10b ? ; Data in buffer

, :

WaitForData:

in al, 64h

cmp al, 10b

je WaitForData

250
-12 ()

, :60h

in al, 60h

, , .2-

ESC .
. 15 .
,ESC 15 . 16 "
" . , 60h
, .

251
) (-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

252
-12 ()

15 , 15

, .,
- .

- scan code- 80h


( ) . ,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

253
) (-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

254
-12 ()

exit:

mov ax, 4c00h

int 21h

END start

BIOS

BIOS 16h , ( ) ""


( , ).

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

, . ,
, .

16h . ah=1 , 0
1 , . al , ah- ASCII scan code .

( ). -

. -

. -

:
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

255
-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

21h ah=0Ch , al
, int 21h . ,al=7h ,
,7h . :

256
-12 ()

al ASCII .

":

; Clear keyboard buffer and read key without echo


mov ah,0Ch

mov al,07h

int 21h

. :

, , .
'.

, ASCII , ASCII
. , . ,
ASCII .

:12.1

ISR scan code Type Ahead Buffer .


0040:001Eh .

( int 21h ) , TD
,step by step .type ahead buffer ,
' 'a type ahead buffer scan codes.

, wasd : .

W = up -

A= left -

S = down -

D= right -

257
-12 ()

. ,wasd "Move " ,"Move up


"down' . ,Esc . .
, .

: .

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 . ,
60h scan code-
.

258
-12 ()


: ,PIC
, .

, .

. scan code
, .Type Ahead Buffer
:

60h ,64h -

int 16h ,BIOS -

,21h ,DOS -

259
260 13

13

, .
, . ,
. ""
, . .
.
. , : ,
.
, , .


? ,
. ,
.

. 1000 2000 .
1000 , .
2000 , .
:

, . , -
, .

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

, -
. ,
. ,

260
261 13

.
.

: -

, o

: -

ASCII o

BMP o

: -

: -

() o

: -

261
262 13

, (
, .)... ,DOS
.ah=3Dh :

AL

:0 -

:1 -

:2 -

DS:dx .

.0:

Filename db file.txt,0

ax , filehandle .DOS , ,CF


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:

262
263 13

mov dx, offset ErrorMsg


mov ah, 9h
int 21h
ret
endp OpenFile

.ah=3Fh :

filehandle bx DOS .

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:

filehandle bx DOS- .

cx . : cx=0 ,filehandle.

263
264 13

dx () .

ax , , .

: ( )cx=1, cx=2 ax=5


.access denied

proc WriteToFile

mov ah,40h

mov [bx, [filehandle

mov cx,12

mov dx,offset Message

int 21h

ret

endp WriteToFile

( 21h ,)ah=4Ch
. , ?
, . , , .
: ( , , ,
) . , .

.ah=3Eh :

filehandle bx DOS .
proc CloseFile

mov ah,3Eh

mov [bx, [filehandle

int 21h

ret

endp CloseFile

264
265 13

, .
, ,
, . ' assembly int 21h
'=ah :

AH=3Ch

AH=41h

AH=42h

filewrt.txt

,testfile.txt ' '!Hello world .

; ----------------------------------------------------------------------------
; 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

265
266 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

266
267 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

267
268 13


. , .

I/O- .

video memory . video memory A000:0000

.B000:FFFF

, :modes

, ,text mode video memory -

B800:0000 4,096( 4K) . , 25

80 .ASCII

, ,graphic mode video memory -

,A000:0000 65,536( 64K) . , 200

320 .

( ,DOSBOX

).

( ).

.video memory ,

.video memory , -

.BIOS BIOS . BIOS , -

. BIOS .

,DOS . -

268
269 13

, text mode .graphic mode

Text Mode

,
.ASCII , ASCII .

.text mode , .DOS


25 80 .

,text mode .BIOS, int 10h :

mov ah, 0

mov al, 2

int 10h

. , .

ASCII

DATASEG .,

,DATASEG , include

.DATASEG

:monalisa.asm

269
270 13

, . $
.

, int 21h .ah=9h

( monalisa.asm IDEAL
, include):

; ---------------------------------------------------------
; Display ASCII graphics
; Author: Barak Gonen 2014
; ---------------------------------------------------------
model small

stack 100h

DATASEG

include monalisa.asm

CODESEG

start:

270
271 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

271
272 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/

272
273 13

Graphic Mode

, .
. , . . ,VGA
.Video Graphics Array ,
:

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

VGA , ,320X200 200 320


.

BIOS, int 10h :

mov ax, 13h

int 10h

, 200x320.

,video memory ,A000:0000- .


: , . , .

273
274 13

: . ,
A000:0000 ,0:0 A000:0001 0:1 . ,Y
,320 X .

. , VGA , ,
8 . 256=28 . ,
,Red, Green, Blue .RGB 256 ,RGB
256 ( ) . video memory
.0 ,0 RGB 0,0,0
.

, .standard palette standard


:palette

, . 256 standard palette


. , 256 ,standard palette
, . palette
, bmp palette .


, .
video memory .out
, x,y.

, ,BIOS .ah=0Ch
:

al

bl ( )0

cx X

dx Y

274
275 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

275
276 13

int 16h

; Return to text mode


mov ah, 0

mov al, 2

int 10h

exit:

mov ax, 4c00h

int 21h

END start

BIOS , .ah=0Dh
, . ,
.
. .

; Set graphics mode 320x200x256


mov ax,13h

int 10h

; Read dot
mov bh,0h

mov ]cx,[x

mov ]dx,[y

276
277 13

mov ah,0Dh

int 10h ; return al the pixel value read

( : )

, ( ,
).

, , X 1
( ) Y ( 1 ) . X
.Y , , Bresenham algorithm
, .

, Y
.1 , .

, ( : )

277
278 13

BMP

, , . (
) , ,BMP ,Bit Map .
, . BMP
,video memory , JPG ,
.

.BMP , BMP
. .assembly read bmp file in :

www.brackeen.com/vga/bitmaps/html

www.ragestorm.net/tutorial?id=7

BMP :

Header .1 54 . header ' 'BM


.BMP

256 Palette .2 , ( 1,024) . ,


( RGB ,) , palette BMP
( BGR) , palette
.

Data .3 . ,2550
.palette , . , data
,video memory . BMP
video memory VGA . 320
200 , 64,000.

. 320x200
. ( ) . BMP
, ,BMP .data , 1079
.(1079=54+256x4) BMP

1079 ' .'0 ,


0 .palette ,palette BGR 0
,palette .54:57

278
279 13

BMP 320 .200 ,


Microsoft Paint .
, .

.test.bmp . -
.tasm/bin

. -

.header -

.palette -

palette , .3C8h, 3C9h BGR -


.RGB

data , video memory -


.

, . -

279
280 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

280
281 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

281
282 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.

mov al,[si+1] ; Get green value.

shr al,2

out dx,al ; Send it.

mov al,[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.)

282
283 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

mov cx,200

PrintBMPLoop:

push cx

; di = cx*320, point to the correct screen line


mov di,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

mov si,offset ScrLine

283
284 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

284
285 13

int 21h

; Back to text mode


mov ah, 0

mov al, 2

int 10h

exit:

mov ax, 4c00h

int 21h

END start


, .
. bmp .graphics :

https://piazza.com/class/i98gbkdp1mg15m?cid=20

" "Graphics- advanced . :

"
, .
,
.
:
.1 BMP ? , BMP- 10x10
80,100
.2 ? , .
.3 ? ,
.
.4 ? , (
).
, , +
):

"

-1 ,

285
286 13

-2,3 ,

-4 ,


, , .
. pdf Art of Assembly
(.)The Physics of Sound

, , .
, "" . "" ,
. "" .
"" . .
12- . . ,
12 ( 2 ,)1.06 110,
,1.06*110 117 , 2*110
220 ,.

( .)speaker ,61h
. , ,00 .61h:

in al, 61h

or al, 00000011b

out 61h, al

in al, 61h

and al, 11111100b

out 61h, al

, , . 43h
.42h

286
287 13

, . 0B6h :43h

mov al, 0B6h

out 43h, al

" port 42h" ( )divisor 16 , .


.

1193180 .:

1193180
Divisor = ---------------
Frequency

port 42h 8 , ,
.

, "" , 440 , ,2712


.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

287
288 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

288
289 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

289
290 13

. , int 21h .ah=2Ch

.:

, . -

, . -

, . -

. ,

. , ,

.:

, . -

, , . -

, . -

( , dl
) . .

, 55 , 55

. 55

. .

55

. 0040:006Ch ,
55 . . (

290
291 13

, ) . ,
.

( ) . :

.0040:006Ch .

182 ( )182x0.055sec=10.01 sec .

;---------------------------------------------------------------

(; 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

291
292 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

292
293 13

Random Numbers

. ,

( ,
) .

. , .

( .)Pseudo-random

, (
, ' . ,

) . , "" "
".

. .

, .

.0040:006Ch

, 0 : .1 ( 0 )1 :al

mov ax, 40h

mov es, ax

mov ax, es:6Ch

and al, 00000001b

.11 ,10 ,01 ,00 : , .03


:

and al, 00000011b

. 015 ,70'.

,90 ,2 .

90- :

.10 -

293
294 13

.10 -

.70 -

. , 0

( )0,0,0 0,0,7( 7 1,0,6 1,1,5 1,5,1

6,1,0.)...

. , ,90 0

15 .9

, .

,0040:006Ch 55 . , ,

, . ,

, . , ,
xor . , ,

.xor , .

xor

. CODESEG

, .

; ---------------------------------------------------------------------------------------------

; Generate 10 random numbers between 015

; The method is by doing xor between the timer counter and some bits in CODESEG

; Author: Barak Gonen 2014

; ---------------------------------------------------------------------------------------------

IDEAL

MODEL small

294
295 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

295
296 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

296
297 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

297
298 13


/ .

Scan Codes -

BIOS -

DOS -

, .
:

.1 ,
( .)BIOS / DOS / , BIOS
, DOS . ,
.

.2 DOS int 21h ASCII .


( ) ASCII.
ASCII , ASCII ,scan code
ASCII .? . ,
.scan code ,DOS :

in al, 60h

.int 33h
,DOS " "int 33h mouse function calls .
, .

, , :

mov ax,13h

298
299 13

int 10h

.33h ( ax ax , ah
.)int 21h , . ax=0h
:

mov ax,0h

int 33h

, :ax=1h

mov ax,1h

int 33h

mov ax,3h

int 33h

bx -

xxxx xxx1 0 ' '1 o

xxxx xx1x 1 ' '1 o

, bx ' '00 .

cx , ( 6390 , 320 -
, cx ).

dx , 199 0 -

.
. , .

299
300 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

300
301 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

301
302 13

Debug

, . , .
,
. ,
, , ,
.

, ,
.

, ,
.

, .
. .
. :

, : . -

, . -

, , ., -
, cx 2 ?

shr cx,1 ; adjust cx to range 0-319, to fit screen

, . , ,
. :

mov ax, 5 ; copy 5 into ax


. . ,
, , ...
.

. , ,
' . ( " ").

302
303 13

( )

303
304 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 , .
DATASEG .header header

304
305 13

. ' 'BM ,
.

, . -
, ,
. ,
. , - ,
. ,
.

, ,

, , .

, , . ,

( , .)cx

. .

, ,

. ,

, . ,DATASEG

CODESEG .

, .

, .

305
306 13

, ,:

.A2034: must be in segment block ? .

, ( )StackOverflow

. , Key pressed
.Key released ESC . -
. , scan code-
.

. , . .

, 5X5 . .
.:

.a , 25

" .b"

. , ,

306
307 13

ASCII bmp .

, , .

, .

: , , .

. , . ,

.!

307
308 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

308
309 13

-CLC

-STC

-CLI

Unsigned Signed

)JG (JNLE )JA (JNBE

)JL (JNGE )JB (JNAE

)JGE (JNL )JAE (JNB

)JLE (JNG )JBE (JNA

JE JE

JNE JNE

-JCXZ cx=0

JC

JNC

309
310 13

JZ

JNZ

JS

JNS

JO

JNO

JP / JPO

JNP / JPE

-LAHF 8 ah-

-PUSHF

-POPF

LOOP- , -

) -LOOPE (LOOPZ ,LOOP cx . -

. cmp LOOPE .LOOPZ

) -LOOPNE (LOOPNZ ,LOOP cx . -

310
311 13

SHL, SHR , -

-ROL , SHL , -

. ah ROL , .

-ROR ,SHR , -

-RCL ,SHL .

-RCR ,SHR .

-SAL SHL-

-SAR SHR-

-SBB ,SUB .

311
312 13

' :
Piazza . ,

. , "" ,

. , !

:Piazza-

.1 www.piazza.com ""students get started

.2 ""gvahim

312
313 13

.3 ""other

.4 assembly

313
314 13

.5 student- ""assembly

.6 , , submit email

314
315 13

.7 submit

.8 . I am not pursuing a degree

. -Continue , . .

315
316 13

, .new post

- Post type = Question

- Post to = Entire class

- Folder = Other

, Post my question

316
317 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/

317

You might also like