Ch 35 ¦³¸¹¼Æ


­ì²z

¦p¦ó©w¸q¦³¸¹¼Æ

¦b¼Æ¾Ç¤W¡A¾ã¼Æ¥i¤À¦¨¥¿¾ã¼Æ¡B­t¾ã¼Æ»P¹s¡A§Ú­Ì¥H¡y¡Ï¡z©Î¡y¡Ð¡zªí¥Ü³o¾ã¼Æ¬O¥¿¾ã¼ÆÁÙ¬O­t¾ã¼Æ¡A¦pªG¬O¥¿¼Æ¡A¤@¯ë«e­±¤£¥[¤W¥¿¸¹¡C¦Ó¦b¹q¸£¬ì¾Ç¤W¡A§Ú­Ì¥u¯à¥Î¼Æ­Ó¦ì¤¸ªºÀx¦sªÅ¶¡ ( ¤@¯ë¬O 8¡B16¡B32¡B64 ¦ì¤¸ ) ¨Óªí¥Ü³o¨Ç¾ã¼Æ¡C¤£¹L¦b¤é±`¥Í¬¡¤¤ÁÙ¬O¥H¥¿¾ã¼Æ¸û¬°±`¥Î¡A¦]¦¹¦b¹q¸£¤¤¤j³¡¤ÀÀ³¥ÎùØ¡A§Ú­ÌÁÙ¬O¼Æ¦r¬Ý¦¨¥¿¼Æ¦Ó©¿²¤¥¿­t¸¹¡AºÙ¤§¬°µL¸¹¼Æ ( unsigned numbers )¡C·íµM¤F¡A¦³®É§Ú­ÌÁÙ¬O§Æ±æ¯à°÷¶i¦æ¥¿¼Æ©Î­t¼Æªº¹Bºâ¡C»PµL¸¹¼Æ¤Ï­±ªº´N¬O¦³¸¹¼Æ ( signed numbers ) ¤F¡A©Ò¿×¦³¸¹¼Æ¤D¬O«ü¨ã¦³¥¿­t¸¹ªº¼Æ¡A§Ú­Ì¥H³Ì°ª¦ì¤¸³]©w³o­Ó¼Æ¬O¥¿¼ÆÁÙ¬O­t¼Æ¡A·í³Ì°ª¦ì¤¸¬° 0 ®É¡Aªí¥Ü¦¹¼Æ¬°¥¿¼Æ¡F·í³Ì°ª¦ì¤¸¬° 1 ®É¡Aªí¥Ü¦¹¼Æ¬°­t¼Æ¡C³o³Ì°ª¦ì¤¸ºÙ¬°²Å¸¹¦ì¤¸¡C

¦]¬°¥Î¤F¤@­Ó¦ì¤¸¨Óªí¥Ü¥¿­t¼Æ¡A©Ò¥H¦P¼Ë¬O 8 ¦ì¤¸¡AµL¸¹¼Æ¥i¥Hªí¥Ü 0¡ã255 ªº½d³ò¡A¦ý¦³¸¹¼Æ©Ò¯àªí¥Üªº¥u¦³ ¡Ð128 ¨ì ¡Ï127¡A¨ä¥Lªº½Ð°Ñ¨£¤Uªí¡G

Àx¦sªÅ¶¡µL¸¹¼Æ½d³ò¦³¸¹¼Æ½d³ò ²Õ¦X»y¨¥¤¤
©Ò¥Î¤À°tªÅ¶¡
8 ¦ì¤¸ ( ¦ì¤¸²Õ¡Abyte )0 ¨ì 255 ¡Ð128 ¨ì ¡Ï127DB ©Î BYTE
16 ¦ì¤¸ ( ¦r²Õ¡Aword )0 ¨ì 65535 ¡Ð32768 ¨ì ¡Ï32767DW ©Î WORD
32 ¦ì¤¸ ( Âù¦r²Õ¡Adouble word )0 ¨ì 4294967295 ¡Ð2147483648 ¨ì ¡Ï2147483647DD ©Î DWORD
64 ¦ì¤¸ ( ¥|¦r²Õ¡Aquad word )0 ¨ì 18446744073709551615 ¡Ð9223372036954775808 ¨ì ¡Ï9223372036954775807DQ ©Î QWORD

