Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
05
:
, :
.
, , , , , ,
. - ,
,
. "
. ©
. -
2015 " . " ©
http://www.cyber.org.il
12 ....................................................................................................................................
15 ............................................................................................................ 1–
18 .............................................................................................. –2
18 .......................................................................................................... ? –
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 ....................................................................................................................... ASCII
44 .................................................................................................................................
45 ................................................................................................................... –3
45 ..................................................................................................................................
50 ................................................................................................................................
53 ..........................................................................................................................
56 .................................................................................................... CPU –
56 .......................................................................................................... Registers –
63 ............................................................................................................. ) (
65 .................................................................................................................................
66 ................................................................................................ –4
66 ..................................................................................................................................
67 ............................................................................................................... Base.asm
83 .................................................................................................................................
84 ..................................................................................................................................
92 ...............................................................................................Trace Flag –
93 .................................................................................................................................
94 .................................................................................................. mov –6
94 ..................................................................................................................................
94 .......................................................................................................................
95 .............................................................................................................
99 ..............................................................................................
100 ..................................................................................................................
104 .....................................................................................................
105 ......................................................................................................
105 ..............................................................................................
106 ..............................................................................................
108 ........................................................................................................
108 ...............................................................................................
109 .................................................................................................
111 ..................................................................................................
112 .................................................................................................................. offset
116 ............................................................................................ ) (
117 ...............................................................................................................................
118 .................................................................................. , –7
118 ................................................................................................................................
118 .................................................................................................................
127 .......................................................................................................................
130 ...................................................................................................................... OR
133 .........................................................................................................................
135 ......................................................................................................
136 ...............................................................................................................................
137 ................................................................................................................. –8
137 ................................................................................................................................
142 ..................................................................................................................
144 ................................................................................................ ) (
146 ..................................................................................................................
150 .................................................................................................................
151 ...............................................................................................................................
152 ......................................................................................................... –9
152 ................................................................................................................................
154 ..................................................................................................................
160 ..........................................................................................................................
160 ............................................................................................................
167 ........................................................................................
170 ..................................................................................................
173 ..................................................................................................
178 ........................................................................................................... BP
183 ........................................................... ) (
186 ...................................................................................
196 ...............................................................................................................
197 ...............................................................................................................................
198 ................................................................................................................................
199 .........................................................................................................
210 ...............................................................................................................................
211 ...................................................................................................................... – 11
211 ................................................................................................................................
213 ..................................................................................................................
239 ...............................................................................................................................
240 .................................................................................................. ) ( – 12
240 ................................................................................................................................
240 ..................................................................................................... Interrupts –
244 ...........................................................................................................
247 .............................................................................................................................
247 ..........................................................................................................................
250 ...................................................................................................
259 ...............................................................................................................................
260 .......................................................................................................... – 13
260 ...............................................................................................................
260 ................................................................................................................
262 ....................................................................................................................
262 .....................................................................................................................
263 ...................................................................................................................
263 ....................................................................................................................
264 .....................................................................................................................
265 .......................................................................................................
268 ..............................................................................................................................
274 .........................................................................................................
276 ........................................................................................
277 ..............................................................................................
278 ............................................................................................... BMP
285 .............................................................................................
286 .....................................................................................................................
290 .................................................................................................................................
290 .......................................................................................................................
298 ......................................................................................................................
298 .......................................................................................................
298 .........................................................................................................
302 ............................................................................................................................
302 ........................................................................................... –
304 .............................................................................................................
305 ...............................................................................................................
305 ..................................................................................................................
306 ...................................................................................................
307 ...............................................................................................................................
317 ..................................................................................................... –
12
. ,
.
,
, .
. , , .
." "
. - ,
- ,TASM
. ,
. , ,
1 -
.
. 2 -
, . 3 -
.
. , 4 -
. - 5 -
, , 6 -
.
. ,)' , ( 7 -
, . 8 -
.
, , 9 -
. .
, 10 -
. ," " Reverse Engineering
DOS . 11 -
.
12
13
. , 12 -
, .
.
, - 13 -
. –
2.0
, 25- , "
: . ,
. reverse engineering -
. : , -
) ( -
.
.
. ,
, .
, : .
, , . , ,
.
13
14
: ,
14
15 –1
1–
. , !
. . , ?
, ,
, , .
. ?1- 0 ? .
.
. ,
,"10111000" , , ,
."10111000" ,mov ,
.
15
16 –1
: .
.
.
.
. ) (
. , .
.
, . ,
, .
: ,
.
, " "
. ,
Reverse , .
.Stack Overflow Engineering
. , ,
, .
, Apple , . ,
.
.
– .
: , ־
Cyber SecurityResearcher
Familiarity with programming languages (e.g. C++, Java, C#, PHP, Assembly, etc.)
Knowledge of networking and internet protocols (e.g. TCP/IP, DNS, SMTP, HTTP)
16
17 –1
. ,
. , ,
. ,
.
. ,
. .
. ,
, ,
. – .
17
18 –2
–2
? –
, )Decimal , (
– , .
. )Hexdecimal , (
, . ,
. )Binary , (
, ,
, , , .
.
,
,
. ." "
, .
.3 8 ,10 -
9 .9 0- ,10
.
7 .7 0- 8
.
2 .2 0- ,3
.
18
19 –2
? .
22 3 10 ,8 . 8 ,10
. , .
. , . ,
www.transience.com.au/pearl3.html :
! . ," "
! ." "
, , ...
.
19
20 –2
,
.0,1,2,3,4,5,6,7,8,9 : .
501 .105 501 , . .
. 5 105 , 5
: , , –
4710
: .
4710 ≠ 478
: ,
4710 = 7*100+4*101
37510 = 5*100+7*101+3*102
199410 = 4*100+9*101+9*102+1*103
20
21 –2
:5 19910
.4 39 .199:5
.4 ,7 .39:5
.2 ,1 7:5
.1 ,0 1:5
.1244 - ,
.19910 = 12445-
: 12445 - - ,
12445= 4*50 + 4*51 + 2*52+ 1*53 =4*1 + 4*5 + 2*25 + 1*125 = 4+20+50+125 = 19910
2 .0,1: , ,
, . -
.2 .
,20 .2 ,
,2n-1 n-
:
21
22 –2
. .100112
: ,
100112 = 1 + 2 + 16 = 1910
"
:19 .2 ,"
.100112 ,
. , , 16
B ,10 A . ,) 9 0- (
: . 11
. o ,0
22
23 –2
C0DE16 – 16 -
. '0' , , . h -
- , ( 0C0DEh
)
0xC0DE – 0x -
: ,4F16 , ,
:19910 , :
.7 ,12 .199:16
19910 = C716
.2 16־ , .
:
23
24 –2
, . ־
. , ־
:9B16
916=10012
B16=10112
9B16 = 100110112
. , ,
,
, – ,9B2C16 .
? ?1001101100101100
:2.1
,2 . ,10 –
. ,16 .
24
25 –2
. – , , –
.70 ־133 .
.
–carry " "( , 10־
. )
,
. , .2־
.0 0 0
.1 1 0
.1 0 1
.) ( 10 1 1
25
26 –2
. ,1 1 0 .
. ,0 1 1
. ,1 ,0 0 ,1 ,
. ,1 0 1 ,
16־ ,
:12316 ABCD16 , .
.1 0 ,16 3 ) 13(D .
. ,F 2 C
. ,C 1 B
.A־ 0 A
26
27 –2
:2.2
27
28 –2
) (
: ,
.5 ־,6
. ,
28
29 –2
:16
.D־ A־ . E D
.)15 ,14 16 13– (F
. , .D A־ ,
.)12 ,13 16 ,1 10– (C
.D ,E־ ,
.1 C D
:2.3
29
30 –2
) (
, .
2X2 ,10X10
:
:) 3 10( 11 1010
30
31 –2
. ,1010 1010 1
. ,1010 1010 1–
.)2+4+8+16=30– ( 11110
, ,10 :2 –
.52x10=520 , .0 ,
.52x100=5200 :10 ,
, . ,2
.0
.11x10=110 : ,
.11x100=1100 :
.11x1000=11000 :
– . 256 , 16X16 16 :
, . .
. 101 1100 ,5 C
:2.4
31
32 –2
) (
:
1/1 = 1
0/1 = 0
. ,
. ,
.
–:101102/1012
.10 ,100
:2.5
32
33 –2
־
.)Bit( )1 0( . ,
, .) Binary digit(
, .
. 64 32 ,16 ,8 .
.
, ? . N
:8 ־1 ־
N ־ , : ,
.2N-1
, ? N ־
? 8 ,255+1
. , ! 8 ־
. ,11111111 , :
. .) ( ,
. 0=255+1 , 8 , , .00000000
. ,
33
34 –2
0001 , 0000 .
, . –unsigned . 0010 ,
?
.signed –
, . , N
: .
. –1 . –0
,unsigned ,
.
? , .0011 :
0011 , .3 ,011 . ,0
.3
.3 ,011 . ,1 ,1011
.3 1011 ,
! 1000 0000 -
34
35 –2
, .
.
:(-3) 3 .
, .(-6) ,
.
, )One's complement(
.
1 ,1־ 0 . - ,
.1110– ־ "-1" ."1" 0001 , .0־
: 7 0־
1 0 . -
.0 ,1111 , .
.
: , ,
. ,
35
36 –2
. 0־1
, ( 1 –
).
. ,
.00000110 6
.11111001
36
37 –2
. ,6 6.
.) 1– ־ ( ,
, 8־ , .
. 4־
, . .00000000– ,
!8
.(-2N-1) (2N-1-1) N ,
16 .128 127 8 ,
.32,768 32,767
, –
.
37
38 –2
( ,
: . )
:
20+26 =65
:2.6
. 2-
: . 2- 8
2 00001100 12 .
-9 247
-128 128
-94 162
-102 154
-1 255
? ,
38
39 –2
?
? Pearls3 ...
( ,Nim Pearls3
. Nim-Sum .)https://en.wikipedia.org/wiki/Nim
, . ,
. , ,
: ,
0 1+1 ( - .2
)10
, . , - )Nim Sum ( .3
.
. 5- 4 ,3 : 3
:) ,15 , (
)3( 0011
)4( 0100
)5( 0101
: , .
0010
? – - .
: , 2 ...
39
40 –2
)1( 0001
)4( 0100
)5( 0101
0000
:2.7
. - !
40
41 –2
, ,
, – .
. , .
Bit –
. .
340 , , , , , .
, ־ .519
.
, . , ? , ,
, , . , ־
.
2 1100 . ,
.) ( 4 ,12־
.unsigned signed –
. , ,
, . , ,2
. –
, . ,
.
Nibble–
. 16 , 4 2 Nibble . Nibble
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F Nibble ,
. ,2 , –
. . 16
: –
11011110101011011100000011011110
41
42 –2
11011110101011011100000011011110
D E A D C 0 D E
? ...
Byte–
– , )Byte(
. ,
־ , , , .
. ) , (
:Nibbles
42
43 –2
Word –
,16 . , 16 )Word(
, , 16 . ,65,536
.
ASCII
43
44 –2
726976767932877982766833
H E L L O W O R L D!
48454C 4C 4F 20 57 4F 52 4C 44 21
: ) ( "
.
, , , :
.
. - , , -
, : .
. ,
. ,
,
.
44
45 –3
–3
.
, . ,
. , בס ס ת,
. ,
. ב
. 80x86
, . ־X , ־80x86
. , .1978 ,8086
: ,
45
46 –3
. ,80x86־ ,VNA
, . ־ ) (
. , /
.) ( / , /
46
47 –3
,Bus /
. , .
,VNA־
47
48 –3
. .) " 33 , ( 8086־
I/O- .
.
SYSTEM BUSES –
80x86- .VNA )System Buses(
.CONTROL BUS– ADDRESS BUS– ,DATA BUS– :
– .
.1־ 0־ , .1 0
? , , ?
? , ." 1000h"
.
48
49 –3
DATA BUS–
.
. 64 32 ,16 .
16 . 16 16
16־ . 16
.
, – , 64 32 ,16־
, , .
, .
.
ADDRESS BUS–
, – . )I/O (
, . ?
.) , (
, I/O ,
.
. " ,
, .
, n .00,01,10,11 :
( 1,048,576 . 20 , ,8086־ . n
. , .)
, . ' – 4,294,976,296־ , 32
Windows 7 , '
. 64
49
50 –3
CONTROL BUS–
.
, ,
?
. ,)write( )read( ,
, read־ . ,1 write־ read־
. , write־ .
. , 64 32 ,16
. byte enable
.I/O- 80x86־
I/O־ ,80x86־
. 65,536־ . 16
.I/O־ ,
. n ,2n ,
.
.)2n-1) ,0 .
. 1,048,576 , 20 ,
: ,"0" 1976־ ,
. "0" .1
. 1976 .2
50
51 –3
: , 1976־
. 1976 .1
. .2
51
52 –3
. , –
. h
. ah־ ,ah־ ,
: ,
.0ABh 1970 .1
. H.O־ , L.O־ –
52
53 –3
: , .
, , . 220 8086־ ,
16 .)registers( , 16
, .) 0FFFFh ( 65,535 0־
. , ,
)offset( , segment- - .)segments( –
: .
Segment:offset
53
54 –3
: 16 ־ ,80x86־
. .
, 16 , .
0000h , 216 ,64K
,64K 8086- .0FFFFh
.
.16־ ,
. )2*16( 32 0002h , . 16
.) 272 16 17 ,17 16 11( 272 0011h
3DD6h , . ,
3DD6h:0012h– .12h
,16־ 0־
.
. ,
codeview- . , .dosbox codeview
.
, . ,
.0627:000C , 13 .0627:0000
.0627:000D :
54
55 –3
.' 0,1,2– , .
.
ASCII .ASCII
.
. ־ .
.0627:0001
. ASCII-
. ,
: 16 .0628:0000
. 16 ,
55
56 –3
CPU–
.
,)Registers( . –
, ,)Arithmetic & Logical Unit(
, ,)Control Unit( , ,
. ,
Registers–
, . , ,
– ־ .
.zero wait . ,
. , ,
.80x86־
. 16 .
56
57 –3
: ,
.
Accumulator
, ax
register
. ax־
. Base address bx
. register
. cx
Count register
. ,
, :
, . Data register dx
. dx ,I/O
Source Index si
(
.)bx
.
Destination di
Index
. sp
sp־ , sp
Stack Pointer
.
. sp
57
58 –3
. 8־ . , 16 "
– , ax . ,
,
.
)move ( mov –
: .
: ax ,
: ־
. ,ax al . 8
: 8
58
59 –3
:21 17 .
mov bx, 0
mov cx, 0
. "
59
60 –3
.bx 8 bl :bx bl -
.cx 8 ch :cx ch -
. ,
. .
.
60
61 –3
Segment Registers –
SegmentRegister . . 16
.
. ־ . cs
, ?64K .64K ,
( , cs- ,
model small base.asm .
. .)
ds- ,ds
small , . ,DATA־
ds DATA , 64K־
.
61
62 –3
. )STACK( ss
ss – sp .
.
FLAGS -
)pointer( 16 . IP־
.code segment-
, 16 8 .
. .
. ,
- , . ,
, . ־ IP
. ,
, . ,
.
:ax 3 ,
.ALU־ ax־
.3 ALU־
. " ALU-
.ax ALU-
62
63 –3
Control Unit–
? –
. ,
, .
.
.
.
. 8 OpCode , OpCodes־
IP ־. ,IP ,Instruction register ,
IP־ , OpCode- .
.
) (
. ,
:bx+5 ax ,
mov ax, bx
add ax, 5
. ,ax bx
.bx+5 ,5 bx ax , ,
, ,) ( ,OpCode־ .
. – , .
0 ,Control Bus
. 1 ־0 .1־
, . clock cycle .clock cycle 0־ 1 ־0־
.) 1/1,000,000( 1 clock cycle 1MHz
63
64 –3
0־ ( ,
.)0 ־1־ ( )1־
, clock cycle־ ,
, .
. clock cycle־ ,
. .
clock . ־
:clock cycles , , cycle
64
65 –3
:80x86־ ,
, 80x86־
. VNA .VNA
. , ,
, 80x86־ ,
. , .
, , , .
– .
. ,
. ,
– ,
. .
65
66 –4
–4
, .
–) ( . ,
, . ,
. ,
,base.asm ,
, .)Debugger(
.Opcodes– .
.Opcodes־
Editor– Notepad++
. . -
.' , , – -
.
, Notepad++ :
66
67 –4
Notepad++־ , .asm -
.
.DOS . 8 asm־ -
Base.asm
. .
, . – ,
, ( Notepad++־
.base.asm , .)
http://data.cyber.org.il/assembly/TASM/BIN/base.asm
base.asm
, .base.asm
, . ,
– –
.
67
68 –4
.DATASEG :
DATASEG .
. DATASEG . .
: ,DATASEG־
var1 db 5
, ) ( .5 var1
.5 var1
.CODESEG
– – , .
.
: ,CODESEG־
.al , 5 ,var1־
.base.asm
. . –STACK 100h -
. –DATASEG -
. –CODESEG -
- . –start -
. end־ -“main”
68
69 –4
exit . –exit -
. ,
.DOS
Command Line
Command ־.Command Line ־ , ,
, , , Line
.
. Command Line
, asm , .
, .Command Line־
64 ־ , ( 64־ .
.80x86 20 - )
. – , ,
Command - .Command Line־ ,
Command >---- >---- ,7 . Line
.Start menu- cmd .Prompt
69
70 –4
.Command Line-
. http://www.computerhope.com/msdos.htm
CD DirectoryName
: ."DirectoryName"
CD Games
:
CD..
:
CD\
:
70
71 –4
DIR
.
: page־ p .dir /p ,
EXIT
DOSBOX
. 80x86־
, . . )Emulator(
. , ,
,Commodore 64
,1982
Commodore 64־
71
72 –4
,DOS . ,
DOS .1994 ,Disk Operating System
"Dosboxdownload" .0.74 Dosbox
.
data.cyber.org.il/assembly/dosbox : 0.74
.Z .
, . Intro
,C .mount
: ,Assembly
Mount c: c:\
C:
.C
. Command Line-
- ,
.
,
.) ( 3000cycles
:DosBox-
Cycles = max
72
73 –4
(
CPU .)3000cycles־ ,
.speed: max 100% cycles
:Dosbox
, - .
. ,
- dosbox-0.74.conf
: DOSBox 0.74 Options-
: , .
[autoexec[
: .
mount c: c:\
c:
cd tasm
cd bin
cycles = max
73
74 –4
TASM Assembler
. Assembler
.Turbo Assembler ,TASM ,
.1996 5.0 TASM
. Linker
. ,
, , .
. .
, . TASM
.
data.cyber.org.il/assembly/TASM.rar : tasm.rar
.c:\tasm\bin -
.bin base.asm -
: base.asm -
.base.obj .debug zi -
: - tilnk – -
tlink /v base.obj
.base.exe .debug v
74
75 –4
Turbo Debugger–TD
: ,
td base
view ,)F10 ( ,
:CPU ,
75
76 –4
Turbo Debugger־
. TD base.exe –
:DATA־ , ,CPU־ -
76
77 –4
: -
־
: , –STACK־ ,STACK־ -
.watch –Watches -
:DATASEG־ , .
77
78 –4
Var1 dw 1234h
Var2 dw 2345h
: var1+var2 Watch
: . – Variables -
78
79 –4
: CTRL+G . ,
:) ,ds:0 , (
: " "
,DATASEG ds
F8 , . ds
.
79
80 –4
breakpoints־
, . breakpoint ,
. .Toggle- Breakpoints
.
. .breakpoint
. F9-
TD
ALT+X :Dosbox- -
CTRL+F2 : -
. F10 :) ( -
. Tab
ALT+Enter :) ( -
80
81 –4
Opcodes־ –) (
. mov
. mov , ,
. . mov
mov ds, ax
.TurboDebugger , .2
mov ax, 2 , . .3
.B80200
81
82 –4
. mov ?mov .1
:
mov ax,5
mov ax, bx
mov [120], ax
: ,Opcode־ .2
mov ax, 5
mov ax, 6
:Opcode־ .3
mov ax, 2
mov bx, 2
mov cx, 2
mov dx, 2
: Opcode- .4
mov bx, ax
mov ax, cx
. .mov ax, dx :
־ ,
.
82
83 –4
. ,
: . ,
Editor Notepad++ -
DOSBOX -
Turbo Assembly -
Tlink -
Turbo Debugger -
. .
.
83
84 IP, FLAGS – 5
IP, FLAGS – 5
. – IP –
,) (
.
,Special Purpose Registers , ,
.FLAGS IP
IP- Instruction Pointer -
FLAGS–
IP־ :
. FLAGS־ .
,
.
:) ( 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
: ,F8
.) mov bx, 0 ( , , .1
85
86 IP, FLAGS – 5
. ,0008 ־0005־ IP .3
.IP־
5.1
. IP .
? IP
, IP .IP=0005h , .
. )enter (
:
86
87 IP, FLAGS – 5
:FLAGS-
:Condition Codes– ,
Zero Flag -
Overflow Flag -
Carry Flag -
Sign Flag -
,
. .)" ... " (
87
88 IP, FLAGS – 5
Zero Flag–
: ( .0 , . 1
.mov :
.) mov ,
. - Destination Operand -
.
: ( ah al 4Bh :
. )subtract( .)?
:
'z'
,al־ ,100h .
.1 ,00h " " ,
88
89 IP, FLAGS – 5
" " .
־ .1 , .
. ,
5.2
, . 16 ,
. 16 ,
Overflow Flag–
." " ,1 , " "
.signed ,
, 8 ,
)-128( ,+127 (-128) signed
.0 , .+127
: 8 ,al
'o'
? , 16 ) ( –
89
90 IP, FLAGS – 5
5.3
. ,base.asm
Carry Flag–
, ,Carry Flag , ,
, .
, 8 , .unsigned
,+255 0- unsigned
.+65,535 0־ 16 .+255 0
’ c‘
) ( ) (
. ) (
:
mov al, 1h
mov bl, 2h
sub al, bl
90
91 IP, FLAGS – 5
: , ,
, . ,unsigned ,-88
.unsigned signed al -
,al -128 :al .unsigned ,
.unsigned al ,+128 .80h al
. ,+168 ,40 +128
5.4
. ,base.asm
Sign Flag–
.1 ) ( 1
:Signed , .0
.1 – -
5.5
. . base.asm
Direction Flag–
,0 .
. ,1 .
91
92 IP, FLAGS – 5
Interrupt Flag–
Trace Flag–
, .Trace
.turbo debugger debugger .
. ,0־
Parity Flag–
. .
.1 ,)0,2,4,8( '1' ,
.
Auxiliary Flag–
.AL 4- 1 .
.0
5.6
mov ax, 0
sub ax, bx
92
93 IP, FLAGS – 5
5.7
? 5.6
) ( 5.8
, ,
.
) ( 5.9
, ,
.
: ,
, IP־ ,
.
93
94 mov –6
mov –6
, , , -
. mov
. ,) ( -
,)"... , "( -
. ,
, , .
. ,
. ,
: . - ,
.)DS ( 1h ds al
,5 ds:1h .
. , . al 5
, .1h
. ,1h ,
.
.ds:1h age- ,age ds:1h ,
:
94
95 mov –6
– ,
. –
. )Variables(
. ,
,6 1
.12 2 ,
mov ds, ax
, , .DATA־ ds־
, .ds־
, , age , .ds
." " age
, .
.
: ,DATA־
ByteVarName db ?
95
96 mov –6
–DB( db . ByteVarName
,ByteVarName .)Define Byte
. .
: ,
–" " , , al ;
.
: ,
DATASEG
ByteVarName db ?
ByteVar2 db ?
ByteVar3 db ?
.ds:0 ,ByteVarName , )0 (
,ByteVar2 , ByteVarName .
. ds:1
: ,
DATASEG
; DefineDouble
96
97 mov –6
6.1
CODESEG
start:
mov ax, @data
mov ds, ax
mov [var], 5
exit:
mov ax, 4c00h
int 21h
END start
mov .DATASEG .
Turbo ־ , . ds, ax
? . Debugger (TD)
Signed, Unsigned
–unsigned signed
Bytes , .
.
: Byte .
DATASEG
Var1 db ?
Var2 db ?
: CODESEG
97
98 mov –6
! . DATASEG־
.88h
? ?136 120
. ?
136 120 ,+120 ,
:
. ,120 120 -
-unsigned signed ,
. .
98
99 mov –6
, . CODESEG־
.
,
. ,
DATASEG
, .5
. – .) (
: ,
DATASEG
ByteVarName db 'HELLO'
. ,ASCII , ,
: ,
99
100 mov –6
DATASEG
ByteVarName1 db 'H'
ByteVarName2 db 'E'
ByteVarName3 db 'L'
ByteVarName4 db 'L'
ByteVarName5 db 'O'
.)ARRAY( – .
.)STRING( ASCII ,
.
:6.2
, – DATASEG־ ,base.asm
.ASCII , , ,
. DATASEG־ ,
, , , .
, .
, , , .
.
. ,
, , ,
:
100
101 mov –6
0 , 0200h ,)words( ,
1 ,) ,0201h ( 0200h
. 020Ah 5 ,0202h
: DATA־
. , ArrayName
. N. N
. ,duplicate Dup
. N ,
. ,
:5 , ,
. ,
101
102 mov –6
. ,1,2,3,4 – 32
:6.3
,TD־ ,
.
.'5' 10 .'5' 10 .
!
. ,4,5,6 20 .
MOV
. .
, ."move" ,mov
. .
: . mov־
: ) ( 22 ax ,
mov ax, 22
: 22
102
103 mov –6
mov dx, 10
mov ax, bx
mov ax, cx
mov ax, dx
mov ax, ax
.ax ax . ,
:mov ־
mov register,register
, .memory memory ,
: .
mov opcode־ . , ,
.
103
104 mov –6
, ) , ( 16 , 8
: .
mov si, bp ; The mov instruction works with ALL general purpose registers
: ,
mov ax, bl
.
.mov ds, cs ,
ds cs . mov cs,ax : . cs .
.
6.4
.bx ax .
.ax bx .
.ch ah .
.dl al .
104
105 mov –6
16־ –
. , 8 )257 (
mov ah, 10 ; Note the difference from last command! 10 decimal, not 10h (=16(
6.5
, : ,) ( 100 al
al־ TD־ .)00001111b ,b : (
.
. ־
( ,)Direct addressing ( ,
.)Indexed addressing ( )Indirect addressing
.1 ax ,
.Var ax ,
: .bx־ ax ,
105
106 mov –6
mov [1], ax
:
mov bx, 1
mov [bx], ax
,1 bx־ ax ,
.bx־
6.6
al .0 , var DATASEG .
: .2 bx ) ( 100
mov [Var], al
mov [1], al
mov [bx], al
mov [bx+1], al
. DATASEG־ TD־ .
.) , ( 100 DATASEG־
– ,
:
: , ,
mov ax, [1]
106
107 mov –6
6.7
:) ( ,
mov [Var], al
mov [1], al
mov [bx], al
mov [bx+1], al
; -----1-----
mov al, 0
mov al, [var[
; -----2-----
mov al, 0
mov al, [1[
; -----3-----
mov al, 0
mov al, [bx[
; -----4-----
mov al, 0
mov al, [bx+1[
al . al־ TD־
.) , ( 100
107
108 mov –6
:bx ־ 5
mov [bx], 5
: :
: , ,
: . bx־ -
mov [bx], ax
mov [bx+2], ax
108
109 mov –6
mov [bx+si], ax
mov [bx+di], ax
: . ,di si bx־ , -
mov [bx+si+2], ax
mov [bx+di+2], ax
. .mov
:
mov [1], ax
mov [Var], ax
mov [bx], ax
,"1" . 20 :
? 20 ,) 16 , ( bx var
– 16 , .
. ]1[ .
, – – , ?
.DATASEG 1 ax .DATASEG
, ? , 16 ,ax :
.mov־ .
: , DATASEG , DATASEG־
109
110 mov –6
DATASEG:
mov [1], ax
:DATASEG
? ,DATASEG-
: ,DATASEG־
mov [1], ax
110
111 mov –6
,)' bx ,ax ( ,
: ,
8086־ , .
: .Little Endian
http://en.wikipedia.org/wiki/Endianness
. mov
: .
DATASEG:
111
112 mov –6
.) –ax al , ( al 2
:
,DATASEG Array .
: DATASEG 0002 Array
. 2 ,0CCh al־
.1 ,0 ,
: . ,
mov [Array+1], al
, 2 , al
.
offset
:offset , bx
, . ?
bx ,
. bx
112
113 mov –6
LEA
:BB0000 , –
? – ? :
)db , (
.) 16 ( ax
. – ,
:
.) ( word , byte ?
? ax –
113
114 mov –6
: .Array+2 .word ax :
. , –
type override
: ,bx ־ 5
mov [bx], 5
5 ,5 ?
.5 ... , ,
: .
. ,
.) 5 ( 00000101 ,bx :
. ,
,bx+1 ,00000101 ,bx :
.00000000
, .
.word ptr byte ptr ,
114
115 mov –6
-mov
:mov
: . .1
mov al, bx
mov ax, bl
. .
. ,
: . .2
( .
:)base.asm
mov ds, ax
: . .3
,var1 var2 , .
:
mov [var1], ax
: . .4
mov [ax], 5
: . .5
mov [bx], 5
115
116 mov –6
( 5
.) 64 32
: . .6
mov 5, ax
) (
?
– – ,
: . .
,[1]
mov [1], al
? 20 [1]
.
,ds , ds- .
.) (1 ) ( 16-
mov [ds:1], al
,
.
mov [cs:1], al
. ,cs al- ?
, .
? ?
. , ,
116
117 mov –6
- :6.8
: .
xor ax, ax
xor bx, bx
add ax, 2
add ax, 2
.4 ax , .
: .3 ax ,
) ( o
mov o
.3 bx 3 ax , .
. '
:mov , ,
, :
.
117
118 , –7
, –7
, .mov –
:
, , : -
shr, shl : -
. – .
.) ( , , : 80x86־
.NEG ־DEC ,INC ,IDIV ,DIV ,IMUL ,MUL ,SUB ,ADD :
:
118
119 , –7
ADD
פ
,ax -
! .
. 16 8 -
add :7.1
. al . . 6 .
.al־ ,TD־
)? ( al , .
.ax ,
: .
var1 -
var2 -
sum -
)?sum ( sum
119
120 , –7
SUB
פ
sub :7.2
: .
var1 -
var2 -
diff -
)?diff ( diff
. . 4 .
.6,7,8,9 9,8,7,6 (
)3,1,-1,-3(
120
121 , –7
INC / DEC
פ
MUL / IMUL
8 , . )multiply ( mul
. 32 16 16
, 16 .ax , 8
.dx 16 ax 16
פ
121
122 , –7
. ,unsigned signed
. 2־ 11111011 .
. , , .251
CODESEG:
mov ax, 0
mul bl
mov ax, 0
imul bl
ax, bx
122
123 , –7
,0FFF6 , al ,imul -
mul :7.3
,255-0 ,byte .
)? : (
. .signed , 4 , .
: .word , .sum
,b ־a
sum = a[0]*b[0]+a[1]*b[1]+…
123
124 , –7
DIV, IDIV
.1 3 .al=7h, bl=2h : 8
.ah=1, al=3 ,
.1 3 .ax=7h, bx=2h : 16
.dx=1, ax=3 ,
( 8 8 , div –
.) 16 16
,al . 16 ) ( , 8 ) (
. . mov ah, 0 : ah־
, 16 ax , . 32 , 16 ,
, .mov dx, 0 : dx־
!
: ?
)? ( -
פ
al = ax div bl
div bl div register (8 bit)
ah = ax mod bl
ax = dx:ax div bx
div bx div register (16 bit)
dx = dx:ax mod bx
al = ax div ByteVar
div [ByteVar] div memory (8 bit)
ah = ax mod ByteVar
ax = dx:ax div WordVar
div [WordVar] div memory (16 bit)
dx = dx:ax mod WordVar
124
125 , –7
:div־
IDEAL
MODEL small
STACK 100h
DATASEG
CODESEG
start:
mov 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:
int 21h
END start
125
126 , –7
div :7.4
,unsigned־ ,byte .
.
,signed־ ,byte .
.
,unsigned־ ,word .
.
NEG
, , ) negative ( neg־
: .
neg dest
dest = 0 - dest
פ
126
127 , –7
.
, . , ,
. ,MASKING
, . , ?
: 0 , .Packed data–" "
0000000000000001000000010000000100000000000000000000000100000000
01110010
,
.
, , .
:
00100101
. ,
. . " " ,
: . ,
127
128 , –7
: not
not register
not memory
AND
.0 , .1 ,1 . and ,
, ." "
. and־ – .
and
– and , 16 8 , and
1 , 0 0
. 2 2 , 1
00000111and
10010110
---------------
00000110
128
129 , –7
. –and
–0 . ,
. –1 .
, .)MASK( ,
.00000001 : , .
, . and
, . , . 0 and־
and ־,1 – and־
.0 , .1־ ,1 ,
and :7.5
. - ?4־ and
129
130 , –7
OR
: or
.1 ,1־ ,
. " " or
, , ( , –
0, 1– ־ .)
. .
. .)7 0־ (4 ,
:
11000100
. , . ,4 ,4
: ) 4 ( 0001 0000 or ,
11010100
. , , 4
.4
or :7.6
4 ־2 ?2 , .
?) or (
?4 .
130
131 , –7
XOR
: ,)exclusive or ( ,xor
.) , (2 xor ,
: . , xor
!0 , xor
: .
10010011
:) ( . ,
01010100
: .xor
10010011xor
01010100
---------------
11000111
: xor ,
11000111xor
01010100
---------------
10010011
131
132 , –7
, ,
. .
:
xor ax, ax
, ,0 ax־
.
xor :7.7
. – xor .
: ,
print:
mov dx, offset msg
mov ah, 9h
int 21h
mov ah, 2 ; new line
mov dl, 10
int 21h
mov dl, 13
int 21h
132
133 , –7
NOT
:not . not
. not . not
1 ,1 0 , . " "
. . 2
. ,
.shl(shift left) ־shr(shift right):
SHL
, . ,shl ,
: .cl
shl register, cl
shl memory, cl
: shl־
. , -
, n . 0 , ( 0 -
.) n
.CF -
. . 1 OF -
. 1 ZF -
133
134 , –7
. -
. 8־ 1 -
16 . 8 shl
:8 16
shl
SHR
. .shl־ ,shr ,
.cl ,
:
shr register, cl
shr memory, cl
: shr־
. , -
n . 0 , ( 0 -
.) n ,
.CF -
. OF -
.
. 1 ZF -
. , -
. 8־ 1 -
16 . 8 shr
:8 16
134
135 , –7
shr
) ( -
, ( , , :
. , , .)
, .
, ,
.' , – ,
. ,
– , ,1
. ,
.LFSR . :
, xor .
, LFSR . xor
:
.http://en.wikipedia.org/wiki/Linear_feedback_shift_register
. ,
, . :
:
http://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
. , :
.
135
136 , –7
:7.8
:unsigned ־signed ,
o
o
o
o
:
And o
Or o
Xor o
Not o
, ,
.
: ,
Shr o
Shl o
. ,
136
137 –8
–8
.)"... , "(
: ,
.) jmp ( -
.)cmp– ( -
.)loop– ( -
, , , . ,
.CODESEG ,IP
, , .
– . ,1־ "-
"– , ."
." ,
)"... , "( :
, . .)"... "(
.
JMP
, jmp . , , jmp
IP־ ,jmp- .CODESEG
.
DATASEG
address dw 000Ah
CODESEG
mov ds, ax
137
138 –8
mov ax, 1
jmp [address]
:jmp־
:ip ,jmp־
.CODESEG־
FAR ־NEAR
– ,jmp־
, jmp־ .address –000Ah
( , . jmp-
jmp־ ,near .near ,)
. –
, . far
, . ,
: .
. cs-
138
139 –8
, . –
. far ־near־
jmp :8.1
:)base.asm CODESEG- (
xor ax, ax
add ax, 5
add ax, 4
.ax=4 , jmp
LABELS
, . jmp־
. CODESEG־
jmp- , )label( , .
, label־ . label־ ,
: .label־
:LoopIncAx
inc ax
jmp LoopIncAx
. ax ,
label- . –
. ,
. ,label
( , .startloop- StartLoop -
. - )
139
140 –8
.label :
.
label :8.2
140
141 –8
CMP
" " . )compare ( cmp
, . ,
) , , ( . – .
. – ,sub cmp .
:cmp
פ
. – cmp
: , ,
Code CF ZF SF
mov al, 3h ? ? ?
cmp al, 3h 0 1 0
cmp al, 2h 0 0 0
cmp al, 5h 1 0 1
, mov .3 al -
.
141
142 –8
. 2 al .2 ־al -
) (
– .)"... ... "(
. ,
.cmp , -
. , cmp־
.1־ , . , , -
.label . ־ , -
, ( , -
.)
. ,cmp , :
. cmp , .
.cmp־ ,
signed ,
.unsigned
?10000001b 1b–
,unsigned . – –
10000001b, signed , .1b־ ) 129 ( 10000001b־
.1b־ ,127
142
143 –8
, ,cmp , ,
: , . –
: .
JE - Jump Equal
143
144 –8
) (
, .
: cmp־
mov bl, 1b
cmp al, bl
: cmp
) ( :8.3
. , :
,) signed- ax ( ax .
.
.ax bx – ,ax=bx .
- – (Var2- Var1 .
.ax=0 ,ax=1 - .)unsigned
.var2- var1 , : .
ax – .var1+var2 ax –
.var1-var2
:'x' .
144
145 –8
. ? ? TimesToPrintX
LOOP
, .
, .TimesToPrintX 'x'
,TimesToPrintX־ , 'x'־
– – . 'x' –
. , .)Loop(
: loop
.cx 1 -
. cx -
. label- jmp –) cx , (
loop SomeLabel
dec cx
cmp cx, 0
jne SomeLabel
: 'x' loop
mov cl, TimesToPrintX ; we use cl, not cx, since TimesToPrintX is byte long
145
146 –8
PrintX:
mov ah, 2h
int 21h
loop PrintX
.TimesToPrintX , :
? ,
. TimesToPrintX
loop־ , . cx loop ,
- 1 (65,535 cx , , cx
cx 65,536 – 65,534 cx .)unsigned
.
:) ( ,
xor cx, cx
mov cl, TimesToPrintX ; we use cl, not cx, since TimesToPrintX is byte long
cmp cx, 0
je ExitLoop
PrintX:
Loop PrintX
ExitLoop:
146
147 –8
) ( Nested Loops
. , –
: ־
mov cx, 10
LoopA:
mov cx, 5
LoopB:
loop LoopB
loop LoopA
5 LoopA ,LoopA 10 –
? . 50 LoopB , .LoopB
( ,
: , .) - cx
mov bx, 10
LoopA:
mov cx, 5
147
148 –8
LoopB:
loop LoopB
dec bx
cmp bx, 0
jne LoopA
:8.4
,0 – ' :' .
,1=0+1 ( ,1
' .)' 2=1+1
: . 10
0,1,1,2,3,5,8,13,21,34
: .
mov ah, 1h
int 21h ; al stores now the ASCII code of the digit
־ . 5
. HELLO
,unsigned־ ,Var1*Var2 .
sum=Var1+sum : . ־
. loopVar2 ־
'x' : .
. –
: ,4 5
xxxx
xxxx
xxxx
xxxx
xxxx
148
149 –8
9 ־0 , –
:al־
mov ah, 1h
int 21h ; al stores now the ASCII code of the digit
sub al, '0' ; now al stores the digit itself
:'x' –
149
150 –8
jmp -
loop -
: loop ,
. .Error- Relative jump out of range
( 16 jmp .opcode-
8 ,) 32767 32768
, .) 128 127 ( loop
relative jump out of , 8-
.range
: -
. .1
, - .2
.
. , .3
ja , .
:
cmp ax, bx
ja my_label
my_label:
:jmp- ja-
cmp ax, bx
jbe help_label
jmp my_label
150
151 –8
help_label:
my_label:
. .
," " ja ( bx>ax my_label-
, jbe bx>ax , .)" " ,jbe-
. jmp my_label
labels־ . ,jmp
.
. ,cmp
unsigned signed
.
.)cl ( cx loop
. ,
. ,
. . –
151
152 –9
–9
– , .
? ?
, , –
)" " ( )Procedure( .
.)Function(
– –
: . ,
, . –
, – , .
, .
. ,
. .
,ReadPassword– , ,label ,
? , , ? jmp
? jmp . ,
, ? – .
, .
? , .
: label־
OpenComputer:
jmp ReadPassword
152
153 –9
OpenEmail:
jmp ReadPassword
ReadPassword:
–cmp ־ –
. ...
. –
. . . , . ReadPassword
,ReadPassword . ,
. –
.
call , . –
: , .
call ReadPassword
, . –
, . ,
" " .
. ,
. , ? ,
, . –
. ,
. ,
. –
. ,
153
154 –9
. , . –
" " . ,
.
. , , ,
.
STACK
, . , )STACK(
.
, . ,
. ,
Stack Segment־
. ,
: . ־
:)base.asm ( , 256 ,
STACK 100h
: , .
. . stack segment–ss
154
155 –9
. . stack pointer–sp
.bx . ,sp ,
- STACK־ ,ds:bx DATASEG־
.ss- sp
,100h , . sp ,
.100h sp
ss .100h
sp .
. ,100h
, sp־
,LIFO– Last In First Out .
sp , .
, . sp
.
155
156 –9
PUSH
: . push
push operand
?
.sp=sp-2 : sp -
.ss:sp , -
,push sp :
.word– .
. – push
:push
push ax
push 10
push var
.ax
.)byte־ ,word- ( 10
.word – var
push al
. push al . :
? .100h ,
push ax
156
157 –9
2 ־sp push
. ,ah־ ,ax
: .sp 2 .
push 0CCDDh
157
158 –9
POP
) ( .push־ pop
:
pop operand
. -
.2־ sp -
:pop
pop ax
pop ]var[
. ax
var , ( var
.)
pop al
. pop –
158
159 –9
?bx ? sp :
pop bx
.0FEh sp ,0CCDDh bx :
: pop־ , :
pop var
? var sp
? ?
sp־ , !
: .
sub sp, 4
. sp .10h, 20h . .
.mov bx ax .
159
160 –9
. ,) ( ,
: " "
.call -
– , . -
.num1, num2–
, num1+num2 , . -
.
) ( -
.
: .CODESEG ,CODESEG
proc ProcedureName
endp ProcedureName
10 ,ZeroMemory –
,CODESEG .)0־ , ( DATASEG־
:
160
161 –9
IDEAL
MODEL small
Stack 100h
DATASEG
CODESEG
xor al, al
mov cx, 10
ZeroLoop:
mov [bx], al
inc bx
loop ZeroLoop
ret
endp ZeroMemory
start:
mov ds, ax
call ZeroMemory
exit:
int 21h
END start
161
162 –9
) ( 8 DATASEG
9.2
CALL, RET
: . call
call ZeroMemory
.)... : ( call־
162
163 –9
:call־ ip
163
164 –9
:ip=0Ah .ret –
ip ,ret־ –
:
.15h ־0Ah־ ip ,
: ret ־call־
164
165 –9
–call
– sp ( sp .1
.) –FAR
. ,ss:sp .2
jump ( ip .3
)
-ret
. ,ss:sp .1
.) ; ( sp .2
,ss:sp- ip־ .3
.
ret־ call־ . ip , ,
! ip
NEAR, FAR
. call .CODESEG־
:
. ,CODESEG־ .1
.) ( CODESEG־ .2
, ,model small ,
, – ,CODESEG־
.CODESEG־ ,
165
166 –9
:"far" –ZeroMemory .
: . . ,
.) ,00FEh ( 00FCh sp , -
. cs .near ,
.far ־near ,
166
167 –9
– .'X' , ,
. X ,
:
CODESEG
proc Print10X
PrintXLoop:
mov ah, 2h
loop PrintXLoop
ret
endp Print10X
start:
mov ds, ax
Row:
call Print10X
mov ah, 2h
loop Row
int 21h
END start
167
168 –9
. , . , ,
.cx .TD־ , ?
,0 .4־ .3־ cx :
unsigned- , ( 65,535- loop Row
,) cx( row־ .)
... 0 cx ,
, ,
. ) (
CODESEG
proc Print10X
push cx
PrintXLoop:
mov ah, 2h
loop PrintXLoop
pop cx
ret
endp Print10X
start:
168
169 –9
mov ds, ax
Row:
call Print10X
mov ah, 2h
loop Row
int 21h
END start
, .) ( pop push
cx , PrintXLoop .4 ־cx . cx
, cx pop־ .
.
! cx TD־ ,
169
170 –9
:9.3
:
CODESEG
proc ChangeRegistersValues
; ???
mov ax, 1
mov bx, 2
mov cx, 3
mov dx, 4
; ???
ret
endp ChangeRegistersValues
start:
mov ax, @data
mov ds, ax
xor ax, ax
xor bx, bx
xor cx, cx
xor dx, dx
call ChangeRegistersValues
exit: mov ax, 4c00h
int 21h
END start
( . ,ChangeRegistersValues
. )'???'
.
? – .
. ? ,
,
170
171 –9
( , ?
. ,)
, , .
, ? ? .
. ,
: ,
DATASEG־ -
bx , ,ZeroMemory . , ,
.
.ax
proc ZeroMemory
mov cx, ax ; ax holds the number of bytes that should become zero
xor al, al
ZeroLoop:
mov [bx], al
inc bx
loop ZeroLoop
ret
endp ZeroMemory
. ax־
,cx ־ :
.ax־
, . ,
, ? – ax, bx, cx, dx .
.
171
172 –9
. ,DATASEG־ ,
.) (
?
proc ZeroMemory
xor al, al
ZeroLoop:
mov [bx], al
inc [MemoryStart]
loop ZeroLoop
ret
endp ZeroMemory
,ax NumOfZeroBytes .
,bx MemoryStart .
.
, , bx־ al־ , (
.)
172
173 –9
, –
. , DATASEG־
, – ,
?) ( ?
, .
( :
,) ,64K־ ,
– , ־ ,
.
Pass by Value– .1
Pass by Reference– .2
Pass by Value
. , .
, . – ,
.
.2 ,SimpleAdd .
, . ,
DATASEG־ SimpleAdd
. SimpleAdd .
SimpleAdd־ .parameter ,
.parameter
173
174 –9
: ,
push [parameter]
call SimpleAdd
. , . parameter
, .DATASEG־ parameter־
" " parameter , 2 .
.
. , Pass by Value
.Pass by Value
.i+j-k ax k– j ,i : ,SimpleProc
: )Pass by Value (
push [i]
push [j]
push [k]
call SimpleProc
174
175 –9
:) ( ,
i, j, k
.pop SimpleProc
pop־ .call־ ,ip , –
.
.
, . ,
.
. pop־
proc SimpleProc
pop ReturnAddress
pop ax ;k
pop bx ;j
pop ax ;i
push ReturnAddress
175
176 –9
ret
endp SimpleProc
. , .DATASEG־ ReturnAddress
- .
.
.min ־max , .
max pass by value
. min
Pass by Reference
, .
.
. – ,
. ,
,SimpleAdd . :
:Pass by Reference
push offset parameter ; Copy the OFFSET of "parameter" into the stack
call SimpleAdd
- –
, . parameter-
:
176
177 –9
push seg parameter ; Copy the SEGMENT of "parameter" into the stack
: .
:parameter
proc SimpleAdd
add [byte ptr es:bx], 2 ; This actually changes the value of "parameter"
push ReturnAddress
ret
endp SimpleAdd
. pass by reference .
177
178 –9
– ( ,pass by reference .
.)var1=5, var2=4 .var1=4, var2=5
BP
. pop [ReturnAddress]-
.ip , ,
proc SimpleProc
push bp
mov bp, sp
pop bp
ret 6
endp SimpleProc
.
? .bp sp bp
sp , .sp ,
. bp ,
.bp־
178
179 –9
k ־i .bp 8 , ,i
. bp־ .bp , , 4 ־6
.ax=i+j-k ,SimpleProc .
:
proc SimpleProc
push bp
mov bp, sp
; Compute I+J-K
xor ax, ax
add ax, [bp+8] ; [bp+8] = I
add ax, [bp+6] ; [bp+6] = J
sub ax, [bp+4] ; [bp+4] = K
pop bp
ret 6
endp SimpleProc
equ . ,
: . ,
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
mov bp, sp bp , :
.
) ( pop push ,
. ,bp־ , ,
:bp־
proc WrongBP
push ax
push bp
mov bp, sp
180
181 –9
WrongBP
. ret 6־
jmp bx
jmp bx
. push ret־
. 6 , 2 push
. " " sp ret 6
181
182 –9
:bp־
. pop .1
.bp . pop .2
. ־ .3
. bp- , ,bp
bp־ :9.6
– ( ,pass by reference , .
.)var1=5, var2=4 .var1=4, var2=5
.bp
182
183 –9
) (
. ,
, . ,
.
, .sp , ?
, 6 , , .sp
:
sub sp, 6
sp ( ,
:)ip –
add sp, 6
proc XY
push bp
mov bp, sp
183
184 –9
mov AddXY, ax
mov SubXY, ax
pop bp
ret 4
endp XY
:push ax
184
185 –9
: XY־
push 5
push 4
call XY
.SubXY ־AddXY ,
:bp
. .4 5
,)0001( )0009( ,
. ,SubXY ־AddXY
,]bp-2[ ,ss:00F6 :
. ]bp-4[ ,ss:00F4 .
:9.7
.XYZ ,
. .LocalX, LocalY, LocalZ–
.
185
186 –9
. ,
: .pass by value ,
. , -
. -
. -
: , ,pass by reference
:DATASEG .
DATASEG
num_elements equ 15
, ,CODESEG
:
push num_elements
call SomeProcedure
, , -
.
:9.8
. , -
.
, . sum sum .
.sum=16 2,2,3,4,5
186
187 –9
, SortArray .
.1,2,3,5,6 : 3,6,5,2,1 .
:
, FindMin -
.
, SortArray -
- SortArray , . FindMin־
. FindMin : Swap
. -
, ,Sort2Arrays .
, sorted
: .
Array1 = 4,9,5,3,2
Array2 = 3,6,4,1
:
Sorted = 1,2,3,4,5,6,9
:
, Merge -
.
.Merge SortArray־ -
Filter -
.
187
188 –9
) ( Stack Overflow -
. Stack Overflow
.) (
: , .
. , .1
, .2
.
, .Buffer Overflow
. 12 , . ," "
? 13 . 12- ,
, 257 ,100h – – , .Buffer Overflow
.Buffer Overflow 257-
. Buffer Overflow
.Stack Overflow ,
.Stack Overflow
? , .
; ------------------------------------------------------------------------------
; Program StackOF – demonstration of stack overflow
; Author: Barak Gonen 2015
; ------------------------------------------------------------------------------
IDEAL
MODEL small
STACK 100h
DATASEG
188
189 –9
CODESEG
proc GetName
mov bp, sp
mov ah, 1
xor bx, bx
get_char:
int 21h
je quit_proc
inc bx
jmp get_char
quit_proc:
pop bp
ret
endp GetName
start:
mov ds, ax
mov ah, 9
int 21h
189
190 –9
call GetName
mov ah, 9
int 21h
exit:
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)-
mov ah, 9
int 21h
jmp exit
END start
.enter . :
: .Program finished
. 10 , ,
, push
.
. , , ,
. ip . ,
? ,
190
191 –9
. ,
: , . 10 GetName
. 10 - .GetName
.ss:00F2 –:ASCII(4Ah (J – .
8 10 . ,ss:00F9
bp " " . , ," " ,
. ,ip
5 ,'Jon Snow' .
.Stack Overflow , 10 14 .
:
191
192 –9
ASCII- , bp ,
.21h - 20h ASCII- :
, . , ,
? - .
. enter
bp , .bp- pop .
ip 2120h .ret- .2020h -
.)little endian – ?2021h ( 2120h
, .1265- Psalter
. ,
192
193 –9
Stack Overflow- ,
( , .
. )
: .Stack Overflow ,
Stack - 'Here be dragons' .
. , Overflow
193
194 –9
) ( Calling Conventions
. . ,
– -
, .
( endians- . ? .
-big endian little endian .)mov
. ,
.
. . –Convention
Calling , ,
?Calling Conventions- .Conventions
:C
integer ?MyProc
. .integer
int c = MyProc(1,2);
,C MyProc
?Callee- Caller- - .
: .1
:' .
push 1
push 2
call MyProc
:'
push 2
194
195 –9
push 1
call MyProc
callee- caller-
callee- caller- , , . ,
.
: .2
, . , MyProc
callee- , MyProc
.c - ,
. callee- caller-
. callee- caller- ,
: .3
. , ,
. sp
: 4 , . ret- , .
ret 4
:sp
add sp, 4
call MyProc
add sp, 4
. callee- caller- ,
? ? callee- caller- :
195
196 –9
, . ,
, )https://en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions(
.STDCALL- CDECL
:CDECL
.Right to Left -
) 16- , ax ( ax -
. caller- -
:STDCALL
.Right to Left -
) 16- , ax ( ax -
. callee- -
? . , STDCALL CDECL
caller- .caller - sp , caller-
, ,callee- .
.
print , ?
,print . ,
. print
, ?CDECL STDCALL
caller- ret . ret
, . . sp
.
http://www.codeproject.com/Articles/1388/Calling-Conventions-Demystified :
196
197 –9
. ,
, . ,
–
. ,
.push ־pop , ,
, , –
. .ret ־call .
:
Pass by value– -
Pass by reference– -
,
.equ bp
,Stack Overflow , .
.
,Calling Conventions , .
.
, – .
. ,
.
197
) ( -10
) ( CodeGuru Extreme– 10
, CodeGuru Extreme
." " .
. 2-5
CodeGuru Extreme
/http://www.codeguru.co.il/xtreme :
. , .
, .Reverse Engineering ,
- .
. ,
, . ,
, . . 64
- , .
. , .
.
. , ." " ,
. ,
198
) ( -10
,
: ,
http://data.cyber.org.il/assembly/codeguru-guide.pdf :
http://data.cyber.org.il/assembly/codeguru-slides.pdf :
: ,
http://www.codeguru.co.il/wp/?forum=%D7%90%D7%A7%D7%A1%D7%98%D7%A8%D7%99%D7%9D
. ,
Reverse Engineering .
.
,
.
.
, , XCHG :XCHG .1
xchg ax, bx
.mov .bx- ax
0, 1, 1, 2, 3, 5, 8, 13 …
- ( 13 7 ,8 6
.)
199
) ( -10
- . ?
b ,m- a . (
,m a ( .)' f-
- LUT- .)... f b
97 a ASCII- , . LUT
,98 ASCII- ,b .109 ASCII- ,m
: .102 98 109 97 .102 ,f
: . mf
xlat
. ,m ASCII- al-
, . : MOVSW .5
. , 200
mov ,
, . ,
. MOVSW
200
) ( -10
CLD STD .
.
rep movsw
my_label:
movsw
dec cx
jnz my_label
.'
Reverse Engineering
. ,RE ,Reverse Engineering-
RE , . RE
. , .
data.cyber.org.il/assembly/zombies.zip
. -
, .
. . ,
201
) ( -10
duck.com
. ? - .duck.com .
jmp .cs:100h .
. . .100h
. .f7
: , .100h jmp 100h
start:
jmp start
end start
?cs:102h ,
. , ." " ,
202
) ( -10
coffee.com
RE .coffee.com , ,RE
.
. .
ret- .ret- push mov-cmp-jne
)int 21h ,ax=4C00h( dos- .
push 0000 , . ret- .
. ,
: (properties) ,
203
) ( -10
,cs:0100h .40h , 64 ,
. . cs:0140h-
?coffee –
. int 87 -
.
? . ,
. cs:013Fh ret- . ip-
.ret- , .
ret- , .ret- " " ,
bx bx 0006h . ip-
0006h .
. . ,
RE .coffee.com-
.
204
) ( -10
codeguru.com
.cs:011Ch ret- , .
. .
2015h ax .
205
) ( -10
.1Dh bx
.cs:011Dh- ds:bx .11Dh bx ,100h
. ? . 0,1,1,2,1,2,2,3 .
. - ,
. -
2015 .
. ,LUT- xor ,
.ah al , xlat- .
?ip .ret ,6 - .
- ,ax ? .
.2015h word
,ret- ,
" " . ip- .2015h word-
.
206
) ( -10
, ." "
. , ."Accessgranted":
. "Access granted" ,
.
Make it -'
,
, . , : .
.
,int 21h ,1 ah :
: .al
mov ah, 1
int 21h
ISR- ( '$' ,
: . 13,10 .)... –
: dx
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 (?)
CODESEG
start:
mov ds, ax
mov ah, 9
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:
jne fail
cmp cx, 3
jne fail
success:
mov ah, 9
int 21h
jmp exit
fail:
mov ah, 9
int 21h
exit:
int 21h
END start
, . .1
?
. ,'X' , RE .2
.
209
) ( -10
. .3
! . .4
Break it -'
.) - , (
. .
Fix it -'
,
.
.
, .
. - .
210
-11
–11
- )Interrupt(
.)jmp ־cmp – (
( .
? .)
– , –
. , .
. –
, . –
. ,
. ־ , .Traps , -
, .Exceptions , -
. , .
( . Interrupts , -
. .)
. ,
int- . , .int
. ,
int operand
:1 ,
int 1h
, , .
:
int 21h
, , .
DOS .Disk Operating Systems –DOS
211
-11
. , ,
BIOS ־.Basic Input Output System –BIOS ,80x86־ ,
BIOS־ . ,
, BIOS־ .
. ,
, . ,
:
.9h -
:) ( ,
. 8h -
, :
. ,
. ,
. ,DOS
. , ,
.ISR Interrupt Service Routine .
ISR , ,
. .
212
-11
mov ax,5 – . .1
. 5 ax ,
.2
.ip ־cs , .)ISR( ־
.ISR־ .5
)' ( , ISR־ .6
.ip ־cs ,
.cs:ip .7
) 100h (
213
-11
.
, 55 . , .
– , ISR־ . ,
... 55 . F7־
... , ISR F7־
.
, " " .disable interrupts
.
,if , , ISR־
.enable interrupts .
) ( IVT ־ISR
: ISR
iret
endp ISRname
– ret iret־
, . , pop iret־ ret ־iret .ISR־
. , ISR
. :
? .ISR־ ,
, 255 ־0 . ,
: , .
int 21h
.21h
214
-11
, , .
.IVT ,Interrupt Vector Table .
4־ , , ISR ,
. 84h 21h : . ISR־
.21h ISR־ , ,IVT־ ,84h
215
-11
DOS
. ־ ,Disk Operating System ,DOS
.Windows ־ ,
– .
, .
. ,
, 'ISR' , .
.
IVT־ DOS־
, . ,21h
.int 21h ־
int 21h־ ?
.
. ah ,
216
-11
http://spike.scu.edu.au/~barry/interrupts.html
. – ah– int 21h
.
AH=1h–
mov ah, 1
int 21h
.al
ASCIIĄ , ,2 al־
.)30h ( "0"
217
-11
:al ASCII־
IDEAL
MODEL small
STACK 100h
DATASEG
CODESEG
start:
mov ds, ax
mov ah, 1
int 21h
exit:
int 21h
END start
)ASCII 37h (7
al
218
-11
:11.1
. ASCII .
ASCII־ ( .
.)9- 0 ASCII־
, .
.) : ( DL
. 4 : .
4־ , .
.)0250־ 250 (
. 30־
AH=2h–
mov ah, 2
int 21h
. al
: , ASCII
:'Y' ,'X'
219
-11
IDEAL
MODEL small
STACK 100h
DATASEG
CODESEG
start:
mov ds, ax
;print x
mov ah, 2
int 21h
;newline
mov dl, 10
mov ah, 2
int 21h
;carriage return
mov dl, 13
mov ah, 2
int 21h
;print y
mov ah, 2
int 21h
exit:
int 21h
END start
220
-11
:11.2
.'A' .
.'a' .
. ,'HELLO' .
, ,'WORLD' , , ,'HELLO' .
.
. .
– . .
, : .
.7 5 – .
. ,
221
-11
AH=9h–
ISR- ( '$' ,
: .)... –
: dx
mov ah, 9h
int 21h
: ,
IDEAL
MODEL small
STACK 100h
DATASEG
CODESEG
start:
mov ds, ax
pop ds
222
-11
mov ah, 9h
int 21h
exit:
int 21h
END start
:11.3
. 'Enter a digit' : .
,9 ־0 , .
.''Wrong input" :
: . , .
AH=0Ah–
.)Buffer( " - ,
.
.0Ah ah Int 21h )ds- ( dx
223
-11
, , Enter-
.
. Enter ASCII-
: 20
- , 23 - (
:)Enter ASCII- - ,
IDEAL
MODEL small
STACK 100h
DATASEG
message db 23 dup)?(
CODESEG
start:
mov ds, ax
mov bx, dx
mov [byte ptr bx], 21 ;21 not 20, the last input is ENTER
int 21h
exit:
int 21h
END start
224
-11
:11.4
)abc( , 10
.)ABC(
225
-11
) : ( -
. :11.5
. 10 :11.6
)! – ( :11.7
** ** * * ***** *****
: .11.8
ABCDEFGE….Z
: :11.9
. . .
. .
. .
. .
: :11.10
226
-11
AH=4Ch–
) ( AH=2Ch ,AH=2Dh– /
55 . –
." 1/18 " . 18.2 ,) 0.055(
.cl - .ch -
.dl - .dh -
, . 55
. , 55־
. 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
savetime dw ?
divisorTable db 10,1,0
CODESEG
proc printNumber
push ax
push bx
push dx
nextDigit:
xor ah,ah
228
-11
add al,'0'
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:
229
-11
mov ds, ax
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 al, dh
call printNumber
230
-11
mov ah, 9
int 21h
xor ax, ax
mov al, dl
call printNumber
quit:
int 21h
END start
:11.12
.1 ,0 .
, :
55 ,960 0
.
. . , .
.
231
-11
Exceptions–
exception .IVT־
. . , –
. ,
:
mov cl, 0
. ,
.
.int 0h , ,
. debugger־
, .
." " , .
.int 1h , ,
.int 3h ,breakpoint ,
Traps–
. , , exceptions־
: ,
: , int
int operand
int 80h
, –
.ISR־ IVT־ ISR־
, ?
. -
232
-11
. ,
–
, .
.
. ,
IVT־ . IVT־
. " "
.Trap
) ( ISR
Hello ' ,Trap ISR ISR־
.'World
.ISR- –'
: ISR־
proc SimpleISR far
…
iret
endp SimpleISR
. ISR־
,iret- ,
. , opcodes-
233
-11
pop ds
mov ah, 9h
int 21h
iret
endp SimpleISR
ISR־ . ISR־
. ,
push dx
push ds
pop ds
mov ah, 9h
int 21h
pop ds
pop dx
iret
endp SimpleISR
.ISR־ ,
234
-11
mov al, 0FFh ; The ISR will be placed as number 255 in the IVT
mov dx, offset SimpleISR ; dx should hold the offset of the ISR
int 21h
: . int 0FFh- .
IDEAL
MODEL small
STACK 100h
DATASEG
CODESEG
push dx
push ds
pop ds
mov ah, 9h
int 21h
pop ds
pop dx
235
-11
iret
endp SimpleISR
start:
mov ds, ax
pop ds
int 21h
; Call SimpleISR
int 0FFh
int 21h
END start
ISR :11.13
. , ASCII al ISR .
.int 0FEh ־
: ax, bx ISR .
.bx- ax 'ax' -
.ax- bx 'bx' -
236
-11
. 'SAME' -
-
) DOS (
.
, .
' c b ,b a -1 .) (
.fdw cat ,3 , .a ,z
. , .1
.
. Anna Karenina .2
. .
. .'$'
.
all happy families resemble one another every unhappy family is unhappy in its own way
all was confusion in the house of the oblonskys
the wife had discovered that her husband was having an intrigue with a french governess who had been in
their employ and she declared that she could not live in the same house with him
this condition of things had lasted now three days and was causing deep discomfort not only to the husband
and wife but also to all the members of the family and the domestics
all the members of the family and the domestics felt that there was no sense in their living together and
that in any hotel people meeting casually had more mutual interests than they the members of the family
and the domestics of the house of oblonsky
the wife did not come out of her own rooms
the husband had not been at home for two days
the children were running over the whole house as if they were crazy
the english maid was angry with the housekeeper and wrote to a friend begging her to find her a new place
the head cook had departed the evening before just at dinner time
the kitchen maid and the coachman demanded their wages$
. , .3
. , .4
. .
, .5
, ( .
.)' 9.1% T . 12.02% E
:
237
-11
Letter Frequency (%)
E 12.02
T 9.10
A 8.12
O 7.68
I 7.31
N 6.95
S 6.28
R 6.02
H 5.92
D 4.32
L 3.98
U 2.88
C 2.71
M 2.61
F 2.30
Y 2.11
W 2.09
G 2.03
P 1.82
B 1.49
V 1.11
K 0.69
X 0.17
Q 0.11
J 0.10
Z 0.07
238
-11
. ,
,IVT־ , ,ISR־
. 'ISR'-
.DOS 21h
. ,
.IVT־ ISR
)Traps( -
)Exceptions( -
. ,)Interrupts( ,
239
) ( -12
) ( – 12
, DOS
, . DOS .
? , , ?
.
. – -
. / –I/O Ports -
, ,
:
BIOS -
DOS -
Interrupts–
. ) ( , ,
. , .
, , –
– , .
.
, .
?
, , .Polling–
, , .
240
) ( -12
– , – ? .
. .
( , Polling
. Polling .) ,
, , .
, .
– .
. ,
, 10 .
. 1000
, , .
. ,
, . , –Polling
.
241
) ( -12
, . –Interrupt
.
8086-
,"INTR" ,18
242
) ( -12
PIC–
. ,8086־
? .
Programmable Interrupt , .
.PIC ,Controller
243
) ( -12
, PIC־ . PIC־
. , : .
? ...
, . PIC ־.
, .
.
: PIC־
. -
. – I/O Port , -
. -
, end of interrupt -
.
. -
, . PIC ־, ,
PIC־ . PIC ־,
244
) ( -12
PIC־ , , .
. PIC־
55־ , . 55 , , –
.
.
I/O Ports– /
, )buses(
–address bus– , . /
.
, . ,
. , 16 80x86־
.)Input/Output ( I/O , /
: , I/O
.)Port( I/O -
.) 64K ( 16 - I/O -
:in, out -
. 255 ־0 , : o
.dx ,255 : o
in ax/al, port
in ax/al, dx
245
) ( -12
:255־ in / out
– .) , (,
, in
.out ־
:255־
in al, dx
. dx- ,255־
.)Device manager>- ( : –
246
) ( -12
247
) ( -12
: , , .
.Scan Codes . – .1
.60h , I/O- .2
. ,
:
. .1
, BIOS– (BIOS .2
16h BIOS ־.) .
. – –
,21h ,DOS .3
.
20 ,Art of Assembly . ,
.) (
248
) ( -12
Scan Codes
. PIC־ ,
alt, . , –
. control, delete
.
scan codes־
. IR1 PIC־ .2
int . , PIC־ .3
.9h
.9 ISR , .4
249
) ( -12
–0040:001A -
–0040:001C -
)words( 16–0040:001E -
( 16 ,
– ? 16 .)scan code, ASCII code–
, .2- , .
. .
,BIOS . ,
.2־ ,
. 16 " "
? " " . BIOS
. ,
, ,
15 , ?
. BIOS־ , ) (
,
, . –
.
250
) ( -12
, ASCII ,
:
BIOS , –
.DOS
, .microcontrollers , ,
: .
. microcontroller־ 60h .1
. scan codes- ,
. microcontroller־ 64h .2
.60h scan code ,
, . ,64h 61h .3
.) (
:) ( 64h
: – – ,
WaitForData:
in al, 64h
je WaitForData
251
) ( -12
:60h – ,
in al, 60h
.2- ־ , ,
. ESC־ –
. – 15 .
" 16־ . 15־ ,ESC־
60h , . "
. ,
252
) ( -12
; --------------------------------------------------------------------
; Use keyboard ports to read data, until ESC pressed
; Author: Barak Gonen 2014
; --------------------------------------------------------------------
IDEAL
MODEL small
STACK 100h
DATASEG
CODESEG
start:
mov ds, ax
WaitForData :
jne WaitForData
ESCPressed:
mov ah, 9
int 21h
exit:
int 21h
END start
253
) ( -12
15 , 15־
, . ,
. -
. .
; -----------------------------------------------------
;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:
254
) ( -12
mov ds, ax
WaitForKey:
je WaitForKey
in al, 60h
je exit
je WaitForKey
mov [saveKey[, al
jnz KeyReleased
KeyPressed:
;print "Start"
mov dx, offset msg1
jmp print
KeyReleased:
;print "Stop"
mov dx, offset msg2
print:
mov ah, 9h
int 21h
jmp WaitForKey
255
) ( -12
exit:
int 21h
END start
BIOS
al .ah=0h 16h ,
" " , . scan code־ ah ASCII־
.2־ ־
, . – ,
. ,
0– , . ah=1 16h
. scan code ־ASCII־ ah- al , . 1,
.) ( -
. -
. -
:
WaitForData:
mov ah, 1
int 16h
jz WaitForData
mov ah, 0 ; there is a key in the buffer, read it and clear the buffer
int 16h
: -ESC
; -------------------------------------------------------------------------------------
; Use BIOS int 16h ports to read keyboard data, until ESC pressed
; Author: Barak Gonen 2014
256
) ( -12
; -------------------------------------------------------------------------------------
IDEAL
MODEL small
STACK 100h
DATASEG
message db 'ESC key pressed',13,10,'$'
CODESEG
start:
mov ax, @data
mov ds, ax
WaitForData :
mov ah, 1
Int 16h
jz WaitForData
mov ah, 0
int 16h
cmp ah, 1h
jne WaitForData
ESCPressed:
mov dx, offset message
mov ah, 9
int 21h
exit:
mov ax, 4C00h
int 21h
END start
DOS
257
) ( -12
. ASCII־ al
: "
mov al,07h
int 21h
: .
. , ,
.'
ASCII , ASCII־ ,
, . , .
. – ASCII־
:12.1
: wasd , .
W = up -
A= left -
S = down -
D= right -
258
) ( -12
: .
in al, 61h
or al, 00000011b
out 61h, al
mov al, 0b6h
out 43h, al
mov ax, 2394h
out 42h, al
mov al, ah
out 42h, al
:
in al, 61h
and al, 11111100b
out 61h, al
. ,
, . 16h :
scan code- 60h
.
259
) ( -12
,PIC־ :
. ,
. ,
scan code . –
.Type Ahead Buffer ־,
:
64h ־60h , -
,21h ,DOS -
260
261 – 13
– 13
. ,
, . ,
" " .
. . ,
.
, : , .
– .
. , ,
, ?
, .
. –
261
262 – 13
.
.
: -
, o
: -
ASCII o
BMP o
: -
: -
) ( o
: -
262
263 – 13
– ( ,
,DOS .)... ,
: .ah=3Dh
–AL
:0 -
:1 -
:2 -
. –DS:dx
: .0־
Filename db ‘file.txt’,0
. :2 -
. :5 -
. :12 -
. – ,
. ,
proc OpenFile
; Open file
mov ah, 3Dh
xor al, al
lea dx, [filename]
int 21h
jc openerror
mov [filehandle], ax
ret
openerror:
263
264 – 13
: .ah=3Fh ־
. DOS־ filehandle–bx
. –cx
. ) ( –dx
, dx :
.
proc ReadFile
; Read file
mov ah,3Fh
mov cx,NumOfBytes
int 21h
ret
endp ReadHeader
. , ax ,
: .ah=40h . –
. DOS- filehandle–bx
264
265 – 13
. ) ( –dx
. , ax ,
proc WriteToFile
mov ah,40h
mov cx,12
int 21h
ret
endp WriteToFile
,)ah=4Ch 21h (
? , .
. , , . ,
, , , ( :
. , .)
: .ah=3Eh ־
. DOS־ filehandle–bx
proc CloseFile
mov ah,3Eh
int 21h
ret
endp CloseFile
265
266 – 13
. ,
, ,
assembly int 21h ' ־ . ,
: '…=ah
–AH=3Ch
–AH=41h
–AH=42h
filewrt.txt–
; ----------------------------------------------------------------------------
; Write to file
; Author: Barak Gonen, 2014
; ----------------------------------------------------------------------------
IDEAL
MODEL small
STACK 100h
DATASEG
filename db 'testfile.txt',0
filehandle dw ?
CODESEG
proc OpenFile
266
267 – 13
mov al, 2
int 21h
jc openerror
mov [filehandle], ax
ret
openerror:
mov ah, 9h
int 21h
ret
endp OpenFile
proc WriteToFile
mov cx,12
int 21h
ret
endp WriteToFile
proc CloseFile
; Close file
mov ah,3Eh
267
268 – 13
int 21h
ret
endp CloseFile
start:
mov ds, ax
; Process file
call OpenFile
call WriteToFile
call CloseFile
quit:
int 21h
END start
268
269 – 13
. , .
. I/O-
.B000:FFFF
:modes ,
80 25 , .) 4,096( 4K B800:0000־
.ASCII
. 320
,DOSBOX ( –
.)
.) ( –
, .video memory־ –
, .video memory־ -
. BIOS .
. ,DOS -
269
270 – 13
Text Mode־
,
. ASCII , .ASCII
mov ah, 0
mov al, 2
int 10h
. , – .
ASCII
, . DATASEG־
include , ,DATASEG־
.DATASEG
:monalisa.asm
270
271 – 13
$־ . ,
. –
IDEAL ( monalisa.asm
:) include־ ,
; ---------------------------------------------------------
; Display ASCII graphics
; Author: Barak Gonen 2014
; ---------------------------------------------------------
model small
stack 100h
DATASEG
include monalisa.asm
CODESEG
start:
271
272 – 13
mov ax,@data
mov ds,ax
; Print string
mov ah,9h
int 21h
int 16h
exit:
mov ax,4C00h
int 21h
end start
272
273 – 13
: , ,
For beginners:
www.en.wikipedia.org/wiki/ASCII_art_converter
http://www.afn.org/~afn39695/collect.htm
http://chris.com/ascii/
273
274 – 13
Graphic Mode־
. ,
,VGA . . , .
, .Video Graphics Array
:
http://en.wikipedia.org/wiki/Video_Graphics_Array
int 10h
. 200x320 ,
274
275 – 13
, . ־ : ־
,Y . 0:1 A000:0001 ,0:0 A000:0000
. X ,320־
, ־ , VGA , .
, . 256=28 . 8
,RGB 256־ .RGB– ,Red, Green, Blue
video memory־ .) ( 256
–0,0,0 RGB־ – – ,0 .0
.
. ,
.out video memory־
. x,y ,
.ah=0Ch ,BIOS ,
:
–al
)0 ( –bl
X –cx
Y –dx
275
276 – 13
; -------------------------------------------------------------
; Paint a red pixel in the center of the screen
; Author: Barak Gonen 2014
; -------------------------------------------------------------
IDEAL
MODEL small
STACK 100h
DATASEG
x dw 160
y dw 100
color db 4
CODESEG
start:
mov ds, ax
; Graphic mode
mov ax, 13h
int 10h
mov cx,[x]
mov dx,[y]
mov al,[color]
mov ah,0ch
int 10h
276
277 – 13
int 16h
mov al, 2
int 10h
exit:
int 21h
END start
.ah=0Dh , BIOS־
, – . ,
.
. – .
int 10h
; Read dot
mov bh,0h
mov cx,[x]
mov dx,[y]
277
278 – 13
mov ah,0Dh
) : (
, ( ,
.)
1־ X , ,
X־ .) ( 1־ Y ) (
–“Bresenham algorithm” , , . Y־
. ,
Y־ ,
. , .1־
) : ( ,
278
279 – 13
BMP
( . , ,
. ,Bit Map ,BMP , )
BMP . ,
, JPG , ,video memory־
.
www.brackeen.com/vga/bitmaps/html
www.ragestorm.net/tutorial?id=7
: BMP
, .) 1,024 ( , 256–Palette .2
BMP־ palette־ ,) – – , ( RGB
palette־ ,) – – ( BGR
.
,255–0 . –Data .3
data־ , . , .palette־
BMP . ,video memory־
320 . VGA video memory־
. 64,000 , 200
320x200 .
BMP־ .) ( .
1079־ , .data־ ,BMP־ ,
.BMP(1079=54+256x4)־
, .'0' 1079־
0 BGR־ ,palette־ .palette ־0
.54:57 ,palette־
279
280 – 13
. .test.bmp -
.tasm/bin
. -
.header־ -
.palette־ -
. , -
280
281 – 13
; -------------------------------------------------------------------------------------------------------------
; -------------------------------------------------------------------------------------------------------------
IDEAL
MODEL small
STACK 100h
DATASEG
filename db 'test.bmp',0
filehandle dw ?
CODESEG
proc OpenFile
; Open file
mov ah, 3Dh
xor al, al
int 21h
jc openerror
mov [filehandle], ax
281
282 – 13
ret
openerror:
mov ah, 9h
int 21h
ret
endp OpenFile
proc ReadHeader
mov cx,54
int 21h
ret
endp ReadHeader
proc ReadPalette
mov cx,400h
int 21h
ret
endp ReadPalette
282
283 – 13
proc CopyPal
mov cx,256
mov dx,3C8h
mov al,0
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
shr al,2
283
284 – 13
loop PalLoop
ret
endp CopyPal
proc CopyBitmap
mov es, ax
movcx,200
PrintBMPLoop:
pushcx
shl cx,6
shl di,8
add di,cx
mov cx,320
int 21h
mov cx,320
movsi,offset ScrLine
284
285 – 13
;inc si
;inc di
;dec cx
pop cx
loop PrintBMPLoop
ret
endp CopyBitmap
start:
mov ds, ax
; Graphic mode
mov ax, 13h
int 10h
call ReadHeader
call ReadPalette
call CopyPal
call CopyBitmap
285
286 – 13
int 21h
mov al, 2
int 10h
exit:
int 21h
END start
. ,
: .graphics bmp .
https://piazza.com/class/i98gbkdp1mg15m?cid=20
: . "Graphics- advanced"
"
. ,
,
.
:
10x10 BMP- , ? BMP .1
80,100
. , ? .2
, ? .3
.
( , ? .4
.)
+ , ,
):
"
, -1
286
287 – 13
, -2,3
, -4
. , ,
Art of Assembly pdf־ .
.)The Physics of Sound(
. , , –
, " " ." " ,
. " " .
. . –" "
, . . 12- –
, 110 ,)1.06 (2 12
2*110 , 117 ,1.06*110
. 220 ,
in al, 61h
or al, 00000011b
out 61h, al
in al, 61h
out 61h, al
43h . , ,
.42h
287
288 – 13
:43h 0B6h . ,
out 43h, al
: . 1193180
1193180
Divisor = ---------------
Frequency
, – , 8 port 42h
.
. 131 , ,
; ----------------------------------------------------------------
; ----------------------------------------------------------------
IDEAL
MODEL small
288
289 – 13
STACK 100h
DATASEG
CODESEG
start:
mov ds, ax
; open speaker
in al, 61h
or al, 00000011b
out 61h, al
out 43h, al
mov al, ah
mov ah, 9h
int 21h
289
290 – 13
mov ah, 1h
int 21h
in al, 61h
out 61h, al
exit:
int 21h
END start
290
291 – 13
. –
. , -
. , -
. , -
, .
, , .
: .
. , -
. , , -
. , -
, – ־
–dl , (
. .)
55 , 55 ,
55־ .
. .
55־
, . 0040:006Ch
( . . 55
291
292 – 13
, .) ,
: .) (
. – .0040:006Ch
---------------------------------------------------------------;
---------------------------------------------------------------;
IDEAL
MODEL small
STACK 100h
DATASEG
EndMessage db '...Stop.',13,10,'$'
CODESEG
start:
mov ds, ax
mov es, ax
FirstTick:
292
293 – 13
je FirstTick
mov ah, 9h
int 21h
; count 10 sec
DelayLoop:
Tick:
je Tick
loop DelayLoop
mov ah, 9h
int 21h
quit:
int 21h
END start
293
294 – 13
Random Numbers–
, .
, ( –
. –)
. , .
.)Pseudo-random( ־
( ,
, .' ,
."
. .
. ,
.0040:006Ch –
:al )1 0( .1 0: ,
mov es, ax
. ,2 ,9–0
: - 9–0
.1–0 -
294
295 – 13
.1–0 -
.7–0 -
0 , .
.)... 6,1,0
–0 ,9–0 , . –
.9־ 15
. ,
, , . 55 ,0040:006Ch
, . ,
, , . ,
, , . – xor
. , .xor
xor־
. CODESEG־
. ,
; ---------------------------------------------------------------------------------------------
; The method is by doing xor between the timer counter and some bits in CODESEG
; ---------------------------------------------------------------------------------------------
IDEAL
MODEL small
295
296 – 13
STACK 100h
DATASEG
EndMessage db 'Done',13,10,'$'
divisorTable db 10,1,0
CODESEG
proc printNumber
push ax
push bx
push dx
nextDigit:
add al,'0'
jne nextDigit
mov ah,2
mov dl,13
int 21h
mov dl,10
int 21h
296
297 – 13
pop dx
pop bx
pop ax
ret
endp printNumber
proc printCharacter
push ax
push dx
mov ah,2
mov dl, al
int 21h
pop dx
pop ax
ret
endp printCharacter
start:
mov ds, ax
; initialize
mov es, ax
mov cx, 10
mov bx, 0
297
298 – 13
RandLoop:
inc bx
call printNumber
loop RandLoop
mov ah, 9h
int 21h
exit:
int 21h
END start
298
299 – 13
. / –
Scan Codes -
BIOS -
DOS -
. ,
:
, .1
BIOS , .)BIOS/ DOS / (
, . DOS ,
.
in al,60h
: , ,
mov ax,13h
299
300 – 13
int 10h
ah ax , ( ax .33h
ax=0h . , .)int 21h
:
mov ax,0h
int 33h
:ax=1h ,
mov ax,1h
int 33h
mov ax,3h
int 33h
–bx -
. – '00' bx ,
199– 0 , –dx -
.
. , .
300
301 – 13
; ----------------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------------
IDEAL
MODEL small
STACK 100h
DATASEG
color db 12
CODESEG
start:
mov ax,@data
mov ds,ax
; Graphics mode
mov ax,13h
int 10h
mov ax,0h
int 33h
;Show mouse
mov ax,1h
int 33h
301
302 – 13
MouseLP:
mov ax,3h
int 33h
jne MouseLP
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
mov ah,00h
int 16h
; Text mode
mov ax,3h
int 10h
exit:
mov ax,4C00h
int 21h
END start
302
303 – 13
Debug
–
. , . ,
,
, .
, , ,
.
, – ,
.
, ,
.
. ,
. .
: .
. : , -
. , -
, . , , -
? 2 ־cx ,
, , . ,
: .
, . .
... , ,
.
, , .
.)" " ( .'
303
304 – 13
) (
304
305 – 13
. ,
, .
, - .
: .
, , –" " -
. ,
:bmp
call ReadHeader
call ReadPalette
call CopyPal
call CopyBitmap
? . . .
, " " , " "
: , .
;call ReadHeader
;call ReadPalette
;call CopyPal
;call CopyBitmap
( . ,
. ? .)
. TD ־. -
. , ReadHeader־
header .header־ DATASEG
305
306 – 13
, – 'BM' .
.
. , -
, , ־
, .
, - , .
, .
.
, ,
. , ,
, . , ,
.)cx , (
. .
, ,
, .
– ,DATASEG־ . ,
. CODESEG־ –
. ,
. ,
306
307 – 13
: , ,
)StackOverflow ( ,
“Key pressed” , .
- . ESC .“Key released”
scan code- , .
.
. . , .
. 5X5 , .
: .
25 , .a
" " .b
, , .
307
308 – 13
– .
. bmp ASCII
. , ,
. ,
. , , :
, . , .
! .
308
309 – 13
–'
- .
. )" " (
. ,
309
310 – 13
-CLC
-STC
-CLI
Signed Unsigned
JG (JNLE) JA (JNBE)
JL (JNGE) JB (JNAE)
JE JE
JNE JNE
cx=0 -JCXZ
JC
JNC
310
311 – 13
JZ
JNZ
JS
JNS
JO
JNO
JP / JPO
JNP / JPE
ah- 8 -LAHF
-PUSHF
-POPF
- , LOOP-
311
312 – 13
- , SHL, SHR
- , , SHL -ROL
. , ROL ah .
- , ,SHR -ROR
. ,SHL -RCL
. ,SHR -RCR
SHL- -SAL
SHR- -SAR
. ,SUB -SBB
312
313 – 13
: –'
, . Piazza
, " " , .
! , .
:Piazza-
"gvahim" .2
313
314 – 13
"other" .3
assembly .4
314
315 – 13
"assembly" student- .5
submit email , , .6
315
316 – 13
submit .7
. . , -Continue .
316
317 – 13
.new post ,
- Folder = Other
Post my question ,
317
318 – 13
–
http://edjudo.com/wordpress_livedec10/wp-content/uploads/slider/digital.jpg
http://visual6502.org/images/pages/Intel_8086_die_shots.html
http://www.ousob.com
http://en.wikipedia.org/wiki/MS-DOS_API
http://iitestudent.blogspot.co.il/
318