¦b Windows §@·~¨t²Î¤U¥i°õ¦æªºµ{¦¡¡A¨ä¹ê¤À¦¨¨âÃþ¡G①±±¨î»Oµ{¦¡ ( console programs ) »P②µøµ¡µ{¦¡ ( windows programs )¡C°Ï¤À¤è¦¡«Ü²³æ¡A«eªÌ°õ¦æ®É¤£²£¥Íµøµ¡¡A¦b¡u©R¥O´£¥Ü¦r¤¸¡v¤¤°õ¦æ¡AÄÝ©ó©R¥O¦C¤¶± ( command-line interface¡ACLI )¡F«áªÌ«h·|²£¥Íµøµ¡¡AÄÝ©ó¹Ï§Î¨Ï¥ÎªÌ¤¶± ( Graphical User Interface¡A²ºÙ GUI )¡C·íµM¥H«áªÌ¸û¨ã¿Ë©M¤O¦Ó¥B¸û©ö¨Ï¥Î¡A¦]¦¹¤j³¡¤Àªº¦b Windows §@·~¨t²Îªºµ{¦¡³£ÄÝ©ó«áªÌ¡A©¹«áªº¤º®e³£·|¥H³oÃþµ{¦¡¬°¥D¡A¦ý³o¤@³¹¥ý½Í½Í±±¨î»Oµ{¦¡¡C
±±¨î»Oµ{¦¡ÁöµM¨S¦³º}«Gªº¹Ï§Î¬É±¡A¦ý¬O¤p¦Ó§Ö³t¬O¨äÀuÂI¡A©Ò¥H¤´µM¦³³\¦h¤HÁÙ¦b¨Ï¥ÎµÛ³o¨Çµ{¦¡¡A¹³¬O ping¡Bipconfig¡Bftp µ¥µ{¦¡³£¬O±`±`³Q¨Ï¥Îªº¡C³o¨Çµ{¦¡ªº®æ¦¡»P¤@¯ë¯à²£¥Íµøµ¡ªºµ{¦¡¬Û¦P¡A¤]¯à°õ¦æ¦h°õ¦æºü¡A¤]¥i¥H©I¥s¦U¦¡¦U¼Ëªº Windows API¡A³o¨Çµ{¦¡¤]ºÙ¬°¡u¥D±±»Oµ{¦¡¡v¡CŪªÌ¶·ª¾¡A¥¦Ì©M¥H«eªº DOS µ{¦¡ªº®æ¦¡¬O¤£¦Pªº¡C
¹q¸£³Ì«nªº¤u§@«K¬O¿é¤J¡÷¹Bºâ¡÷¿é¥X¡C¦b¹q¸£¤¤¡A³Ì«nªº¿é¥X¸Ë¸m´N¬O¿Ã¹õ¡AºÙ¬°¼Ð·Ç¿é¥X¸Ë¸m ( standard output device )¡F³Ì«nªº¿é¤J¸Ë¸m¬OÁä½L¡AºÙ¬°¼Ð·Ç¿é¤J¸Ë¸m ( standard input device )¡C
ÁöµM¹q¸£¥u¯à§â¸ê®Æ¿é¥X¦Ü¿Ã¹õ¤W¡A¦ÓµLªk¦Û¿Ã¹õ¿é¤J¸ê®Æ¡F¤]¥u¯à±µ¦¬¨Ó¦ÛÁä½Lªº¸ê®Æ¡A¦ÓµLªk§â¸ê®Æ¿é¥X¦ÜÁä½L¡C¦ý¬O¦³³\¦h§@·~¨t²Î¡A§â¿Ã¹õ»PÁä½L·í°µÀɮרӹï«Ý¡A¥]§t Windows §@·~¨t²Î»P DOS §@·~¨t²Î ( ¨£¡uDOS ²Õ¦X»y¨¥²Ä 20 ³¹¡v) ³£¦p¦¹¡C
³\¦hÀɮ׳£¯à¼g¶i¸ê®Æ©ÎŪ¨ú¸ê®Æ¡A¹q¸£§â¸ê®Æ¼g¶iÀɮ׸̪º¹Lµ{¬Û·í©ó§â¸ê®Æ¿é¥X¦ÜÀɮסAŪ¨úÀɮתº¤º®e¬Û·í©ó¦ÛÀɮ׿é¤J¸ê®Æ¡C¤]¦³¤@¨ÇÀɮ׬O¤£¯àŪ¨ú¥u¯à¼g¤J¡A©Î¤£¯à¼g¤J¥u¯àŪ¨úªº¡F©Ò¥H¥i¥H§â¿Ã¹õ¬Ý¦¨¬O¤£¯àŪ¨ú¥u¯à¼g¤JªºÀɮסA§âÁä½L¬Ý¦¨¬O¤£¯à¼g¤J¥u¯àŪ¨úªºÀɮסC
¦pªG§â¼Ð·Ç¿é¥X¸Ë¸m ( §Y¿Ã¹õ ) »P¼Ð·Ç¿é¤J¸Ë¸m ( §YÁä½L ) ³£¬Ý¦¨ÀɮסA«eªÌ´N¦n¹³¬O¤£¯àŪ¨ú¥u¯à¼g¤JªºÀɮסA«áªÌ¬O¤£¯à¼g¤J¥u¯àŪ¨úªºÀɮסA¨º»ò¹ï©ó§@·~¨t²Î³B²z³o¨âºØ¸Ë¸mªº¤è¦¡¡A´N»PÀÉ®×µL²§«D±`¤è«K¡C
DOS ©Î Windows ³B²zÀɮתº¤è¦¡¡A¬O¥H¡uÀÉ®×¥N½X¡vªº¤è¦¡³B²z¡C¤]´N¬O»¡¡An¦b¡u©R¥O´£¥Ü¦r¤¸¡v¤¤¿é¥X¸ê®Æ¦Ü¿Ã¹õ¡A©Î¦ÛÁä½L¿é¤J¸ê®Æ¡A³£n¥ý¨ú±o¼Ð·Ç¿é¥X¸Ë¸m¥N½X ( handle of standard input device ) ©Î¼Ð·Ç¿é¤J¸Ë¸m¥N½X¡C§ÚÌ¥i¥H§â³o¨âÓ¥N½X¬Ý¦¨¬OÀÉ®×¥N½X¡A¥H«á¿é¥X¤å¦r®É¡A´N¥Î³oÓ¥N½X§@¬°¿é¥X¹ï¶H¡Aµø¬°¹ïÀÉ®×¼g¤J¡A¥u¬O³o¸ÌªºÀɮ׬O¼Ð·Ç¿é¥X¸Ë¸m¡A¥ç§Y¿Ã¹õ¡C¥ÑÁä½L¿é¤J¸ê®Æ¡A¤]¬O¤@¼Ëªº·Qªk¡C³o¨Ç°Ê§@³£¦³¬ÛÀ³ªº Windows API ¥i¨Ñ©I¥s¡C
°²³]§AÁÙ¤£¤ÓÀ´ÀÉ®×¥N½X¡A¨º¤]¨SÃö«Y¡A©³¤Uµy§@¸ÑÄÀ¡C§ÚÌÀ³¸Óª¾¹D¡A¹q¸£³B²z¼ÆÈ»·¤ñ³B²z¦r¦ê®e©ö¡C¦ý¬O§Ú̦bºÏºÐ¾÷¤WªºÀɮצWºÙ¡A¬O¤@ªø¦êªº¦r¦ê¡A¥i¯à¥]§tºÏºÐ¾÷¦WºÙ¡B¸ô®|¡A¤D¦Ü¨ì¥DÀɦW¤Î°ÆÀɦW¡C
°²·Q¡A²{¦bn½s¿è¤@Ó WORD ÀɮסA¨º¤@©w¬O¥ý¶}±Ò³oÓÀɮסAµM«áŪ¨ú¨ä¤¤ªº¤º®e¡A¦b½s¿è¹Lµ{¤¤¡A®É¤£®ÉªºÁÙnÀx¦s¡A³Ì«á½s¿è§¹²¦ÁÙnÃö³¬ÀɮסC¦pªG¨C¦¸³£n³B²z¨º¤@ªø¦êªºÀɦW¡A®Ä²v´N®t¤F¡C©Ò¥H§ÚÌ¥u¦b¶}±ÒÀɮ׮ɡA¨Ï¥ÎÀɦW¡A¦pªG¦¨¥\¶}±Ò¡A§@·~¨t²Î´N·|¥H¤@Ó¿W¤@µL¤Gªº¥¿¾ã¼Æ¥N´ÀÀɦW¡C¦b¤§«áªºÀx¦s¡BŪ¨ú¡BÃö³¬µ¥¹Lµ{³£¥H³oÓ¥¿¾ã¼Æ¥h³B²z¡A³oÓ¥¿¾ã¼Æ´NºÙ¬°ÀÉ®×¥N½X ( file handle )¡C
³oºØ±¡§Î¦³ÂIÃþ¦ü°Ñ¥[¤j¾Ç¤J¾Ç¦Ò¸Õ¡A¸Õ°È©eû·|µ¹§A¤@Ó¿W¤@µL¤Gªºã¦ÒÃÒ¸¹½X¡A¦Ó«á¥h¸Õ³õÀ³¸Õ¡B¬dº]¡B©ñº]³£¥H³oÓ¸¹½X¥N´À©m¦W¡C
ÁA¸Ñ¤F¼Ð·Ç¿é¤J¿é¥X¸Ë¸m»PÀɮתºÃö«Y«á¡A¤p¤ì°¸¤¶²Ð´XºØ»P±±¨î»Oµ{¦¡¬ÛÃöªº Win64 API¡C
¨ú±o¼Ð·Ç¸Ë¸m¥N½X¡A¥i¥H©I¥s GetStdHandle¡A¨ä»yªk¬°
invoke GetStdHandle,\
nStdHandle ; input, output, or error device
GetStdHandle ¥u¦³¤@ӰѼơAnStdHandle¡A¥¦¥i¥H¬O¤U±¤TÓ±`¼Æªº¨ä¤¤¤§¤@¡A³o¤TÓ±`¼Æ¦b¥]§tÀɤ¤«Å§i¤¤«Å§i¡G
nStdHandle | ¼ÆÈ | ·N¸q |
STD_INPUT_HANDLE | -10 | ¨ú±o¼Ð·Ç¿é¤J¸Ë¸m¥N½X |
STD_OUTPUT_HANDLE | -11 | ¨ú±o¼Ð·Ç¿é¥X¸Ë¸m¥N½X |
STD_ERROR_HANDLE | -12 | ¨ú±o¼Ð·Ç¿ù»~¸Ë¸m¥N½X |
¦pªG©I¥s¦¨¥\¡A¦^¶ÇȦs©ó RAX ¸Ì¡A¥Nªí¸Ë¸m¥N½X¡F¦pªG¥¢±Ñ¡A¦^¶ÇȬ° INVALID_HANDLE_VALUE ( ¬°¤@±`¼Æ¡A¦b¥]§tÀɤ¤¤]¦³«Å§i )¡AYn¨ú±o¸Ô²Óªº¿ù»~¸ê®Æ¡A©I¥s GetLastError API¡C·íµ{¦¡µ²§ô«á¡A¤£»Ýn©I¥s CloseHandle Ãö³¬³o¨Ç¼Ð·Ç¿é¥X¡B¿é¤J¸Ë¸m¥N½X¡C¤U±ªº¨Ò¤l¬O¨ú±o¼Ð·Ç¿é¥X¸Ë¸m¥N½Xªº¤èªk¡G
invoke GetStdHandle,STD_OUTPUT_HANDLE
¦pªG¦¨¥\¡ARAX¡×¼Ð·Ç¿é¥X¸Ë¸m¥N½X¡F¦pªG¥¢±Ñ¡ARAX¡×INVALID_HANDLE_VALUE¡C
WriteConsole ·|¦b±±¨î»Oµ{¦¡ªº¿Ã¹õ´å¼Ð¦ì¸m¤W¡A¦L¥X¦r¦ê¨Ó¡C¨ä»yªk¬O¡G
invoke WriteConsole,\ hConsoleOutput,\ ; handle to a console screen buffer lpBuffer,\ ; pointer to buffer to write from nNumberOfCharsToWrite,\ ; number of characters to write lpNumberOfCharsWritten,\ ; pointer to number of characters written lpReserved ; reserved
WriteConsole ¦³¤Ó°Ñ¼Æ¡A¨ä·N¸q¦p¤U¡G
¦pªG©I¥s¦¨¥\¡A¦^¶ÇȬ°«D¹s¡F¦pªG¥¢±Ñ¡A¦^¶ÇȬ°¹s¡AYn¨ú±o¸Ô²Óªº¿ù»~¸ê®Æ¡A©I¥s GetLastError API¡C
WriteConsole °õ¦æ®É¡A·|Åý´å¼Ð¦V¥k«e¶i¡A¨ì³Ì¥k¤@Äæ´N·|´«¦æ¡C¦pªGn§ïÅÜ´å¼Ð¦ì¸m¡A¥i¥H©I¥s SetConsoleCursorPosition¡CÁöµM nNumberOfCharsToWrite »P lpNumberOfCharsWritten ©Ò«üªºÅܼƳæ¦ì³£¬O¦r¤¸¼Æ¡A¦ý¬O¦b²Õ¦X»y¨¥¤¤¡A¨CÓ ASCII ¦r¤¸³£¬O¤@Ӧ줸²Õ¤j¤p¡A¤@Ó¤¤¤å¦r¬O¨âӦ줸²Õ¡A¦Ó¬ü°ê¤H¦b¼g³o¨Ç API ®É¤£·|¦Ò¼{³oÓ°ÝÃD¡A¦]¦¹³o¸Ìªº¦r¤¸¼Æ³Ì¦n·Q¦¨¦ì¤¸²ÕӼơF¦ýY¦b¦ì§} lpBuffer ªº¦r¦ê¬°¸U°ê½X ( UNICODE ) ½s½X¡A±¡§Î¤S¦³©Ò¤£¦P¡C
ReadConsole ¯àÅý¨Ï¥ÎªÌ¦b±±¨î»Oµ{¦¡¤¤¿é¤J¦r¦ê¡A¨Ã
invoke ReadConsole,\ hConsoleInput,\ ; handle to a console input buffer lpBuffer,\ ; address of buffer to receive data nNumberOfCharsToRead,\ ; number of characters to read lpNumberOfCharsRead,\ ; address of number of characters read pInputControl ; address of CONSOLE_READCONSOLE_CONTROL
ReadConsole ¦³¤Ó°Ñ¼Æ¡A¨ä·N¸q¦p¤U¡G
nNumberOfCharsToRead »P lpNumberOfCharsRead ©Ò«üªºÅܼƳæ¦ì³£¬O¦r¤¸Ó¼Æ¡A¦ý¹ï©ó¤£¬O¸U°ê½Xµ{¦¡¡A³£À³§â¥¦Ìªº³æ¦ìµø¬°¦ì¤¸²Õ¡C
¦pªG¨Ï¥ÎªÌ¿é¤Jªº¦ì¤¸²Õ¼Æ ( ¥]§t 0dh¡B0ah ) ¤ñ nMumberOfCharsToRead ¦h¡A¨º»ò¶W¹Lªº³¡¤À¡AÁöµM·|Åã¥Ü¦b¿Ã¹õ¤W¡A¦ý¬O«o¤£·|¦s©ñ¦b½w½Ä°ÏùØ¡C¬°¤F«OÃÒ¤£¿òº|¨Ï¥ÎªÌ¿é¤Jªº¦r¤¸¡AnNumberOfCharsToRead À³¤ñ³Ì¦h¥i¯à¿é¤Jªº¦ì¤¸²ÕӼƦA¼W¥[ 2¡A¥H®e¯Ç 0dh¡B0ah ¨âÓ¦r¤¸¡C
©³¤U¤p¤ì°¸´N¥H¤@Ó½d¨Ò¡AGREETING.ASM¡A¨Ó»¡©ú±±¨î¥xµ{¦¡ªº¿é¥X»P¿é¤J¡C
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
25 26 27 28
29 30 31 32
33 34 35 36
37 38 39 40
41 42 43 44
45 46 47 48
49 50 51 52
53 54 55 56
57 58 59 60
61 62 63 64
65 66 67 68
69 70 71 72
73 74 75 76
77 |
OPTION CASEMAP:NONE
EXTRN GetStdHandle:PROC,WriteConsoleA:PROC,ReadConsoleA:PROC,ExitProcess:PROC
INCLUDELIB e:\masm32\lib64\kernel32.lib
MAX_NAME EQU 4*2+2 ;¤¤¤å©m¦W³Ì¦h¥|Ó¤¤¤å¦r¡A¨CÓ¤¤¤å¦r¦û¨âӦ줸²Õ¡A¦A¥[¤W0dH¡B0aH
STD_INPUT_HANDLE EQU -10
STD_OUTPUT_HANDLE EQU -11
INVALID_HANDLE_VALUE EQU -1
;***************************************************************************************************
.CONST
sName DB "½Ð¿é¤J±zªº©m¦W¡]³Ì¦h¥|Ó¤¤¤å¦r¡^¡G"
sHowAreYou DB "¡A±z¦n¶Ü¡H"
;***************************************************************************************************
.DATA
hOutput DQ ? ;¼Ð·Ç¿é¥X¸Ë¸m¥N½X
hInput DQ ? ;¼Ð·Ç¿é¤J¸Ë¸m¥N½X
qWritten DQ ?
qRead DQ ?
sBuffer DB MAX_NAME+SIZEOF sHowAreYou DUP (0)
;***************************************************************************************************
.CODE
;---------------------------------------------------------------------------------------------------
main PROC
sub rsp,28h ;main°Æµ{¦¡©Ò©I¥sªºWin64 API¡A°Ñ¼Æ³Ì¦hªº¦³¤Ó¡A¬GRSP´î28H
mov rcx,STD_OUTPUT_HANDLE
call GetStdHandle
cmp rax,INVALID_HANDLE_VALUE;Y©I¥s¥¢±Ñ¡ARAX¡×-1¡A¸õ¦Üexit:µ²§ôµ{¦¡
je exit
mov hOutput,rax ;§_«h§â¼Ð·Ç¿é¥X¸Ë¸m¥N½X¦s¤JhOutputÅܼƤ¤
mov rcx,STD_INPUT_HANDLE
call GetStdHandle
cmp rax,INVALID_HANDLE_VALUE;Y©I¥s¥¢±Ñ¡ARAX¡×-1¡A¸õ¦Üexit:µ²§ôµ{¦¡
je exit
mov hInput,rax ;§_«h§â¼Ð·Ç¿é¤J¸Ë¸m¥N½X¦s¤JhInputÅܼƤ¤
;¦b¼Ð·Ç¿é¥X¸Ë¸m¤W¡A¦L¥XsName¦r¦ê¡A·í°µ´£¥ÜÅý¨Ï¥ÎªÌ©ú¥Õ¸Ó¿é¤J¤°»ò
mov rcx,hOutput
mov rdx,OFFSET sName
mov r8,SIZEOF sName
mov r9,OFFSET qWritten ;¹ê»Ú¦L¥Xªº¦r¦ê¦ì¤¸²ÕӼƦs©óqWrittenÅܼƤ¤
mov QWORD PTR [rsp+20h],0
call WriteConsoleA
;¦b¼Ð·Ç¿é¤J¸Ë¸m¤WŪ¨ú¦r¦ê¡C
mov rcx,hInput
mov rdx,OFFSET sBuffer ;Ū¨úªº¦r¦ê¦s©ósBuffet¦r¦ê¸Ì
mov r8,MAX_NAME ;³Ì¦hŪ¨ú (MAX_NAME-2) Ӧ줸²Õ
mov r9,OFFSET qRead ;¹ê»ÚŪ¨ú¦r¦êªº¦ì¤¸²ÕӼƦs©óqReadÅܼƤ¤
mov QWORD PTR [rsp+20h],0
call ReadConsoleA
;§âsHowAreYou¦r¦ê·h²¾¨ì¨Ï¥ÎªÌ¿é¤Jªº©m¦W¤§«áªº¦ì§}
sub qRead,2 ;¹ê»ÚŪ¨ú¦r¦êªº¦ì¤¸²ÕӼơA¤£¥]§t0dH¡B0aH
mov rdi,OFFSET sBuffer
add rdi,qRead ;RDI¡×¨Ï¥ÎªÌ¿é¤Jªº©m¦W¤§«áªº¦ì§}
mov rcx,SIZEOF sHowAreYou
mov rsi,OFFSET sHowAreYou
mov r8,rcx
cld
rep movsb
mov rcx,hOutput
mov rdx,OFFSET sBuffer
add r8,qRead ;R8¡×sHowAreYou¦r¦êªø«×¥[¤W¤£¥]§t0dH¡B0aHªº©m¦Wªø«×
mov r9,OFFSET qWritten
mov QWORD PTR [rsp+20h],0
call WriteConsoleA
exit: xor rcx,rcx
call ExitProcess
main ENDP
;***************************************************************************************************
END |
±N¤W±ªºì©lµ{¦¡¦s¤J¡uE:\HomePage\SOURCE\Win64\CONSOLE\GREETING.ASM¡v¡AµM«á¥Ñ®à±¥ª¤U¤èªº¡u¶}©l¡v¶}±Ò¡u©R¥O´£¥Ü¦r¤¸¡v¡A¨Ì·Ó¤U¹Ï¤è¦¡²ÕĶ»P³sµ²¡C¹Ï¤¤¡A¥²¶·¿é¤Jªº«ü¥O¥H²HÂŦâ¼Ð¥Ü¡An°O±o¨C¿é¤J¦n¤@Ó«ü¥O¡A¥²¶·«ö¡uEnter¡vÁä¤~·|°õ¦æ¡C ª`·N¨ì¹Ï¤¤³sµ²®É¡A¹ï LINK.EXE ¤U¹F°Ñ¼Æ¬O¡uset link=/subsystem:consol /entry:main¡v¡C»¡©ú¦p¤U¡G
©³¤U¤p¤ì°¸¥ýÁ¿Á¿ GREETING.ASM ¥Îªºªº·s«ü¥O¡C
DUP ¹Bºâ¤l¬O¥Î¨Ó©w¸q¤@Ө㦳«½Æ¥X²{ªº¼ÆÈ¡A¥²¶··f°t DB¡BDW¡BDD µ¥°²«ü¥O¨Ï¥Î¡A¨ä»yªk¬O
¦¸¼Æ DUP ( ªì©lÈ[,ªì©lÈ[,...]] )
¤W¦¡¤¤ªº¦¸¼Æ´N¬O«½Æ¦¸¼Æ¡An«½Æªº³¡¤À¦b«á±¤@¹ï ( ) ¤ºªºªì©lÈ¡A³oªì©lȤ]¥i¥H¬O¦r¦ê¡C¦pªG¤£½T©wªì©lÈ¡A¤]¥i¥H¥Î¡u?¡v¥N´À¡C¥t¥~¡A¦b ( ) ¤º¤]¥i¥H¦A«×¨Ï¥Î DUP ¹Bºâ¤l¡C©³¤U¬O´XÓ¨Ò¤l¡G
a BYTE 4 DUP (1) ;¬Û·í©ó¡ua DB 1,1,1,1¡v b DB 5 DUP (10 DUP (2)) ;¬Û·í©ó©w¸q¤F 50 Ӧ줸²Õªº 2 c DB 3 DUP (1,2,3) ;¬Û·í©ó©w¸q¤T¦¸¡u1,2,3¡v¡A¤]´N¬O¡uc DB 1,2,3,1,2,3,1,2,3¡v d DB 2 DUP ("so ") ;¬Û·í©ó¡ud DB "so ","so "¡v¡A¤]´N¬O¡ud DB "so so "¡v
°£¤F SIZEOF ¹Bºâ¤l¤§¥~ÁÙ¦³ TYPE¡BLENGTHOF ¨âÓ¡A³o¤TÓ¹Bºâ¤lªº»yªkÃþ¦ü¡A¥Î³~¤]¬Û¦ü¡A¦]¦¹¤@°_»¡©ú¡C¥¦Ìªº»yªk¤À§O¬O
LENGTHOF ÅÜ¼Æ SIZEOF ÅܼƩθê®ÆÃþ«¬ TYPE ÅܼƩθê®ÆÃþ«¬
LENGTHOF ¬O¥Î¨ÓÅý²ÕĶ¾¹pºâ©Ò©w¸qªºÅܼƦ¨û¦³¦h¤ÖÓ¡ASIZEOF ¥Î¨ÓÅý²ÕĶ¾¹pºâ«á±ªºÅܼƩθê®ÆÃþ«¬¦@§t¦³¦h¤ÖӦ줸²Õ¡ATYPE «h¬O¶Ç¦^¨CÓ¦¨û¦³¦h¤ÖӦ줸²Õ¡A¤]¥i¥H¶Ç¦^¦¹¸ê®ÆÃþ«¬¦³´XӦ줸²Õ¡C¨Ò¦p©³¤Uªº¨Ò¤l¡G
;TYPE LENGTHOF SIZEOF v1 DB 3 DUP (1,2,3) ; 1 9 9 v2 DW 40 DUP (40 DUP (5)) ; 2 1600 3200 v3 DD 3 DUP (20,30) ; 4 6 24 str1 DB "After all, Tomorrow is", ; 1 35 35 " another day." str2 DB "After all, Tomorrow is" ; 1 22 22 DB " another day."
¤W±¨Ò¤lùØ¡Av1 ¨ä¹ê¬O¡uv1 DB 1,2,3,1,2,3,1,2,3¡v¡A©Ò¥H¨CÓ¦¨û¤j¤p¬°¤@Ӧ줸²Õ¡ATYPE v1 ¬° 1¡A¦¹¥~ v1 ¦³¤EÓ¦¨û¡A¬G LENGTHOF v1 ¬° 9¡Fv1 Á`¦@¦û¥Î 9 Ӧ줸²Õ ( 9¡Ñ1 )¡A¬G SIZEOF v1 ¬° 9¡C
v2 ¬O¥H DW ©w¸q¡A©Ò¥H¨CÓ¦¨û¤j¤p¬°¨âӦ줸²Õ¡A¬G TYPE v2 ¬° 2¡F¦¹¥~¦b DUP ªº¬A¸¹ùئ³ 40 Ó¦¨û¡A¦ýn«½Æ 40 ¦¸¡A¦]¦¹¦³ 40¡Ñ40 Ó¦¨û¡A©Ò¥H LENGTHOF v2 ¬° 1600¡Fv2 Á`¦@¦û¥Î 3200 Ӧ줸²Õ ( 1600¡Ñ2 )¡A¬G SIZEOF v2 ¬° 3200¡C
¦]¬° v3 ¬O¥H DD ©w¸q¡A©Ò¥H¨CÓ¦¨û¤j¤p¬°¥|Ӧ줸²Õ¡A¬G TYPE v3 ¬° 4¡Fv3 ¦b DUP ªº¬A¸¹ùئ³ 20¡B30 ¨âÓ¦¨û¡A¦ýn«½Æ 3 ¦¸¡A¦]¦¹Á`¦@¦³¤»Ó¦¨û¡A©Ò¥H LENGTHOF v3 ¬° 6¡Fv3 Á`¦@¦û¥Î 24 Ӧ줸²Õ ( 6¡Ñ4 )¡A¬G SIZEOF v3 ¬° 24¡C
³Ì«á¨âÓ¨Ò¤l¬O¦³´«¦æªº¦r¦ê¡Aª`·N¨ì str1 ¦b¤Þ¸¹«á±¦³³r¸¹¡Aªí¥Ü str1 ¦r¦ê·|©µ¦ù¨ì¤U¤@¦æ¡A³o®ÉÔ¤U¤@¦æ¤£»Ýn¥H¡uDB¡v©w¸q¡A¤]´N¬O»¡ str1 ¬O¡uAfter all,Tomorrow is another day.¡v¡A¦]¦¹ SIZEOF pºâ¥X¨Óªºµ²ªG¬° 35 Ӧ줸²Õ¡C
³Ì«á¤@Ó¨Ò¤l¡Astr2¡A¤Þ¸¹«á±¨S¦³³r¸¹¡Aªí¥Ü¦¹¦r¦ê¥u¦³¤@¦æ¦Ó¤w¡F¦b str2 ©³¤Uªº¦r¦ê¡A¦b²ÕĶ¾¹ªºµø¨¤¤¤»P str2 ¬O²@¤£¬Û¤zªº¡C¤]´N¬O»¡ str2 ¦r¦ê¬O¡uAfter all, Tomorrow is¡v¡A¦]¦¹ SIZEOF pºâ¥X¨Óªºµ²ªG¥u¦³ 22 Ӧ줸²Õ¡C¥Ñ¦¹¬Ý¨Ó¡A¦³¨S¦³¡u,¡v©M¡uDB¡v·N¸q§¹¥þ¤£¦P¡A·í©w¸qªº¦r¦ê¤Óªøn´«¦æ®É¶·¤p¤ßª`·N¡C
CMP ¬O¤ñ¸û«ü¥O¡A¤ñ¸û¨âÓ¹Bºâ¤¸ªº«ü¥O¡A¨ä»yªk¬O¡G
CMP ¥Øªº¹Bºâ¤¸,¨Ó·½¹Bºâ¤¸
¨Æ¹ê¤W¡ACPU ¦b°õ¦æ CMP «ü¥O®É¡A¨ä¹ê¬O°õ¦æ SUB «ü¥O¡A®t§O¥u¬O¦b©ó¡ACMP ¨Ã¨S¦³§âµ²ªG¦s¤J¥Øªº¹Bºâ¤¸¡A¦Ó SUB «h§âµ²ªG¦s¤J¥Øªº¹Bºâ¤¸¡C¨âªÌ³£·|§ïÅܺX¼Ð¼È¦s¾¹¤§È¡A¦Ó CMP «ü¥O«á±³q±`·|±µ±ø¥ó¸õ¶V«ü¥O¡A¥H§¹¦¨¬yµ{±±¨î¡C
JE «ü¥O»P JZ «ü¥O¬O¬Û¦Pªº«ü¥O¡A¨ä·N¤À§O¬°¡ujump if equal¡v©Î¡ujump if zero¡v¡A³oÓ«ü¥Oªº»yªk¬O¡G
JE ¼Ð°O
¼Ð°Oªº^¤å¬O label¡A¬O¤@¦ê^¤å¦r²Õ¦¨¡A¤]¥i¥H¥]§t¡u_¡v¡Bªü©Ô§B¼Æ¦r¡A¥²¶·¥H¡u:¡vµ²§À¡C¥¦¥NªíµÛ¦ì§}¡A´£¨Ñ¸õÅD«ü¥O±N±±¨îÅvÂಾªº¥Øªº¦a¡C¸õÅD«ü¥Oªº¥Øªº¦a¡A³q±`¥u¯à¦b¦P¤@°Æµ{¦¡¤º¡F¦ý¼Ð°Oªºµ²§À¤]¥i¥H¬O¡u::¡v¡A¥ç§Y¨âÓ³sÄòªº«_¸¹¡A³oºØ¼Ð°O¥i¥H´£¨Ñ¨ä¥L°Æµ{¦¡ªº¸õÅD¥Øªº¦a¡C
JE ·|Àˬd¹sȺX¼Ð¡A¦pªG¹sȺX¼Ð³]©w ( ¤]´N¬O¹sȺX¼Ð¬°¤@ )¡A´N·|µo¥Í¸õÅD¡A±±¨îÅv·|¸õÅD¨ì¨ä«á©Ò±µªº¼Ð°O¤§³B°õ¦æ¡C¨º¤°»ò®ÉÔ¹sȺX¼Ð·|³]©w©O¡H·í«e±ªº«ü¥O¹Bºâµ²ªG¬°¹s®É¡ACPU ·|³]©w¹sȺX¼Ð ( °Ñ¦Ò²Ä¤@³¹¶i¦ìºX¼Ð»P¹sȺX¼Ð )¡C
¥H GREETING.ASM ªº²Ä 30¡ã31 ¦æ¤Î²Ä 72 ¦æ¡G
cmp rax,INVALID_HANDLE_VALUE;Y©I¥s¥¢±Ñ¡ARAX¡×-1¡A¸õ¦Üexit:µ²§ôµ{¦¡
je exit
⁝
exit: .
INVALID_HANDLE_VALUE ¬°¤@±`¼Æ¡A©ó GREETING.ASM ªº²Ä 9 ¦æ«Å§i¡A¨äȬ°t¤@¡C¤£½× INVALID_HANDLE_VALUE ¤§È¬°¦ó¡A·í RAX »P¨ä¬Ûµ¥®É¡A¶i¦æ CMP ¹Bºâ¡A¬Û´î®Éµ²ªG´N·|µ¥©ó¹s¡ACPU ³]©w¹sȺX¼Ð¡C¤Ï¹L¨Ó»¡¡A¥un¹sȺX¼Ð¬°¤@¡Aªí¥Ü¨â¹Bºâ¤¸¬Ûµ¥¡A´Nµo¥Í¸õÅD¡C©Ò¥H
cmp ¥Øªº¹Bºâ¤¸,¨Ó·½¹Bºâ¤¸ jz ¼Ð°O µ{¦¡¤ù¬q¤@ ⁝ ¼Ð°O: µ{¦¡¤ù¬q¤G
¥i¥H¬Ý¦¨¬O·í¥Øªº¹Bºâ¤¸µ¥©ó¨Ó·½¹Bºâ¤¸®É¡A´N·|¸õ¦Ü¼Ð°O³B°õ¦æµ{¦¡¤ù¬q¤G¡FY¤£µ¥©ó¡A«h°õ¦æµ{¦¡¤ù¬q¤@¡C¬J¤w¤¶²Ð¹L¡uJZ/JE¡v¡A¶¶«K´£¤@´£¨ä¥L´XÓ±`¨£ªº±ø¥ó¸õ¶V«ü¥O¡C
³o¤Ó«ü¥Oªº»yªk³£¬Û¦P¡A¦ý·N¸q¤£¦P¡G
JNE ¼Ð°O ;Y¤£µ¥©ó«h¸õÅD JA ¼Ð°O ;Y°ª©ó«h¸õÅD JB ¼Ð°O ;Y§C©ó«h¸õÅD JAE ¼Ð°O ;Y°ª©ó©Îµ¥©ó«h¸õÅD JBE ¼Ð°O ;Y§C©ó©Îµ¥©ó«h¸õÅD
JNE ªº·N«ä¬O¡ujump if not equal¡v¡A¦pªG¤£µ¥©ó´N¸õÅD¡FJA ªº·N«ä¬O¡ujump if above¡v¡A¦pªG°ª©ó´N¸õÅD¡FJB ªº·N«ä¬O¡ujump if below¡v¡A¦pªG§C©ó´N¸õÅD¡FJAE ªº·N«ä¬O¡ujump if above or equal ¡v¡A¦pªG°ª©ó©Îµ¥©ó´N¸õÅD¡CJBE ªº·N«ä¬O¡ujump if below or equal ¡v¡A¦pªG§C©ó©Îµ¥©ó´N¸õÅD¡C³o¸Ì¤§©Ò¥H¥Î°ª©ó©M§C©ó¡A¬O¦]¬°«á¥|Ó«ü¥O¥u¯à¥Î¨Ó¤ñ¸ûµL¸¹¼Æ¡A¬°¤F©M¦³¸¹¼Æªº±ø¥ó¸õÅD°Ï§O¡A©Ò¥H¥Î°ª©ó©M§C©ó¡A¦³¸¹¼Æªº±ø¥ó¸õÅD«h¥Î¤j©ó©Î¤p©ó¡C
³o¤Ó«ü¥O»P JE ¤@¼Ë¡A¤]¬O¨Ì¿àÀˬd¶i¦ìºX¼Ð©Î¹sȺX¼Ð¡A¨Ó§PÂ_¬O§_µo¥Í¸õÅD¡A¤]¦³¦P¸q«ü¥O¡C¦³ÂI¨à½ÆÂø¡A¤p¤ì°¸¶È»¡©ú JA¡A¨ä¾l¦C¥X¤Uªí¡A½Ð¦Û¦æ°Ñ¦Ò¡C
JA ªº^¤å·N«ä¬°¡ujump if above¡v¡A·í¥Øªº¹Bºâ¤¸°ª©ó¨Ó·½¹Bºâ¤¸¡A¨âªÌ¬Û´î¤£µo¥Íɦì¥B©Ò±o¤§®t¤£¬°¹s¡A¬G¶i¦ìºX¼Ð¬°¹s¥B¹sȺX¼Ð¥ç¬°¹s¡C¤Ï¹L¨Ó±À½×¡A¦pªG¶i¦ìºX¼Ð»P¹sȺX¼Ð¬Ò¬°¹s¡Aªí¥Ü¥Øªº¹Bºâ¤¸¸û°ª¡Aµo¥Í¸õÅD¡C¥Ñ¥t¤@¨¤«×¨Ó¬Ý¡AY¬Y¼Æ°ª©ó¥t¤@¼Æ¡A¥i¥H»¡¦¨¬Y¼Æ¤£§C©ó©Îµ¥©ó¥t¤@¼Æ¡A³o¤]´N¬O¡ujump if not below or equal¡vªº¨Ó¥Ñ¡AÁY¼g¬°¡uJNBE¡v¡A»P¡uJA¡v¦P¸q¡C
«ü¥O | ¦P¸q«ü¥O | ÀˬdºX¼Ð | ¤¤¤å·N¸q | ^¤å·N¸q |
JE | JZ | ZF¡×1 | Yµ¥©ó´N¸õÅD | jump if equal |
JNE | JNZ | ZF¡×0 | Y¤£µ¥©ó´N¸õÅD | jump if not equal |
JA | JNBE | CF¡×0 ¥B ZF¡×0 | Y°ª©ó´N¸õÅD | jump if above |
JB | JNAE¡BJC | CF¡×1 | Y§C©ó´N¸õÅD | jump if below |
JAE | JNB¡BJNC | CF¡×0 | Y°ª©ó©Îµ¥©ó´N¸õÅD | jump if above or equal |
JBE | JNA | CF¡×1 ©Î ZF¡×1 | Y§C©ó©Îµ¥©ó´N¸õÅD | jump if below or equal |
CLD »P STD ¨S¦³¹Bºâ¤¸¡A¥¦Ìªº»yªk¬O
CLD STD
CLD «ü¥O¥Î¨Ó²M°£¤è¦VºX¼Ð¡C¤è¦VºX¼Ð^¤å¦WºÙ¬O direction flag¡A²ºÙ DF¡A¬O¦b CPU ¤ººX¼Ð¼È¦s¾¹ªº²Ä 10 Ӧ줸¡C§ÚÌ¥i¥H§Q¥Î CLD «ü¥O¨Ó²M°£¤è¦VºX¼Ð¡A¤]´N¬O±N¨äȳ]¬°¹s¡A¦b°£¿ù¾¹¤¤¥H UP ªí¥Ü¡C§Q¥Î STD «ü¥O³]©w¤è¦VºX¼Ð¡A¤]´N¬O±N¨ä³]¬°¤@¡A¦b°£¿ù¾¹¤¤¥H DN ªí¥Ü¡C¦³Ãö¤è¦VºX¼Ð¡A¥i°Ñ¦Ò DOS ²Õ¦X»y¨¥ªþ¿ý¤T¡C¾ã²z¦p¤Uªí¡G
¤è¦VºX¼Ð¤§È | °£¿ù¾¹ªí¥Ü | »¡©ú | ¤è¦V | RSI¡BRDI |
0 | UP | ²M°£¤è¦VºX¼Ð | ©¹°O¾ÐÅé°ª¦ì§} | »¼¼W |
1 | DN | ³]©w¤è¦VºX¼Ð | ©¹°O¾ÐÅé§C¦ì§} | »¼´î |
MOVSB ªº¥Î³~¬O§â¦b¬Y¤@¦ì§}¤¤¤@Ӧ줸²Õªøªº¸ê®Æ·h²¾¨ì¥t¤@Ó¦ì§}¥h¡A¨ä»yªk¬°¡G
MOVSB
¥E¬Ý¤§¤U¡A¦ü¥G¨S¦³¹Bºâ¤¸¡A¦ý¹ê»Ú¤W«o¦³¨âÓÁôÂ꺹Bºâ¤¸¡C¥Øªº¹Bºâ¤¸¬O RDI ©Ò«üªº¦ì§}¡A¨Ó·½¹Bºâ¤¸¬O RSI ©Ò«üªº¦ì§}¡CRDI »P RSI ¬O CPU ¤ºªº¨âӼȦs¾¹¡A¨ä^¤å¦WºÙ¤À§O¬O source index register ( ¨Ó·½¯Á¤Þ¼È¦s¾¹ ) ©M destination index register ( ¥Øªº¯Á¤Þ¼È¦s¾¹ )¡C¦b°õ¦æ MOVSB ¤§«e¡A¥²¶·¥ý§â³o¨âӼȦs¾¹©Ò«üªº¦ì§}³]¸m¥¿½T¡A´N¯à±N¤@Ӧ줸²Õªº¸ê®Æ²¾¨ì EDI ©Ò«üªº¦ì§}¤º¡C
³o¼Ë¤j¶O©P³¹¤~·h²¾¤@Ӧ줸²Õªº¸ê®Æ¡A¤Ó¤£¦Eºâ¤F¡A¦³Å²©ó¦¹¡A¦b³]p MOVSB ®É²K¥[¤F¨âÓ¯S©Ê¡A©Ò¥H¦³§ó¶i¶¥ªº¥Îªk¡A¯à·h²¾¤j¶qªº¸ê®Æ¡C²Ä¤@¡AMOVSB °õ¦æ¤§«á¡ARDI »P RSI ÁÙ·|¨Ì¾Ú¤è¦VºX¼Ðªºª¬ºA¼W¥[¤@©Î´î¤Ö¤@¡C¦pªG¤è¦VºX¼Ð¬°¹s ( §Y¤è¦VºX¼Ð³Q²M°£¡A°£¿ù¾¹Åã¥Ü UP )¡ARDI¡BRSI ¦b°õ¦æ§¹ MOVSB ¤§«á³£·|¼W¥[¤@¡F¦pªG¤è¦VºX¼Ð¬°¤@ ( §Y¤è¦VºX¼Ð³Q³]©w¡A°£¿ù¾¹Åã¥Ü DN )¡ARDI¡BRSI ¦b°õ¦æ§¹ MOVSB ¤§«á³£·|´î¤Ö¤@¡C²Ä¤G¡AMOVSB ¥i¥H·f°t¥t¤@Ó x86 «ü¥O¡AREP ¨Ï¥Î¡C
REP «ü¥O¬O¤@ºØ«Ü¯S§Oªº«ü¥O¡A¥¦¥²¶··f°t INS¡BOUTS¡BMOVS¡BLODS¡BSTOS ©M SCAS «ü¥O¤@°_¨Ï¥Î¡CREP ¥²¶·©ñ¦b³o¨Ç«ü¥Oªº«e±¡A¯à«½Æ°õ¦æ³o¨Ç«ü¥O¡A«½Æ¦¸¼Æ«h©ñ¦b RCX ¼È¦s¾¹¸Ì¡C¦b°õ¦æ³o¨Ç«½Æªº«ü¥O®É¡A¨C°õ¦æ¤@¦¸¡ARCX ´N´î¤Ö¤@¡A¤@ª½¨ì RCX Åܬ°¹s¤~°±¤î ( ³o¤]´N¬O¬°¤°»ò RCX ºÙ¬° count register¡A·N«ä¬Op¼Æ¼È¦s¾¹ªº½t¬G¤F )¡C¨Ò¦p©³¤Uªº¨Ò¤l¡G
str1 DB "Beauty is found within." str2 DB SIZEOF str1 DUP (0) ⁝ cld mov rcx,SIZEOF str1 mov rdi,OFFSET str2 mov rsi,OFFSET str1 rep movsb
¤W±µ{¦¡¤ù¬q¡A·|§â str1 ¦r¦ê½Æ»s¨ì str2 ¦r¦ê¤º¡C¨ä¤¤²Ä¤G¦æ¤Î²Ä¤»¦æªº¡uSIZEOF str1¡v³£¬O¨ú±o str1 ¦r¦ê¦³¦h¤ÖӦ줸²Õªø¡A²Ä¤G¦æ©w¸q str2 »P str1 ¤@¼Ëªø¡A²Ä¤»¦æ«h¬O§â str1 ªºªø«×¦s¤J RCX ¸Ì¡C±µ¤U¨Ó«ü©w RDI¡BRSI ªº¦ì§}¤À§O¬O str2¡Bstr1 ªº¦ì§}¡AµM«á°õ¦æ¡urep movsb¡v´N§¹¦¨¤F¡C
©³¤UÂǵۤU¹Ï¨Ó¬Ý¬Ý¡uREP MOVSB¡v°õ¦æªº¸Ô²Ó±¡§Î¡C¤U±¹Ï¤@¤¤¡A©|¥¼°õ¦æ¡uREP MOVSB¡v¡A°²³] str1 ¦r¦ê¦b¦ì§} 13F043000 ³B¡Astr2 ¦b¦ì§} 13F043017 ³B¡ARSI¡BRDI ¤À§O«ü¦V¸Ó³B¡F¦]¬° str1 ªø 17H Ӧ줸²Õ¡A©Ò¥H RCX ¬° 17H¡Aªí¥Ün«½Æ MOVSB «ü¥O 17H ¦¸¡F¤è¦VºX¼Ð¬°¹s¡A³o¬O¦]¬°¦r¦êÀx¦s¦b°O¾ÐÅé®É¡A¶V«á±ªº¦r¤¸¦b¶V°ª¦ì§}¡C¤U¹Ï¤G¬O CPU °õ¦æ¤@¦¸ MOVSB «áªºµ²ªG¡ACPU ¥ý±N RSI ©Ò«üªº¦ì§}³B·h²¾¤@Ӧ줸²Õªº¸ê®Æ¨ì RDI ©Ò«ü¦ì§}¡A§A·|¬Ý¨ì¦ì§} 13F043017 ³B¤wÅܦ¨¡uB¡v¦r¤¸¡F¦]¬°¤è¦VºX¼Ð¬°¹s¡A¬G RSI¡BRDI ¼W¥[¤@«ü¦V¤U¤@Ón·h²¾ªº¦ì§}¡F¦P®É RCX ´î¤@¡C ¤W¹Ï¤T¬O¦A°õ¦æ¤@¦¸ MOVSB «áªºµ²ªG¡A§A·|¬Ý¨ì¦r¤¸¡ue¡v±q RSI ©Ò«ü¦ì§}·h²¾¨ì RDI ©Ò«ü¦ì§}¡F¦P®É RSI¡BRDI ¼W¥[¤@«ü¦V¤U¤@Ón·h²¾ªº¦ì§}¡F¦Ó RCX ´î¤@¡C¦p¦¹¤@ª½¶i¦æ¡Aª½¨ì RCX Åܬ°¹s¬°¤î¡C
ÁöµM¤p¤ì°¸«e±»¡«½Æ¦¸¼Æ¬O©ñ¦b RCX ¸Ì±¡A¦ý¨ä¹ê¤£¥¿½T¡C¹ï©ó 64 ©Î 32 ¦ì¤¸¼Ò¦¡¤U¡A¹w³]ªº«½Æ¦¸¼Æ¡A¬O©ñ¦b ECX ¸Ì±¡A©Ò¥H³Ì¦h¥u¯à«½Æ 4G ¦¸¡C¦¹¥~¡A¨C«½Æ¤@¦¸ ECX ´î¤@¡Aª½¦Ü¹s¬°¤î¡F¦ý¬O³o¤£·|¼vÅT¥ô¦óºX¼Ð¡A¤]¤£·|¼vÅT¹sȺX¼Ð¡C¥t¥~¡A¤£¾å±o·|¤£·|¦³¤H»~·|¡A§â RSI ©Ò«üªº¦r¦ê·h²¾¨ì§O³B¡A¨º»ò¦b°õ¦æ§¹¡uREP MOVSB¡v«á¡Aì¨Ó¦r¦ê©Ò¦bªº°O¾ÐÅ餺®e¬O§_·|¤£¨£¡Hµª®×¬O§_©wªº¡Aì¨Ó RSI ©Ò«üªº¦r¦ê¤´§¹¦nµL¯Ê¡C©Ò¥H¡A»P¨ä»¡ MOVSB ¬O·h²¾¡AÁÙ¤£¦p»¡¬O½Æ»s¡C
³o¤TÓ«ü¥O¬Ý°_¨Ó»P MOVSB «Ü¬Û¹³¡A¥¦Ìªº»yªk¬O
MOVSW MOVSD MOVSQ
¤£¶Èªø¬Û¬Û¹³¡A¥\¥Î¤]«Ü¹³¡C³o¤TÓ«ü¥O»P MOVSB ªº®t§O¬O¡A¥¦Ì¤À§O¤@¦¸·h²¾¤@Ó¦r²Õ¡BÂù¦r²Õ¡B¥|¦r²Õ¡A¦Ó RSI¡BRDI ¤]ÀHµÛ¤è¦VºX¼Ð¦Ó¼W´î¤G¡B¥|¡B¤K¡C¥¦Ì¤]¥i¥H·f°t REP «ü¥O¨Ï¥Î¡C
GREETING.ASM «Ü²³æ¡A´N¬OÓ¥´©Û©Iªºµ{¦¡¡CGREETING ¦b¡u©R¥O´£¥Ü¦r¤¸¡v¤¤¦L¥X¡u½Ð¿é¤J±zªº©m¦W¡]³Ì¦h¥|Ó¤¤¤å¦r¡^¡G¡v¡A«Ý¨Ï¥ÎªÌ¿é¤J©m¦W«á¡AGREETING ¦A¦L¥X¡u¨Ï¥ÎªÌ©m¦W¡A±z¦n¶Ü¡H¡v¡C¤ÀªR±oª¾¡AGREETING n³B²z¤TÓ¦r¦ê¡G①¡u½Ð¿é¤J±zªº©m¦W¡]³Ì¦h¥|Ó¤¤¤å¦r¡^¡G¡v¡B②¨Ï¥ÎªÌ¿é¤Jªº©m¦W¡B③¡u¡A±z¦n¶Ü¡H¡v¡C
²Ä①Ó¦r¦ê¨S¦³¯S§O¤§³B¡A¥u¬O¥Ñ WriteConsoleA ¦L¦b©R¥O´£¥Ü¦r¤¸¤º¡A·í°µµ¹¨Ï¥ÎªÌªº´£¥Ü¡C²Ä②Ó¦r¦ê쥻¬OªÅªº¡Aµ¥¨Ï¥ÎªÌ¿é¤J©m¦W«á¡A¤~·|¦³¤º®e¡F±µµÛªº´N¬O§â¨âÓ¦r¦ê¡G¨Ï¥ÎªÌ©m¦W¤Î²Ä③Ó¦r¦ê¡u¡A±z¦n¶Ü¡H¡v¦L¦b¿Ã¹õ¤W¡C§ÚÌ¥i¥H©I¥s¨â¦¸ WriteConsoleA¡A¥ý«á§â¨âÓ¦r¦ê¦L¥X¨Ó¡C¦ý¦b GREETING.ASM ¸Ì¡A¤p¤ì°¸¥ý±N¨âÓ¦r¦ê³s±µ°_¨ÓÅܦ¨¤@Ó¦r¦ê¡AµM«á¥u©I¥s¤@¦¸ WriteConsoleA ´N§â³s±µ¦b¤@°_ªº¦r¦ê¦L¥X¨Ó¡C
³s±µ«áªº¦r¦ê¬O¨Ï¥ÎªÌ©m¦W¦b«e¡B¡u¡A±z¦n¶Ü¡H¡v¦b«á¡A«eªÌ¦³´XӦ줸²Õ¥¼ª¾¡AºÝµø¨Ï¥ÎªÌ¿é¤J¦Ó©w¡F¦Ó«áªÌ«o¤w½T©w¡A¥i¥H¥Î¡uSIZEOF¡v°²«ü¥O¨D¥X¡C¨Ï¥ÎªÌ©m¦WÁö¤£½T©w¡A¦ý¤@¯ë¤Hªº¦W¦r¤j¦h¬O¤TÓ¤¤¤å¦r¡A¤]¦³¥|Óªº¡A©Ò¥H¤p¤ì°¸±Ä¥Î©m¦W³Ì¦h¥|Ó¤¤¤å¦r¡F¦A¦Ò¼{¨ì©I¥s ReadConsoleA ®É¡A¥¦·|¥D°Ê¥[¤W 0DH¡B0AH ¨âÓ¦r¤¸¡A©Ò¥H¨Ï¥ÎªÌ©m¦W¦r¦êªø«×¬°¡u4¡Ñ2+2¡v¡C©ó GREETING.ASM ²Ä¥|¦æ«Å§i¡uMAX_NAME EQU 4*2+2¡v¡Aªí¥Ü MAX_NAME ¬°¥|¼¥H¤G¥[¤G¡C¦b¹q¸£¤¤¡A¼¸¹¦h¥H¡u*¡vªí¥Ü¡C
¦b GREETING.ASM ªº²Ä 21 ¦æ©w¸q sBuffer ¦r¦ê¡A¦¹¦r¦ê´N¬O®e¯Ç¨Ï¥ÎªÌ©m¦W»P¡u¡A±z¦n¶Ü¡H¡v¨â¦r¦ê¡G
sBuffer DB MAX_NAME+SIZEOF sHowAreYou DUP (0)
¦b©I¥s ReadConsoleA ®É¡A§â²Ä¤GӰѼƳ]¬° sBuffer ªº¦ì§} ( ¨£ GREETING.ASM ²Ä 50 ¦æ )¡A³o¼Ëªº¸Ü¡AReadConsoleA ´N·|§â¨Ï¥ÎªÌ©m¦W¦s©ó¦¹¦r¦ê¸Ì¡C«Ý¨Ï¥ÎªÌ¿é¤J§¹²¦¡A¦A§Q¥Î¡uREP MOVSB¡v§â¡u¡A±z¦n¶Ü¡H¡v¦r¦ê·h²¾¨ì¨Ï¥ÎªÌ©m¦W«á±¡A´N§¹¦¨¤F¦r¦êªº³s±µ¤F¡C
¦ýnª`·Nªº¬O¡AReadConsoleA ·|¦b¨Ï¥ÎªÌ©m¦W¤§«á²K¥[¨âÓ¦r¤¸¡G0DH¡B0AH¡C³o¨âÓ¦r¤¸¬O ASCII ¦r¤¸ªºÂk¦ì»P´«¦æ¦r¤¸¡A¦pªG¤£§R±¼³o¨âÓ¦r¤¸¡A¦L¥X¨Óªº¡u±i¤T¡A§A¦n¶Ü¡H¡v·|µo¥Í´«¦æªº²{¶H¡AÅܦ¨
±i¤T ¡A±z¦n¶Ü¡H
¨ºn¦p¦ó§R±¼³o¨âÓ¦r¤¸©O¡H¨ä¹ê¤£Ãø¡A¤p¤ì°¸±Ä¥Îªº¤èªk¬O§â¦r¦ê¡u¡A±z¦n¶Ü¡H¡v±µ¦b¨Ï¥ÎªÌ©m¦W®É¡A¨Ã«Dª½±µ±µ¦b«á±¡A¦Ó¬O±µ¦b¡u¨Ï¥ÎªÌ©m¦Wªº§ÀºÝ«e¨âÓ¦r¤¸¡v¡CÁÙ°O±o¶Ü¡H³Ì«á¨âÓ¦r¤¸¬O ReadConsoleA ²K¥[ªº¡A¥Î¡u¡A±z¦n¶Ü¡H¡vÂл\±¼¡Cµ{¦¡½X¦p¤U¡G
sub qRead,2 ;¹ê»ÚŪ¨ú¦r¦êªº¦ì¤¸²ÕӼơA¤£¥]§t0dH¡B0aH mov rdi,OFFSET sBuffer add rdi,qRead ;RDI¡×¨Ï¥ÎªÌ¿é¤Jªº©m¦W¤§«áªº¦ì§} mov rcx,SIZEOF sHowAreYou mov rsi,OFFSET sHowAreYou mov r8,rcx cld rep movsb
©I¥s ReadConsoleA ®É¡A²Ä¥|ӰѼƬO qRead ªº¦ì§}¡AReadConsoleA ·|§â¨Ï¥ÎªÌ¿é¤J¦h¤Ö¦ì¤¸²Õ¥~¥[¨âӦ줸²Õ¡A0DH¡B0AH¡A©ñ¤J qRead ÅܼƤº¡C¤W±µ{¦¡¤ù¬qªº²Ä¤@¦æ¡A´N¬O¦©°£¥~¥[ªº¨âӦ줸²Õ¡C²Ä¤G¦æ¬O§â RDI «ü¦V sBuffer ¦r¦êªº¦ì§}¡A¦¹¦r¦ê¬O¥Î¨Ó±µ¦¬¨Ï¥ÎªÌ©m¦Wªº¡C²Ä¤T¦æ¬O§â RDI ¥[¤W qRead¡A³o¼Ë RDI ´N·|«ü¦V¡u¨Ï¥ÎªÌ©m¦Wªº§ÀºÝ«e¨âÓ¦r¤¸¡vªº¦ì§}¤F¡C
¬°¤F§ó¸ÔºÉªº¤F¸Ñ¨ä¹Lµ{¡A¥Î x64dbg ¸ü¤J GREETING.EXE ¨ÓÆ[¹î¡C¤p¤ì°¸³]¸m¨âÓ¤¤Â_ÂI¡A¤À§O¦b©I¥s ReadConsoleA ¤§«e»P¤§«á¡C«eªÌ¯à¦b©I¥s ReadConsoleA «e°±¤U¨Ó¡A¥H§ïÅܸê®ÆÅã¥Ü°Ïªº¦ì§}¡F«áªÌ¯àÆ[¹î¨Ï¥ÎªÌ¿é¤J¤§µ²ªG¡C°Ñ¦Ò¤U¹Ï¡A³o¨âÓ¤¤Â_ÂI¤À§O¦b¦ì§} 13FD9106E »P 13FD9109E ( ¤Ï²ÕĶ°Ï¥Hºñ®Ø®Ø¦íªº«ü¥O )¡G ©I¥s§¹ ReadConsoleA ¤§«áªºµe±¦p¤W¹Ï¡A¬Ý¨ì¸ê®ÆÅã¥Ü°Ï¡A¥H¬õ®Ø®Ø°_¨Óªº¬O qRead ÅܼơA¦pªG¨Ï¥ÎªÌ¿é¤J¡u±i¤T¡v¡A¨º»ò¨âÓ¤¤¤å¦r¡A¨CÓ¤¤¤å¦r¶·¦û¥Î¨âӦ줸²Õ¡A©Ò¥H¤@¦@¥|Ӧ줸²Õ¡A¥[¤W ReadConsoleA ÃB¥~¥[¤Wªº¨âӦ줸²Õ¡AÁ`¦@¬O¤»Ó¦ì¤¸²Õ¡C( ¨£µù¤@ )
±µ¤U¨Ó·|°õ¦æ¦ì§} 13FD9109E ³Bªº¡usub qRead,2¡v¡Aª`·N¡A¡u[13FD93018]¡v´N¬O¥Nªí qRead ÅܼƤ§¼ÆÈ¡A¤§«á qRead Åܬ°¥|¡CµM«áªº«ü¥O¬O RDI «ü¦V sBuffer ªº¦ì§}¡A¸Ó¦ì§}¬O 13FD93020¡A¨ä¤º®e¦b¸ê®ÆÅã¥Ü°Ï¥HÂŮخئí¡C±µ¤U¨Óªº«ü¥O¬O¡uadd rdi,qRead¡v¡A¥[¤W¥|¤§«á¡ARDI ´N«ü¦V¡u¨Ï¥ÎªÌ©m¦Wªº§ÀºÝ«e¨âÓ¦r¤¸¡vªº¦ì§}¤W¡A¤]´N¬O¡u0DH¡v©Ò¦b¦ì§}¡C±µ¤U¨Óªº«ü¥O·|§â¡u¡A±z¦n¶Ü¡H¡v¦r¦ê·h¨ì¦¹¦ì§}¤W¡A³o¼Ë´NÂл\±¼¡u0DH¡B0AH¡v¤F¡C
ª`·N¨ì¤W¹Ï¡A¬õ®Ø®Ø°_¨Óªº¬O qRead ÅܼƦb x64dbg ±Æ¦C¤è¦¡¬O¡u06 00 00 00 00 00 00 00¡v¡CqRead ªº¸ê®ÆÃþ«¬¬O¥|¦r²Õ¡A»Ý¦û¥Î¤KӦ줸²Õªº°O¾ÐÅé¡C¨Ì¾Ú¤W±»¡ªº¡AqRead ¬° 6¡A¥u¦³³Ì§C¦ì ( §Y¡uÓ¡v¦ì¼Æ ) ¬O 6¡A¨ä¾l°ª¦ì¼Æ ( §Y¤Q¦ì¼Æ¡B¦Ê¦ì¼Æ¡B¤d¦ì¼Æ¡K¡Kµ¥µ¥ ) §¡¬°¹s¡C¦Ó 6 ¬O³Ì§C¦ì¼Æ¡A¦s©ñ¦b³Ì§C¦ì§}¡F¨ä¾l¦ì¼Æ§¡¬°¹s¡A¨Ì§Ç©¹°ª¦ì§}©µ¦ù¡C
¹³³o¼Ë§C¦ì¼Æ¦s©ñ¦b°O¾ÐÅ骺§C¦ì§}¡A°ª¦ì¼Æ¦s©ñ¦b°O¾ÐÅ骺°ª¦ì§}¡A³o¼Ëªº±Æ¦Cªº¤è¦¡ºÙ¬°¡u¤pºÝ§Ç¡v( little-endian )¡Cx86 ®a±Úªº CPU ÄÝ©ó³oºØ¡C¤pºÝ§Ç¸û²Å¦X¹q¸£Åª¨úªº¤è¦¡¡A¦ý»P¤HÃþ¾\Ū²ßºD¬Û¥ª¡C¥t¦³¤@ºØºÙ¬°¤jºÝ§Ç ( Big-Endian ) ªº±Æ¦C¤è¦¡¡A«h¬O°ª¦ì¼Æ¦s©ñ¦b°O¾ÐÅ骺§C¦ì§}¡B§C¦ì¼Æ©ñ¦s¦b°O¾ÐÅ骺°ª¦ì§}¡CMotorola 68000 ¨t¦C¥H¤Îºô¸ô¶Ç¿é§¡±Ä¥Î¤jºÝ§Ç¡A¥¦¸û²Å¦X¤HÃþ¾\Ū²ßºD¡A¦ý¤£²Å¦X¹q¸£Åª¨ú¤è¦¡¡C
¬°¤F¥[²`¤pºÝ§Çªº±Æ¦C¤è¦¡¡A¤p¤ì°¸¼¶¼g¤F©³¤Uªº TST_LE.ASM¡G
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24 |
;À˵ø¤pºÝ§ÇÀx¦s¼ÆȪº¤è¦¡
;²ÕĶ¨BÆJ¡G1.set link=/subsystem:console /entry:main
;¡@¡@¡@¡@¡@2.ml64 tst_le.asm
OPTION CASEMAP:NONE
EXTRN ExitProcess:PROC
INCLUDELIB e:\masm32\lib64\kernel32.lib
;***************************************************************************************************
.CONST
string1 DB "With great power, comes great responsibility.",0
x DW 1234h
y DD 0
z DQ 56789abh
string2 DB "Life was like a box of chocolates. You never know what you're gonna get."
;***************************************************************************************************
.CODE
;---------------------------------------------------------------------------------------------------
main PROC
sub rsp,28h ;main°Æµ{¦¡©Ò©I¥sªºWin64 API¡A°Ñ¼Æ³Ì¦hªº¦³¤Ó¡A¬GRSP´î28H
lea rax,string1
xor rcx,rcx
call ExitProcess
main ENDP
;***************************************************************************************************
END |
¤U¹Ï¬O¥Î x64dbg ¸ü¤J TST_LE.EXE «á¡A«ö¡uF9¡vÁä¡A¦A«ö¡uF7¡vÁ䪺µe±¡C¥Ñ¥k¤W¨¤ RIP ©Ò«üªº«ü¥O¡A±oª¾§Y±Nn°õ¦æ¡ulea rax,string1¡v¡A±q«á±ªº¡u13F7E2010¡v´N¥i¥H±oª¾ string1 ªº¦ì§}¬O¡u13F7E2010¡v¡C§â¸ê®ÆÅã¥Ü°Ï¤Á´«¨ì¦¹¦ì§}¡C ª`·N¬Ý¸ê®ÆÅã¥Ü°Ï¡A¥Hºñ¦â®Ø®Ø°_¨Óªº¬O x ÅܼơA¥Ñì©lµ{¦¡ª¾¨äȬ° 1234H¡A¦ý¦b x64dbg «o¬O¡u34 12¡v¡CÂŦâ®Ø®Ø°_¨Óªº¬O z ÅܼơA¨äȬ° 56789abH¡A¦ý«oÅã¥Ü¬°¡uAB 89 67 05 00 00 00 00¡v¡C