Title: E-mail: xq_xiu@yahoo.com.cn
1????
??? ???????????? E-mail xq_xiu_at_yahoo.com.cn
22
????
3?????????
- ????
- ???????(??)??,????????????????????????(??)??
- -digitsradix
- ?..????????,?..?????????????????????(?????)?
- Radix(????)???????(?????)
- d ??? t ???(??)
- b ??? y ???(??)
- h ???? q/o ???
- r ????
- ????????????,??????????
- ???????????????????0,?????????????????
3
????
4- ?????
- ??????????????????????????????????????????32?????
(0--FFFFFFFFh)? - ?????????
??? ?? ???
() ??? 1
,- ????? 2
,/ ??? 3
MOD ?? 4
,- ??? 5
????
4
5- ????
- ??????????????????(????)???
- ???????????????????????????????????
- signinteger.integerexponent
- ?????????????
- sign ,-
- exponent E,-integer
- ????????????????????,???????,???????????
- ????
- ??IEEE Standard for Binary Floating-Point
Arithmetic(754-1985)
????
5
6- ????
- ????????????????????????????????????????ASCII??
- ??A,d
- ?????
- ??????????????????????
- ??ABC,It is OK
- ????????
- ??Its OK,Say Hello,
7- ???
- ?????????????????,?????????,?????????????,???????
??? - ???????MOV,ADD?SUB?,????Intel?????????????
- ???????MASM??????,??.DATA,PROC?EQU??
- ??????????????????????????,??BYTE?WORD??
- ????????????
- ???????_at_data,????????????
8- ???
- ????????????,???????????????????????????????
- ??????1247???
- ????????????????????????,???????-Cp???????????????
??? - ??????????????(AZ?a--z)????(_)?_at_?,???????????
- ??????????????????
- ???????????????????????
9- ???
- ?????????????????????????????????????????????????
?????? - ?????????????????,?Intel??????????????????Intel???
???????,????????????? - .DATA????????????????
- .data
- .CODE????????????????
- .code
- PROC????????,???name????????
- name PROC
10- ??
- ???????????????????,???????????????????????(??)??
????(??)????(????)???(??)4?????? - ???????????????
?? ????? ??? ??
11- ??
- ??????????????????,????????????????????(?????????
??????????????????????) - ?????????
- mov ax,0020 ? mov ax,myVariable
- ?????????????,????????????????????????()????????
?????????????????? - target
- mov ax,bx
- jmp target
- ???????????,????????????????????????????
- first BYTE 10
12- ?????
- ?????????????,?????????????????
- mov ??????(??)??????
- add ?????
- sub ????????????
- mul ?????
- jmp ????????
- call ??????
13- ???
- ???????????03????,????????????????????????I/O???
- ?????????????????????
- stc ??????
- inc ax ?ax???1
- mov count,bx ?bx????count
- imul r32, m32,imm8 r32?m32 imm8
14- ??
- ??????????????
- ???????()????,??????????????????,??????????
- ????COMMENT????????????????,?????????????,???????
?????????? - COMMENT !
- This line is a comment.
- This line is also a comment.
- !
15AddSub.asm
- TITLE Add and Subtract (AddSub.asm)
- This program adds and subtracts 32-bit
integers. - Last update 2/1/02
- INCLUDE Irvine32.inc
- .code
- main PROC
- mov eax,10000h EAX 10000h
- add eax,40000h EAX 50000h
- sub eax,20000h EAX 30000h
- call DumpRegs display registers
- exit
- main ENDP
- END main
16- TITLE Add and Subtract (AddSub.asm)
- TITLE???????????,??????????
- This program adds and subtracts 32-bit
integers. - Last update 2/1/02
- ???,????????????????
- INCLUDE Irvine32.inc
- INCLUDE????Irvine32.inc???????????????,Irvine32.in
c???\Masm615\INCLUDE???? - .code
- .code?????????????,?????????????????
17- main PROC
- PROC?????????????,???????main?
- mov eax,10000h EAX 10000h
- MOV?????10000h???eax???,??????(eax)???????,???????
?????? - add eax,40000h EAX 50000h
- ADD???40000h??eax?????
- sub eax,20000h EAX 30000h
- SUB???eax???20000h?
- call DumpRegs display registers
- CALL????????CPU?????????
18- exit
- exit???(??)??????MS-Windows????????
- main ENDP
- ENDP?????main??????
- END main
- END?????????????????,???????????????????main??????
?(????????????,??????)????
19- ?
- ?????????,??????????????????
- ???????????????,??????????????????,??????????
- ??????????????????
- ?????????
20??????????
21make32.bat
- REM make32.bat - Batch file for
assembling/linking 32-bit Assembly programs - REM Revised 11/15/01
- _at_echo off
- cls
- REM The following three lines can be customized
for your system - REM BE
GIN customize - SET PATHC\Masm615
- SET INCLUDEC\Masm615\INCLUDE
- SET LIBC\Masm615\LIB
- REM EN
D customize - ML -Zi -c -Fl -coff 1.asm
- if errorlevel 1 goto terminate
- REM add the /MAP option for a map file in the
link command. - LINK32 1.obj irvine32.lib kernel32.lib
/SUBSYSTEMCONSOLE /DEBUG - if errorLevel 1 goto terminate
- dir 1.
- terminate
- pause
22AddSubAlt.asm
- TITLE Add and Subtract
(AddSubAlt.asm) - This program adds and subtracts 32-bit
integers. - 32-bit Protected mode version
- Last update 2/1/02
- .386
- .MODEL flat,stdcall
-
- ExitProcess PROTO,dwExitCodeDWORD
- DumpRegs PROTO
- .code
- main PROC
- mov eax,10000h EAX 10000h
- add eax,40000h EAX 50000h
- sub eax,20000h EAX 30000h
- call DumpRegs
- INVOKE ExitProcess,0
- main ENDP
23- .386
- .386???????????CPU(Intel 386)?
- .MODEL flat,stdcall
- .MODEL?????????????????,STDCALL????MS-Windows???
-
- ExitProcess PROTO,dwExitCodeDWORD
- DumpRegs PROTO
- ??PROTO???????????????ExitProcess?????????(??)???
DumpRegs?Irvine32??????????????? - INVOKE ExitProcess,0
- INVOKE????????????,????ExitProcess????????,???????
??????,???0?
24?????????
- TITLE Program Template (template.asm)
- Program Description
- Author
- Date Created
- Last Modification Date
- INCLUDE Irvine32.inc
- (insert symbol definitions here)
- .data
- (insert variables here)
- .code
- main PROC
- (insert executable instructions here)
25????
?? ??
BYTE 8??????
SBYTE 8??????
WORD 16??????(??????????????)
SWORD 16??????
DWORD 32??????(?????????????)
SDWORD 32??????
FWORD 48???(??????????)
QWORD 64???
TBYTE 80?(10??)??
REAL4 32?(4??)IEEE???
REAL8 64?(8??)IEEE???
REAL10 80?(10??)IEEE??????
26- ??????
- ??????????????????????????????????????????
- ?? ??????? ??? ,???
- ????????????????????????????????????????????????
?????????????????????? - ??????,???????,??????????????
27- ??BYTE?SBYTE??
- ??????????BYTE?SBYTE???,???????????????????????,?
???????8??????????????? - value1 BYTE A ????
- value2 BYTE 0 ??????????
- value3 BYTE 255 ??????????
- value4 SBYTE -128 ??????????
- value5 SBYTE 127 ??????????
- value6 BYTE ? ?????????
28- ????????,??????????????????
- .data
- value1 BYTE A
- value2 BYTE 0
- ?????????????????,???????????????????,???????list?
???0 - .data
- list BYTE 10,20,30,40
29- ?????????????????,??????????????
- Greeting BYTE Good Morning,0
- ????????????(???NULL,???0)??????,C/C,Java?Micros
oft Windows?????????????? - ?????????,????????????????
- Greeting BYTE Good Morning, ,0dh,0ah
- BYTE My Friends,0
- ??????0dh?0ah?CR/LF(???????,?????)?
- MASM?????(\)??????????????,????(\)????????????
- Greeting \
- BYTE Good Morning, My Friends,0
30- DUP?????????????????????????????
- BYTE 20 DUP(0) 20??,????0
- BYTE 20 DUP(?) 20??,????
- BYTE 4 DUP(STACK) 20??STACKSTACKSTACKSTACK
- ????????????,???????????,??????????????MASM????(?
??????)?????????????,??????????????????? - list BYTE 10,20,30,40
- listSize ( - list)
- Greeting BYTE Good Morning, My Friends,0
- stringSize ( - Greeting)
31- ??WORD?SWORD??
- ??DWORD?SDWORD??
- ??QWORD??
- ??TWORD??
- ????
- ???????????????????????
???? ?????? ??????
?????(REAL4) 6 1.18e-38 3.40e38
?????(REAL8) 15 2.23e-308 1.79e308
?????? 19 3.37e-4932 1.18e4932
32- ????
- Intel???????????(little endian
order)?????????,???????????????????????????,??????
??????????
??????? ??0?
?? 12345678h
33AddSub2.asm
- TITLE Add and Subtract, Version 2
(AddSub2.asm) - This program adds and subtracts 32-bit integers
- and stores the sum in a variable.
- Last update 2/1/02
- INCLUDE Irvine32.inc
- .data
- val1 dword 10000h
- val2 dword 40000h
- val3 dword 20000h
- finalVal dword ?
- .code
- main PROC
- mov eax,val1 start with 10000h
- add eax,val2 add 40000h
- sub eax,val3 subtract 20000h
- mov finalVal,eax store the result (30000h)
- call DumpRegs display the registers
- exit
34????
- ????(?????)???????(???)??????????????????????????
??????,????????????????????????????,??????????????
?? ??
???????? ? ?
??????????? ? ?
35- ?????
- ??()??????????????????????
- ?? ???
- ??,???(expression)?32?????,???????,??????(name)???
???????????????????????? -
-
- ?????????????????
-
COUNT 500 mov al,COUNT
mov al,500
36- EQU???
- EQU??????????????????????,???????
- name EQU expression
- name EQU symbol
- name EQU lttextgt
- ???????,expression??????????????????,symbol?????
?EQU?????????????,text??????????????????name?,?
?expression???????text?????? - matrix1 EQU 10 10
- matrix2 EQU lt10 10gt
- .data
- M1 WORD matrix1 M1 WORD 100
- M2 WORD matrix2 M2 WORD 10 10
- ?EQU?????????????????????
37- TEXTEQU???
- 6.0???MASM????TEXTEQU????EQU????,?????Microsoft??
????(text macro)???????????? - name TEXTEQU lttextgt
- name TEXTEQU textmacro
- name TEXTEQU constExpr
- ???????,??????????????,?????????????????????,???
???????????? - rowSize 5
- Count TEXTEQU (rowSize 2) count TEXTEQU lt10gt
- move TEXTEQU ltmovgt
- setupAL TEXTEQU ltmove al,countgt setupAL TEXTEQU
ltmov al,10gt - ?EQU????,TEXTEQU???????????
38????
38
2007?09?
38
????
39?????
??? ??
r8 8??????AH?AL?BH?BL?CH?CL?DH?DL
r16 16??????AX?BX?CX?DX?SI?DI?SP?BP
r32 32??????EAX?EBX?ECX?EDX?ESI?EDI?ESP?EBP
reg ????????
sreg 16?????CS?DS?SS?ES?FS?GS
imm 8?16?32????
imm8 8????(??)
imm16 16????(?)
imm32 32????(??)
r/m8 8????(???8???????????)
r/m16 16????(???16??????????)
r/m32 32????(???32???????????)
mem 8?16?32??????
40.data val BYTE 10h . . .code mov al,val
mov al,00010400
??val1????10400h?
??????????????????,CPU??????????????????
41- MOV??
- MOV???????????????????????,??????????????????????
???????? - MOV destination,source
- ??MOV???????????
- ????????????
- ???????????????
- ????????CS?EIP?IP
- ?????????????
- ????????
.data val1 WORD ? val2 WORD ? .code mov
ax,val1 mov val2,ax
42.data signeVal SWORD -16 FFF0h .code mov
eax,0 mov eax,signeVal EAX0000FFF0h
.data count WORD 1 0001h .code mov eax,0 mov
eax,cout EAX00000001h
.data signeVal SWORD -16 FFF0h .code mov
eax,0FFFFFFFFh mov eax,signeVal EAXFFFFFFF0h
43- MOVZX??
- MOVZX(move with zero-extend)???????????????????,?
??????(zero-extend)?16??32??????????????,????????
- MOVZX r32,r/m8
- MOVZX r32,r/m16
- MOVZX r16,r/m8
mov bx,0A69Bh movzx eax,bx EAX 0000A69Bh movzx
edx,bl EDX 0000009Bh movzx cx,bl CX 009Bh
44- MOVSX??
- MOVSX(move with sign-extend)???????????????????,?
????????16??32??????????????,???????? - MOVSX r32,r/m8
- MOVSX r32,r/m16
- MOVSX r16,r/m8
- ?????????????????????????????????
10000111
????
mov bx,0A69Bh movsx eax,bx EAXFFFFA69Bh movsx
edx,bl EDXFFFFFF9Bh movsx cx,bl CXFF9Bh
10000111
11111111
?????
45- LAHF?SAHF??
- LAHF(load status flags into AH)???EFLAGS??????????
AH???,???????????????????????????????????????????
????????????? - .data
- saveflags BYTE ?
- .code
- lahf
- mov saveflags,ah
- SAHF(store AH into status flags)???AH????????EFLAG
S?????????? - mov ah,saveflags
- sahf
46- XCHG??
- XCHG(exchange data)????????????,????????
- XCHG reg,reg
- XCHG reg,mem
- XCHG mem,reg
- XCHG???????MOV?????????????
- ?????????
mov ax,val1 xchg ax,val2 mov val1,ax
47- ???????
- ?????????????,????????(direct-offset)???,????????
????????????
.data arrayB BYTE 10h,20h,30h,40h,50h .code mov
al,arrayB AL10h mov al,arrayB1 AL20h mov
al,arrayB2 AL30h
.data arrayW WORD 100h,200h,300h .code mov
ax,arrayW AX100h mov ax,arrayB2 AL200h mo
v ax,arrayB4 AL300h
????(effective address)???????????????????()??
????,?????????????????????????
48?????
- INC?DEC??
- INC(increment)?DEC(decrement)????????1??1,???
- INC reg/mem
- DEC reg/mem
- ADD??
- ADD???????????????????,???
- ADD ?????,????
- ????????????,??????????????????MOV?????????????
49- SUB??
- SUB????????????????,????
- SUB ?????,????
- SUB???????MOV?????????????
- CPU???????,???????????????4-1????4(-1)
- NEG??
- NEG(negate)??????????????????????????????????????
?? - NEG reg/mem
50- ?????????
- ????,?????????????????????????,????????????????,?
??????????bug?????,??????????????????CPU??????,???
??????????? - ????????
- ?????????????????????,??????????
- ????????????,?????????
51- ????(???????)
- ???CPU????????????,??????????????????????????????
??????????,????????? - INC?DEC???????????
52- ????(???????)
- ??????????????,??????????????????????????????????
??,????????? - ???????????????,??????????,??????
- ???????????
- ???????????
- ????????????,?????????
- CPU?????????????????????????????????,????????????
???? - NEG??
mov al,-128 AL10000000b neg al AL10000000b,OF
1
53?????????????
- OFFSET???
- OFFSET????????????????????????????????,??????????
??????????32???,??????????16??
.data bVal BYTE ? wVal WORD ? dVal1 DWORD ? dVal2
DWORD ?
.code mov esi,offset bValESI00404000h mov
esi,offset wValESI00404001h mov esi,offset
dVal1ESI00404003h mov esi,offset
dVal2ESI00404007h
54- ALIGN???
- ????ALIGN?????????????????????,????,?????????????
??????? - ALIGN ???
- ????1?2?4???????1,??????????
- ????????,CPU????????????????????????????
bVal1 BYTE ? 00404000 ALIGN 2 wVal1 WORD
? 00404002 bVal2 BYTE ? 00404004 ALIGN 4 dVal1
DWORD ? 00404008 dVal2 DWORD ? 0040400C
55- PTR???
- PTR????????????????PTR?????????????????????BYTE?
SBYTE?WORD?SWORD?DWORD?SDWORD?FWORD?QWORD?TBYTE???
.data myDouble DWORD 12345678h .code mov
ax,myDouble ERROR !
.data myDouble DWORD 12345678h wordList WORD
5678h,1234h .code mov ax,WORD PTR
myDouble AX5678h mov ax,WORD PTR
myDouble2 AX1234h mov eax,DWORD PTR
wordList EAX12345678h
56- LABEL???
- LABEL??????????????????????????????????LABEL?????
?BYTE,WORD,DWORD,QWORD?TBYTE??????????? - LABEL??????????????????????????,???????????????
.data val16 LABEL WORD val32 DWORD
12345678h longVal LABEL DWORD dVal1 WORD
5678h dVal2 WORD 1234h .code mov
ax,val16 AX5678h mov ax,val162 AX1234h mov
eax,longVal EAX12345678h
57- TYPE???
- TYPE????????????????????????
??? ?
TYPE bVal 1
TYPE wVal 2
TYPE dVal 4
TYPE qVal 8
.data bVal BYTE ? wVal WORD ? dVal DWORD ? qVal
QWORD ?
58- LENGTHOF???
- LENGTHOF?????????????,????????????????????
??? ?
LENGTHOF arrayB 3
LENGTHOF arrayW1 302
LENGTHOF arrayW2 53
LENGTHOF arrayD 5
LEGNTHOF digitStr 9
.data arrayB BYTE 10,20,30 arrayW1 WORD 30
DUP(?),0,0 arrayW2 WORD 5 DUP(3 DUP(?)) arrayD
DWORD 1,2,3,4 digitStr BYTE 12345678,0
SIZEOF??? SIZEOF?????????LENGTHOF?TYPE??????
???
??? ?
SIZEOF arrayD 54
59????
.data arrayB BYTE 10,20,30,40,50,
?????????????????????
????????????
.data bVal1 10 bVal2 20 bVal3 30 bVal4 40 bVal5
50 . .
.code mov ax,arrayB mov ax,arrayB1 mov
ax,arrayB2 mov ax,arrayB3 mov
ax,arrayB4 . .
60- ?????
- ????????????????????????????,???????(indirect
addressing),???????????????(indirect operand)? - ??????????????????32??????(EAX,EBX,ECX,EDX,ESI,EDI
,EBP?ESP),????????????????
.data bVal BYTE 10h .code mov esi,OFFSET bVal mov
al,esi AL10h mov bl,20h mov esi,bl bVal20h
61- ??????,?????????????????,CPU????????????(GP,genera
l protection fault)?????????????????? - PTR???????????
- ??????????,?????????????????PTR???????????????
inc esi ERROR operand must have size
inc BYTE PTR esi
62- ??
- ??????????????,??????????????????
.data arrayD DWORD 10000h,20000h,30000h .code mov
esi,OFFSET arrayD mov eax,esi ?1?? add
esi,4 mov eax,esi ?2?? add esi,4 mov
eax,esi ?3??
63- ?????
- ???????????????????????,??32?????????????????MASM
???????????? - ??reg
- ??reg(MASM??)
.data arrayW WORD 10000h,20000h,30000h .code mov
esi,OFFSET arrayW mov ax,esi ?1?? mov
ax,esi2 ?2?? mov ax,esi4 ?3??
.data arrayW WORD 10000h,20000h,30000h .code mov
esi,0 mov ax,arrayWesi ?1?? add esi,2 mov
ax,arrayWesi ?2?? add esi,2 mov
ax,arrayWesi ?3??
64- ??
- ?????????????????(pointer variable)???(pointer),?
???????????????? - ??Intel??????????????????NEAR?FAR,???????????????
?(16?????32?????)?
16???? 32?????
NEAR?? ????????16????? ????????32?????
FAR?? 32???-???? 48???-????
arrayB BYTE 10h,20h,30h,40h arrayW WORD
1000h,2000h,3000h ptrB DWORD OFFSET arrayB ptrW
DWORD OFFSET arrayW
65- ??TYPDEF???
- ?????,TYPDEF???????????????????????????TYPDEF????
??????????,???????????????????????PBYTE
PBYTE TYPDEF PTR BYTE .data arrayB BYTE
10h,20h,30h,40h Ptr1 PBYTE ? ???? Ptr2 PBYTE
arrayB ????
??Pointers.asm
66JMP?LOOP??
- CPU????????????,??????????????,CPU????????????????
??,?????????????????????,????????????????,????????
????????????????????(transfer of
control)???(branch)???????????????????????????????
???????????? - ?????????????,????????????,??????????,???????????
???JMP? - ????????????????????LOOP?
67- JMP??
- JMP????????????????????????????????(??????????)??
,??? - JMP ????
- ?CPU??JMP?????,?????????????????,CPU??????????????
?????,????????????,JMP????????????????? - JMP????????????????,???????????????
top . . jmp top repeat the endless loop
68- LOOP??
- LOOP????????????????????????????,??ECX????????,??
??????1,???? - LOOP ????
- LOOP???????????ECX?1,???0??,??ECX???0????????????
????ECX??0??????,????????????LOOP??????? - ???????,???????????CX???ECX??????,LOOPD?????ECX??
?????,?LOOPW????CX????????
mov ax,0 mov ecx,5 L1 inc ax loop L1
?????AX5,ECX0?
69- ??????????????????ECX????0,?????,LOOP?????ECX?1???
FFFFFFFh,????????4 294 967 296?? - ?????????????????-128?127??????????????????3????,
????????????????42???? - ?????
.data count DWORD ? .code mov ecx,100 L1 mov
count,ecx mov ecx,20 L2 . . loop L2 mov ecx
count loop L1
70- ??????
- SumArray.asm
- ?????
- CopyStr.asm