¦b¼Æ¾Ç¤W¡A§Ú­Ìª¾¹Dµ¥­È¤£µ¥¸¹ªº¨â¼Æ¤¬¬°¬Û¤Ï¼Æ ( ¤°»ò¡H¬Û¤Ï¼Æ¬O¤°»ò¡H¬Û¤Ï¼Æ´N¬O¨â¼Æ¤§©M¬°¹sªÌ§Y¬°¬Û¤Ï¼Æ )¡A¨Ò¦p ¡Ï1 »P ¡Ð1 ¨â¼Æ§Y¬°µ¥­È¤£µ¥¸¹¡A¦¹¤G¼Æ¬Û¥[µ²ªG¬°¹s¡C¥Ñ¦¹­ì²z¥i±Àª¾¡A°²¦p§Ú­Ì­n©w¸q­t¼Æ¡A³Ì¦n¬O²Å¦X¤W­±ªº³W«h¡C¸Õ·Q°²¦p§â 1 ¦s¤J AL ¼È¦s¾¹ùØ¡A¨º»ò AL ¥[¤W¦h¤Ö·|Åܦ¨¹s©O¡Hµª®×¬O 0FFH §a¡A¦]¦¹§Ú­Ì§â 0FFH ©w¦¨ ¡Ð1¡A¦P²z§â 0FEH ©w¦¨ ¡Ð2¡K¡K¡A¥i¥H°Ñ¦Ò¤U¹Ï¡G

¦³¸¹¼Æ»P¤G¶i¦ì¼Æ

¦b¤W¹Ï¤¤¤p¤ì°¸§â 0¡ã0FFH ³o 128 ­Ó¼Æ­È±Æ¦¨¶êÀôª¬¡A¦pªG AL ¼È¦s¾¹ªº¼Æ­È¥Ñ¹s¶}©l»¼¼W¨ì 0FFH ( ¨£¬õ¦âªº¼Æ )¡A¦b¤W¤è¥H²HÂŦ⪺¼Æ¦r§Y¬°©Ò¥NªíªºµL¸¹¼Æ¡A¦Ó¤U¤è¥H¶À¦â¼Æ¦r¬°¨ä¦³¸¹¼Æ¡C¥Ñ¹Ï¤W¥i¥H¬Ý¨ì¡A·í AL ¼È¦s¾¹ªº¼Æ­È¥Ñ 0 ¨ì 7FH ®É¡A¤£ºÞ¬OµL¸¹¼Æ©Î¦³¸¹¼Æ³£¬°¥¿¼Æ¡A§Y 0 ¨ì 127¡F¦ý¬O·í³Ì°ª¦ì¤¸¬° 1 ®É ( ¥Ñ 80H ¨ì 0FFH ®É )¡A«h¦³¸¹¼Æ»PµL¸¹¼Æ©Ò¥Nªíªº·N¸q¬O¤£¦Pªº¡C¥i¯àŪªÌ·|°Ý¡A¬J¬O¦p¦¹¡A¨º»ò¦P¼Ë¤@­Ó¤Q¤»¶i¦ì¼Æ¨ì©³¬O¥NªíµL¸¹¼ÆÁÙ¬O¦³¸¹¼Æ©O¡H¥H 0FFH ¬°¨Ò¡A³o­Ó°ÝÃD¬O»¡¡A0FFH ¨ì©³¬O¥Nªí 255 ÁÙ¬O ¡Ð1 ©O¡H¨ä¹ê³o¬O¦Ñ°ÝÃD¤F¡A¦b²Õ¦X»y¨¥ùØ¡A¼È¦s¾¹©Î°O¾ÐÅ骺¸ê®Æ¥þ¿àµ{¦¡¦p¦ó¥h¸ÑÄÀ³o¨Ç¸ê®Æ¡C

2 ªº¸É¼Æ ( two's complement )

¤°»ò¬O 2 ªº¸É¼Æ©O¡H²³æªº»¡´N¬O§â­ì¨Óªº¤G¶i¦ì¤¤ªº¦U¦ì¤¸¤ÏÂà ( ¤ÏÂà¬O«ü 1 Åܦ¨ 0¡F0 Åܦ¨ 1 )¡AµM«á¦A¥[ 1¡C¨Ò¦p 1 ªº 2 ªº¸É¼Æ¬°¦ó¡H¥i¥H³o¼Ëºâ¡G

0000 0001
¡@¡@¡õ¤ÏÂà¦U¦ì¤¸
1111 1110
¡@¡@¡õ¦A¥[ 1
1111 1111

³Ì«á±o¨ì 0FFH¡A´«¥y¸Ü»¡ 1 ©M 0FFH ¤¬¬° 2 ªº¸É¼Æ¡C¹ï·Ó¤W­±ªº¹Ï¤ù¡A·í¥iµo²{¤@­Ó¤½¦¡¡G¤¬¬° 2 ªº¸É¼Æªº¨â¼Æ«ê¦n¬°¬Û¤Ï¼Æ¡C¤W­±ªº¤èªk¬O¥H¤G¶i¦ì¨Ó«ä¦Ò¡A¦pªG¥H¤Q¤»¶i¦ì¨Ó«ä¦Ò¡A¥i¥H¥H 0FH ¥h´î±¼¨C¦ìªº¼Æ­È¡A³Ì«á¦A¥[¤@¡C¨Ò¦p­n¨D 7DH ªº¤Gªº¸É¼Æ¡A¥i¥H³o¼Ë°µ¡G

F-7=8
F-D=2

±o 82H¡A³Ì«á¦A¥[¤@¡A±o 83H¡A¬G 7DH ªº 2 ªº¸É¼Æ¬° 83H¡A¹ï·Ó¤W¹Ï¡A§A¤]¥iµo²{ 7DH(¡Ï125) »P 83H(¡Ð125) ¤]¤¬¬°¬Û¤Ï¼Æ ( §A¤]¥i¥H§ï¦¨¤G¶i¦ì¨Ì¾Ú¤ÏÂà¦U¦ì¤¸¦A¥[¤@Åçºâ¬Ý¬Ý )¡C

¦³Ãö 2 ªº¸É¼ÆÁÙ¦³¤@¶µ©w²z¡A¨º´N¬O 2 ªº¸É¼Æ¹Bºâ¨ã¦³¥i°f©Ê¡A¥ç§Y¤@¼Æ¸g¹L¨â¦¸¤Gªº¸É¼Æ¹Bºâ·|«ì´_­ì¼Æ¡C¨Ò¦p¥H¤W­± 7DH ¬°¨Ò¡A¥¦¸g¹L 2 ªº¸É¼Æ¹Bºâ«á±o 83H¡A¦A¸g¹L¤@¦¸¹Bºâ¡G

F-8=7
F-3=C

³Ì«á¦A¥[¤@Åܦ¨ 7DH¡A¬O¤£¬O¤S«ì´_­ì¼Æ¤F©O¡H¥Ñ©ó 2 ªº¸É¼Æ³o¨â­Ó©w²z¡A¨Ï±o§Ú­Ì¦b¨D¬Y¤@µL¸¹¼Æªºµ¥­È²§¸¹¼Æ®É¡A¥i¥H§Q¥Î 2 ªº¸É¼Æ¨Ó¹Bºâ¡C

¤Gªº¸É¼Æ¤]¥i¥H¥Î¥t¤@ºØ¤èªk¨D±o¡G

  1. ¥Ñ²Ä 0 ¦ì¤¸¶}©l¦V°ª¦ì¤¸Àˬd¬O§_¬°¹s¡A¦pªG¬°¹s«h¸Ó¼Æªº¤Gªº¸É¼Æ¶ñ¹s¡C
  2. ¨Ì¦¸¦V°ª¦ì¤¸ª½¨ì§ä¨ì¬Y¦ì¤¸¬°¤@®É¡A§â¸Ó¼Æªº¤Gªº¸É¼Æ¶ñ¤J¤@¡C
  3. ¨ä¾l°ª¦ì¤¸¨Ì¦¸¤ÏÂà¡C

¨Ò¦p 0011 0100 ªº¤Gªº¸É¼Æ¥i¥H³o¼Ëºâ¡G¥ý¥Ñ¼Ð¥Ü¬õ¦âªº²Ä¹s¦ì¤¸¶}©l¡A¨ä¤Gªº¸É¼Æ¬° 0¡A¦A¨Ó¬O¥Õ¦âªº¡A¨ä¤Gªº¸É¼Æ¬° 0¡A¦A¨Ó¬OÂŦ⪺¡A¤£¹L³o¦¸¬O 1¡A¦]¦¹§â¥¦¶ñ¤J 1¡A±µ¤U¨Óªº¦ì¤¸¨Ì§Ç¤ÏÂà¡A¦]¦¹³Ì«á±o¨ìªºµª®×¬O 1100 1100¡C

¤W­±¤p¤ì°¸¥H 8 ¦ì¤¸¬°¨Ò¡A»¡©ú¤F¦³¸¹¼Æªº©w¸q¥H¤Î¬°¦ó¦p¦¹©w¸q¡A¤]»¡©ú¤F¦p¦ó¨Ï¥Î 2 ªº¸É¼Æ¡C¹ï©ó 16¡B32¡B64 ¦ì¤¸ªº¾ã¼Æ¨ÓÁ¿¡A¤]¬O¤@¼Ëªº¡C

»¡¤F³o»ò¦h¤Gªº¸É¼Æªº¨Dªk§Y­ì²z¡A¨º»ò¤Gªº¸É¼Æ¨s³º¦³¦ó¥Î³~©O¡H­ì¨Ó¤Gªº¸É¼Æ°£¤F¥i¥Î¨Ó¨D¬Û¤Ï¼Æ¤§¥~ ( ©Î»¡¨D¤G¶i¦ìªº­t¼Æ )¡AÁÙ¥i¥H¥Î¦b§â´îªk·í¦¨¥[ªk¨Ó¹Bºâ¡C¨Ò¦p 100¡Ð20 ¥i¥H¬Ý¦¨ 100¡Ï(¡Ð20)¡A20 ªº¤Gªº¸É¼Æ¤£´N¬O ¡Ð20 ¶Ü¡H¦]¦¹¦b³]­p CPU ªº¹q¸ô¤W¡A¥u­n¦b CPU ùØ¥[¤W§â¼Æ­ÈÅܦ¨¤Gªº¸É¼Æªº½u¸ô¡A´N¥i¥H¤£¥Î¥h³]­p´îªkªº¹q¸ô¡C

NEG «ü¥O

NEG «ü¥O¬O 8086 «ü¥O¶°¤¤ªº¤@­Ó«ü¥O¡A³o­Ó«ü¥Oªº»yªk¬O¡G

NEG     ¥Øªº¹Bºâ¤¸

¦¹«ü¥O§â¥Øªº¹Bºâ¤¸§@ 2 ªº¸É¼Æ¹Bºâ ( §Y§â©Ò¦³¦ì¤¸¤ÏÂà¦A¥[¤@ ) ¤§«áªºµ²ªG¡A¦A¦s¦^¥Øªº¹Bºâ¤¸ùØ¡A¦]¦¹¥Øªº¹Bºâ¤¸·|³Q¯}Ãa¡C¥Øªº¹Bºâ¤¸¥i¥H¬O 8 ©Î 16 ¦ì¤¸ªº¼È¦s¾¹©ÎÅܼơA­Y¦b 80386 µ¥¯Å¥H¤Wªº CPU¡A¥Øªº¹Bºâ¤¸¥i¥H¨Ï¥Î 32 ¦ì¤¸ªº¼È¦s¾¹©ÎÅܼơC

NOT «ü¥O

NOT «ü¥O¤]¬O 8086 «ü¥O¶°¤¤ªº¤@­Ó«ü¥O¡A¨ä»yªk¬O¡G

NOT     ¥Øªº¹Bºâ¤¸

¦¹«ü¥O§â¥Øªº¹Bºâ¤¸§@ 1 ªº¸É¼Æ¹Bºâ ( §Y§â©Ò¦³¦ì¤¸¤ÏÂà ) ¤§«áªºµ²ªG¡A¦A¦s¦^¥Øªº¹Bºâ¤¸ùØ¡A¦]¦¹¥Øªº¹Bºâ¤¸·|³Q¯}Ãa¡C¥Øªº¹Bºâ¤¸¥i¥H¬O 8 ©Î 16 ¦ì¤¸ªº¼È¦s¾¹©ÎÅܼơA­Y¦b 80386 µ¥¯Å¥H¤Wªº CPU¡A¥Øªº¹Bºâ¤¸¥i¥H¨Ï¥Î 32 ¦ì¤¸ªº¼È¦s¾¹©ÎÅܼơC

