Ch 10¡@°Æµ{¦¡¡]¤G¡^

«e¤@³¹¤¶²Ð¤F°Æµ{¦¡¡Aª¾¹D¥¦¬O¤@¬q¥i¥H§¹¦¨¯S©w¥Øªºªºµ{¦¡¡C­nÅý°Æµ{¦¡°õ¦æ®É¡A±o¥Ñ¥Dµ{¦¡©I¥s¥¦¡C·íµM­n§¹¦¨¬Y¨Ç¯S©w¥Øªº¡A¥²©w­n¥Ñ¥Dµ{¦¡¶Çµ¹°Æµ{¦¡¬Y¨Ç¸ê®Æ¡A³o¨Ç¸ê®Æ¤@¯ëºÙ¬°°Ñ¼Æ ( parameter )¡A¦Ó«á°Æµ{¦¡¸g¹L¹Bºâ¤~¯à§¹¦¨¡C¤W¤@³¹©Ò¤¶²Ðªºµ{¦¡¡A¬OÂǥѼȦs¾¹§@¬°°Ñ¼Æ§â¸ê®Æ¶Çµ¹°Æµ{¦¡ªº¡A¦ý¬O¼È¦s¾¹²¦³º¦³­­¼Æ¶qµ}¤Ö¡A¦³®É¤£°÷¨Ï¥Î¡C¦]¦¹¥»³¹¤¶²Ð¥t¤@ºØ¶Ç»¼°Ñ¼Æªº¤èªk¡AÂǥѰïÅ|§@¬°°Ñ¼Æ¶Ç»¼¸ê®Æ¡C


­ì²z

¨Ï¥Î°ïÅ|±N°Ñ¼Æ¶Çµ¹°Æµ{¦¡¡A¤´µM­n¨Ï¥Î CALL «ü¥O©I¥s°Æµ{¦¡¡A¦ý¬O MASM 6.x ùØ·s¼W¤@­Ó°²«ü¥O¡AINVOKE¡A¯à´î¤Öµ{¦¡³]­p®v³\¦h·Ð¤Hªº¤u§@¡CµM¦Ó¨Ï¥Î INVOKE «ü¥O©I¥s°Æµ{¦¡¤§«e±o«Å§i°Æµ{¦¡ªº­ì«¬¡A³o¥i¥H¥Î PROTO °²«ü¥O¹F¦¨¥Øªº¡A¦]¦¹©³¤U¥ý¤¶²Ð PROTO °²«ü¥O¡C


PROTO °²«ü¥O

«Å§i°Æµ{¦¡­ì«¬ªº·N«ä¤j­P¥i»{¬°¬O§i¶D²ÕĶ¾¹°Æµ{¦¡»Ý­n´X­Ó°Ñ¼Æ¡A³o¥i¥Î PROTO ¹F¦¨¡CPROTO °²«ü¥Oªº»yªk¬O¡G

label   PROTO   [distance][language-type][°Ñ¼Æ¦Cªí]

distance »P language-type ªº¥Îªk»P PROC ¤@¼Ë¡C¥ý»¡ distance ¥i¥H¬O NEAR ©Î¬O FAR¡A¤À§Oªí¥Ü°Æµ{¦¡¬Oªñµ{ÁÙ¬O»·µ{¡Cªñµ{ªº·N«ä¬O«ü°Æµ{¦¡»P¥Dµ{¦¡¦b¦P¤@°Ï¬q¤º¡F»·µ{«h¬O¦b¤£¦P°Ï¬q¡C

distance ¥i¥H¬Ù²¤¡A¦pªG¬Ù²¤´N¥Ñµ{¦¡¶}©lªº .MODEL ¨M©w¡C¦pªG .MODEL «ü©wªº°O¾ÐÅé¼Ò¦¡¬O TINY¡BSMALL¡BCOMPACT ©Î FLAT¡A¨º»ò°Æµ{¦¡¬Oªñµ{ªº¡F¦pªG¬O MEDIUM¡BLARGE ©Î HUGE¡A¨º»ò´N¬O»·µ{¡C¦pªG¬O 80386 µ¥¯Å©Î§ó°ªµ¥¯Åªº CPU¡A¥i¥H¥Î 16 ©Î 32 ¦ì¤¸°Ï¬q¡Adistance ÁÙ¥i¥H¨Ï¥Î NEAR16¡BNEAR32¡BFAR16 ©Î FAR32¡C

language-type ¬O»y¨¥Ãþ«¬ªº·N«ä¡A¦b²Ä¤E³¹´£¨ì PROC/ENDP ®É¤]»¡¹L PROC ¤¤¦³¤@¿ï¶µ¬O»y¨¥Ãþ«¬¡A»P PROTO ªº»y¨¥Ãþ«¬·N¸q¬Û¦P¡A³£¬O¥Î¨Ó³]©w©I¥sºD¨Ò ( calling convention ) ªº¡C¦pªG¬Ù²¤ language-type¡A¥H .MODEL ³B©w¸qªº¬°·Ç¡C»y¨¥Ãþ«¬¥i¥H¦³ C¡BBASIC¡BFORTRAN¡BPASCAL¡BSYSCALL »P STDCALL ¤»ºØ¡A»P©I¥s®É°Ñ¼Æ±À¤J°ïÅ|ªº±¡§Î¦³Ãö¡C©³¤U¬O»y¨¥Ãþ«¬ªº¸ê®Æ¡G

ªí¤@¡@»y¨¥Ãþ«¬
»y¨¥Ãþ«¬STDCALLSYSCALL CBASICFORTRANPASCAL
¥Ñ­þ­Ó°Ñ¼Æ¶}©l±À¤J°ïÅ|¥k¥k¥k¥ª¥ª¥ª
½Ö­t³d²M°£°ïÅ|°Æµ{¦¡¥Dµ{¦¡¥Dµ{¦¡°Æµ{¦¡°Æµ{¦¡°Æµ{¦¡
¬O§_¤¹³\¤£©w°Ñ¼Æ¬O ( ¨£©³¤U )¬O¬O§_§_§_
­×§ï°Æµ{¦¡¦WºÙ¥[¤J _ ¤Î @¤£­×§ï¥[¤J _§ï¦¨¤j¼g§ï¦¨¤j¼g§ï¦¨¤j¼g

¥J²ÓÀˬd¤W­±»y¨¥Ãþ«¬ªºªí®æ¡A§A´N·|µo²{¡A¨Æ¹ê¤W BASIC¡BFORTRAN¡BPASCAL ³o¤TºØ»y¨¥Ãþ«¬¨ä¹ê¬O¤@¼Ò¤@¼Ë¡C¦A¬Ý C »P SYSCALL ¦ü¥G¤]¤@¼Ë¡A¦ý¬OÁÙ¬O¦³®t§Oªº¡F²ÕĶ¾¹¤£·|¬° SYSCALL ¤¤ªº°Æµ{¦¡¦Wµ¥²Å¸¹²K¥[©³½u©Î¨ä¥L¦r¤¸¡A¦ý C ·|¡F³o´X¥G¤£·|¼vÅTµ{¦¡¹B§@¡A±N¥¦­Ì­Çµø§@¬Ûµ¥¥ç¥¼¹Á¤£¥i¡CSTDCALL ¦b°Ñ¼Æ­Ó¼Æ¤£½T©w®É¡A¥ç§Y¨Ï¥Î VARARG ®É¬O¥Ñ¥Dµ{¦¡²M°£ªº°ïÅ|¡A¨ä¥L±¡§Î³£¥Ñ°Æµ{¦¡²M°£¡C

°Ñ¼Æ¦Cªí¬O¥Ñ³\¦h²Õªº°Ñ¼Æ»P¸ê®ÆÃþ«¬²Õ¦¨¡A¨C¤@²Õ³£¬O¡u°Ñ¼Æ:¸ê®ÆÃþ«¬¡v¡A¦Ü©ó­n¦h¤Ö²Õ«hµø°Æµ{¦¡ªº»Ý­n¨M©w¡A¤]¥i¥H¤@²Õ³£¨S¦³¡C©³¤U¬O´X­Ó¨Ò¤l¡G

addup   PROTO   NEAR C arg1:WORD, arg2:WORD, arg3:WORD
myproc  PROTO   :WORD, :BYTE

PROTO ³Ì­«­nªº¥Øªº¬O§i¶D½sĶ¾¹°Ñ¼Æªº¼Æ¶q¡A°Ñ¼Æªº¦WºÙ¨Ã¨S¦³¤Ó¤j¥Î³~¡A¶È¶È¬O¬°¤F¥iŪ©Ê¡A¦]¦¹¥i¥H¬Ù²¤¡A©Ò¥H¤W­±³Ì«á¤@¥y¤]¬O¥i¥Hªº¡C¦b¨Ï¥Î INVOKE ©I¥s°Æµ{¦¡«e¡A¤@©w­n¥ý¥Î PROTO «Å§i°Æµ{¦¡­ì«¬¡F¦ý¬O¦pªG¦b INVOKE ©I¥s°Æµ{¦¡«e¡A°Æµ{¦¡¤w¸g¥ý¥Ñ PROC/ENDP ©w¸q¹L¤F¡A¨º»ò PROTO «Å§i­ì«¬´N¨S¦³¥²­n¤F¥i¥H¬Ù²¤¤£¼g¡C


INVOKE °²«ü¥O

INVOKE ¯à«Ø¥ß¤@¨t¦Cªº PUSH «ü¥O¥H¤Î CALL «ü¥O¡CINVOKE ªº»yªk¦p¤U

INVOKE  °Æµ{¦¡¦W,°Ñ¼Æ1,°Ñ¼Æ2¡K¡K

°Æµ{¦¡¦W¥i¥H¬O¼Ð°O©Î¬O¶¡±µ©w§}¡A°Ñ¼Æ1¡B°Ñ¼Æ2¡K¡K¤¤ªº¨C­Ó°Ñ¼Æ³£·|¥Ñ PUSH «ü¥O±À¤J°ïÅ|ùئӫá³Q°Æµ{¦¡©Ò¦s¨ú¡A¨º»ò·|¦³´X­Ó°Ñ¼Æ±oµø PROC ©Î PROTO °²«ü¥O©Ò©w¸qªº°Ñ¼Æ¦Cªí¤¤ªº°Ñ¼Æ­Ó¼Æ¨M©w¡C³oùتº°Ñ¼Æ1¡B°Ñ¼Æ2¡K¡K¥i¥H¨Ï¥Î¥ß§Y­È¡BÅܼơB¼È¦s¾¹¡BADDR ¹Bºâ¤l¡B¦ì§}¹Bºâ¦¡¡C


©I¥sºD¨Ò ( Calling Convention )¡G¥H¨Ò¤l»¡©ú PROTO¡BPROC¡BINVOKE ªºÃö«Y

.MODEL¡BPROTO¡BPROC ³o¤T­Ó°²«ü¥O³£¦³¤@­Ó¿ï¶µ¡A»y¨¥Ãþ«¬ ( language type )¡A¥¦³W©w¤F¥Dµ{¦¡»P°Æµ{¦¡¤§¶¡ªº°Ñ¼Æ¨Ì·Ó¤°»ò¶¶§Ç¶Ç¹F¡B¤S¥Ñ½Ö­t³d²M°£°ïÅ|µ¥¸ê°T¡A³o¨Ç¸ê°TºÙ¬°¡u©I¥sºD¨Ò¡v( calling convention )¡F¤]¥i¥H»¡¡A©I¥sºD¨Ò¬O¥Ñ»y¨¥Ãþ«¬³]©wªº¡C

¤@¯ë¨Ó»¡¡A¦b¤@­Óµ{¦¡ùØ·|©I¥s³\¦h°Æµ{¦¡¡A¦Ó³o¨Ç°Æµ{¦¡³£·|³]©w¦¨¬Û¦Pªº©I¥sºD¨Ò¡A¦]¦¹¦bµ{¦¡¤@¶}©l¥Ñ¡u.MODEL¡v³]©w¦n¡APROTO¡BPROC ³£¬Ù²¤»y¨¥Ãþ«¬¡A³o¼Ë¸û¬°³æ¯Â¥B¤è«K¡C¦pªG¡u.MODEL¡v¨S¦³³]©w»y¨¥Ãþ«¬¡A¨º»ò¦P¤@­Ó°Æµ{¦¡¦b¡uPROTO¡v«Å§i®É»P¡uPROC¡v©w¸q®É³£­n³]©w¬Û¦Pªº»y¨¥Ãþ«¬¡A¦ý¤£¦Pªº°Æµ{¦¡¥i¥H±Ä¥Î¤£¦Pªº»y¨¥Ãþ«¬¡A³o¼Ë´N¦³¸û¤jªº¼u©Ê¡C¤£¹L­nª`·Nªº¬O¡A¡uPROTO¡v»P¡uPROC¡v³£­n³]©w¡A§_«h²ÕĶ¾¹·|²£¥Í¿ù»~°T®§¡F·íµM¦pªG¡uPROC¡v¦b¡uPROTO¡v¤§«e¡AÁÙ¬O¥i¥H¬Ù²¤¡uPROTO¡v¡C

¨Ò¦p¦³­Ó°Æµ{¦¡ sum_of ¬°­pºâ¤T­Ó¾ã¼Æ¤§©M¡A©³¤U¬Oµ{¦¡¤ù¬q¡G

sum_of  PROTO   »y¨¥Ãþ«¬ :WORD,:WORD,:WORD
        ...
        INVOKE  sum_of,num1,num2,num3
        ...
sum_of  PROC    »y¨¥Ãþ«¬ n1:WORD,n2:WORD,n3:WORD
        mov     ax,n1
        add     ax,n2
        add     ax,n3
        ret
sum_of  ENDP

¦]¬°¡usum_of PROC¡v¦b¡uINVOKE sum_of¡v¤§«á¡A¦]¦¹­n¦b¡uINVOKE sum_of¡v¤§«e¥H PROTO «Å§i sum_of ªº­ì«¬¡A§_«h²ÕĶ¾¹µLªkª¾¹D­n¦p¦ó³B²z°Ñ¼Æ¡C¦pªG¬O¡usum_of PROC¡v¦b¡uINVOKE sum_of¡v¤§«e¡A´N¤£»Ý­n¼g¡uPROTO¡v¤F¡A¦]¬°¡usum_of PROC¡v´N¤w¸g§i¶D²ÕĶ¾¹¦p¦ó³B²z°Ñ¼Æ¤F¡C©Ò¥H¤W­±ªºµ{¦¡¥i¥H¼g¦¨¤U­±¨º¼Ë¡G

sum_of  PROC    »y¨¥Ãþ«¬ n1:WORD,n2:WORD,n3:WORD
        mov     ax,n1
        add     ax,n2
        add     ax,n3
        ret
sum_of  ENDP
        ...
        INVOKE  sum_of,num1,num2,num3
        ...

©³¤U»¡©ú²ÕĶ¾¹¦p¦ó°Ñ¼Æ¡C¦pªG§â¤W­±µ{¦¡¤ù¬q¤¤ªº¡u»y¨¥Ãþ«¬¡v¨Ì§ÇÅÜ§ó¬° STDCALL¡BSYSCALL¡BC¡BBASIC¡A²ÕĶ­ì©lµ{¦¡«áªºµ²ªG¡A¦p¤Uªí¡G

ªí¤G¡@»y¨¥Ãþ«¬½d¨Ò
­ì©l½XSTDCALLSYSCALLCBASIC
INVOKE sum_of,num1,\
         num2,num3
PUSH    num3
PUSH    num2
PUSH    num1
CALL    sum_of
...
PUSH    num3
PUSH    num2
PUSH    num1
CALL    sum_of
ADD     SP,+06
...
PUSH    num3
PUSH    num2
PUSH    num1
CALL    sum_of
ADD     SP,+06
...
PUSH    num1
PUSH    num2
PUSH    num3
CALL    sum_of
...
sum_of   PROC  »y¨¥Ãþ«¬\
  n1:WORD,n2:WORD,n3:WORD
   mov   ax,n1
   add   ax,n2
   add   ax,n3
   ret
sum_of   ENDP
PUSH    BP
MOV     BP,SP
MOV     AX,[BP+04]
ADD     AX,[BP+06]
ADD     AX,[BP+08]
POP     BP
RET     0006
PUSH    BP
MOV     BP,SP
MOV     AX,[BP+04]
ADD     AX,[BP+06]
ADD     AX,[BP+08]
POP     BP
RET
PUSH    BP
MOV     BP,SP
MOV     AX,[BP+04]
ADD     AX,[BP+06]
ADD     AX,[BP+08]
POP     BP
RET
PUSH    BP
MOV     BP,SP
MOV     AX,[BP+08]
ADD     AX,[BP+06]
ADD     AX,[BP+04]
POP     BP
RET     0006

²ÕĶ¾¹·|§â¡uINVOKE sum_of,num1,num2,num3¡v²ÕĶ¦¨¤@¨t¦Cªº PUSH »P CALL «ü¥O¡A­Y¬O¡u»y¨¥Ãþ«¬¡v¬° STDCALL¡A°Ñ·Ó¤W­±ªí¤@»y¨¥Ãþ«¬ªº»¡©ú¡A³Ì¥kÃ䪺 num3 °Ñ¼Æ³Ì¥ý³Q±À¤J°ïÅ|¡AµM«á¨Ì§Ç¬O num2¡Bnum1¡A³Ì«á¬O CALL «ü¥O¡A©ó¬O´NÅܦ¨¤Wªí¤Gªº±¡§Î¡C¨£¤U¹Ï¹Ï¤@¡A³o¬O¶i¤J sum_of «á¡A°ïÅ|¦w±Æ±¡§Î¡A¥i¥H¨£¨ì BP¡Ï4 ¦ì§}ªº¦a¤è¬O num1¡ABP¡Ï6 ¬O num2¡K¡K¡A·í°Æµ{¦¡­n¦s¨ú³o¨Ç°Ñ¼Æ®É¡A´N¬O¨ì³o¨Ç¦ì§}¥h¦s¨ú¡C

¤W¹Ï¹Ï¤G¬O°Æµ{¦¡µ²§ô®É¡A¥ý¥Ñ°ïÅ|¨ú¦^­ì¨Óªº BP¡A¦¹®É°ïÅ|³»ºÝ¬Oªð¦^¦ì§}¡AµM«á°õ¦æ¡uRET 6¡v¡A¥Ñªð¦^¥Dµ{¦¡¨Ã±Ë±ó°ïÅ|¤W¤»­Ó¦ì¤¸²Õªº¸ê®Æ¡A©ó¬O°ïÅ|¤ºªº±¡§Î´N©M¥¼¶i¤J sum_of «e¤@¼Ë¡C¥Ñ©ó¡uRET 6¡v¬O¦b°Æµ{¦¡ùØ°õ¦æªº¡A©Ò¥Hªí¤@ùØ»¡ STDCALL ¥Ñ°Æµ{¦¡²M°£°ïÅ|ùتº°Ñ¼Æ¡C

¦pªG»y¨¥Ãþ«¬§ï¦¨¡uC¡v¡A²ÕĶµ²ªG·|¦³¨Ç¤£¦P¡C¥ý»¡¬Û¦Pªº³¡¤À¡C¦b§â°Ñ¼Æ±À¤J°ïÅ|®É¡A¥Ñ¥k¦Ü¥ª¨Ì§Ç±À¤J°ïÅ|¡A¦]¦¹ num3 ¦b³Ì°ª¦ì§}¡Anum1 ¦b³Ì§C¦ì§}¡C°õ¦æ¡uCALL¡v®É¦A§âªð¦^¦ì§}±À¤J°ïÅ|¡A©ó¬O°ïÅ|¤¤ªº±¡§ÎÅܦ¨¤U¹Ï¥|¡C¶i¤J sum_of «á¡ABP¡Ï4 ¦ì§}ªº¦a¤è¬O num1¡ABP¡Ï6 ¬O num2¡K¡K¡A·í°Æµ{¦¡­n¦s¨ú³o¨Ç°Ñ¼Æ®É¡A¤]¬O¨ì³o¨Ç¦ì§}¥h¦s¨ú¡C

¤W¹Ï¤­¬O°Æµ{¦¡µ²§ô®É¡A¥ý¥Ñ°ïÅ|¨ú¦^­ì¨Óªº BP¡A¦¹®É°ïÅ|³»ºÝ¬Oªð¦^¦ì§}¡AµM«á°õ¦æ¡uRET¡v¡Cµ{¦¡¥D±±Åv¦^¨ì¥Dµ{¦¡ùØ¡A³o®É­Ô°ïÅ|³»ºÝ¬O num1 ªº¦ì§}¡C¦^¨ì¥Dµ{¦¡«áªº²Ä¤@­Ó«ü¥O¬O¡uADD SP,6¡v¡A¨Ï±o SP ¼È¦s¾¹­«·s«ü¦V¥¼©I¥s°Æµ{¦¡«eªº°ïÅ|³»¤F¡C¥Ñ©ó¬O¦b¥Dµ{¦¡¤¤²M°£°ïÅ|¤ºªº°Ñ¼Æ¡A¦]¦¹ªí¤@ùØ»¡ C ¥Ñ¥Dµ{¦¡²M°£°ïÅ|ùتº°Ñ¼Æ¡C

STDCALL »P C ¬O¸û¬°±`¥Îªº»y¨¥Ãþ«¬¡C·L³n©Ò¥X«~ªº Windows §@·~¨t²Î¦³ 16¦ì¤¸¡B32 ¦ì¤¸»P 64 ¦ì¤¸¤TºØ¡A¨ä¤¤ 32 ¦ì¤¸ªº§@·~¨t²Î¡A±q Windows 95/98/XP ¤D¦Ü¨ì Windows 7/8/10¡A©Ò¨Ï¥Î¨ìªº Win32 API ªº©I¥sºD¨Ò³£¬O STDCALL¡A©Ò¥H¼¶¼g Win32 µ{¦¡¤@©w­n¤F¸Ñ STDCALL ¤~¦æ¡C¦Ü©ó C¡A¨º¤]¤£¥²¦h»¡¡Aªñ¤Q¦~¨Ó³Ì¬y¦æ¥B¾ú¤[¤£°Iªºµ{¦¡»y¨¥«D C »y¨¥²öÄÝ¡A­n¾Ç¦n C ¤]¤£¥i¤£ª¾ C ³oºØ©I¥sºD¨Ò¡C


¤@­Ó¨Ò¤l¡GSEC

¤p¤ì°¸·Q¥H¤@­Ó¨Ò¤l¨Ó»¡©ú¦p¦ó¥Î STDCALL ©I¥sºD¨ÒÅý°ïÅ|¶Ç»¼°Ñ¼Æ¡C©³¤Uªº SEC.ASM µ{¦¡¬O­pºâ¤­¤p®É¥|¤Q¤K¤À¤Q¤C¬í¬Û·í©ó¦h¤Ö¬íÄÁ¡A³Ì«á¦b¿Ã¹õ¤W¦L¥X¦r¦ê¡G¡u05:48:17=20897seconds¡v¡C­ì©lµ{¦¡¦p¤U¡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
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
;SEC§â5¤p®É48¤À17¬í´«ºâ¦¨¦@¦³´X¬íÄÁ
.MODEL  TINY,STDCALL
.386
seconds PROTO   :WORD,:WORD,:WORD,:WORD
;*******************************************************************************
.CODE  
ORG     100h
;-------------------------------------------------------------------------------
;¥Dµ{¦¡
main    PROC
        jmp     begin
hour    DW      5
minute  DW      48
second  DW      17
string  DB      "05:48:17=00000seconds$"
begin:  mov     dx,OFFSET string
        INVOKE  seconds,hour,minute,second,OFFSET string+9
        mov     ah,9
        int     21h
        int     20h
main    ENDP
;-------------------------------------------------------------------------------
;seconds´«ºâ®É¡B¤À¡B¬í¦@¦³´X¬íÄÁ
;°Ñ¼Æ¡Gn1¡Ð®É
;¡@¡@¡@n2¡Ð¤À
;¡@¡@¡@n3¡Ð¬í
;¡@¡@¡@address¡ÐÂà´««áªº¤Q¶i¦ì¦r¦ê
;¦^¶Ç­È¡GAX¡Ð1¡A¦¨¥\µL¿ù»~
;¡@¡@¡@¡@¡@¡Ð0¡A¿ù»~
seconds PROC    USES bx cx dx n1:WORD,n2:WORD,n3:WORD,address:WORD
        LOCAL   ten:WORD
        LOCAL   temp:WORD
        mov     ten,10
        mov     ax,n1
        sub     dx,dx
        mov     cx,3600
        mul     cx      ;AX¡×3600¡Ñ¤p®É
        mov     temp,ax ;§âAX¼È®É¦s©ótempùØ¡A¦]¬°AX¦b°µMUL¹Bºâ·|¥ÎµÛ
        mov     ax,n2
        sub     dx,dx
        mov     cx,60
        mul     cx      ;AX¡×60¡Ñ¤ÀÄÁ
        add     ax,n3   ;AX¡×60¡Ñ¤ÀÄÁ¡Ï¬íÄÁ
        add     ax,temp ;AX¡×¤w´«ºâ¦¨Á`¬í¼Æ
        mov     bx,address
        add     bx,4    ;BX¡×¤Q¶i¦ì¦r¦êªº­Ó¦ì¼Æ¦ì§}
        mov     cx,5    ;¦@¦³¤­¦ì¼Æ
next:   sub     dx,dx
        div     ten
        add     [bx],dl
        dec     bx
        loop    next
        mov     ax,1
        ret
seconds ENDP
;*******************************************************************************
END     main

§â SEC.ASM ¦s¤J¡uMS-DOS 6.20¡vµêÀÀ¾÷¾¹¤¤ªº¡uE:\DOS\SECOND¡v¤l¥Ø¿ýùØ¡A²ÕĶ»PÁpµ²¹Lµ{»P©¹±`¤@¼Ë¡G

C:\>e: [Enter]

E:\>cd dos\second [Enter]

E:\DOS\SECOND>ml sec.asm [Enter]
Microsoft (R) Macro Assembler Version 6.11
Copyright (C) Microsoft Corp 1981-1993.  All rights reserved.

 Assembling: sec.asm

Microsoft (R) Segmented Executable Linker  Version 5.31.009 Jul 13 1992
Copyright (C) Microsoft Corp 1984-1992.  All rights reserved.

Object Modules [.obj]: sec.obj/t
Run File [sec.com]: "sec.com"
List File [nul.map]: NUL
Libraries [.lib]:
Definitions File [nul.def]:

E:\DOS\SECOND>sec [Enter]
05:48:17=20897seconds
E:\DOS\SECOND>

¬Ý¨ì SEC ¦L¥X¡u05:48:17=20897seconds¡v¦r¦ê¡A´Nªí¥Ü SEC ¤w¸g°õ¦æ¦¨¥\¤F¡C©³¤U¤p¤ì°¸­n½Íªº¬O©I¥s°Æµ{¦¡®É©Ò«Ø¥ßªº°ïÅ|®Ø ( stack frames )¡C


°ïÅ|®Ø ( Stack Frames )

°ïÅ|®Ø¬O«ü¦b©I¥s°Æµ{¦¡®É¡A¦b°ïÅ|©Ò«Ø¥ßªº¤@¶ô°O¾ÐÅé¡A¥Ñ°ª¦ì§}¦V§C¦ì§}¡AùØ­±¦s©ñµÛ°Ñ¼Æ¡Bªð¦^¦ì§}¡B­ì¨Óªº BP¡B°Ï°ìÅܼƥH¤Î­n«O¦sªº¼È¦s¾¹µ¥¸ê®Æ¡Aµ¥ªð¦^¥Dµ{¦¡«á³o¨Ç¸ê®Æ³£·|³Q¾P·´¤£¯à¦A¦s¨ú¡C¤p¤ì°¸¦b«e­±¤w¸g´£¹L¤F°Ñ¼Æ¡Bªð¦^¦ì§}¡B­ì¨Óªº BP µ¥¸ê®Æªº¦w±Æ¤è¦¡¡A¤]½Í¹L¦p¦ó¾P·´¥¦­Ì¡C²{¦b¤p¤ì°¸­nÂÇµÛ SEC.COM µ{¦¡¡A¨Ó½Í½Í­n«O¦sªº¼È¦s¾¹¥H¤Î°Ï°ìÅܼƦp¦ó°t¦X°Ñ¼Æ¡Bªð¦^¦ì§}¡B­ì¨Óªº BP µ¥¸ê®Æ¡C

«e¤@³¹´£¨ì PROC ªº»yªk¬O

label  PROC  [distance][language-type][visibility][<prologuearg>][USES ¼È¦s¾¹¦Cªí][°Ñ¼Æ¦Cªí]

¨ä¤¤ªº¡uUSES ¼È¦s¾¹¦Cªí¡v·|³Q²ÕĶ¾¹²ÕĶ¦¨³\¦hÃþ¦ü¡uPUSH ¼È¦s¾¹¡vªº«ü¥O¡A§â¼È¦s¾¹¦Cªí¤ºªº¼È¦s¾¹¼È®É«O¦s¦b°ïÅ|ùØ¡F¦Ó¦b­nªð¦^¥Dµ{¦¡®É¡A­º¥ý·|¥H¬Û¤Ïªº¶¶§Ç¦Û°ïÅ|¨ú¥X¼È®É¦s©ñªº¼È¦s¾¹­ì­È¡C³o·íµM¬O¦]¬°¼È¦s¾¹¼Æ¶q¤£¦h¡A¦ý¦b³\¦h¹Bºâ¹Lµ{³£­n¥Î¨ì¡A¥¼¨¾¤î¯}Àò¼È¦s¾¹­«­nªº¸ê®Æ©Ò¥H PROC ¯S§O¥[¤J USES ¦b°ïÅ|ùØ«O¦s¼È¦s¾¹¡C½Ð°Ñ¦Ò¤Uªí¡G

­ì©lµ{¦¡²ÕĶ«áªºµ{¦¡
¥Dµ{¦¡
INVOKE  seconds,hour,minute,second,
                ADDR string+9
PUSH	0111      
PUSH	[second]  
PUSH	[minute]  
PUSH	[hour]    
CALL	_seconds@8
°Æµ{¦¡
seconds PROC    USES bx cx dx n1:WORD,
                n2:WORD,n3:WORD,
                address:WORD
        LOCAL   ten:WORD
        LOCAL   temp:WORD
        ....
        ret
seconds ENDP
PUSH    BP
MOV     BP,SP
ADD     SP,-04
PUSH    BX
PUSH    CX
PUSH    DX
....
POP     DX
POP     CX
POP     BX
MOV     SP,BP
POP     BP 
RET     0008

¹ï·Ó¤Wªí¡A¥ý¬Ý¥Dµ{¦¡¡C¤@±ø INVOKE °²«ü¥O¡A´N³Q²ÕĶ¾¹²ÕĶ¦¨¥|­Ó PUSH «ü¥O»P¤@­Ó CALL «ü¥O¡C¦A¬Ý°Æµ{¦¡¡A¨â±ø LOCAL «ü¥O¡A³Q²ÕĶ¾¹²ÕĶ¦¨¤@±ø¡uADD SP,-04¡v¡F¤@±ø PROC °²«ü¥OÅܦ¨¥|±ø PUSH «ü¥O»P¤@±ø MOV «ü¥O¡Cªð¦^¥Dµ{¦¡®É¡A¤@±ø RET «ü¥OÅܦ¨ RET 8 «ü¥O¡A¦P®É²ÕĶ¾¹ÁÙ¦b RET 8 ¤§«eÃB¥~²K¥[¤F¥|±ø POP «ü¥O»P¤@±ø MOV «ü¥O¡C

©³¤U¤p¤ì°¸ºt¹¢¥H SYMDEB °lÂÜ SEC.COM¡A¨ÓÆ[¹î°ïÅ|®Øªº¦w±Æ¤è¦¡¡C­º¥ý¸ü¤J SEC.SYM ¤Î SEC.COM¡G

E:\DOS\HEX2DEC>c:\tools\mapsym sec [Enter]
Microsoft (R) Symbol File Generator  Version 4.00
Copyright (C) Microsoft Corp 1984, 1985.  All rights reserved.

	Program entry point at 0000:0100

E:\DOS\HEX2DEC>c:\tools\symdeb sec.sym sec.com [Enter]
Microsoft (R) Symbolic Debug Utility  Version 4.00
Copyright (C) Microsoft Corp 1984, 1985.  All rights reserved.

Processor is [80286]
-t [Enter] ¡÷²Ä¤@­Ó«ü¥O¬O¡uJMP begin¡v
AX=0000  BX=0000  CX=0083  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
DS=20E1  ES=20E1  SS=20E1  CS=20E1  IP=011E   NV UP EI PL NZ NA PO NC
20E1:011E BA0801         MOV    DX,0108
-g 130 [Enter] ¡÷±µµÛ¦³¥|­Ó PUSH «ü¥O¡A§â 0111¡Bsecond¡Bminute¡Bhour ±À¤J°ïÅ|
AX=0000  BX=0000  CX=0083  DX=0108  SP=FFF6  BP=0000  SI=0000  DI=0000
DS=20E1  ES=20E1  SS=20E1  CS=20E1  IP=0130   NV UP EI PL NZ NA PO NC
20E1:0130 E80600         CALL   _seconds@8

¤p¤ì°¸ª½±µ¥Î¡ug 130¡v°õ¦æ¥|¦¸±À¤J°ïÅ|ªº«ü¥O¡A¤£¥Î³æ¨B°lÂÜ¡A¬Û«HŪªÌÀ³¸Ó«Ü¼ô±x PUSH «ü¥O¤F¡C±µ¤U¨Ó¬O¡ucall seconds¡v«ü¥O¡A¬°¤°»ò SYMDEB ·|§â°Æµ{¦¡¦WºÙ¼g¦¨¡u_seconds@8¡v¡H³o¬O²ÕĶ¾¹ªº³Ç§@¡C¦pªG»y¨¥Ãþ«¬¬° STDCALL ®É¡A²ÕĶ¾¹·|§â«Å§i¬° PUBLIC ªº²Å¸¹¦WºÙ¡A¦p°Æµ{¦¡¦WºÙ¡A­×¹¢¦¨¡u_­ì²Å¸¹¦W@n¡v¡An ¬O«ü°Ñ¼ÆÁ`¦@¦³¦h¤Ö¦ì¤¸²Õ¡C±µ着Ä~Äò¬Ý¤U¥h¡G

-t [Enter] ¡÷¶i¤J seconds °Æµ{¦¡
AX=0000  BX=0000  CX=0083  DX=0108  SP=FFF4  BP=0000  SI=0000  DI=0000
DS=20E1  ES=20E1  SS=20E1  CS=20E1  IP=0139   NV UP EI PL NZ NA PO NC
DGROUP:_seconds@8:
20E1:0139 55             PUSH   BP
-t [Enter] ¡÷¡A¦p¤W­±seconds °Æµ{¦¡ªº²Ä¤@­Ó«ü¥O¬O§â BP «O¦s¦b°ïÅ|¤W¡A°õ¦æ¥¦¡I
AX=0000  BX=0000  CX=0083  DX=0108  SP=FFF2  BP=0000  SI=0000  DI=0000
DS=20E1  ES=20E1  SS=20E1  CS=20E1  IP=013A   NV UP EI PL NZ NA PO NC
20E1:013A 8BEC           MOV    BP,SP
-t [Enter] ¡÷¤W­±«ü¥O¬OÅý BP ªº°ò·Ç¬°²{¦b°ïÅ|³»ºÝ¦ì§}¡A¤]¬OÀx¦s­ì BP ­Èªº¦ì§}
AX=0000  BX=0000  CX=0083  DX=0108  SP=FFF2  BP=FFF2  SI=0000  DI=0000
DS=20E1  ES=20E1  SS=20E1  CS=20E1  IP=013C   NV UP EI PL NZ NA PO NC
20E1:013C 83C4FC         ADD    SP,-04
-t [Enter] ¡÷¤W­±«ü¥O¡uADD SP,-4¡v¬OªÅ¥X¥|­Ó¦ì¤¸²Õ¡A¦s©ñ¨â­Ó°Ï°ìÅܼÆ
AX=0000  BX=0000  CX=0083  DX=0108  SP=FFEE  BP=FFF2  SI=0000  DI=0000
DS=20E1  ES=20E1  SS=20E1  CS=20E1  IP=013F   NV UP EI NG NZ NA PE CY
20E1:013F 53             PUSH   BX
-g 142 [Enter] ¡÷±µµÛ¤T­Ó PUSH «ü¥O¡A¤À§O§â BX¡BCX¡BDX ±À¤J°ïÅ|¡Aª½±µ°õ¦æ§¹¤£¤@¤@³æ¨B°lÂÜ
AX=0000  BX=0000  CX=0083  DX=0108  SP=FFE8  BP=FFF2  SI=0000  DI=0000
DS=20E1  ES=20E1  SS=20E1  CS=20E1  IP=0142   NV UP EI NG NZ NA PE CY
20E1:0142 C746FE0A00     MOV    Word Ptr [BP-02],000A              SS:FFF0=7302

°ïÅ|®Ø¥k¹ÏÅã¥Üªº¬O SEC.COM ©I¥s seconds ®É©Ò«Ø¥ßªº°ïÅ|®Ø¡C³z¹L¤W­±ªº°lÂÜ¡A°ïÅ|®Ø«Ø¥ßªº¶¶§Ç¦p¤U¡G

  1. °Ñ¼Æ¡G­þ­Ó°Ñ¼Æ¥ý³Q±À¤J°ïÅ|¥Ñ»y¨¥Ãþ«¬¨M©w¡C
  2. ©I¥s°Æµ{¦¡¡Gªð¦^¦ì§}³Q±À¤J°ïÅ|¡C
  3. §â BP ±À¤J°ïÅ|¡G«O¦s­ì¦³ªº BP¡C
  4. ¨Ï BP ³]¬°©M SP ¬Û¦P­È¡GBP ©Ò«ü¦ì§}¬°°ò·Ç¡A¸Ó¦ì§}¦s¦³­ì¨Óªº BP¡C
  5. «O¯d°Ï°ìÅܼƩһݰO¾ÐÅé¡GSP ´î¥h¤@¼Æ¡A¦¹¼Æ©M°Ï°ìÅܼÆÃþ«¬¦³Ãö¡C
  6. §â PROC ¤¤¡AUSES ©Ò¦C¥Xªº¼È¦s¾¹±À¤J°ïÅ|«O¦s°_¨Ó¡C

¨ì¦¹°ïÅ|®Ø¤w¸g«Ø¥ß§¹¦¨¡A¶}©l¶i¤J°Æµ{¦¡¯u¥¿­n§¹¦¨ªº¥Øªº¡G­pºâÁ`¦@¦³´X¬íÄÁ¡C¤£¹L³o³¡¤À¤£¬O³o¤@³¹ªº­«ÂI¡A¦]¦¹²¤¹L¤£»¡¡A¨£¤U­±ªº«ü¥Oª½±µ°õ¦æ¡ug 17a¡v¡A¬Û«HŪªÌ¤]¯à°÷¬ÝÀ´­pºâ¸g¹L¡Cµ¥­pºâ§¹¦¨¡A´N­nªð¦^¥Dµ{¦¡¤F¡A©³¤U´N¬O¬Ý¬Ý³o­Ó¹Lµ{¡C

-g 17a [Enter]
AX=0001  BX=0110  CX=0000  DX=0002  SP=FFE8  BP=FFF2  SI=0000  DI=0000
DS=20E1  ES=20E1  SS=20E1  CS=20E1  IP=017A   NV UP EI PL NZ NA PO NC
20E1:017A 5A             POP    DX

­º¥ý¬O«ì´_ USES «á©Ò¦C¥Xªº¼È¦s¾¹¤§­ì­È¡A³sÄò¤T­Ó PUSH «ü¥O¡C¤p¤ì°¸¤]¤£¤@¤@³æ¨B°lÂÜ¡A¹³¤U­±ª½±µ¿é¤J¡ug 17d¡v¡G

-g 17d [Enter]
AX=0001  BX=0000  CX=0083  DX=0108  SP=FFEE  BP=FFF2  SI=0000  DI=0000
DS=20E1  ES=20E1  SS=20E1  CS=20E1  IP=017D   NV UP EI PL NZ NA PO NC
20E1:017D 8BE5           MOV    SP,BP

³o®É­Ô SP «ü¦V°Ï°ìÅܼƩҦbªº³Ì§C¦ì§}¡A¤]´N¬O­ì©lµ{¦¡¤¤³Ì«á¤@­Ó°Ï°ìÅܼƪº¦ì§}¡C¦ý¦p¤µ­n±Ë±ó³o¨Ç°Ï°ìÅܼơA¦Ó BP ©Ò«ü¦ì§}«ê¦n´N¬O¤ñ°Ï°ìÅܼƳ̰ª¦ì§}¦A°ª¨â­Ó¦ì¤¸²Õªº¦ì§}¡C²ÕĶ¾¹«Ü¥©§®ªº¥Î¤W­±ªº¡uMOV SP,BP¡v«ü¥O¡A´N¯à§â°Ï°ìÅܼƥh°£¡C¥i¥H°Ñ¦Ò¥k¤W¹Ï¡A·|¤ñ¸û²M·¡¡C

-t [Enter]
AX=0001  BX=0000  CX=0083  DX=0108  SP=FFF2  BP=FFF2  SI=0000  DI=0000
DS=20E1  ES=20E1  SS=20E1  CS=20E1  IP=017F   NV UP EI PL NZ NA PO NC
20E1:017F 5D             POP    BP

±µ¤U¨Ó¬O¥Ñ°ïÅ|³»ºÝ¤§ BP ­ì­È¼u¥X¨Ó«ì´_ BP ¼È¦s¾¹­ì¥»ªº¸ê®Æ¡A¦p¤U­±°õ¦æ¥¦¡G

-t [Enter]
AX=0001  BX=0000  CX=0083  DX=0108  SP=FFF4  BP=0000  SI=0000  DI=0000
DS=20E1  ES=20E1  SS=20E1  CS=20E1  IP=0180   NV UP EI PL NZ NA PO NC
20E1:0180 C20800         RET    0008

°Æµ{¦¡ªº³Ì«á¤@­Ó«ü¥O¡ARET «ü¥O¡A¦ý¬O°£¤Fªð¦^¥Dµ{¦¡¥~¡AÁÙ±o±Ë±ó¥|­Ó°Ñ¼Æ¡A¨C­Ó°Ñ¼Æ¦³¨â­Ó¦ì¤¸²Õ¡A¦]¦¹­n¥Î¡uRET 8¡v¡A¦Ó¤£¯à¥Î¡uRET¡v³o¼Ë³æ¯Â¡C

-t [Enter]
AX=0001  BX=0000  CX=0083  DX=0108  SP=FFFE  BP=0000  SI=0000  DI=0000
DS=20E1  ES=20E1  SS=20E1  CS=20E1  IP=0133   NV UP EI PL NZ NA PO NC
20E1:0133 B409           MOV    AH,09

°õ¦æ§¹¤W­±ªº¡ut¡v«ü¥O«á¡A¬O¤£¬O SP ¼È¦s¾¹¤´«ü¦V©|¥¼°õ¦æ INVOKE «eªº°ïÅ|³»ºÝ¡A·N§Y¦ì§}¡uFFFE¡v¡C


x86 «ü¥O¡GMUL

80x86 ®a±Úªº­¼ªk«ü¥O¦³¨âºØ¡AMUL »P IMUL¡A«eªÌ¬O¥Î¨Ó­pºâµL¸¹¼Æ¡A«áªÌ«h¬O¥Î¨Ó­pºâ¦³¸¹¼Æªº¡C¦¹³B¶È¤¶²Ð MUL¡A¨ä»yªk¦p¤U¡G

MUL     ¹Bºâ¤¸

MUL «á­±±µµÛªº¹Bºâ¤¸¥i¥H¬O¤K¦ì¤¸¡B¤Q¤»¦ì¤¸¡B¤T¤Q¤G¦ì¤¸ªºÅܼƩμȦs¾¹¡A¤£¯à¬O¥ß§Y­È¡A¦Ó©Ò±oªº­¼¿nªø«×¤À§O¬O¤Q¤»¦ì¤¸¡B¤T¤Q¤G¦ì¤¸¡B¤»¤Q¥|¦ì¤¸¡C·í«ü¥O MUL ©Ò±µ¹Bºâ¤¸¬°¤K¦ì¤¸®É¡A´Nªí¥Ü¦¹¹Bºâ¬O AL ­¼¥H¦¹¤K¦ì¤¸ªº¹Bºâ¤¸¡A¦Ó©Ò±o¤§­¼¿n¦s©ó AX ¼È¦s¾¹ùØ¡A­ì¥ý AH¡BAL ¤§­È·|®ø¥¢¡F·í MUL ©Ò±µªº¹Bºâ¤¸¬O¤Q¤»¦ì¤¸®É¡A´Nªí¥Ü¬O AX ­¼¥H¦¹ 16 ¦ì¤¸ªº¹Bºâ¤¸¡A¦Ó©Ò±o¤§­¼¿n¦s©ó DX:AX ¼È¦s¾¹ùØ¡A­ì¥ý AX¡BDX ¤§­È·|®ø¥¢¡F·í MUL ©Ò±µªº¹Bºâ¤¸¬O¤T¤Q¤G¦ì¤¸®É¡A´Nªí¥Ü¬O EAX ­¼¥H¦¹¤T¤Q¤G¦ì¤¸ªº¹Bºâ¤¸¡A¦Ó©Ò±o¤§­¼¿n¦s©ó EDX:EAX ¼È¦s¾¹ùØ¡A­ì¥ý EAX¡BEDX ¤§­È·|®ø¥¢¡C¾ã²z¦p¤Uªí¡G

­¼ªk¤è¦¡³Q­¼¼Æ ­¼¼Æ­¼¿n ½d¨Ò¡]©³¤U¼Æ­È¬°¤Q¤»¶i¦ì¡^
¹Bºâ«e¹Bºâ«á
8 ¦ì¤¸AL8 ¦ì¤¸¼È¦s¾¹
©ÎÅܼÆ
AX AX=0105
BX=0106
MUL BL
AX=001E
BX=0106
16 ¦ì¤¸AX16 ¦ì¤¸¼È¦s¾¹
©ÎÅܼÆ
DX:AX DX=FFFF
AX=0200
BX=0106
MUL BX
DX=0002
AX=0C00
BX=0106
32 ¦ì¤¸
¥u¯à¥Î©ó
386µ¥¯Å¥H¤W
EAX32 ¦ì¤¸¼È¦s¾¹
©ÎÅܼÆ
EDX:EAX EDX=88887777
EAX=80000001
EBX=00000002
MUL EBX
EDX=00000001
EAX=00000002
EBX=00000002
64 ¦ì¤¸
¥u¯à¥Î©ó
x64µ¥¯Å¥H¤W
RAX64 ¦ì¤¸¼È¦s¾¹
©ÎÅܼÆ
RDX:RAXEDX=123456789ABCDEF0
RAX=8
RBX=8
MUL RBX
RDX=0
RAX=40
RBX=8

¤p¤ì°¸¨p¤U²q·Q¡A³o¼Ëªº¦w±ÆÁÙ¬O«Ü¦X²zªº¡C¥H§Ú­Ì¼ô±xªº¤Q¶i¦ì¼Æ¨Ó»¡¡A¨â¦ì¼Æ­¼¨â¦ì¼Æ³Ì¤j­È¬O¥|¦ì¼Æ¡A¨Ò¦p 99¡Ñ99¡×9801¡C¦]¦¹¤K¦ì¤G¶i¦ì¼Æ­¼¤K¦ì¤G¶i¦ì¼Æ©Ò±o¤§­¼¿n¬°¤Q¤»¦ì¤G¶i¦ì¼Æ¡A²³æªº»¡¡A¤K¦ì¤G¶i¦ì¼Æ¡A¤£´N¤K¦ì¤¸ªº¼Æ¶Ü¡H¤Q¤»¦ì¤G¶i¦ì¼Æ¤£´N¬O¤Q¤»¦ì¤¸ªº¼Æ¶Ü¡H¦]¦¹ AL¡ÑBL ©Ò±oªº­¼¿n­n©ñ¦b¤Q¤»¦ì¤¸ªº¼È¦s¾¹ùØ¡C

¨Ò¦p¡A¦pªG­n­pºâ 9 ­¼¥H 8¡A¥i¥H¥Î©³¤Uªº¤è¦¡¡G

        mov     al,9
        mov     bl,8
        mul     bl

³o¬O¦]¬° 9¡B8 ³£¤£¶W¹L 255 ( ÁÙ°O±o§a¡H¤K¦ì¤¸ªº¼È¦s¾¹©Î°O¾ÐÅé¼e«×¥i¥Hªí¥Ü 0 ¨ì 255 ½d³òªº¥¿¼Æ )¡A¦]¦¹³Q­¼¼Æ¡B­¼¼Æ³£¦s©ó¤K¦ì¤¸ªº¼È¦s¾¹ùØ¡A¦ý¬O¨Ì¾Ú 80x86 MUL »yªk¡A³Q­¼¼Æ¥u¯à¥Î AL¡A©Ò¥H²Ä¤@¦æ¬°¡umov al,9¡v¡A¦Ü©ó­¼¼Æ¡A8¡A¥i©ñ©ó¥ô¦ó¤@­Ó¤K¦ì¤¸ªº¼È¦s¾¹¡A¦p AH¡BBL¡BBH¡K¡K³£¥i¥H¡A¤]¥i¥H©ñ¦b¥H DB ©Ò©w¸qªº¤K¦ì¤¸°O¾ÐÅéÅܼÆùØ¡A³Ì«á¦A¥H«ü¥O¡umul bl¡v­pºâ¡A©Ò±o­¼¿n·|¦b AX ¼È¦s¾¹ùØ¡AAX ·|Åܦ¨ 0048h¡ABL ¤´¬O 8¡C


«á¸Ü

²Ä¤E¡B¤Q¨â³¹´£¨ì¤F°Æµ{¦¡¡A¤]Á¿¤F¤@¤j°ï¡A¤p¤ì°¸¬ðµM·Q¨ì¡ADOS ªºªA°Èµ{¦¡ INT 20H ©Î INT 21H ¤]¹³¬O°Æµ{¦¡¤@¼Ë¡A¦ý¬O¥¦­Ì¨Ã«D¨Ï¥Î°ïÅ|¶Ç»¼°Ñ¼Æ¡A¦Ó¬O§Q¥Î¼È¦s¾¹¡AµM«á©I¥s³o¨Ç°Æµ{¦¡À°§Ú­Ì§¹¦¨¯S©wªº¤u§@¡A´N¹³¦b¥»³¹½d¨Òªº seconds °Æµ{¦¡¤@¼Ë¡C¦]¦¹±q³o­Ó¨¤«×¨Ó¬Ý¡A§Ú­Ì¤]¥i¥H§â DOS ªA°Èµ{¦¡¬Ý¦¨¬O¤@ºØ°Æµ{¦¡¡C

¥t¥~ÁÙ¦³¤@¨â­Ó«ü¥O¡A¤]»P°Æµ{¦¡¦³Ãö¡A¥¦­Ì¬O ENTER »P LEAVE¡C

x86 «ü¥O¡GENTER

ENTER «ü¥O¯à²¤Æ«Ø¥ß°ïÅ|®Øªº¤u§@¡A¥¦·|§¹¦¨¥H¤U¤T¥ó¨Æ±¡¡G①§â BP ±À¤J°ïÅ|¡F②§â SP ¤§­È¦s¤J BP ùØ¡F③«O¯d°Ï°ìÅܼƪºªÅ¶¡¡C¾¨ºÞ¦p¦¹¡AENTER ¨Ã¤£·|§â°Ñ¼Æ§Yªð¦^¦ì§}±À¤J°ïÅ|¡A¤]¤£·|«O¦s¼È¦s¾¹ªº¸ê®Æ¡CENTER ªº»yªk¬O¡G

ENTER   ¹Bºâ¤¸1,¹Bºâ¤¸2

¹Bºâ¤¸1¥Nªí­n¬°°Ï°ìÅܼƫO¯d¦h¤Ö¦ì¤¸²ÕªºªÅ¶¡¡A¨Ò¦p¤W­±ªº some_of °Æµ{¦¡¨S¦³°Ï°ìÅܼơA¬G¥i¥Î¡uENTER 0,0¡v¡A³o¼Ë´N¬Û·í©ó¡G

        push    bp
        mov     bp,sp

¦A¨Ò¦p«e­±»¡ªº seconds ¦³¨â­Ó°Ï°ìÅܼơA¦]¦¹¥i¥Î¡uENTER 4,0¡v¡Aµ¥®Ä©ó¤U­±ªº«ü¥O¡G

        push    bp
        mov     bp,sp
        sub     sp,4

x86 «ü¥O¡GLEAVE

LEAVE «ü¥O¥Î¨Ó¾P·´³¡¤À°ïÅ|®Ø¡A¥¦»P ENTER ªº¹Lµ{«ê¦n¬Û¤Ï¡G①§â BP ¤§­È¦s¤J SP ùØ¡F②¦Û°ïÅ|¼u¥X¤@¼Æ­È¨ì BP¡CLEAVE ¨Ã¨S¦³°£¥h°Ï°ìÅܼƪº¥\¯à¡CLEAVE ªº»yªk¬O

LEAVE

¬Ý°_¨Ó¡A³oùؤ¶²Ðªº ENTER/LEAVE «ü¥O¥i¥H¥Î¤@±ø«ü¥O¥N´À¨â¤T±ø PUSH/POP/MOV «ü¥O¡A¦ü¥G«Ü¦³®Ä²v¡C¦ý¹ê»Ú±¡§Î¨Ã«D¦p¦¹¡ACPU ¦b°õ¦æ¹Bºâ¹Lµ{®Éªº®Ä²v¡A¤£¬O¶È¬Ý¦³´X¹D«ü¥O¦Ó¤w¡A¹ê»Ú¤W¥H»ÅºÍ 11 ¦Ó¨¥¡A ENTER ¥­§¡°õ¦æ³t²v¤ñ PUSH/MOV/ADD Á`©MÁÙ­nºC¤W 7 ­Ó®É¯ß¥ª¥k¡ALEVEL ¤]ºC¤W 3 ­Ó®É¯ß¥ª¥k¡A¦]¦¹¤@¯ë²ÕĶ¾¹©Î½sĶ¾¹«Ü¤Ö¨Ï¥Î ENTER/LEVEL «ü¥O¡C


¦^ÅU

²³æ¨Ï¥Î PROC/ENDP »P INVOKE

³o¤@³¹Á¿¤F³\¦h©I¥s°Æµ{¦¡º¾¸Hªº¹Lµ{¡A³o¨Ç³£¬O¬°¤F©I¥s¨ä¥L°ª¶¥»y¨¥©Ò¼¶¼gªº°Æµ{¦¡©Ò»Ý­nªº¡C¦pªG¤£¯à§¹¥þÁA¸Ñ³o¨Ç¹Lµ{¡A¥u·Q²³æ¨Ï¥Î INVOKE »P PROC/ENDP ªº¸Ü¡A¥u­n¿í´`¤U­±­ì«h§Y¥i¡G

  1. ¦bµ{¦¡¤@¶}©l¥Î¡u.MODEL¡v«Å§i»y¨¥Ãþ«¬¡A¦Ó¥B»y¨¥Ãþ«¬¾¨¶q¥Î STDCALL ©Î C¡A¦]¬°«eªÌ¬O¼¶¼g Windows µ{¦¡©Ò¥²¶·¿í¦uªº¡A«áªÌ¬O C »y¨¥³W©wªº¡C
  2. §â°Æµ{¦¡©ñ¦b¥Dµ{¦¡¤§«e¡A³o¼Ë´N¤£¥²¨Ï¥Î¡uPROTO¡v«Å§i°Æµ{¦¡­ì«¬¡C
  3. ¼g°Æµ{¦¡®Éª`·N¨BÆJ 3¡ã6 ¶µ¡C¥Î¡uPROC USES ¼È¦s¾¹¦Cªí parm1:WORD,parm2:WORD¡K¡K¡v«Å§i°Æµ{¦¡¡C
  4. °Ï°ìÅܼƥΡuLOCAL var1:¸ê®ÆÃþ«¬¡v«Å§i¡A¦Ó¥Bºò±µµÛ PROC ªº¤U¤@¦æ¡C¦pªG¦³¦h­Ó°Ï°ìÅܼơA¥i¥H±µ¦b¤U­±´X¦æ¡C
  5. °Æµ{¦¡µ²§ô®É¥Î¡uRET¡vªð¦^¥Dµ{¦¡¡A¥u»Ý¼g¡uRET¡v§Y¥i¤£¥²¼g¡uRET n¡v¡A¦p»Ý®ø°£°Ï°ìÅܼơA²ÕĶ¾¹¯à¦Û¦æ¨M©w¡C
  6. RET ¤U¤@¦æ´NÀ³¸Ó¬O¡uENDP¡v¡A²ÕĶ¾¹·|¦Û¦æ¦Ò¼{¨ä¥L²Ó¸`¡Aµ{¦¡³]­p®v¤£¥²¥h¾á¤ß³o­Ó°ÝÃD¡C
  7. ©I¥s°Æµ{¦¡®É¡A¥Î¡uINVOKE °Æµ{¦¡¦WºÙ,num1,num2¡K¡K¡v§Y¥i¡A¦ý­nª`·N num1 ªº¸ê®ÆÃþ«¬­n»P parm1 ¤@¼Ë¡Bnum2 ªº¸ê®ÆÃþ«¬­n»P parm2 ¤@¼Ë¡K¡K¡A§_«h·|²£¥Í¿ù»~¡C

¦pªG·Qª¾¹D§ó¦h

¦pªGÁÙ·Qª¾¹D§ó¦h¡A´N­n¤F¸Ñ°ïÅ|®Øªººc³y¡A©³¤U¬O´X­Ó­«ÂI¡G

  1. °ïÅ|®Ø¥Ñ°ª¦ì§}¦V§C¦ì§}¡AùØ­±¦s©ñµÛ°Ñ¼Æ¡Bªð¦^¦ì§}¡B­ì¨Óªº BP¡B°Ï°ìÅܼƥH¤Î­n«O¦sªº¼È¦s¾¹¡C
  2. ¦b¾ã­Ó°Æµ{¦¡°õ¦æ¹Lµ{¤¤¡ABP ¤§­È³£¤£¯à§ïÅÜ¡A§_«h·|¦s¨ú¨ì¿ù»~ªº°Ñ¼Æ©Î°Ï°ìÅܼơC
  3. ¦s¨ú°Ñ¼Æ©Î°Ï°ìÅܼƳ£¬O¥H BP ¬°·Ç¡C»y¨¥Ãþ«¬¬° C ©Î STDCALL ®É¡A²Ä¤@­Ó°Ñ¼Æ¦b¦ì§} [BP+04]¡B²Ä¤G­Ó°Ñ¼Æ¦b¦ì§} [BP+06]¡B²Ä¤T­Ó°Ñ¼Æ¦b¦ì§} [BP+08]¡K¡K
  4. ²Ä¤@­Ó°Ï°ìÅܼƦb¦ì§} [BP-02]¡B²Ä¤G­Ó°Ï°ìÅܼƦb¦ì§} [BP-04]¡B²Ä¤T­Ó°Ï°ìÅܼƦb¦ì§} [BP-06]¡K¡K
  5. ¦pªG¦b Win32 µ{¦¡³]­pùØ¡A²Ä¤@­Ó°Ï°ìÅܼƦb¦ì§} [BP-04]¡B²Ä¤G­Ó°Ï°ìÅܼƦb¦ì§} [BP-08]¡B²Ä¤T­Ó°Ï°ìÅܼƦb¦ì§} [BP-0C]¡K¡K¡F²Ä¤@­Ó°Ñ¼Æ¦b¦ì§} [BP+08]¡B²Ä¤G­Ó°Ñ¼Æ¦b¦ì§} [BP+0C]¡B²Ä¤T­Ó°Ñ¼Æ¦b¦ì§} [BP+10]¡K¡K