Professional Documents
Culture Documents
04
:
: ,
.
, , , , , ,
, - .
,
".
.
- .
" . " 2015
http://www.cyber.org.il
12 ....................................................................................................................................
1 15 ........................................................................................................
2 18 .........................................................................................
? 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 .................................................................................................................IP, FLAGS 5
84 ..................................................................................................................................
93 .................................................................................................................................
6 94 .............................................................................................. mov
94 ..................................................................................................................................
94 ......................................................................................................................
95 ...........................................................................................................
99 ............................................................................................
100 .................................................................................................................
104 ....................................................................................................
105 ....................................................................................................
105 ..........................................................................................
106 ..........................................................................................
108 .....................................................................................................
108 ............................................................................................
109 ..............................................................................................
111 ...............................................................................................
112 .................................................................................................................. offset
() 116 ........................................................................................
117 ...............................................................................................................................
7 , 118 ...............................................................................
118 ................................................................................................................................
118 ................................................................................................................
126 ...................................................................................................................NEG
127 ......................................................................................................................
130 ..................................................................................................................... OR
131 ...................................................................................................................XOR
133 ........................................................................................................................
135 ....................................................................................................
136 ...............................................................................................................................
8 137 ...............................................................................................................
137 ................................................................................................................................
142 .................................................................................................................
() 144 ..............................................................................................
146 ................................................................................................................
150 ...............................................................................................................
151 ...............................................................................................................................
9 152 ........................................................................................................
152 ................................................................................................................................
154 ................................................................................................................STACK
154 .................................................................................................................
160 ..........................................................................................................................
160 ..........................................................................................................
167 ....................................................................................
170 ................................................................................................
173 ...............................................................................................
178 .......................................................................................................... BP
() 183 .......................................................
186 ...............................................................................
196 ..............................................................................................................
197 ...............................................................................................................................
198 ................................................................................................................................
199 .......................................................................................................
210 ...............................................................................................................................
11 211 .....................................................................................................................
211 ................................................................................................................................
213 ................................................................................................................
232 ..........................................................................................................Traps
( ISR)233 .............................................................................................................
239 ...............................................................................................................................
12 () 240 ................................................................................................
240 ................................................................................................................................
240 ................................................................................................... Interrupts
244 ..........................................................................................................
247 .............................................................................................................................
247 ..........................................................................................................................
250 ................................................................................................
259 ...............................................................................................................................
13 260 .........................................................................................................
260 .............................................................................................................
260 ..............................................................................................................
262 ..................................................................................................................
262 ....................................................................................................................
263 ..................................................................................................................
263 ...................................................................................................................
264 ....................................................................................................................
265 ....................................................................................................
265 .................................................................................................filewrt.txt
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 "" , .
11 . DOS -
.
12
13
12 , . -
. ,
.
13 - , -
.
2.0
" , 25- ,
, . :
reverse engineering . -
, : . -
( ) -
.
"" . ,stackoverflow
- . ,
, . .
.
, .
. ,
. : ,
, , . , ,
.
13
14
, :
14
15 1
1
! , .
? , . .
, ,
. , ,
. ? 0 ?1- .
.
, .
, , , " ,"10111000
, ,mov " ."10111000
.
15
16 1
. :
( ) .
. , .
, . ,
. ,
, :
.
" " ,
, .
. , Reverse
Engineering .Stack Overflow
, , .
. ,
, . , Apple,
.
.
, :
)Knowledge of networking and internet protocols (e.g. TCP/IP, DNS, SMTP, HTTP
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
. , .
, . , .
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 . :
,0 o .
22
23 2
16 C0DE16 -
h . , , ' '0 . -
( 0C0DEh , -
)
0x 0xC0DE -
, ,4F16 , :
: , :19910
.199:16 ,12 .7
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
. 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
:
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
, (
) . :
( )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.
, . ,
, , .
, :
.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
41
42 2
D E A D C 0 D E
... ?
Byte
( )Byte ,
, .
. , , ,
( , )
.
:Nibbles
42
43 2
Word
( )Word 16 , . ,16
,65,536 . 16 , ,
.
Double Word
ASCII
43
44 2
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
,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
? , , ?
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.
.1 " "0 .
.2 1976 .
.3 write ( 0- "").
50
51 3
1976 , :
.1 1976 .
.4 read ( 0- "").
.2 .
51
52 3
, .
h .
, ,ah ah .
, :
.1 1970 .0ABh
L.O , H.O .
52
53 3
. , :
, 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
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
, :
.
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
.:
, ax :
8 . al ,ax.
8:
58
59 3
. 17 :21
mov bx, 0
mov cx, 0
" .
59
60 3
bl :bx bl 8 .bx -
ch :cx ch 8 .cx -
, .
. .
.
60
61 3
Segment Registers
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
.
IP - Instruction Pointer-
FLAGS -
IP . 16 ()pointer
.code segment-
. 8 16 ,
. .
, .
, . , -
IP . ,
, .
, . ,
.
, 3 :ax
ax .ALU
ALU .3
ALU- ".
ALU- .ax
62
63 3
Control Unit
, .
. ,
.
.
.
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 .
, , ' . -
.
: Notepad++ ,
66
67 4
.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:
, 5 ,var1 .al
.base.asm
STACK 100h . . -
DATASEG . -
CODESEG . -
start . - -
-main end .
68
69 4
exit . exit -
, .
.DOS
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
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:
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
.:
\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 -
base.asm .bin -
base.asm : -
zi .debug .base.obj -
- tilnk : -
tlink /v base.obj
v .debug .base.exe
74
75 4
Turbo Debugger TD
, :
td base
, ( ,)F10 view
, :CPU
75
76 4
Turbo Debugger
. ,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 .
. F9-
TD
:Dosbox- ALT+X -
CTRL+F2 : -
( ) : F10 . -
Tab .
( ) : ALT+Enter -
, : "" ( Alt)Ctrl - -
80
81 4
() Opcodes
mov .
, , mov .
mov . .
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++ -
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
,F8 :
. 1 , , ( .) mov bx, 0
85
86 IP, FLAGS 5
.3 IP 0005 ,0008 .
.IP
5.1
IP .
. IP ?
, .IP=0005h IP , .
( )enter .
:
IP ,0010h enter IP . .
.F8 ? 0005 0010
( ?) cx ,bx ,ax
86
87 IP, FLAGS 5
: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 .
:
''z
. ,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:
''o
( ) 16 , ?
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, 1h
mov bl, 2h
sub al, bl
90
91 IP, FLAGS 5
, , :
,-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
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 .
( ) , . -
(" , ,)"... -
, .
. , ,
, .
, .
, - .:
al ds ( 1h .)DS
. ds:1h ,5
5 . al , .
.1h ,
, ,1h .
.
, ds:1h ,age age- .ds:1h
:
94
95 6 mov
,
.
(. )Variables
, .
1 ,6
, 2 .12
mov ds, ax
ds .DATA , ,
.ds ,
.ds , age , ,
age "".
. ,
.
,DATA :
ByteVarName db ?
95
96 6 mov
ByteVarName . db (DB
.) Define Byte ,ByteVarName
. .
, :
; al , , ""
.
, :
DATASEG
ByteVarName db ?
ByteVar2 db ?
ByteVar3 db ?
( )0 ,ByteVarName , .ds:0
. ByteVarName ,ByteVar2 ,
ds:1 .
, :
DATASEG
; Define Double
96
97 6 mov
6.1
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 :
97
98 6 mov
DATASEG . !
.88h
120 ?136 ?
, ( )-120 +136 , , :
.10001000 !
? .
,+120 , 120 136
:
120 ,120 . -
, signed - unsigned
. .
98
99 6 mov
CODESEG . ,
.
,
, .
DATASEG
.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 .
. , , ,
. ,
. , , ,
.
, .
, , ,
:
100
101 6 mov
, ( ,)words 0200h , 0
( 0200h ,0201h ) , 1
,0202h 5 020Ah .
DATA :
ArrayName , .
N N . .
Dup ,duplicate.
, N .
, .
, , :5
, .
101
102 6 mov
32 ,1,2,3,4 .
:6.3
, ,TD
.
DATASEG 5 ,3 : 7 (.)Bytes .
20 ,4,5,6 . .
MOV
. .
mov . :
, ax ( 22) :
mov ax, 22
22 :
102
103 6 mov
mov dx, 10
mov ax, bx
ax .bx bx- .
.ax :
mov ax, cx
mov ax, dx
mov ax, ax
, . ax .ax
:mov
, memory .memory ,
. :
, , . opcode mov
.
103
104 6 mov
8 16 , ( , ) ,
. :
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
16
( )257 8 , .
mov ah, 10 (; Note the difference from last command! 10 decimal, not 10h (=16
6.5
al ( 100) , : ,
( : ,b .)00001111b TD al
.
.
, ( ,)Direct addressing (
)Indirect addressing (.)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 ]ax, [1
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
5 :bx
mov [bx], 5
: :
, , :
bx . : -
mov [bx], ax
,bx bx .: -
mov [bx+2], ax
108
109 6 mov
,bx di ,si -
.:
mov [bx+si], ax
mov [bx+di], ax
, bx si ,di .: -
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 [1], ax
:DATASEG
,DATASEG- ?
,DATASEG :
mov [1], ax
110
111 6 mov
, "" ,ax
ah . ?
. ah .
, ( bx ,ax ') ,
, :
. , 8086
.Little Endian :
http://en.wikipedia.org/wiki/Endianness
mov .
. :
DATASEG:
111
112 6 mov
2 ( al al , ax ) .
:
. Array ,DATASEG
Array 0002 DATASEG :
al ,0CCh 2 .
, ,0 .1
, .:
mov [Array+1], al
, al 2 ,
.
offset
bx , :offset
? . ,
, bx
bx .
112
113 6 mov
LEA
:BB0000 ,
: ? ?
( , )db
ax ( 16).
, .
:
? byte , ( word ).
ax ?
113
114 6 mov
: ax .word .Array+2 :
, .
type override
5 ,bx:
mov [bx], 5
? ,5 5
, , ... .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 .:
. (
:)base.asm
mov ds, ax
.3 .:
. , var2 ,var1
:
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 ,
:
: , , -
shr, shl : -
. .
80x86 : , , ( ).
DEC ,INC ,IDIV ,DIV ,IMUL ,MUL ,SUB ,ADD : .NEG
:
118
119 7 ,
ADD
,ax -
. !
8 16. -
:7.1 add
6 . . al . .
,TD .al
, al ( ?) .
, .ax
: .
var1 -
var2 -
sum -
sum ( )?sum
119
120 7 ,
SUB
: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
MUL / IMUL
( mul ) multiply . , 8
16 16 32 .
8 , .ax 16 ,
16 ax 16 .dx
121
122 7 ,
signed ,unsigned .
. 11111011 .2
.251 , , .
CODESEG:
mov ax, 0
mul bl
mov ax, 0
imul bl
ax, bx
122
123 7 ,
- ,imul al , ,0FFF6
:7.3 mul
,byte ,255-0 .
( : ?)
, 4 .signed , . .
.sum , .word:
a ,b
sum = a[0]*b[0]+a[1]*b[1]+
123
124 7 ,
DIV, IDIV
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 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 ,
:7.4 div
,byte ,unsigned .
.
,byte ,signed .
.
,word ,unsigned .
.
NEG
( neg ) negative , ,
.:
neg dest
dest = 0 - dest
126
127 7 ,
.
, , . ,
,MASKING .
? , . ,
"" .Packed data , 0 :
01110010
,
.
. , ,
:
00100101
, .
, "" . .
, . :
127
128 7 ,
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 .
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 :
0101 0100
---------------
1100 0111
, xor :
0101 0100
---------------
1001 0011
131
132 7 ,
, ,
. .
:
xor ax, ax
ax ,0 ,
.
:7.7 xor
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 .
"" . , 0 ,1 1
2 . .
, . shr :
) (shift right.(shift left) shl
SHL
, shl , . ,
.cl :
shl register, cl
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, cl
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 ds, ax
137
138 8
mov ax, 1
jmp ][address
:jmp
,jmp :ip
.CODESEG
NEARFAR
,jmp
000Ah .address jmp ,
jmp- . , (
) , .near ,near jmp
.
far . ,
, . ,
. :
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- , (
) - .
139
140 8
: .label
.
label :8.2
140
141 8
CMP
( cmp )compare . ""
, . ,
. . ( , ,)
. cmp ,sub .
: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
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 ,
. , :
. :
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 bl, 1b
cmp al, bl
cmp :
: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
TimesToPrintX ? ? .
LOOP
. ,
' 'x .TimesToPrintX ,
' 'x , ,TimesToPrintX
' 'x .
( .)Loop , .
loop :
1 .cx -
cx. -
( , cx ) jmp label-.
loop SomeLabel
dec cx
cmp cx, 0
jne SomeLabel
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
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:
Loop PrintX
ExitLoop:
146
147 8
( Nested Loops)
, .
mov cx, 10
LoopA:
mov cx, 5
LoopB:
loop LoopB
loop LoopA
10 ,LoopA LoopA 5
.LoopB LoopB , 50 . ?
, (
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 ( ,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
149
150 8
jmp -
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
my_label:
ja-:jmp-
cmp ax, bx
jbe help_label
jmp my_label
150
151 8
help_label:
my_label:
. .
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
152
153 9
OpenEmail:
jmp ReadPassword
ReadPassword:
cmp
... .
.
. ReadPassword , . . .
, . ,ReadPassword
.
.
. , call
. ,:
call ReadPassword
. ,
, . ,
. " "
, .
,? , .
. ,
, .
, .
.
, .
153
154 9
. , .
, . ""
.
, , , .
.
STACK
( , )STACK . ,
.
, . ,
, .
Stack Segment
, .
. :
, 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 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 ?
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 ?
? ?
! , sp
. :
sub sp, 4
"" ,sp .
.10h, 20h . sp . .
ax bx .mov .
159
160 9
, () , .
"" :
.call -
. , -
.num1, num2
. num1+num2 , , -
.
( ) -
.
,CODESEG .CODESEG :
proc ProcedureName
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
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
DATASEG 8 ( )
9.2
.ZeroMemory TD ip
sp
CALL, RET
call . :
call ZeroMemory
( call .)... :
162
163 9
ip :call
163
164 9
.ret :ip=0Ah
,ret ip
:
ip , 0Ah.15h
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
165
166 9
, near .far:
. :"far" ZeroMemory
, . . :
, sp ( 00FCh ,00FEh ). -
, .near cs.
, near.far
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
, , . , .
? , .TD .cx
: cx .3 .4 ,0
loop Row ( 65,535- , unsigned-
) . row ( cx ) ,
, cx 0 ...
, ,
( ) .
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
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
xor al, al
ZeroLoop:
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
pop ax ;i
push ReturnAddress
175
176 9
ret
endp SimpleProc
ReturnAddress .DATASEG , .
.-
.
, 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
add [byte ptr es:bx], 2 ; This actually changes the value of "parameter"
push ReturnAddress
ret
endp SimpleAdd
.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
proc SimpleProc
push bp
mov bp, sp
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
, .:
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
: , 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 .
jmp bx
jmp bx
ret push .
push 2 , 6 .
ret 6 sp "" .
181
182 9
:bp
.1 pop .
.2 pop . .bp
.3 .
,bp , bp- .
:9.6 bp
, ,pass by reference ( .
.var1=4, var2=5 .)var1=5, var2=4
.bp
182
183 9
()
, .
, . ,
.
? , .sp ,
.sp , , 6,
:
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
, 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
,CODESEG ,
:
push num_elements
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 , -
.
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
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
, .
GetName 10 . , :
.GetName - 10.
191
192 9
, bp , ASCII-
: ASCII- 20h .21h -
, , . ,
. - ?
enter .
. pop .bp- , bp
.2020h - .ret- 2120h ip
( 2120h ?2021h .)little endian
Psalter .1265- ,
,.
192
193 9
, 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
?MyProc integer
.integer .
;)int c = MyProc(1,2
MyProc ,C
. - Caller-?Callee-
.1 :
. ':
push 1
push 2
call MyProc
':
push 2
194
195 9
push 1
call MyProc
caller- callee-
, . , , caller- callee-
.
.2 :
MyProc , . ,
MyProc , callee-
, - .c
caller- callee- .
, caller- callee- .
.3 :
, , .
sp .
. , ret- . , 4 :
ret 4
:sp
add sp, 4
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
/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
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 :
mf . :
xlat
al- ASCII- ,m .
:MOVSW .5 . ,
200 , .
, mov
, . ,
MOVSW .
200
-10 ()
. STD CLD
.
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 - ? .
. .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 .
.
, . ?
ip- . ret- cs:013Fh .
. , .ret-
, "" .ret- , ret-
ip- . 0006h bx bx
. 0006h
, . .
.coffee.com- RE
.
204
-10 ()
codeguru.com
. , ret- .cs:011Ch
. .
. ax 2015h
205
-10 ()
bx .1Dh
,100h bx .11Dh ds:bx .cs:011Dh-
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 ()
"" . , :
" ."Access granted , .
, " "Access granted .
.
'Make it -
,
. : , . ,
.
: ah ,1 ,int 21h
.al:
mov ah, 1
int 21h
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 . ,
?
.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 -
, ():
8h . -
: ,
, .
, .
,DOS .
, , .
. Interrupt Service Routine .ISR
, , ISR
. .
212
-11
mov ax,5 .1 .
, ax 5 .
.2
( .)ISR , cs.ip
.5 .ISR
.6 , ISR ( ')
cs ,.ip
.7 .cs:ip
( 100h )
213
-11
.
. , . 55 ,
, . ISR ,
F7 . 55 ...
F7 ISR , ...
.
. disable interrupts "" ,
.
, ISR , ,if
. .enable interrupts
ISR( IVT)
ISR :
iret
endp ISRname
iret ret
.ISR iret ret iret pop , . ,
ISR , .
: .
, .ISR ?
, . 0 255,
. , :
int 21h
.21h
214
-11
. , ,
. ,Interrupt Vector Table .IVT
, ISR , , 4
ISR . : 21h 84h .
,84h ,IVT , ISR .21h
215
-11
DOS
,DOS ,Disk Operating System .
, .Windows
.
. ,
, .
. , ''ISR ,
.
DOS IVT
,21h . ,
.int 21h
? int 21h
.
, ah .
216
-11
http://spike.scu.edu.au/~barry/interrupts.html
int 21h ah .
.
AH=1h
mov ah, 1
int 21h
.al
al , 2 , ASCII
"( "0 .)30h
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 .
ASCII 0.)9-
, .
( DL : ).
: 4 . .
. , 4
( 250 .)0250
30 .
AH=2h
mov ah, 2
int 21h
al .
ASCII , :
219
-11
:'Y' ,'X'
IDEAL
MODEL small
STACK 100h
DATASEG
CODESEG
start:
mov ds, ax
;print 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 ah, 2
int 21h
exit:
int 21h
END start
220
-11
:11.2
'.'A .
'.'a .
' ,'HELLO . .
. .
. .
. : ,
. 5 .7
, .
221
-11
AH=9h
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' : . .
, 0 ,9 .
.''Wrong input" :
, .: .
AH=0Ah
, - " (.)Buffer
.
dx ( )ds- Int 21h ah .0Ah
223
-11
, Enter- ,
.
ASCII- Enter .
20 :
( - 23 , -
, - ASCII- :)Enter
IDEAL
MODEL small
STACK 100h
DATASEG
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
10 , ( )abc
(.)ABC
225
-11
- ( : )
:11.5 .
:11.6 10 .
:11.7 ( !)
** ** * * ***** *****
.11.8 :
ABCDEFGE.Z
:11.9 :
. . .
. .
. .
. .
:11.10 :
226
-11
AH=4Ch
/ ( AH=2Ch ,AH=2Dh)
. 55
( 0.055) , 18.2 . " 1/18".
.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
. ,0 .1
: ,
0 ,960 55
.
. , . .
.
231
-11
Exceptions
.IVT exception
, . .
, .
:
mov cl, 0
, .
.
, , .int 0h
debugger .
. ,
. , "".
, , .int 1h
, ,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- , .
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
' ISR-IVT-
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 .
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-
242
-12 ()
PIC
,8086 .
. ?
. Programmable Interrupt ,
,Controller .PIC
243
-12 ()
PIC . PIC ,
. : , .
... ?
. PIC . ,
. ,
.
PIC :
. -
, I/O Port . -
. -
end of interrupt , -
.
. -
, disable interrupts .
. , (
) , .
?
. .
" "?
, , PIC . ,
, PIC . PIC
244
-12 ()
. , , PIC
PIC .
, , 55 . , 55
.
.
/ I/O Ports
( )buses ,
/ . , address bus .
, . , 80x86
16 , .
/ , ( I/O .)Input / Output
I/O , :
I/O (.)Port -
I/O - 16 ( 64K). -
,mov in.out -
:in, out -
: , 0 255. o
: ,255 .dx o
in ax/al, port
in ax/al, dx
245
-12 ()
in / out :255
( , , ) .
in ,
.out
:255
in al, dx
,255 dx- .
, "" ( .)Properties ,
"" . 60 .64
246
-12 ()
. , , :
.1 . .Scan Codes
.2 I/O- , .60h
, .
:
.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
.2 PIC IR1 .
.3 PIC , . int
.9h
.4 , ISR .9
248
-12 ()
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 ( ):
, :
WaitForData:
in al, 64h
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
CODESEG
start:
mov ds, ax
WaitForData :
jne WaitForData
ESCPressed:
mov ah, 9
int 21h
exit:
int 21h
END start
252
-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:
253
) (-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
254
-12 ()
exit:
int 21h
END start
BIOS
, 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 .
":
mov al,07h
int 21h
. :
, , .
'.
, ASCII , ASCII
. , . ,
ASCII .
:12.1
( 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 ()
: .
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 -
,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
: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
.ah=3Fh :
filehandle bx DOS .
cx .
dx () .
: dx ,
.
proc ReadFile
; Read file
mov ah,3Fh
mov cx,NumOfBytes
int 21h
ret
endp ReadHeader
ax , , .
. .ah=40h:
filehandle bx DOS- .
cx . : cx=0 ,filehandle.
263
264 13
dx () .
ax , , .
proc WriteToFile
mov ah,40h
mov cx,12
int 21h
ret
endp WriteToFile
( 21h ,)ah=4Ch
. , ?
, . , , .
: ( , , ,
) . , .
.ah=3Eh :
filehandle bx DOS .
proc CloseFile
mov ah,3Eh
int 21h
ret
endp CloseFile
264
265 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
265
266 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
266
267 13
int 21h
ret
endp CloseFile
start:
mov ds, ax
; Process file
call OpenFile
call WriteToFile
call CloseFile
quit:
int 21h
END start
267
268 13
. , .
I/O- .
.B000:FFFF
, :modes
80 .ASCII
320 .
( ,DOSBOX
).
( ).
.video memory ,
.video memory , -
. BIOS .
,DOS . -
268
269 13
Text Mode
,
.ASCII , ASCII .
mov ah, 0
mov al, 2
int 10h
. , .
ASCII
DATASEG .,
,DATASEG , include
.DATASEG
:monalisa.asm
269
270 13
, . $
.
( 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
int 21h
int 16h
exit:
int 21h
end start
271
272 13
: , ,
For beginners:
www.en.wikipedia.org/wiki/ASCII_art_converter
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
int 10h
, 200x320.
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
.
, .
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 ds, ax
; Graphic mode
mov ax, 13h
int 10h
mov cx,[x]
mov dx,[y]
mov al,[color]
mov ah,0ch
int 10h
275
276 13
int 16h
mov al, 2
int 10h
exit:
int 21h
END start
BIOS , .ah=0Dh
, . ,
.
. .
int 10h
; Read dot
mov bh,0h
mov ]cx,[x
mov ]dx,[y
276
277 13
mov ah,0Dh
( : )
, ( ,
).
, , 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 :
Data .3 . ,2550
.palette , . , data
,video memory . BMP
video memory VGA . 320
200 , 64,000.
. 320x200
. ( ) . BMP
, ,BMP .data , 1079
.(1079=54+256x4) BMP
278
279 13
.test.bmp . -
.tasm/bin
. -
.header -
.palette -
, . -
279
280 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
280
281 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
281
282 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
282
283 13
loop PalLoop
ret
endp CopyPal
proc CopyBitmap
mov es, ax
mov cx,200
PrintBMPLoop:
push cx
shl cx,6
shl di,8
add di,cx
mov cx,320
int 21h
mov cx,320
283
284 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
284
285 13
int 21h
mov al, 2
int 10h
exit:
int 21h
END start
, .
. bmp .graphics :
https://piazza.com/class/i98gbkdp1mg15m?cid=20
"
, .
,
.
:
.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
out 61h, al
, , . 43h
.42h
286
287 13
, . 0B6h :43h
out 43h, al
1193180 .:
1193180
Divisor = ---------------
Frequency
port 42h 8 , ,
.
, , 131.
; ----------------------------------------------------------------
; ----------------------------------------------------------------
IDEAL
MODEL small
287
288 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
288
289 13
mov ah, 1h
int 21h
in al, 61h
out 61h, al
exit:
int 21h
END start
289
290 13
.:
, . -
, . -
, . -
. ,
. , ,
.:
, . -
, , . -
, . -
( , dl
) . .
, 55 , 55
. 55
. .
55
. 0040:006Ch ,
55 . . (
290
291 13
, ) . ,
.
( ) . :
.0040:006Ch .
;---------------------------------------------------------------
;---------------------------------------------------------------
IDEAL
MODEL small
STACK 100h
DATASEG
'EndMessage db '...Stop.',13,10,'$
CODESEG
start:
mov ds, ax
mov es, ax
FirstTick:
291
292 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
292
293 13
Random Numbers
. ,
( ,
) .
. , .
( .)Pseudo-random
, (
, ' . ,
) . , "" "
".
. .
, .
.0040:006Ch
, 0 : .1 ( 0 )1 :al
mov es, ax
. 015 ,70'.
,90 ,2 .
90- :
.10 -
293
294 13
.10 -
.70 -
. , 0
6,1,0.)...
. , ,90 0
15 .9
, .
,0040:006Ch 55 . , ,
, . ,
, . , ,
xor . , ,
.xor , .
xor
. CODESEG
, .
; ---------------------------------------------------------------------------------------------
; The method is by doing xor between the timer counter and some bits in CODESEG
; ---------------------------------------------------------------------------------------------
IDEAL
MODEL small
294
295 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
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 ds, ax
; initialize
mov es, ax
mov cx, 10
mov bx, 0
296
297 13
RandLoop:
inc bx
call printNumber
loop RandLoop
mov ah, 9h
int 21h
exit:
int 21h
END start
297
298 13
/ .
Scan Codes -
BIOS -
DOS -
, .
:
.1 ,
( .)BIOS / DOS / , BIOS
, 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 -
, bx ' '00 .
cx , ( 6390 , 320 -
, cx ).
dx , 199 0 -
.
. , .
299
300 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
300
301 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
301
302 13
Debug
, . , .
,
. ,
, , ,
.
, ,
.
, ,
.
, .
. .
. :
, : . -
, . -
, , ., -
, cx 2 ?
, . , ,
. :
. . ,
, , ...
.
. , ,
' . ( " ").
302
303 13
( )
303
304 13
, .
. ,
. - ,
. :
" " , , -
, .
:bmp
call ReadHeader
call ReadPalette
call CopyPal
call CopyBitmap
. . . ?
" " ,"" ,
. , :
; call ReadHeader
; call ReadPalette
; call CopyPal
; call CopyBitmap
, . (
) . ? .
. TD . -
ReadHeader , .
DATASEG .header header
304
305 13
. ' 'BM ,
.
, . -
, ,
. ,
. , - ,
. ,
.
, ,
, , .
, , . ,
( , .)cx
. .
, ,
. ,
, . ,DATASEG
CODESEG .
, .
, .
305
306 13
, ,:
, ( )StackOverflow
. , Key pressed
.Key released ESC . -
. , scan code-
.
. , . .
, 5X5 . .
.:
.a , 25
" .b"
. , ,
306
307 13
ASCII bmp .
, , .
, .
: , , .
. , . ,
.!
307
308 13
'
. -
( "") .
, .
308
309 13
-CLC
-STC
-CLI
Unsigned Signed
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- , -
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-
.2 ""gvahim
312
313 13
.3 ""other
.4 assembly
313
314 13
.5 student- ""assembly
.6 , , submit email
314
315 13
.7 submit
. -Continue , . .
315
316 13
, .new post
- 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