¤Q¤»¶i¦ì¦³¸¹¼Æ»P¤Q¶i¦ì¼Æ

¦pªG­n§â¤Q¤»¶i¦ì¦³¸¹¼ÆÂà´«¦¨¤Q¶i¦ì¼Æªº¨BÆJ¦p¤U¡G

  1. ¦pªG¸Ó¤Q¤»¶i¦ì¦³¸¹¼Æ¬°­t¡A¨D¥X¥¦ªº 2 ªº¸É¼Æ¡F¦pªG¬°¥¿¡A«h«O¯d­ì¼Æ¤£ÅÜ¡C¸g¹L¦¹¨BÆJ©Ò±oªº¼Æ¥²¬°¥¿¼Æ¡C
  2. §â²Ä¤@¨Bªºµ²ªGÂà´«¦¨¤Q¶i¦ì¼Æ¡C
  3. ¦pªG­ì¤Q¤»¶i¦ì¦³¸¹¼Æ¬°­t¡A«h¦b«e­±¥[¤W¡§-¡¨¸¹¡A§_«h¤£¥[¥ô¦ó²Å¸¹¡C

¦pªG­n§â¤Q¶i¦ì¦³¸¹¼ÆÂà´«¦¨¤Q¤»¶i¦ì¼Æªº¨BÆJ¦p¤U¡G

  1. ¥ý§â¤Q¶i¦ì¼Æªºµ´¹ï­ÈÂàÅܦ¨¤Q¤»¶i¦ì¼Æ¡C
  2. ¦pªG­ì¤Q¶i¦ì¼Æ¬°­t¡A«h¦A¶i¦æ 2 ªº¸É¼Æ¹Bºâ¡F¦pªG­ì¤Q¶i¦ì¼Æ¬°¥¿¡A«h«O¯d­ì¼Æ¡C¸g¦¹¨BÆJ«áªºµ²ªG§Y¬°©Ò¨D¡C

«e­±´£¹L¡A§Ú­Ì¥H¼Æ­Ó¦ì¤¸¡A¥i¯à¬O¤K­Ó¡B16 ­Ó¡B32 ­Ó¬Æ¦Ü¬O 64 ­Ó¦ì¤¸¨ÓÀx¦s¾ã¼Æ¡A¦b³\¦h¹Bºâ¤¤¡A±`±`»Ý­n§â³o¨ÇÀx¦sªÅ¶¡Åܤj¡A¨Ò¦p§â¦s¦b AL ¼È¦s¾¹ùتº¥¿¾ã¼Æ©Î­t¾ã¼ÆÅܦ¨¦s¦b AX ¼È¦s¾¹ùØ¡C¦³Ãö³o¨Ç¥\¯à¡A80x86 «ü¥O¶°´£¨Ñ¤F¥|ºØ«ü¥O¡G

CBW «ü¥O

³o­Ó«ü¥Oªº»yªk¬O

        CBW

¨ä«á¨S¦³¹Bºâ¤¸¡A¦]¬°¥¦©T©w§â AL ªº²Å¸¹¦ì¤¸«þ¨©¨ì AH ¼È¦s¾¹ùØ ( §Ú­Ì¥ç¥i¥H·Q¦¨¡y²Å¸¹ÂX¥R¡z)¡C¨Ò¦p­ì¨Ó AX ¬° 1B23H¡A¸g CBW ¹Bºâ«á¡AAX Åܬ° 0023H¡F¦pªG AX ­ì¨Ó¬° 1B89H ( AL¡×¡Ð119d )¡A¸g CBW ¹Bºâ«á¡AAX Åܦ¨ FF89H ( AX¡×¡Ð119d )¡C¬G¨ä CBW ¥i¥H§â¤K¦ì¤¸ªº¼Æ­È¡AÅܦ¨¤Q¤»¦ì¤¸¥B¨ä¥¿­t¸¹¤£ÅÜ¡A­^¤å¬°¡yconvert byte to word¡z¡C

CWD «ü¥O

³o­Ó«ü¥Oªº»yªk¬O

        CWD

CWD ¬O§â AX ¼È¦s¾¹ªº²Å¸¹¦ì¤¸ÂX¥R¨ì DX¡A¥ç§Y§â¦s¦b AX ¼È¦s¾¹ªº¦³¸¹¼ÆÂX¥RÅܦ¨ DX:AX¡CCWD ªº­^¤å¬O¡yconvert word to doubleword¡z¡C

CDQ «ü¥O

³o­Ó«ü¥Oªº»yªk¬O

        CDQ

CDQ ¬O§â EAX ¼È¦s¾¹ªº²Å¸¹¦ì¤¸ÂX¥R¨ì EDX¡A¥ç§Y§â¦s¦b EAX ¼È¦s¾¹ªº¦³¸¹¼ÆÂX¥RÅܦ¨ EDX:EAX¡CCWQ ªº­^¤å¬O¡yconvert doubleword to quadword¡z¡A¥¦¥²¶·¦b 80386 µ¥¯Å©Î§ó°ª¯Åªº CPU ¤~¥i¥H¨Ï¥Î¡C

CWDE «ü¥O

³o­Ó«ü¥Oªº»yªk¬O

        CWDE

CWDE ¬O§â AX ¼È¦s¾¹ªº²Å¸¹¦ì¤¸ÂX¥R¨ì EAX¡A¥ç§Y§â¦s¦b AX ¼È¦s¾¹ªº¦³¸¹¼ÆÂX¥RÅܦ¨ EAX¡CCWDE «ü¥O»P CWD «ü¥OÂX¥Rªº²Å¸¹¦ì¤¸¬O¤£¦Pªº¡CCWDE ¥²¶·¦b 80386 µ¥¯Å©Î§ó°ª¯Åªº CPU ¤~¥i¥H¨Ï¥Î¡C


½d¨Ò¡G¨â¼Æ¬Û´î

¥ú»¡¤£½m¬O¤£¦æªº¡A©³¤U¤p¤ì°¸Á|¤@­Ó¨Ò¤l¨Ó»¡©ú¦³¸¹¼Æªº¹Bºâ¡C³o­Ó¨Ò¤l¥i¥Ñ¨Ï¥ÎªÌ¿é¤J¤Q¶i¦ìªº¨â¼Æ ( ¥i¬°¥¿©Î­t¼Æ )¡Aµ{¦¡·|­pºâ¨â¼Æ¤§®t¨ÃÅã¥Ü©ó¿Ã¹õ¤W¡Cµ{¦¡¦p¤U¡G

;SUB¡Ð¥»µ{¦¡¥iÅý¨Ï¥ÎªÌ¿é¤J¨â­Ó¼Æ¦r¡AµM«á­pºâ¦¹¤G¼Æ¤§®t¡AÅã¥Ü©ó¿Ã¹õ
        .386
;***********************************************************
code    segment use16
        assume  cs:code,ds:code
        org     100h
;-----------------------------------------------------------
start:  jmp     short begin
msg0    db      'Input minuend : $'             ;¿é¤J³Q´î¼Æ¤Î´î¼Æ¡A¥²
msg1    db      0dh,0ah,'Input subtrahend : $'  ;¶·¦b-2147483648»P
msg2    db      7,'Input error!$'               ;2147483647¤§¶¡
answer  db      0dh,0ah,40 dup (' ')
buffer  db      12,13 dup (0)
n1      dd      ?
n2      dd      ?

begin:  mov     dx,offset msg0
        call    input
        jc      error
        mov     n1,edx
        mov     di,offset answer+2
        call    hex2dec
        mov     al,'-'
        stosb

        mov     dx,offset msg1
        call    input
        jc      error
        mov     n2,edx
        call    hex2dec
        mov     al,'='
        stosb

        mov     edx,n1
        sub     edx,n2
        call    hex2dec
        mov     al,'$'
        stosb

        mov     dx,offset answer
        jmp     short ok
error:  mov     dx,offset msg2
ok:     mov     ah,9
        int     21h
        mov     ax,4c00h
        int     21h
;-----------------------------------------------------------
input   proc    near
        mov     ah,9
        int     21h
        mov     dx,offset buffer
        mov     ah,0ah
        int     21h

        sub     eax,eax
        mov     cl,'+'          ;CL°O¿ý¨Ï¥ÎªÌ¿é¤Jªº¥¿­t¸¹
        mov     edx,eax         ;EDX¬°¨Ï¥ÎªÌ¿é¤Jªº¤Q¤»¶i¦ì¦³¸¹¼Æ
        mov     si,offset buffer+2
;§â¿é¤Jªº¤Q¶i¦ì¦³¸¹¼Æ¡AÅܦ¨¤Q¤»¶i¦ì¤§µ´¹ï­È¡A¦s©óEDX
ipt0:   lodsb
        cmp     al,'-'          ;§PÂ_¨Ï¥ÎªÌ¿é¤J¥¿¼ÆÁÙ¬O­t¼Æ
        jne     ipt1
        mov     cl,al
        jmp     ipt0
ipt1:   cmp     al,'+'
        je      ipt0
        cmp     al,0dh
        je      ipt2
        cmp     al,'0'
        jb      ipt4
        cmp     al,'9'
        ja      ipt4
        sub     al,'0'
        shl     edx,1           ;³o¥|¦æ¹Bºâ¨ÏEDX­¼¥H10­¿
        mov     ebx,edx
        shl     edx,2
        add     edx,ebx
        add     edx,eax
        jmp     ipt0
;­Y¨Ï¥ÎªÌ¿é¤Jªº¬°­t¼Æ¡A«h§âµ´¹ï­ÈÅܦ¨2ªº¸É¼Æ
ipt2:   cmp     cl,'-'
        jne     ipt3
        neg     edx
ipt3:   clc
        jmp     short ipt5
ipt4:   stc
ipt5:   ret
input   endp
;-----------------------------------------------------------
hex2dec proc    near
;§âEDX¤º©Ò¦sªº¤Q¤»¶i¦ì¦³¸¹¼ÆÅܦ¨¤Q¶i¦ì¦³¸¹¼Æ¡A¦s©óDI©Ò«üªº¦ì§}
        jmp     short h2d0
tmp     db      10 dup (' ')
h2d0:   test    edx,80000000h   ;Àˬd¬O§_¬°­t¼Æ
        jz      h2d1
        mov     al,'-'          ;­Y¬°­t¼Æ¡A«h¨ú¨ä2ªº¸É¼Æ¡A¨Ã
        neg     edx             ;©óDI©Ò«ü¦ì§}¦s¤J­t¸¹
        stosb
h2d1:   push    di
        mov     ebx,10          ;¨ÏEDX¨C¦¸°£¥H10¡A©Ò±oªº¾l¼Æ
        mov     eax,edx         ;¤À§O¬°¤Q¶i¦ìªº­Ó¦ì¼Æ¡B¤Q¦ì¼Æ¡B
        mov     di,offset tmp+9 ;¦Ê¦ì¼Æ¡K¡K¡A§â³o¨Ç¾l¼Æ¼È®É¦s©ó
        mov     cx,bx           ;tmp¦r¦ê¤¤(¥Ñ°ª¦ì§}¶}©l¦s)
h2d2:   sub     edx,edx
        div     ebx
        add     dl,'0'
        mov     [di],dl
        or      eax,eax
        jz      h2d3
        dec     di
        loop    h2d2
h2d3:   mov     si,di           ;§âtmp¦r¦ê¤¤ªº¨C¤@¦ì¼Æ¡A¦s¤J
        pop     di              ;DI©Ò«ü¦ì§}
        sub     bx,cx
        mov     cx,bx
        inc     cx
        rep     movsb
        ret
hex2dec endp
;-----------------------------------------------------------
code    ends
;***********************************************************
        end     start

§â¤W­z­ì©l½X¦s©ó SUB.ASM ùØ¡AµM«á¥Î MASM 5.x ©Î MASM 6.x ²ÕĶ¡A¦pªG§A¥Î MASM 5.x ²ÕĶªº¸Ü¡A¶·¸g¹L¤T­Ó¨BÆJ¡G

E:\HOMEPAGE\SOURCE>masm sub; [Enter]
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987.  All rights reserved.

  51564 + 336180 Bytes symbol space free

      0 Warning Errors
      0 Severe  Errors

E:\HOMEPAGE\SOURCE>link sub; [Enter]

Microsoft (R) Personal Computer Linker  Version 2.40
Copyright (C) Microsoft Corp 1983, 1984, 1985.  All rights reserved.

Warning: no stack segment

E:\HOMEPAGE\SOURCE>exe2bin sub sub.com [Enter]

E:\HOMEPAGE\SOURCE>

¦pªG¨Ï¥Î MASM 6.x ²ÕĶ¡A«h¥u»Ý¤@­Ó¨BÆJ¡G

E:\HOMEPAGE\SOURCE>ml /AT sub.asm [Enter]
Microsoft (R) Macro Assembler Version 6.11
Copyright (C) Microsoft Corp 1981-1993.  All rights reserved.

 Assembling: sub.asm

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

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

E:\HOMEPAGE\SOURCE>

¤£ºÞ¥Î MASM 5.x ©Î MASM 6.x ³£À³¸Ó¯à¶¶§Q²ÕĶ¦¨¥\¡A¦pªG¦³¿ù»~¡A¥i¯à¬O§ä¤£¨ìµ{¦¡¦ì¸m¡A½Ð¥Î path «ü¥O³]¦n MASM.EXE¡BML.EXE¡BLINK.EXE µ¥µ{¦¡©Ò¦b¤l¥Ø¿ý¡C²ÕĶ¦n¤§«á¥i°õ¦æ SUB.COM

E:\HomePage\SOURCE>sub [Enter]
Input minuend : 10 [Enter]
Input subtrahend : 100 [Enter]
10-100=-90
E:\HomePage\SOURCE>

§Ú·Q©Ò¦³ªº­ì²z³£¦b¤W­±¡y­ì²z¡zªº¬q¸¨¤¤±Ô­z¹L¤F¡A¦]¦¹¤£¦A»¡©ú¤F¡A©³¤U¨Ó½Í½Í·¸¦ì ( overflow ) ªº°ÝÃD¡C


·¸¦ì

SUB.ASMµ{¦¡¦³¨â­Ó¿ù»~ªº¦a¤è¡A¨Ò¦p¡A¦pªG§A¹ï SUB §@¥H¤U¿é¤J¡G

E:\HOMEPAGE\SOURCE>sub [Enter]
Input minuend : 3 [Enter]
Input subtrahend : -2147483648 [Enter]
3--2147483648=-2147483645¡@¡@¡@¡÷²Ä¤@­Ó·¸¦ì¿ù»~ªº¨Ò¤l
E:\HOMEPAGE\SOURCE>sub [Enter]
Input minuend : 2147483648 [Enter]
Input subtrahend : 3 [Enter]
-2147483648-3=2147483645¡@¡@¡@¡÷²Ä¤G­Ó·¸¦ì¿ù»~ªº¨Ò¤l
E:\HOMEPAGE\SOURCE>

¦b¤W­±²Ä¤@­Ó¨Ò¤l¤¤¡A¥ô¦ó¾Ç¹L°ê¤@¼Æ¾Çªº¤H³£ª¾¹D 3 ´î¥h ¡Ð2147483648 ©Ò±o®tÀ³¬° 2147483651 ¤~¹ï¡A¨Ã«D ¡Ð2147483645¡C¨º»ò¬°¤°»ò·|²£¥Í¿ù»~©O¡H­ì¨Ó¥Î 32 ¦ì¤¸ªºªÅ¶¡­nªí¥Ü¦³¸¹¼Æ¡A³o¦³¸¹¼Æ¥u¯à¦b ¡Ð2147483648 ¨ì ¡Ï2147483647 ¤§¶¡¡A²{¦b©Ò±oµ²ªG¶W¹L³o½d³ò¤F¡A´N²£¥Í·¸¦ì ( overflow ) ¿ù»~¤F¡C

©Ò¿×·¸¦ì¡A¬O«ü­pºâªºµ²ªG¶W¹L¼È¦s¾¹©Î°O¾ÐÅé©Ò¯àªí¥Üªº½d³ò¡C¦b 80x86 ®a±Úªº CPU ùØ¡A¦pªG¦³·¸¦ìªº±¡§Î²£¥Í¡ACPU ·|¦Û¦æ±N·¸¦ìºX¼Ð³]¬° 1¡A§Y OV ( ½Ð°Ñ¾\ªþ¿ý¤G )¡C·|µo¥Í·¸¦ìªº±¡§Î¦³¤TºØ¡G

  1. ¨â¦P¸¹¼Æ¬Û¥[©Î¨â²§¸¹¼Æ¬Û´î¡A¥i¯à·|²£¥Í·¸¦ì¡A¨Ò¦p¡G

            mov     eax,2147483647
            add     eax,2

    ©Ò±oµ²ªGÀ³¬° 2147483649¡A¦ý¶W¹L¤F 32 ¦ì¤¸¦³¸¹¼Æªº½d³ò¡A¦]¦¹²£¥Í·¸¦ì¡C§ó¸Ô²Óªº¨Ò¤l¦b¦¹³B¡C

  2. ­¼°£¹Bºâ®É¡A©Ò±oªº¿n©Î°Ó¶W¹L¹Bºâ¤¸¦s©ñ½d³ò¡A¨Ò¦p¡G

            mev     eax,2147483640
            imul    eax,2

    ©Ò±oµ²ªGÀ³¬° 4294967280¡A¤]¶W¹L¤F ¡Ð2147483648 ¨ì ¡Ï2147483647¡A¦]¦¹¤]·|²£¥Í·¸¦ì¡C

  3. ¦b²¾¦ì©Î±ÛÂà«ü¥O®É¡A³Ì°ª¦ì¤¸­È¨ü¨ì§ó°Ê ( 0 Åܦ¨ 1 ©Î 1 Åܦ¨ 0 ) ®É¡A¨Ò¦p¡G

            mov     ax,7000h    ; AX=0111 0000 0000 0000
            shl     ax,1        

    ¹Bºâ«á¡AAX Åܦ¨ 1110 0000 0000 0000¡A¦]¦¹¤]·|¨Ï·¸¦ìºX¼Ð³]¬° OV¡C

¤F¸Ñ·¸¦ì²£¥Íªº­ì¦]«á­n¸Ñ¨M²Ä¤@­Ó°ÝÃD¡AÀ³¸Ó¬O¤£Ãøªº¡C¥u­n¦b¨â¼Æ¬Û´î¤§«á¡AÀˬd¬O§_¦³·¸¦ì§Y¥i¡C

²Ä¤G­Ó¨Ò¤lµo¥Í¿ù»~ªº­ì¦]¬O¨Ï¥ÎªÌ¿é¤Jªº¦³¸¹¼Æ¶W¹L¤F ¡Ð2147483648 ¨ì ¡Ï2147483647 ½d³ò ( ¨Ï¥ÎªÌ¿é¤J ¡Ï2147483648 )¡A¥H­P©ó¦b input °Æµ{¦¡ùØ¡A­pºâ¨Ï¥ÎªÌ©Ò¿é¤Jªº¡y2147483648¡z¦r¦ê´«¦¨¦³¸¹¼Æ®É¡A·|Åܦ¨ 80000000h¡AÅܦ¨­t¼Æ¤F¡C³o¬O¤@ºØÅÞ¿è¤Wªº¿ù»~¡A­n¸Ñ¨M³o­Ó°ÝÃD¡A¥i¥HÀˬd·í¨Ï¥ÎªÌ¿é¤J¬O¥¿¼Æ®É¡A³Ì°ª¦ì¤¸À³¬° 0¡F¤Ï¤§­Y¨Ï¥ÎªÌ¿é¤J­t¼Æ¡A³Ì°ª¦ì¤¸À³¬° 1¡C§ï¼g«áªºµ{¦¡¡ASUB1.ASM ¦p¤U¡G( ¬°¤F¸`¬Ù½g´T¡A¥u¦C¥X¤@³¡¥÷ )

¡K¡K
msg2    db      7,0dh,0ah,'Input error!$'       ;2147483647¤§¶¡
msg3    db      'ovwerflow!'
answer  db      0dh,0ah,40 dup (' ')
lenmsg3 equ     offset answer-offset msg3
buffer  db      12,13 dup (0)
¡K¡K
        mov     edx,n1
        sub     edx,n2
        jo      ovflow          ;Àˬd¨â¦³¸¹¼Æ¬Û´î¡A¬O§_·¸¦ì
        call    hex2dec         ;¨Sµo¥Í·¸¦ì¡A¦L¥Xµª®×
        jmp     short ok0

ovflow: mov     ecx,lenmsg3     ;·¸¦ìµo¥Íªí¥Üµª®×¬O¿ùªº
        mov     si,offset msg3
        rep     movsb

ok0:    mov     al,'$'
        stosb
¡K¡K
input   proc    near
        mov     ah,9
        int     21h
        mov     dx,offset buffer
        mov     ah,0ah
        int     21h

        sub     eax,eax
        mov     cl,'+'
        mov     edx,eax
        mov     si,offset buffer+2
;§â¿é¤Jªº¤Q¶i¦ì¦³¸¹¼Æ¡AÅܦ¨¤Q¤»¶i¦ì¤§µ´¹ï­È¡A¦s©óEDX
ipt0:   lodsb
        cmp     al,'-'          ;§PÂ_¨Ï¥ÎªÌ¿é¤J¥¿¼ÆÁÙ¬O­t¼Æ
        jne     ipt1
        mov     cl,al
        jmp     ipt0
ipt1:   cmp     al,'+'
        je      ipt0
        cmp     al,0dh
        je      ipt2
        cmp     al,'0'
        jb      ipt5
        cmp     al,'9'
        ja      ipt5
        sub     al,'0'
        shl     edx,1           ;³o¥|¦æ¹Bºâ¨ÏEDX­¼¥H10­¿
        mov     ebx,edx
        shl     edx,2
        add     edx,ebx
        add     edx,eax
        jmp     ipt0
;­Y¨Ï¥ÎªÌ¿é¤Jªº¬°­t¼Æ¡A«h§âµ´¹ï­ÈÅܦ¨2ªº¸É¼Æ
ipt2:   cmp     cl,'-'
        jne     ipt3
        neg     edx
        test    edx,80000000h   ;Àˬd¨Ï¥ÎªÌ¿é¤J­t¼Æ®É¡A³Ì°ª¦ì¤¸
        jz      ipt5            ;¬O§_¬°1
        jmp     short ipt4
ipt3:   test    edx,80000000h   ;Àˬd¨Ï¥ÎªÌ¿é¤J¥¿¼Æ®É¡A³Ì°ª¦ì¤¸
        jnz     ipt5            ;¬O§_¬°0
ipt4:   clc
        jmp     short ipt6
ipt5:   stc
ipt6:   ret
input   endp
¡K¡K

¥|«h¹Bºâªº·¸¦ì

ADD «ü¥O»P·¸¦ì

CPU ¬O¦p¦óª¾¹D¹Bºâµ²ªG¬O§_·¸¦ì©O¡H¤p¤ì°¸·Q¥H¤U­±¨Ò¤l¨Ó»¡©ú¡G

   0111 1111 ¡× 07FH ¡× 127D
¡Ï 0000 0010 ¡× 002H ¡×   2D
-------------------------------------------
   1000 0001 ¡× 081H ¡× 129D(µL¸¹¼Æ) ©Î ¡Ð127D(¦³¸¹¼Æ)

¤W­±¬O¤K¦ì¤¸ªº¦³¸¹¼Æ¥[ªk¡A³o­Ó¥[ªk¹Bºâ«á©Ò±o¤§©M¶W¹L ¡Ð128 ¨ì ¡Ï127¡A¦]¦¹·¸¦ìºX¼Ð·|³Q³]©w¡CÅý§Ú­Ì¨Ó±´°Q³o¹Bºâªº¹Lµ{¡C¤G¶i¦ìªº¥[ªk¹Bºâ¤@¦p¤Q¶i¦ì¡A¦^·Q¤Q¶i¦ì¥[ªk¡A¦pªG­Ó¦ì¬Û¥[µ²ªG¤p©ó©Îµ¥©ó 9¡A«h¤£¶i¦ì¡F¤Ï¤§¶W¹L 9¡A«h¶È¶È¼g¥X©Mªº­Ó¦ì¼Æ¦Ó¶i¦ì¡C¨Ò¦p¡G

   24
¡Ï 39
------
   63

4 ¥[ 9 ±o 13¡A¼g 3 ¶i¤@¡C¤G¶i¦ì¥[ªk¤]¬O¦p¦¹¡A¤W­±²Ä 0 ¦ì¤¸¬Û¥[¡A±o 1 ( ¦p¶À¦â¦r )¡A²Ä 1 ¦ì¤¸¬Û¥[¶W¹L 1¡A¼g¤W­Ó¦ì¼Æ ( ¦p¬õ¦â¦r ) ¥B¶i¦ì¡A²Ä¤T¦ì¤¸¬Û¥[¦A¥[¤W¶i¦ìªº¤@¡A©Ò±oÁÙ¬O¶W¹L 1¡A¬G¼g¤W 0 ( ÂŦâ¦r ) ¥B¶i¦ì¡A¦p¦¹¬Û¥[¤@ª½¨ì²Ä 7 ¦ì¤¸µ²§ô¬°¤î¡C¥H¤W¬O ADD «ü¥Oªº¹B§@¡A¦ý¬O¦pªG¬O§â³o¬Û¥[ªº¨â¼Æ¬Ý¦¨¦³¸¹¼Æ¡A¦³¸¹¼Æªº²Ä¤C¦ì¤¸¨ä¹ê¶È¥Nªí¥¿­t¸¹¡A·Ó²z»¡¤£À³¸Ó§â²Ä¤C¦ì¤¸°µ¥[ªk¹Bºâ¡A¤£¹L ADD «ü¥O¥i¤£ºÞ¨º»ò¦h¡A¤£ºÞ¦³¸¹©ÎµL¸¹¼Æ³£·Ó¼Ë¹Bºâ¡A¦Ó¥B ADD «ü¥O¤£ºÞ¬O¦³¸¹¼ÆÁÙ¬OµL¸¹¼Æ³£¥i¥Hºâ¥X©M¨Ó¡C¦]¦¹±¡§Î½ÆÂø±o¦h¡A¦Ò¼{©Ò¦³¹Bºâªº±¡§Î¥i¤À¦¨¤U¦C¤TºØ±¡§Î¡G

  1. °²¦p¥¿¼Æ»P¥¿¼Æ¬Û¥[¡G³o®É¨â¼Æªº²Ä¤C¦ì¤¸³£¬°¹s¡A¥B©Ò±o¤§©MÀ³¬°¥¿¼Æ ( ¤]´N¬O©Mªº²Ä¤C¦ì¤¸À³¬° 0 )¡A¥i¥H¤À¦¨¨âºØ±¡§Î¨Ó°Q½×¡C²Ä¤@ºØ±¡§Î¬O¨â¼Æ¬Û¥[®É¡A²Ä¤»¦ì¤¸¤£µo¥Í¶i¦ì ( ÂŦ⪺ 1 )¡A¹³¤U¹Ï¥ª¦¡¡A¤S¦]¬°¥»¬O¨â¥¿¼Æ¬Û¥[¡A©Ò¥H²Ä¤C¦ì¤¸¬Û¥[¤´¬°¹s ( ¶À¦âªº 0)¡A³oºØ±¡§Î¬Û¥[¤£·|¦³·¸¦ìµo¥Í¡C

    ²Ä¤GºØ±¡§Î¬O²Ä¤»¦ì¤¸¬Û¥[µo¥Í¶i¦ì¨ì²Ä¤C¦ì¤¸¡A¹³¤W¹Ï¥k¦¡¡A·Ó²z»¡²Ä¤C¦ì¤¸¬O²Å¸¹¦ì¤¸À³¤£°Ñ»P¹Bºâ¡A©Î¤S¦]¬°¬O¨â¥¿¼Æ¬Û¥[¡A©Mªº²Ä¤C¦ì¤¸À³¬° 0¡A¦ý¦]²Ä¤»¦ì¤¸¶i¦ìµ²ªG©MÅܦ¨ 1¡A©MÅܬ°­t¼Æ¡A³o¼Ë´N·|µo¥Í·¸¦ì¡C

  2. °²¦p­t¼Æ»P­t¼Æ¬Û¥[¡G³o®É¨â¼Æªº²Ä¤C¦ì¤¸³£¬°¤@¡A¥B©Ò±o¤§©MÀ³¬°­t¼Æ ( ¤]´N¬O©Mªº²Ä¤C¦ì¤¸À³¬° 1 )¡A¤]¥i¤À¬°¨âºØ±¡§Î¨Ó°Q½×¡C²Ä¤@ºØ±¡§Î¬O²Ä¤»¦ì¤¸¬Û¥[¤£µo¥Í¶i¦ì¡A¹³¤U¹Ï¥ª¦¡¡A¨º»ò²Ä¤C¦ì¤¸¬Û¥[µ²ªG²Ä¤C¦ì¤¸¬°¹s¡A¥B·|µo¥Í¶i¦ì¡A³oºØ±¡§Î¬O·|µo¥Í·¸¦ì¡C

    ²Ä¤GºØ±¡§Î¬O²Ä¤»¦ì¤¸¬Û¥[µo¥Í¶i¦ì¡A¹³¤W¹Ï¥k¦¡¡A¤S¦]¬°²Ä¤C¦ì¤¸¬O¨â­Ó¤@¬Û¥[¦A¥[¤W¶i¦ì¡Aµ²ªG²Ä¤C¦ì¤¸¤´¬O¤@¡A¥B²Ä¤C¦ì¤¸¤]µo¥Í¶i¦ì¡A³oºØ±¡§Î¤£·|µo¥Í·¸¦ì¡C

  3. °²¦p¥¿¼Æ»P­t¼Æ¬Û¥[¡G¦]¬°¥¿­t¼Æ·|¬Û®ø¡A©Ò¥H¤£ºÞ¥¿¼Æµ´¹ï­È¸û¤jÁÙ¬O­t¼Æµ´¹ï­È¸û¤j¡A³£¤£·|¦³·¸¦ìªº±¡§Îµo¥Í¡C³o®É¤]¤À¦¨¨âºØ±¡§Î¡C²Ä¤@ºØ±¡§Î¬O²Ä¤»¦ì¤¸¬Û¥[¤£µo¥Í¶i¦ì¡A¹³¤U¹Ï¥ª¦¡¡A³o®É²Ä¤C¦ì¤¸¬O¤@»P¹s¬Û¥[¤]¤£·|¶i¦ì¡C

    ²Ä¤GºØ±¡§Î¬O²Ä¤»¦ì¤¸¬Û¥[µo¥Í¶i¦ì¡A¹³¤U¹Ï¥k¦¡¡A³o®É²Ä¤C¦ì¤¸¬O¤@»P¹s¬Û¥[¦A¥[¤W²Ä¤»¦ì¤¸¶i¦ìªº¤@¡Aµ²ªG²Ä¤C¦ì¤¸¬Û¥[«á·|µo¥Í¶i¦ì¡C

¾ã²z¤W­±©Ò¦³¦³¸¹¼Æ¬Û¥[ªº¤»ºØ±¡§Î¡A±z·í¥iµo²{¡A¦pªG¬O²Ä¤»¦ì¤¸¬Û¥[¤§µ²ªG»P²Ä¤C¦ì¤¸¬Û¥[¤§µ²ªG¡A¦P®É¶i¦ì©Î¦P®É¤£¶i¦ì¡A´N¤£·|µo¥Í·¸¦ì¡F¤Ï¤§¦pªG¤@¬°¶i¦ì¡A¤@¬°¤£¶i¦ì¡A´N·|µo¥Í·¸¦ì¡CCPU ¥i¥HÀË´ú²Ä¤»¦ì¤¸¬Û¥[¤§µ²ªG»P²Ä¤C¦ì¤¸¬Û¥[¤§µ²ªG¬O§_¦P®É¶i¦ì©Î¦P®É¤£¶i¦ì¡A¥Î³N»y¨Ó»¡¡A´N¬O¨Ï³o¨â­Ó¶i¦ì­È§@ XOR ¹Bºâ¡A´N¥i§PÂ_¬O§_·¸¦ì¤F¡C

SUB «ü¥O»P·¸¦ì

´îªk¡ASUB¡A¥i¥H¬Ý¦¨¬O³Q´î¼Æ»P´î¼Æªº 2 ªº¸É¼Æ¤§©M¡A½Ð¬Ý¤U­±´X­Ó¨Ò¤l¡G

¡@¡@¡@100¡Ð20¡×100¡Ï(¡Ð20)
¡@¡@¡Ð100¡Ð20¡×¡Ð100¡Ï(¡Ð20)
¡@100¡Ð(¡Ð20)¡×100¡Ï20
¡Ð100¡Ð(¡Ð20)¡×¡Ð100¡Ï20

¤W­±¥|­Ó¦¡¤l¤¤¡A«e¨â­Ó¦¡¤lªº´î¼Æ¬O 20¡A¨ä 2 ªº¸É¼Æ¬° ¡Ð20¡A¦]¦¹¥i¬Ý¦¨¬O³Q´î¼Æ»P´î¼Æªº 2 ªº¸É¼Æ¤§©M¡C¦Ó³Ì«á¨â­Óºâ¦¡¤¤ªº´î¼Æ¡A¡Ð20¡Aªº 2 ªº¸É¼Æ¬O 20¡A¤]¥i¥H¬Ý¦¨¬O³Q´î¼Æ»P´î¼Æªº 2 ªº¸É¼Æ¤§©M¡C¦]¦¹¦b´îªk¤¤¡A·¸¦ìºX¼Ðªº³]©w¥[ªk¬O¤@¼Ëªº¡C

MUL¡BIMUL «ü¥O»P·¸¦ì

MUL «ü¥O¥u¥Î©óµL¸¹¼Æ¤§¹Bºâ¡A¦]¦¹°õ¦æ§¹ MUL «áªº·¸¦ìºX¼Ð¤£­«­n±`©¿²¤¡A¨ä»yªk¬°¡G

        MUL     ¨Ó·½¹Bºâ¤¸

¨Ó·½¹Bºâ¤¸¬O­¼¼Æ¡A¥¦¥i¥H¬O 8¡B16 ©Î 32 ¦ì¤¸ªº¼È¦s¾¹©Î¦s©ó 8¡B16 ©Î 32 ¦ì¤¸°O¾ÐÅ骺ÅܼơA¦Ó³Q­¼¼Æ«h¬O©M­¼¼Æ¤@¼Ëªø«×ªº AL¡BAX¡BEAX¡A80x86 ·|§â³Q­¼¼Æ»P­¼¼Æ¬Û­¼µM«á§â­¼¿n¦s©ó AX¡BDX:AX¡BEDX:EAX ùØ¡C¦p¤Uªí¡G

­¼ªk¤è¦¡³Q­¼¼Æ ­¼¼Æ­¼¿n ½d¨Ò
¹Bºâ«e¹Bºâ«á
8 ¦ì¤¸AL8 ¦ì¤¸¼È¦s¾¹
©ÎÅܼÆ
AX AX=0105
BX=0106
MUL BL
AX=001E
BX=0106
16 ¦ì¤¸AX16 ¦ì¤¸¼È¦s¾¹
©ÎÅܼÆ
DX:AX AX=0200¡ADX=FFFF
BX=0106
MUL BX
DX=0002¡AAX=0C00
BX=0106
32 ¦ì¤¸
¥u¯à¥Î©ó
386µ¥¯Å¥H¤W
EAX32 ¦ì¤¸¼È¦s¾¹
©ÎÅܼÆ
EDX:EAX EAX=80000001
EDX=88887777
EBX=00000002
MUL EBX
EDX=00000001
EAX=00000002
EBX=00000002

¨Ò¦p©³¤U 32 ¦ì¤¸­¼ªk¡G

        mov     eax,80000001h
        mov     edx,88887777h
        mov     ebx,00000002h
        mul     ebx

°õ¦æ§¹¡ymul ebx¡z«á¡AEDX ¬° 1¡AEAX ¬° 00000002¡A¬G©Ò±o­¼¿n¬° 100000002H¡A­ì¨Óªº EDX ­È·|®ø¥¢¤£¨£¡C¥Ñ¦¹¬Ý¨Ó MUL ¶È¶È°µ EAX »P EBX ¤§­¼¿n¡A¥B§â EAX ·í°µ¥¿¼Æ¡CMUL ¹Bºâ§¹«á¦pªG­¼¿nªº°ª¦ì¤¸³¡¥÷¤£¬°¹s¡A·¸¦ìºX¼Ð»P¶i¦ìºX¼Ð§¡·|³Q³]¬°¤@¡F¤Ï¤§¡A¦pªG­¼¿nªº°ª¦ì¤¸³¡¥÷¬°¹s¡A·¸¦ìºX¼Ð»P¶i¦ìºX¼Ð§¡·|³Q³]¬°¹s¡C¨Ò¦p¤W­±ªº¨Ò¤l¡A¦]¬° EDX ¤£¬°¹s¡A¬G·¸¦ìºX¼Ð·|³Q³]©w¡C

¦p­n­pºâ¦³¸¹¼Æªº­¼ªk¡A¥²¶·¥Î IMUL «ü¥O¡AIMUL ªº»yªk»P MUL ¬Û¦P¡A¦ý¬O¥¦·|§â³Q­¼¼Æªº²Å¸¹«O¯d¨ì­¼¿nùØ¡C¨Ò¦p¤W­±ªº¨Ò¤l¡A§â¡yMUL EBX¡z§ï¦¨¡yIMUL EBX¡z¡A©Ò±oµ²ªG¡AEDX:EAX ¬° FFFFFFFF:00000002¡C

IMUL ªº»yªk¦³¤TºØ§Î¦¡¡A¥i¥H¥u±µ¤@­Ó¹Bºâ¤¸¡A©Î¨â­Ó¡B©Î¤T­Ó¹Bºâ¤¸¡G

  1. IMUL    ¼È¦s¾¹©Î°O¾ÐÅéÅܼÆ
  2. IMUL    16¦ì¤¸¼È¦s¾¹,16¦ì¤¸¼È¦s¾¹©ÎÅܼÆ
    IMUL    32¦ì¤¸¼È¦s¾¹,32¦ì¤¸¼È¦s¾¹©ÎÅܼÆ
    IMUL    16¦ì¤¸¼È¦s¾¹,8©Î16¦ì¤¸±`¼Æ
    IMUL    32¦ì¤¸¼È¦s¾¹,8©Î32¦ì¤¸±`¼Æ
  3. IMUL    16¦ì¤¸¼È¦s¾¹,16¦ì¤¸¼È¦s¾¹©ÎÅܼÆ,8©Î16¦ì¤¸±`¼Æ
    IMUL    32¦ì¤¸¼È¦s¾¹,32¦ì¤¸¼È¦s¾¹©ÎÅܼÆ,8©Î32¦ì¤¸±`¼Æ

²Ä¤@ºØ»yªkªº³W«h»P MUL ¬Û¦P¡A´N¤£ÂØ­z¡C²Ä¤GºØ»yªk IMUL «á­±¨â­Ó¹Bºâ¤¸¡A²Ä¤@­Ó¹Bºâ¤¸ºÙ¬°¥Øªº¹Bºâ¤¸¡A²Ä¤G­ÓºÙ¬°¨Ó·½¹Bºâ¤¸¡AIMUL ·|§â¥Øªº¹Bºâ¤¸·í¦¨³Q­¼¼Æ¡A¨Ó·½¹Bºâ¤¸·í¦¨­¼¼Æ¡A¬Û­¼¤§«áªº­¼¿n¦s©ó¥Øªº¹Bºâ¤¸¡C²Ä¤TºØ»yªk«h¬O¦b IMUL ¤§«á±µ¤T­Ó¹Bºâ¤¸¡A²Ä¤@­Ó¹Bºâ¤¸¬O¥Øªº¹Bºâ¤¸¡A²Ä¤G¡B¤T­Ó¹Bºâ¤¸¬O¨Ó·½ºâºâ¤¸¡AIMUL ¬O§â²Ä¤G­Ó¹Bºâ¤¸·í¦¨³Q­¼¼Æ¡A²Ä¤T­Ó¹Bºâ¤¸·í¦¨­¼¼Æ¡A¬Û­¼¤§«áªº­¼¿n¦s©ó²Ä¤@­Ó¹Bºâ¤¸¡A¨â­Ó¨Ó·½¹Bºâ¤¸¤£·|³Q¯}Ãa ( ²Ä¤T­Ó¹Bºâ¤¸¥²©w¬O±`¼Æ¡A·íµM¤£·|§ïÅÜ )¡C

IMUL ³B²z·¸¦ìºX¼Ðªº¤è¦¡¬O§â¦³¸¹¼Æ­¼¿n¦s©ó¤@¼È®ÉÅܼÆùØ¡AµM«á§â¦¹¼È®ÉÅܼƪº¸û§C¦ì¤¸¦s©ó¥Øªº¹Bºâ¤¸ªº§C¦ì¤¸ ( ¦¹³B¸û§C¦ì¤¸«üªº¬O­¼¿n©Ò»Ý¦ì¤¸¤§¤@¥b¡A¨Ò¦p­¼¼Æ¬O 8 ¦ì¤¸¡A«h³Q­¼¼Æ¤]¬O 8 ¦ì¤¸¡A¬G­¼¿nÀ³¬° 16 ¦ì¤¸¡A¸û§C¦ì¤¸§Y­¼¿n¤§¸û§Cªº 8 ¦ì¤¸ )¡A­Y¦¹¸û§C¦ì¤¸ªº¥Øªº¹Bºâ¤¸ªº²Å¸¹ÂX¥R¤§«á§Y¬°¼È®ÉÅܼơA«h¶i¦ìºX¼Ð»P·¸¦ìºX¼Ð·|³Q²M°£¡F¤Ï¤§¡A¶i¦ìºX¼Ð»P·¸¦ìºX¼Ð·|³Q³]©w¡C«Ý§¹¦¨ºX¼Ð³]©w«á¡A¦A§â¥¿½T­¼¿n¦s©ó¥Øªº¹Bºâ¤¸¡C¨Ò¦p¡G

        mov     al,30h
        mov     bl,4h
        imul    bl

¹Bºâ«á¤§­¼¿n¬° 00C0h¡A¦s©ó¼È®ÉÅܼÆùØ¡A¸û§C¦ì¤¸ C0h ¦s©ó AL ÂX¥R²Å¸¹«á¬° FFC0h ( ¦] AL ªº²Ä¤C¦ì¤¸¬° 1¡AÂX¥R²Å¸¹¤§«á AH ©Ò¦³¦ì¤¸§¡¬°¤@ )¡A»P 00C0h ¤£¦P¡A¬G·¸¦ìºX¼Ð»P¶i¦ìºX¼Ð³£³Q³]©w¡C¦A¬Ý¥t¤@¨Ò¤l¡G

        mov     eax,30h
        mov     ebx,4h
        imul    ebx

¹Bºâ«á¤§­¼¿n¬° 000000C0h¡A¦s©ó¼È®ÉÅܼÆùØ¡A¸û§C¦ì¤¸ 00C0h ¦s©ó AX ÂX¥R²Å¸¹«á¬° 000000C0h¡A»P¼È®ÉÅܼƬۦP¡A¬G·¸¦ìºX¼Ð»P¶i¦ìºX¼Ð³£·|³Q²M°£¡C¦A¬Ý¥t¤@¨Ò¤l¡G

        mov     eax,0FFFFFFFFh
        mov     ebx,4h
        imul    ebx

¹Bºâ«á¤§­¼¿n¬° FFFFFFFCh¡A¦s©ó¼È®ÉÅܼÆùØ¡A¸û§C¦ì¤¸ FFFCh ¦s©ó AX ÂX¥R²Å¸¹«á¬° FFFFFFFCh¡A»P¼È®ÉÅܼƬۦP¡A¬G·¸¦ìºX¼Ð»P¶i¦ìºX¼Ð³£·|³Q²M°£¡C¦A¬Ý¥t¤@¨Ò¤l¡G

        mov     al,30h
        mov     bl,-4h
        imul    bl

¹Bºâ«á¤§­¼¿n¬° FF40h¡A¦s©ó¼È®ÉÅܼÆùØ¡A¸û§C¦ì¤¸ 40h ¦s©ó AL ÂX¥R²Å¸¹«á¬° 0040h ( ¦] AL ªº²Ä¤C¦ì¤¸¬° 0¡AÂX¥R²Å¸¹¤§«á AH ©Ò¦³¦ì¤¸§¡¬° 0 )¡A»P FF40h ¤£¦P¡A¬G·¸¦ìºX¼Ð»P¶i¦ìºX¼Ð³£³Q³]©w¡C

DIV¡BIDIV «ü¥O»P·¸¦ì

DIV ªº»yªk¦b²Ä¤C³¹¤w¸g´£¹L¡A¦¹³B¤£¦AÂØ­z¡A³oùبӻ¡»¡·¸¦ìªº±¡§Î¡C·í DIV ¹Bºâ®É¡A¥i¯à·|µo¥Í¨âºØ¿ù»~¡A¤@ºØ¬O°£¥H¹sªº¿ù»~¡F¤@ºØ°Ó¼Æ¤Ó¤j¡A¥H­P©óµLªk¦s¤J¥Øªº¹Bºâ¤¸ùØ¡C²Ä¤@ºØ±¡§ÎÅã¦Ó©ö¨£¡A¦b¼Æ¾Ç¤W°£¥H¹s¬OµL·N¸qªº¡A¦]¦¹³oºØ±¡§Î·|³y¦¨¿ù»~µo¥Í¡C²Ä¤GºØ±¡§Î¬O°Ó¼Æ¤Ó¤j¡A¨Ò¦p¤U­±ªº¨Ò¤l¡G

        mov     ax,1000h
        mov     bl,1
        div     bl

¦b¼Æ¾Ç¤WÁöµM³Ì«á°Ó¼ÆÀ³¸Ó¬O 1000H¡A¦ý¬O¹ï©ó 80x86 ®a±Úªº CPU ¦Ó¨¥¡A³o¬O 8 ¦ì¤¸ªº°£ªk¡A°Ó¼Æ¥u¯à©ñ¦b AL ¼È¦s¾¹ùØ¡AÅãµMªÅ¶¡¬O¤£°÷ªº¡A©Ò¥H·|³y¦¨·¸¦ì¡C¹ï©ó³o¨âºØ¿ù»~¡A80x86 ³£·|²£¥Í INT 0 ªº¿ù»~¡AINT 0 §Y²Ä¹s¸¹¤¤Â_¡A¥¦·|¦b¿Ã¹õ¤W¦L¥X¡yDivide overflow¡z¡AµM«á´Nµ²§ôµ{¦¡¡A¦^¨ì¨t²Î¡C¦p¦óÁקK³o¨âºØ¿ù»~©O¡H¹ï©óÁקK²£¥Í°£¥H¹sªº¿ù»~¡A¥u­n¦b°µ°£ªk¤§«eÀˬd°£¼Æ¬O§_¬°¹s§Y¥i¡A¨Ò¦p¡G

        cmp     bl,0
        jz      no_divide
        div     bl
        ¡K¡K¡K
no_divide:

¹ï©ó¦p¦óÁקK²Ä¤GºØ¿ù»~¡A¦³¤@ºØ¸û¬°®ø·¥ªº¤èªk¡A´N¬O§â°£ªk§ï¦¨¸û¦h¦ì¤¸ªº¹Bºâ¨Ò¦p 32 ¦ì¤¸¡FÁÙ¦³¤@ºØ¤èªk¡A´N¬O­×§ï¤¤Â_¡A¤p¤ì°¸¥´ºâ¤U¤@³¹¤¶²Ð¡C

IDIV «ü¥Oªº»yªk»P DIV ¬Û¦P¡A©Ò¤£¦Pªº¦a¤è¦b©ó IDIV ¬O¶i¦æ¦³¸¹¼Æªº°£ªk¡A¬J¬O¶i¦æ¦³¸¹¼Æ°£ªk¡A´N±o¦b·N³Q°£¼Æ»P°£¼Æªº²Å¸¹¡A¦³®É¥i¥H¥Î CBW¡BCWD¡BCDQ µ¥«ü¥O¡A«Ü§Öªº§â AL¡BAX¡BEAX ªº²Å¸¹¦ì¤¸ÂX¥R¨ì AX¡BDX:AX¡BEDX:EAX¡A¥H¶i¦æ IDIV ¹Bºâ¡CIDIV ©M DIV ¤@¼Ë¡A¤]¥i¯à·|µo¥Í°£¥H¹s¤Î©Ò±o°Ó¤Ó¤jªº°ÝÃD¡A¸Ñ¨M¤èªk¤]Ãþ¦ü¡C¥t¥~¡AIDIV »P DIV ³£¤£·|¼vÅT¶i¦ìºX¼Ð»P·¸¦ìºX¼Ð¡C


¦^¨ì­º­¶¡A ¨ì²Ä¤T¤Q¥|³¹¡A¨ì²Ä¤T¤Q¤»³¹