¤W¤@³¹¡A¤p¤ì°¸½Í¤F¦³¸¹¼Æªº¥[´îªk¡A³o¤@³¹nÄ~Äò½Í¦³¸¹¼Æªº¼°£ªk¡C
x86 «ü¥O¶°ªº¼ªk«ü¥O¦³¤TÓ¡AMUL¡BIMUL¡BAAM¡A¤À§O¥Î©óµL¸¹¼Æ¼ªk¡B¦³¸¹¼Æ¼ªk¡B¼ªk¹Bºâ«á½Õ¾ã¦¨ ASCII ½X¡A³o¤@³¹±N¶°¤¤¦b«e¨âÓ¼ªk«ü¥O¡C
MUL ¬O¥Î¨ÓpºâµL¸¹¼Æ¼ªkªº«ü¥O¡A¥¦ªº»yªk¨Ì¨ä¦ì¤¸¤£¦P¡A¤À¦¨¥|ºØ¡G
MUL r/m8 MUL r/m16 MUL r/m32 MUL r/m64
©³¤U¬O³o¥|ºØªº»¡©ú¡G
MUL ¬Ý°_¨Ó«Ü½ÆÂø¡A¦ý¥un§â´¤¦í³Q¼¼Æ³£¬O²Ö¥[¾¹ ( ²Ö¥[¾¹´N¬O AL¡BAX¡BEAX ©Î RAX )¡A¼¼Æ»P³Q¼¼Æªø«×¤@¼Ë¡A¥ç§Y¥¦Ì¨ã¦³¬Û¦Pªº¦ì¤¸¼Æ¡Fpºâ«á¡A¼¿nªº¦ì¤¸¼Æ¬O³Q¼¼Æªº¨â¿¡A¼¿n³£©ñ¦b¸ê®Æ¼È¦s¾¹:²Ö¥[¾¹ùر¡C¸ê®Æ¼È¦s¾¹¬O«ü DX¡BEDX¡BRDX¡C
¦^·Q¤p¾Ç®É¡A¥Î¤Q¶i¦ìpºâ¼ªk¤]¬O³o¼Ë¡A¨Ò¦p 90¡Ñ90¡×8100¡A¬O¤£¬O¨â¦ì¼Æ¦¨¨â¦ì¼Æ±o¨ì¥|¦ì¼Æ¡C¦]¦¹·í^¯Sº¸³]p x86 CPU ®É¡A¤~·|³o¼Ë°µ¡C¤]¦]¬°¼¿nªº¦ì¤¸Ó¼Æ¬O³Q¼¼Æ¦ì¤¸¼Æªº¨â¿¡A©Ò¥H¤£·|¦³¼¿n¶W¹L¦Ó®e¯Ç¤£¤Uªº°ÝÃD¡A¦]¦¹ MUL «ü¥Oªº¶i¦ì»P·¸¦ì©M¥[´îªk¤£¦P¡C¦pªG¼¿n¸û°ªªº¥b¼ÆӦ줸¤£¬°¹s¡A¶i¦ìºX¼Ð¤Î·¸¦ìºX¼Ð³£·|³Q³]©w¡F¤Ï¹L¨Ó«h³Q²M°£¡C©³¤U§Ų́ӬݨâÓ MUL ªº¨Ò¤l¡G
mov al,8 °õ¦æ«á¡AAX Åܬ° 28h¡A¨ä¾l¼È¦s¼Æ³£¤£ÅÜ¡A¦¹®É¼¿n¸û°ªªº¤KӦ줸¡A§Y AH ¬°¹s¡A¬G¶i¦ìºX¼Ð¡B·¸¦ìºX¼Ð³£·|³Q²M°£ ( NC¡BNV )¡Cmov bl,5 mul bl |
seconds dw 60*60 ;¤@¤p®É¦³3600¬í °õ¦æ«á¡AAX¡×5180h¡ADX¡×1¡ADX:AX¡×15180h¡×86400d¡A¦]¬°¼¿n¸û°ªªº 16 ¦ì¤¸¡A§Y DX ¤£¬°¹s¡A¬G¶i¦ìºX¼Ð¡B·¸¦ìºX¼Ð³£·|³Q³]©w ( CY¡BOV )¡Cmov ax,24 ;¤@¤Ñ24¤p®É mul seconds |
³Ì«áȱo¤@´£ªº¬O¡uMUL r/m64¡v¬O¤»¤Q¥|¦ì¤¸ªº¼ªk¡A¥u¯à¥Î¦b 64 ¦ì¤¸ªº¼Ò¦¡¤U¡A¦b DOS ¯u¹ê¼Ò¦¡¤£¯à¨Ï¥Î¡A¦Ó¥B¥Î ML.EXE ¤]µLªk²ÕĶ¡A¥²¶·¨Ï¥Î ML64.EXE ¤~¯à²ÕĶ¡C
IMUL ¬O¥Î¨Ópºâ¦³¸¹¼Æ¼ªkªº«ü¥O¡A¨Ì¾Ú¹Bºâ¤¸ªº¦h¹è¡A¤À¬°¤TºØ¡G
¥u¦³¤@Ó¹Bºâ¤¸ªº IMUL »P MUL ¤@¼Ë¡A¨ä»yªk¬°¡G
IMUL r/m8 IMUL r/m16 IMUL r/m32 IMUL r/m64
¤À§O¬O¤K¦ì¤¸¡B16 ¦ì¤¸¡B32 ¦ì¤¸¡B64 ¦ì¤¸ªº¼ªk¡A³Q¼¼Æ¤À§O¬O AL¡BAX¡BEAX ©Î RAX¡A³£¤£¶·¼g¥X¨Ó¡A¦Ó¼¼Æ´N¬O IMUL «á±µµÛªº¹Bºâ¤¸¡ACPU ¬Ý¨£¹Bºâ¤¸«á´Nª¾¹D¼¼Æ¬O¦h¤Ö¦ì¤¸¡A¨Ì¦¹´N§ä¨ì¬Û¦P¦ì¤¸¼Æªº³Q¼¼Æ¬Û¼¡A§âpºâ«áªº¼¿n¦s¦b AX¡BDX:AX¡BEDX:EAX¡BRDX:RAX ùØ¡C¨Ò¦p¡uIMUL r/m16¡v«ü¥Oªº¼¼Æ¬° 16 ¦ì¤¸¡A³Q¼¼Æ´N¬O 16 ¦ì¤¸ªº AX¡A¼¿n©ñ¦b DX:AX¡C¦pªG±o¨ìªº¼¿n¸û°ªªº¥b¼Æ¦ì¤¸»P¸û§Cªº¥b¼Æ¦ì¤¸²Å¸¹ÂX¥R¬Û¦P®É¡A²M°£·¸¦ìºX¼Ð»P¶i¦ìºX¼Ð¡F¤Ï¤§«h³]©w·¸¦ìºX¼Ð»P¶i¦ìºX¼Ð¡C¡uIMUL r/m64¡v¥u¯à¥Î¦b 64 ¦ì¤¸¨t²Î¤U¡AµLªk¦b DOS ¯u¹ê¼Ò¦¡¤U¨Ï¥Î¡C¬Ý¬Ý©³¤Uªº¨Ò¤l»¡©ú¡G
mov al,4h mov bl,10h imul bl
¤W±¬O¤K¦ì¤¸ªº¦³¸¹¼Æ¼ªk¡A¼¿n¬° 0040h¡A©ñ¦b 16 ¦ì¤¸ªº AX ùØ¡C¸û§Cªº¥b¼Æ¦ì¤¸ ( ¸û§Cªº¤K¦ì¤¸¬O AL ) ²Å¸¹¦ì¤¸¬° 0¡A±N¨ä²Å¸¹¦ì¤¸ÂX¥R¦Ü¸û°ªªº¥b¼Æ¦ì¤¸ ( §Y AH ) ¡A«ê¦n©M¼¿nªº¥b¼Æ°ª¦ì¤¸¬Û¦P¡A³£¬O 00¡A¦]¦¹·¸¦ìºX¼Ð»P¶i¦ìºX¼Ð¤À§O¬O NV¡BNC¡C¬Ý¤UÓ¨Ò¤l¡G
mov al,1eh ;1eh¡×30d mov bl,0ah ;0ah¡×10d imul bl
¤]¬O¤K¦ì¤¸ªº¦³¸¹¼Æ¼ªk¡Aµ²ªG AX ¬° 012C0h¡A¸û§Cªº¥b¼Æ¦ì¤¸¬O C0h¡A¨ä²Å¸¹¬O¡u1¡v¡AÂX¥R¦Ü AH «áÀ³¸Ó¬O 0FFh¡A¦ý»P¼¿n¸û°ªªº¥b¼Æ¦ì¤¸ ( 01 ) ¤£¦P¡A¦]¦¹ CPU ·|§â·¸¦ìºX¼Ð»P¶i¦ìºX¼Ð¤À§O³]¬° OV¡BCY¡C©³¤U¬O²Ä¤TÓ¨Ò¤l¡G
mov ax,1eh ;1eh¡×30d mov bx,0ah ;0ah¡×10d imul bx
¤W±¬O¤Q¤»¦ì¤¸ªº¦³¸¹¼Æ¼ªk¡A©Ò±o¼¿n¬° 0000 012Ch¡A¬O 32 ¦ì¤¸¦³¸¹¼Æ¡A©ñ¦b DX:AX¡A§â AX ¤ºªº²Å¸¹ÂX¥R«á¬O 0000¡A¦Ó¼¿nªº¸û°ª¤Q¤»¦ì¤¸¤]¬O 0000¡A¦]¦¹ CPU ·|§â·¸¦ìºX¼Ð»P¶i¦ìºX¼Ð¤À§O³]¬° NV¡BNY¡C©³¤U¬O²Ä¥|Ó¨Ò¤l¡G
mov ax,-16384 ;-16384d¡×C000h mov bx,2 imul bx
¤W±¬O¤Q¤»¦ì¤¸ªº¦³¸¹¼Æ¼ªk¡A©Ò±o¼¿n FFFF 8000h¡A¬O 32 ¦ì¤¸¦³¸¹¼Æ¡A©ñ¦b DX:AX¡A§â AX ¤ºªº²Å¸¹ÂX¥R«á¬O FFFF¡A¦Ó¼¿nªº¸û°ª¤Q¤»¦ì¤¸¤]¬O FFFF¡A¦]¦¹ CPU ·|§â·¸¦ìºX¼Ð»P¶i¦ìºX¼Ð¤À§O³]¬° NV¡BNY¡C
¨Ï¥Î¥u¦³¤@Ó¹Bºâ¤¸ªº IMUL «ü¥O¡A¹Bºâ¤¸ªºªø«×´N¬O³Q¼¼Æªºªø«×¡A¦Ó¥B¯àºâ¥X¥¿½Tªº¼¿n¡A¼¿nªºªø«×¬O¹Bºâ¤¸ªº¨â¿¡A©ñ¦b¸ê®Æ¼È¦s¾¹:²Ö¥[¾¹ùØ¡CÁöµM CPU ·|³]©w·¸¦ìºX¼Ð©M¶i¦ìºX¼Ð¡A¦ý¬O»P¼¿nªº¥¿½T»P§_µLÃö¡A¤£¶·Àˬd³o¨âÓºX¼Ð¡C
IMUL ¦³¨âÓ¹Bºâ¤¸®É¡A²Ä¤@Ó¹Bºâ¤¸¬O¥Øªº¹Bºâ¤¸¡A¬°³Q¼¼Æ¡F²Ä¤GÓ¹Bºâ¤¸¡A¬O»P²Ä¤@Ó¹Bºâ¤¸¬Û¦P¦ì¤¸¼Æªº¼È¦s¾¹¡B°O¾ÐÅéÅܼƩΥߧYÈ ( immediate value¡A¨ä¹ê´N¬O±`¼Æ )¡A·í°µ¼¼Æ¡C¨ä»yªk¦p¤U¡G
IMUL r16,r/m16 IMUL r32,r/m32 IMUL r64,r/m64 IMUL r16,imm16 IMUL r32,imm32 IMUL r64,imm64
IMUL ·|§â²Ä¤@Ó¹Bºâ¤¸¼¥H²Ä¤GÓ¹Bºâ¤¸¡AµM«á§â¼¿n¦s¤J²Ä¤@Ó¹Bºâ¤¸ùØ¡C¦pªG²Ä¤@Ó¹Bºâ¤¸®e¯Ç¤£¤U¼¿n®É¡A¸û°ªªº¦ì¤¸·|ºIÂ_¦Ó®ø¥¢¡A³o®ÉÔ CPU ·|³]©w·¸¦ìºX¼Ð©M¶i¦ìºX¼Ð¡F¤Ï¤§¡A«h²M°£·¸¦ìºX¼Ð©M¶i¦ìºX¼Ð¡C¥t¥~®Ú¾Ú¤p¤ì°¸¹êÅ窺µ²ªG¡A¦pªG¥Øªº¹Bºâ¤¸¥i¥H®e¯Ç±o¤U¼¿n¡A¦ý¬O¶W¥X½d³ò¡A·¸¦ìºX¼Ð¡B¶i¦ìºX¼Ð¤´·|³Q³]©w¡C¡uIMUL r64,r/m64¡v»P¡uIMUL r64,imm64¡v¥u¯à¦b 64 ¦ì¤¸¨t²Î¤U¨Ï¥Î¡AµLªk¥Î¦b DOS §@·~¨t²Î¡C¬Ý¬Ý©³¤Uªº¤TÓ¨Ò¤l¡G
mov ax,4000h ;4000h¡×16384d mov bx,2 imul bx,ax
µ²ªG AX ¤´¬° 4000h¡ABX Åܬ° 8000h¡AÁöµM 4000h¡Ñ2¡×8000h¡A¦ý³o¬O¥¿¼Æ¤~¹ï¡A¤Q¤»¦ì¤¸ªº BX Ȭ° 8000h¡A¬Ot¼Æ¡A¡Ð32768d¡A¶W¹L 16 ¦ì¤¸©Ò¯àªí¥Üªº½d³ò¡A¦]¦¹·¸¦ìºX¼Ð¡B¶i¦ìºX¼Ð³Q³]¬° OV¡BCY¡C¦A¬Ý¤@¨Ò¡G
mov si,-16384 ;¡Ð16384¡×C000h mov di,2 imul di,si
µ²ªG SI ¤´¬° C000h¡ADI Åܬ° 8000h¡A8000h ¬O¦³¸¹¼Æ¡A´«¦¨¤Q¶i¦ì¬O ¡Ð32768d¡A¨S¶W¹L½d³ò¡A¤]¨S¦³ºIÂ_°ª¦r²Õ¡A¦]¦¹·¸¦ìºX¼Ð¡B¶i¦ìºX¼Ð³Q³]¬° NV¡BNC¡C²Ä¤TÓ¨Ò¤l¡G
mov si,500h ;1280d¡×500h mov cx,422h ;1058d¡×422h imul si,cx
µ²ªG CX ¤´¬° 422h¡ASI Åܬ° 0AA00h¡A³o¬O¦]¬° 500h¡Ñ422h¡×14AA00h¡ASI ¥u¯à®e¯Ç 0AA00h¡A°ª¦r²Õ³QºIÂ_¤F¡A¦]¦¹·¸¦ìºX¼Ð¡B¶i¦ìºX¼Ð³Q³]¬° OV¡BCY¡C
¨Ï¥Î¨âÓ¹Bºâ¤¸ªº IMUL «ü¥O¡A¥²¶·¦b¹Bºâ«áÀˬd·¸¦ìºX¼Ð¡B¶i¦ìºX¼Ð¡A¥u¦³·í³o¨âÓºX¼Ð³Q²M°£®É¡A¥Øªº¹Bºâ¤¸ªº¼¿n¤~¬O¥¿½Tªº¡A§_«h¥i¯à¶È¶È±o¨ì¼¿nªº§C¥b¼Æ¦ì¤¸©ÎªÌ¬Oµo¥Í·¸¦ì¡C
¤TÓ¹Bºâ¤¸ªº IMUL »yªk¦p¤U
IMUL r16,r/m16,imm8 IMUL r32,r/m32,imm8 IMUL r64,r/m64,imm8 IMUL r16,r/m16,imm16 IMUL r32,r/m32,imm32 IMUL r64,r/m64,imm32
²Ä¤@Ó¹Bºâ¤¸¬O¥Øªº¹Bºâ¤¸¡A¦s©ñ¹Bºâ«áªº¼¿n¡F²Ä¤GÓ¹Bºâ¤¸¬O³Q¼¼Æ¡F²Ä¤TÓ¹Bºâ¤¸¬O¼¼Æ¡C¤TÓ¹Bºâ¤¸ªº IMUL »P¨âÓ¹Bºâ¤¸ªº¥ÎªkÃþ¦ü¡A·¸¦ìºX¼Ð»P¶i¦ìºX¼Ðªº³]©w¤]¤@¼Ë¡A®t§O¥u¦b©ó¥i¥H«ü©w¥t¤@ӼȦs¾¹¦s©ñ¼¿n¡C¨Ò¦p©³¤Uªº¨Ò¤l¡G
mov bx,400h imul si,bx,12h
¹Bºâ«á¡ASI Åܬ° 4800h¡ABX ¤£ÅÜ¡A¼¿nªº°ª¦r²Õ¤]¨S³QºIÂ_¡A¦]¦¹·¸¦ìºX¼Ð¡B¶i¦ìºX¼Ð³]¬° NV¡BNC¡C
x86 «ü¥O¶°ªº°£ªk«ü¥O¤]¦³¤TÓ¡ADIV¡BIDIV¡BAAD¡ADIV ¬OµL¸¹¼Æªº°£ªk¡A¤w¸g¦b²Ä¤C³¹¤¶²Ð¹L¤F¡F³oùØn¤¶²Ðªº¬O¦³¸¹¼Æªº°£ªk¡AIDIV «ü¥O¡F¦Ü©ó AAD «h¬O½Õ¾ã¦¨ ASCII ½Xªº°£ªk¡A¥H«á¦A½Í¡C
IDIV ªº»yªk¬O¡G
IDIV r/m8 IDIV r/m16 IDIV r/m32 IDIV r/m54
IDIV ¤£¹³ IMUL ¦³¤TºØÃþ«¬¡A¥¦¥u¦³¤@ºØÃþ«¬¡A¥u¯à¦³¤@Ó¹Bºâ¤¸¡C³oÓ¹Bºâ¤¸¨ä¹ê´N¬O°£¼Æ¡A¥¦¥i¥H¬O¤K¦ì¤¸¡B16 ¦ì¤¸¡B32 ¦ì¤¸©Î 64 ¦ì¤¸ªº¼È¦s¾¹©Î°O¾ÐÅéÅܼơC·í°£¼Æ¬O¤K¦ì¤¸®É¡A³Q°£¼Æ´N¬O AX¡F°£¼Æ¬O 16 ¦ì¤¸®É¡A³Q°£¼Æ¬O DX:AX¡F°£¼Æ¬O 32 ¦ì¤¸®É¡A³Q°£¼Æ¬O EDX:EAX¡F°£¼Æ¬O 64 ¦ì¤¸®É¡A¥u¯à¦b 64 ¦ì¤¸¼Ò¦¡¤U¨Ï¥Î¡A³Q°£¼Æ¬O RDX:RAX¡C¾ã²z¦p¤Uªí¡G
¹Bºâ¤¸ ( °£¼Æ ) | ³Q°£¼Æ | °Ó¼Æ | ¾l¼Æ | ½d¨Ò | ||
¹Bºâ«e | ¹Bºâ«á | »¡©ú | ||||
8¦ì¤¸ | AX | AL | AH | MOV AX,¡Ð4050d MOV BL,100d IDIV BL |
AL¡×¡Ð40d(°Ó) AH¡×¡Ð50d(¾l) |
¡Ð4050d¡×0F02Eh ¡Ð50d¡×0CEh ¡Ð40d¡×0D8h |
16¦ì¤¸ | DX:AX | AX | DX | MOV EAX,5D50h MOV EDX,0FFF8h MOV EBX,¡Ð500d IDIV EBX |
EAX¡×1000d(°Ó) DX¡×¡Ð400d(¾l) |
FFF8:5D50h¡×¡Ð500400d |
32¦ì¤¸ ¥u¯à¥Î©ó 386µ¥¯Å¥H¤W | EDX:EAX | EAX | EDX | MOV EAX,5D50h MOV EDX,0FFFFFFF8h MOV EBX,¡Ð500d IDIV EBX |
EAX¡×68719428d(°Ó) DX¡×¡Ð480d(¾l) |
FFFFFFF8:00005D50h¡× ¡Ð34359714480d |
64¦ì¤¸ ¥u¯à¥Î©ó x64µ¥¯Å¥H¤W | RDX:RAX | RAX | RDX |
IDIV ªº³Q°£¼Æ¤£»Ýn¼g¥X¨Ó¡A¥²©w¬O²Ö¥[¾¹¡A©Î¬O¸ê®Æ¼È¦s¾¹»P²Ö¥[¾¹ªº²Õ¦X¡A¦Ó¥B³Q°£¼Æªº¦ì¤¸Ó¼Æ¬O°£¼Æªº¨â¿¡A³o¤]«D±`¦n²z¸Ñ¡C¦^·Q¤p¾Ç®É©Ò¾Ç¹Lªº¼Æ¾Ç¡A¨Ò¦p 1000¡Ò20¡×50¡A¥|¦ì¼Æ³Q¨â¦ì¼Æ°£¡A¥i±o¨â¦ì¼Æ¡C
IDIV ¹Bºâ®É¡A¦pªG°£¼Æ¬°¹s¡A©Î¬O³Q°£¼Æªºµ´¹ïȤӤj¦Ó°£¼Æªºµ´¹ïȤӤp¡A¥HP©ó³y¦¨°Ó¤Ó¤j¡A²Ö¥[¾¹®e¯Ç¤£¤U¡A´N·|³y¦¨·¸¦ì¡C³oºØ·¸¦ì·|¸õ¥Xµ{¦¡¦^¨ì§@·~¨t²Î¡CnÁקK³oºØ±¡§Î¡A¥i¥H°Ñ¦Ò²Ä¤C³¹ DIV ªº·¸¦ì¡C
¤£½×¬OµL¸¹¼Æ©Î¦³¸¹¼Æ¡A¶i¦æ°£ªk¹Bºâ¡A³£¯à¬Ý¦¨³sÄòªº¥[ªk©Î´îªk¹Bºâ¡A¥Ñ³Q°£¼Æ¶}©l¨C¦¸©¹¹sªº¤è¦V²¾°Ê¤@Ó°£¼Æ¡Aª½¨ì³Ì±µªñ¹s¥B¤£¥i¶V¹L¹s¬°¤î¡C¨Ò¦p¥H ¡Ð7 °£¥H 3 ¬°¨Ò¡A¡Ð7 Â÷¹s¦³¤C®æªº¶ZÂ÷¡A¦]¬°°£¥H¤T¡A©Ò¥H¨C¦¸¦V¹s²¾¤T®æ¡A¥u¯à²¾¨â¦¸¡A²Ä¤T¦¸´N·|¶V¹L¹s¤F¡A¦]¦¹ ¡Ð7 °£¥H 3 ±o ¡Ð2 ¾l ¡Ð1¡A¼g¦¨¼Æ¾Ç¦¡¬°¡G
¡Ð7¡×3¡Ñ(¡Ð2) ¡Ï (¡Ð1)
±q¤W±ªº¨Ò¤l¡A«K¥i¥H±À½×¡A¤£½×³Q°£¼Æ¬O¥¿¼ÆÁÙ¬Ot¼Æ¡A¤]¤£ºÞ°£¼Æ¬O¥¿¼ÆÁÙ¬Ot¼Æ¡A¾l¼Æªº²Å¸¹¥²»P³Q°£¼Æ¬Û¦P¡C°Ó»P°£¼Æ¼¿nªº²Å¸¹¥²»P³Q°£¼Æ¬Û¦P¡C
¤@¦p¬J©¹¡A¤¶²Ð§¹ì²z«á¡A´N¬O¹ê§@¤@Óµ{¦¡¦LÃÒ²z½×¡C³oÓµ{¦¡¥s COMPUTE1.ASM¡A¥¦Åý¨Ï¥ÎªÌ¿é¤J¨âÓ¦b ¡Ð32768¡ã32767 ¤§¶¡ªº¦³¸¹¼Æ¡ACOMPUTE1 ·|§â³o¨âӼưµ¼ªk©M°£ªk¹Bºâ¡A¨Ã§â¼¿n»P°Ó¡B¾l¼ÆÅã¥Ü¦b¿Ã¥ú¹õ¤W¡C©³¤U¬O COMPUTE1.ASM ªºì©l½X¡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 58 59 60
61 62 63 64
65 66 67 68
69 70 71 72
73 74 75 76
77 78 79 80
81 82 83 84
85 86 87 88
89 90 91 92
93 94 95 96
97 98 99 100
101 102 103 104
105 106 107 108
109 110 111 112
113 114 115 116
117 118 119 120
121 122 123 124
125 126 127 128
129 130 131 132
133 134 135 136
137 138 139 140
141 142 143 144
145 146 147 148
149 150 151 152
153 154 155 156
157 158 159 160
161 162 163 164
165 166 167 168
169 170 171 172
173 174 175 176
177 178 179 180
181 182 183 184
185 186 187 188
189 190 191 192
193 194 195 196
197 198 199 200
201 202 203 204
205 206 207 208
209 210 211 212
213 214 215 216
217 218 219 220
221 222 223 224
225 |
;¥ô¿é¤J¨â¼Æ¡An1»Pn2¡ACOMPUTE1·|¹ï¦¹¤G¼Æ°µ¼°£ªk¹Bºâ¡AµM«á¦L¥Xµª®×¨Ó
.MODEL SMALL,C
.386
;*******************************************************************************
.STACK 256
;*******************************************************************************
.DATA
n1 SWORD ?
n2 SWORD ?
remainder DW ?
n_str DB 12 DUP (?),"$"
msg1 DB "Input a number (-32768~32767):$"
msg2 DB 0dh,0ah,"Error input.$"
msg3 DB 0dh,0ah,"Divide is zero.$"
crlf DB 0dh,0ah,"$"
buffer DB 9 DUP (?)
exit_code DB ?
;*******************************************************************************
.CODE
;-------------------------------------------------------------------------------
print_string PROC p_str:WORD
mov dx,p_str
mov ah,9
int 21h
ret
print_string ENDP
;-------------------------------------------------------------------------------
;get_number·|§âpoint©Ò«ü¦ì§}ªº¼Æ¦r¦r¦êÅܦ¨¦³¸¹¼Æ¦s¤JAX
;¿é¤J¡Gpoint¡G¼Æ¦r¦r¦ê¤§¦ì§}¡A¦¹¦r¦ê²Ä¤@Ӧ줸²Õ¥Nªí¼Æ¦r¦û¥Î¦h¤Ö¦ì¤¸²Õ¡C
;¡@¡@¡@¡@¡@¡@ ¨Ò¦p¡u03,2dh,33h,33h¡v¡A§Y¡u-33¡v¡A³Ì«áAX¡×FFDFh
;¿é¥X¡G¶i¦ìºX¼Ð¡GCX¡×0¡A¿ù»~
;¡@¡@¡@¡@¡@¡@¡@¡@CX¡×1¡A¥¿½T¡AAX¡×¦³¸¹¼Æ
get_number PROC point:WORD
LOCAL n:SWORD
LOCAL sign:WORD
mov si,point
mov n,0
movzx cx,BYTE PTR [si] ;CX¡×¨Ï¥ÎªÌ¿é¤Jªº¦ì¤¸²ÕÓ¼Æ
;Àˬd¨Ï¥ÎªÌ¿é¤Jªº²Ä¤@Ӧ줸²Õ¬O§_¬°"+"¡B"-"¡A¦pªG¬O«h¨Ïsign¶ñ¤J²Å¸¹¡A¨Ã
;½Õ¾ãCX»PSI¡A¨Ï¨ä«ü¦V¤U¤@Ӧ줸²Õ
inc si
cmp BYTE PTR [si],"-"
jne chk_positive
mov sign,2dh ;2dh="-"
jmp short adjust
chk_positive: mov sign,2bh ;2bh="+"¡AY¨Ï¥ÎªÌ¨S¿é¤J²Å¸¹¡A·í¦¨
cmp BYTE PTR [si],"+" ;¥¿¼Æ¡A¦]¦¹¡umov sign,2bh¡v¦b cmp
jne arabic_numeral ;¤§«e
adjust: inc si ;¦pªG¨Ï¥ÎªÌ¦³¿é¤J"+"¡B"-"¡AÅý
dec cx ;SI«ü¦V¤U¤@¦ì¤¸²Õ¡ACX´î¤@
cmp cx,5 ;¤@Ó¦r²Õªº¤Q¶i¦ì¼Æ³Ì¤j¬°5¦ì¼Æ
ja error ;Y¶W¹L¡Aªí¥Ü¿é¤J¿ù»~
arabic_numeral: cld
next: mov dx,n ;¦¹¦æ¨ì¤U±¤T¦æ¡A¨ä¹ê¬Opºâ10*n
shl n,3 ;10*n¡×2*n+8*n¡A¡ushl n,3¡v¥ç§Ypºâ8*n
shl dx,1 ;pºâ2*n
add n,dx ;¤£¨Ï¥ÎMUL«ü¥O¬O¦]¬°MUL¶O®É¸û¦h
jo ovflw
lodsb
sub al,"0"
;Àˬd¨Ï¥ÎªÌ¿é¤Jªº¦Û¬O§_¬°ªü©Ô§B¼Æ¦r¡A¥²¶·¦b0¡ã9¤§¶¡
cmp al,0
jb error
cmp al,9
ja error
cbw
add n,ax
jo ovflw ;¦pªG·¸¦ì¡Aªí¥Ü¤£¦b-32768~32767
loop next
jmp ok
ovflw: .IF sign==2dh
cmp n,8000h
je ok
.ENDIF
error: sub cx,cx
jmp exit
ok: mov ax,n
.IF sign==2dh
neg ax
.ENDIF
mov cx,1
exit: ret
get_number ENDP
;-------------------------------------------------------------------------------
;hex2dec_str§â¤T¤Q¤G¦ì¤¸ªº¤Q¤»¶i¦ì¼ÆÈÅܦ¨¤Q¶i¦ìªº¦r¦ê
;¿é¤J¡GDX:AX¡Ð¤T¤Q¤G¦ì¤¸ªº¤Q¤»¶i¦ì¼ÆÈ¡AAX¬O¸û§Cªº¦r²Õ¡ADX¬O¸û°ªªº¦r²Õ
;¡@¡@¡@sign¡Ð«ü©w¥¿t¸¹¡C0¡G¥Hnªº³Ì°ª¦ì¤¸¨M©w¥¿©Ît¡F"+"¡Gn¬°¥¿¼Æ¡F"-"¡Gn¬°t¼Æ
;¡@¡@¡@address¡Ð¤Q¶i¦ì¦r¦êªºÓ¦ì¼Æ¡A¦Ü¤Ön¹w¯d11Ӧ줸²Õ
;¿é¥X¡GAX¡Ð¤Q¶i¦ì¦r¦ê°_©l¦ì§}
hex2dec_str PROC sign:WORD,address:WORD
LOCAL ten:WORD
LOCAL temp:WORD
LOCAL quotient:WORD ;¼È®É¦s©ñ°Óªº¸û°ª¦r²Õ
mov cx,10 ;32¦ì¤¸¦³¸¹¼Æªü©Ô§B¼Æ¦r³Ì¦h10¦ì
mov ten,cx
mov bx,address
.IF sign==2dh
negative: not ax
not dx
add ax,1
adc dx,0
.ELSEIF sign==2bh
positive: mov sign,2bh
.ELSE
test dx,8000h
jz positive
mov sign,2dh
jmp negative
.ENDIF
next: mov temp,ax ;§â¸û§C¦r²ÕÀx¦s¦btempùØ
mov ax,dx ;¥ýpºâ¸û°ª¦r²Õ°£¥Hten
sub dx,dx
div ten ;°£ten«á¡ADX¡×§C¦r²Õ°£ten®Éªº¸û°ª¦r²Õ
mov quotient,ax ;AX¡×³Ì«á°Óªº¸û°ª¦r²Õ
mov ax,temp ;pºâ¸û§C¦r²Õ°£¥Hten
div ten ;AX¡×³Ì«á°Óªº¸û§C¦r²Õ
add dl,"0" ;DX¡×³Ì«áªº¾l¼Æ
mov [bx],dl
cmp quotient,0 ;Y³Ì«áªº°Óµ¥©ó¹s¡Aªí¥Ü¤w§¹¦¨
jnz q_isnt_zero
or ax,ax
jz finish
q_isnt_zero: dec bx
mov dx,quotient ;¨ú¦^°Óªº°ª¦r²Õ
loop next
finish: .IF sign==2dh
dec bx
mov BYTE PTR [bx],"-"
.ENDIF
mov ax,bx ;ªð¦^¤Q¶i¦ì¼Æ¦r¦r¦ê°_©l¦ì§}
ret
hex2dec_str ENDP
;-------------------------------------------------------------------------------
print_char PROC char:BYTE
mov dl,char
mov ah,2
int 21h
ret
print_char ENDP
;-------------------------------------------------------------------------------
expression PROC char:BYTE
;¦L¥Xn1
mov ax,n1
cwd
INVOKE hex2dec_str,0,OFFSET n_str+SIZEOF n_str-2
INVOKE print_string,ax
;¦L¥X¡u*¡v©Î¡u/¡v
INVOKE print_char,char
;¦pªGn2¬°t¼Æ¡A¨º»òn¦L¥X¡u(¡v¡A§_«h¤£¥²
test n2,8000h
jz positive1
INVOKE print_char,"("
;¦L¥Xn2
positive1: mov ax,n2
cwd
INVOKE hex2dec_str,0,OFFSET n_str+SIZEOF n_str-2
INVOKE print_string,ax
;¦pªGn2¬Ot¼Æ¡A¨º»òn¦L¥X¡u)¡v¡A§_«h¤£¥²
test n2,8000h
jz positive2
INVOKE print_char,")"
;¦L¥X¡u=¡v
positive2: INVOKE print_char,"="
ret
expression ENDP
;-------------------------------------------------------------------------------
.STARTUP
;¦L¥X¡uInput a number (-32768~32767):¡v¡A¨ÃÅý¨Ï¥ÎªÌ¿é¤J¼Æ¦r¦r¦ê
INVOKE print_string,OFFSET msg1
mov ah,0ah
lea dx,buffer
mov buffer,7
int 21h
;§â¼Æ¦r¦r¦êÅܦ¨¦³¸¹¼Æ¡A¦s©ón1
INVOKE get_number,OFFSET buffer+1
jcxz err1
mov n1,ax
INVOKE print_string,OFFSET crlf ;´«¦æ
;¦L¥X¡uInput a number (-32768~32767):¡v¡A¨ÃÅý¨Ï¥ÎªÌ¿é¤J¼Æ¦r¦r¦ê
INVOKE print_string,OFFSET msg1
mov ah,0ah
lea dx,buffer
mov buffer,7
int 21h
;§â¼Æ¦r¦r¦êÅܦ¨¦³¸¹¼Æ¡A¦s©ón2
INVOKE get_number,OFFSET buffer+1
jcxz err1
mov n2,ax
INVOKE print_string,OFFSET crlf
jmp compute
err1: mov dx,OFFSET msg2
jmp err2
;pºân1*n2¤§¼¿n¡A¨Ã¦L¥Xµ²ªG¨Ó
compute: INVOKE expression,"*"
mov ax,n1
imul n2
INVOKE hex2dec_str,0,OFFSET n_str+SIZEOF n_str-2
INVOKE print_string,ax
;pºân1/n2¤§°Ó»P¾l¼Æ¡A¨Ã¦L¥Xµ²ªG¨Ó
cmp n2,0
jnz divide_nonzero
mov dx,OFFSET msg3
err2: INVOKE print_string,dx
mov exit_code,1
jmp short quit
divide_nonzero: INVOKE print_string,OFFSET crlf
INVOKE expression,"/"
mov ax,n1
cwd
idiv n2
mov remainder,dx
cwd
INVOKE hex2dec_str,0,OFFSET n_str+SIZEOF n_str-2
INVOKE print_string,ax
mov cx,5
again: INVOKE print_char,"."
loop again
mov ax,remainder
cwd
INVOKE hex2dec_str,0,OFFSET n_str+SIZEOF n_str-2
INVOKE print_string,ax
mov exit_code,0
quit: .EXIT exit_code
;*******************************************************************************
END |
COMPUTE1.ASM »P«e¤@³¹ªº COMPUTE.ASM «Ü¬Û¦ü¡A¤p¤ì°¸¶È´N¨âªÌ¤£¦P¤§³B½Í¤@½Í¡C
°Æµ{¦¡ hex2dec_str ¬O¥Î¨Ó§â¤Q¤»¶i¦ì¼ÆÈÅܦ¨¤@Ó¤Q¶i¦ì¼Æ¦r¦r¦ê¡A»P«e¤@³¹ªº hex2dec_str ¤£¦Pªº¬O¥»³¹¯à³B²z 32 ¦ì¤¸ªº¦³¸¹¼Æ¡C²z¥ÑÅã¦Ó©ö¨£¡A¨Ï¥ÎªÌ¿é¤Jªº¬O¨âÓ 16 ¦ì¤¸ªº¦³¸¹¼Æ¡A¥¦Ì¬Û¼«á¡A¦³¥i¯à±o¨ì 32 ¦ì¤¸ªº¦³¸¹¼Æ¡A¦]¦¹¥»³¹ªº hex2dec_str ¥²¶·¯à§â 32 ¦ì¤¸ªº¦³¸¹¼ÆÅܦ¨¤Q¶i¦ì¼Æ¦r¦r¦ê¡C
¥»³¹ªº hex2dec_str ©Ò¥Îì²z¨ä¹ê©M«e¤@³¹ªº¤@¼Ë¡A³£¬O¥Î³sÄò°£ªk¡A©Ò¤£¦Pªº¬O 32 ¦ì¤¸ªº¦³¸¹¼Æ°£¥H¤Q¡A¦³¥i¯à·|³y¦¨°Ó¤Ó¤j¡AAX µLªk®e¯Ç±o¤U¡A¦]¦¹¥Î¨â¬q¦¡°£ªk¡C¥ýºâ°ª¦r²Õ°£¥H¤Q¡A¦Aºâ§C¦r²Õ°£¥H¤Q¡C¨£µ{¦¡²Ä 110¡ã118 ¦æ¡C³o¬qµ{¦¡¥i¥H°Ñ¦Ò²Ä¤C³¹¦³Ãö°£ªk·¸¦ìªº»¡©ú¡C
¦pªG DX:AX ¬Ot¼Æ¡An§â¥¦Âà´«¦¨¤Q¶i¦ìªº¼Æ¦r¦r¦ê¡An¥ýÅܸ¹ ( §Y¨D¨ä¤G¸É¼Æ )¡A¦A¨Ì¥¿¾ã¼Æ³sÄò°£¥H¤QÅܦ¨¤Q¶i¦ì¼Æ¦r¦r¦êªº¤è¦¡³B²z¡C°ÝÃD¬OÁö¦³ NEG «ü¥O¥i¥H¨D¬YӼȦs¾¹ªº¤G¸É¼Æ¡A¦ý¬O¨Ã¨S¦³«ü¥O¯à°÷¨D DX:AX ªº¤G¸É¼Æ¡C¤£¹L§Ú̪¾¹D¤èªk¡G①©Ò¦³¦ì¤¸¤ÏÂà¡A②¥[¤@¡C¦]¦¹¥u¦n¤À¶}¨Ó¼g¡A´NÅܦ¨¤U±ªºµ{¦¡¡G
not ax not dx add ax,1 adc dx,0
¦b COMPUTE1.ASM ªº²Ä 98¡ã101 ¦æ¡A´N¬O¤W±¥|¦æµ{¦¡¡A¥¦¬O¥Î¨Ópºâ DX:AX ªº¤G¸É¼Æ¡C«e¤T¦æ«Ü®e©ö²z¸Ñ¡A²Ä¥|¦æªº ADC ¬O¥H«e¥¼¨£¹Lªº«ü¥O¡C
ADC «ü¥O¬O¥[ªk¹Bºâ®É¡A¤]§â¶i¦ìºX¼Ð¥[¶i¨Ó¡CADC ªº»yªk¬O¡G
ADC ¥Øªº¹Bºâ¤¸,¨Ó·½¹Bºâ¤¸
¥Øªº¹Bºâ¤¸¥i¥H¬O¼È¦s¾¹©Î°O¾ÐÅéÅܼơA¨Ó·½¹Bºâ¤¸¥i¥H¬O¼È¦s¾¹¡B°O¾ÐÅéÅܼƩΥߧYÈ¡C¦ý¬O¦³¨Ç¨î¡G①¥Øªº¹Bºâ¤¸¡B¨Ó·½¹Bºâ¤¸¤£¥i¥H¦P®É¬O°O¾ÐÅéÅܼơF②¥Øªº¹Bºâ¤¸¡B¨Ó·½¹Bºâ¤¸ªø«×¥²¶·¬Û¦P¡F③¦pªG¨Ó·½¹Bºâ¤¸¬O¥ß§YÈ¡A¨º»ò¥ß§YÈ¥i¥H¥u¦³¤K¦ì¤¸¡A¦Ó¥Øªº¹Bºâ¤¸¬O¨ä¥Lªø«×¡A¤]´N¬O¤£¨ü²Ä¤GÓ¨î ( ·íµM¥ß§YȪºªø«×¤]¥i¥H©M¥Øªº¹Bºâ¤¸¬Û¦P )¡C
ADC «ü¥O·|§â¨Ó·½¹Bºâ¤¸©M¥Øªº¹Bºâ¤¸¬Û¥[¡A¦P®É¦A¥[¤W¶i¦ìºX¼Ð¡C³o¼Ëªº¤è¦¡¡A¥i¥HÅýì¨Ó¥u¦³ 16 ¦ì¤¸ªº¼È¦s¾¹°µ 32 ¦ì¤¸ªº¥[ªk¹Bºâ¡A©Î¬O 32 ¦ì¤¸ªº¼È¦s¾¹°µ 64 ¦ì¤¸ªº¥[ªk¹Bºâ¡C¨Ò¦pn¥Î 16 ¦ì¤¸¼È¦s¾¹pºâ 160000¡Ï172800¡A160000 ´«¦¨¤Q¤»¶i¦ì¬O 27100h¡A172800 ´«¦¨¤Q¤»¶i¦ì¬O 2A300h¡A°²³]§â DX:AX¡×0002 7100h¡ABX:CX¡×0002 A300h¡A¦p¤U¹Ï¡G
mov dx,2 mov ax,7100h mov bx,2 mov cx,0a300h add ax,cx adc dx,bx
·í°õ¦æ§¹¡uadd ax,cx¡v¤§«á¡A·|µo¥Í¶i¦ì¡ACPU ·|³]©w¶i¦ìºX¼Ð¡A¦]¦¹¶i¦ìºX¼Ð¬° 1 ( ¨£¤W¹Ï¡A¥Õ¦âªº¡u1¡v)¡A¦b°õ¦æ¤U¤@¹D¡uadc dx,bx¡v®É¡ACPU °£¤F§â DX ¥[¤W BX ¤§¥~¡AÁÙ·|¥[¤W¶i¦ìºX¼Ð¡A¦]¦¹ DX ·|Åܦ¨ 5¡C
¦A¦^¨ì¸Ñ»¡ COMPUTE1.ASM¡C¦bpºâ 32 ¦ì¤¸¤G¸É¼Æ®É¡A¥ý¤ÏÂà¦U¦ì¤¸¡AµM«á¥[¤@¡A¦Ò¼{¦³®ÉÔ·|¶i¦ì¡A¦]¦¹¦b²Ä 101 ¦æ¡A¨Ï¥Î«ü¥O¡uadc dx,0¡v¡A³o¼Ë¦pªG§C¦r²Õ¥[¤@«áµo¥Í¶i¦ì¡A¤]¯à§â¶i¦ì¥[¦b°ª¦r²Õ¤F¡C
¥»³¹´N¦¹µ²§ô¡C©³¤U¬O¦³¸¹¼Æªº¦^ÅU¡C
¤HÃþµo©ú¹q¸£ªº¥Øªº¡A¥»·N¬OÀ°§U§ÚÌpºâ¡A¦]¦¹¹q¸£·íµMn¯àªí¥Ü¼ÆÈ¡A¤£ºÞ¬O¥¿¾ã¼Æ¡Bt¾ã¼Æ¡A¬Æ¦ÜÁÙn¯àªí¥Ü±a¦³¤p¼Æªº¦³²z¼Æ¤~¦æ¡C¥»³¹»P«e¤@³¹¡A¤¶²Ð¤F¹q¸£¦p¦óªí¥Ü¥¿¾ã¼Æ»Pt¾ã¼Æ¡C©³¤U¬O¤@¨Ç¾ã²z¡C
¦b¹q¸£ùØ¡A§Ú̥άY¼Æªº¤G¸É¼Æ¥Nªí¨ä¬Û¤Ï¼Æ¡Cpºâ¬Y¼Æªº¤G¸É¼Æ¦³¨âÓ¨BÆJ¡G
¨Ò¦p 5Fh ªº¤G¸É¼Æ¬O 0A1h¡A³oÓ 0A1h ¬O¦p¦óºâ¥X¨Óªº©O¡H
¦]¦¹ 5Fh ªº¤G¸É¼Æ¬O 0A1h¡C§â 5Fh ´«¦¨¤Q¶i¦ì¬O 95¡A¨ät¼Æ¬O ¡Ð95¡AÅܦ¨¤Q¤»¶i¦ì¬O 0A1h¡Cµ²½×´N¬O 5fh¡×95d¡A0A1h¡×¡Ð95d¡C
§â¤Q¤»¶i¦ì¦³¸¹¼ÆÅܦ¨¤Q¶i¦ì¼Æ¦r¦r¦êªº·N«ä¡A¬O§â¦s¦b°O¾ÐÅ餺ªº¼ÆÈ¡AÅܦ¨¥Ñªü©Ô§B¼Æ¦r²Õ¦¨ªº¦r¦ê¡C¨Ò¦p AL¡×0A1h¡A³oÓ 0A1h ´N¬O¤Q¤»¶i¦ì¼Æ¡A§â¥¦Âà´«¦¨¡u2dh¡B39h¡B35h¡B24h¡v¡A¥Î ASCII ½X¨Óªí¥Ü´N¬O¡u-35$¡v¡C¥»³¹ªº hex2dec_str °Æµ{¦¡¥i¥H§â¦s©ó DX:AX ùتº 32 ¦ì¤¸¦³¸¹¼Æ¡AÂàÅܦ¨¥Ñªü©Ô§B¼Æ¦r²Õ¦¨ªº ASCII ½X¦r¦ê¡C
¤èªk«Ü²³æ¡A¥ý§PÂ_ DX:AX ùتº¼ÆȬ°¥¿ÁÙ¬Ot¡C¦pªG¬O¥¿¾ã¼Æ¡A¥Î³sÄò°£¥H¤Qªº¤èªk©Ò±oªº¾l¼Æ´N¬OÓ¦ì¼Æ¡B¤Q¦ì¼Æ¡B¦Ê¦ì¼Æµ¥µ¥¡C¦pªG¬Ot¼Æ¡A´N¥ý¥Î¤G¸É¼Æ¨D¥X¦¹t¼Æªºµ´¹ïÈ¡A¦A¥H¥¿¾ã¼Æªº¤èªk¹Bºâ¡A´N¥i±o¥Ñªü©Ô§B¼Æ¦r²Õ¦¨ªº ASCII ½X¦r¦ê¡A³Ì«á¦b¦r¦ê³Ì«e±¥[¤W¡u¡Ð¡v¸¹§Y¥i¡C
¦³¸¹¼Æ©ÎµL¸¹¼Æªº¥[´îªk¡A§¡¥i¨Ï¥Î ADD¡BSUB «ü¥O¡A¦ýnª`·N¬O§_·¸¦ì¡F¦Ó¼°£ªk¦³°Ï§O¡AµL¸¹¼Æªº¼°£ªk¥Î MUL¡BDIV¡A¤£¥²ºÞ·¸¦ì¬O§_µo¥Í¡A³£¬O¥¿½Tªº¡A¦Ó¦³¸¹¼Æªº¼°£ªk¥Î IMUL¡BIDIV¡A¤@¯ë±¡§Î¤U¤£¥²¾á¤ß·¸¦ìªº°ÝÃD¡A°£«D¼ªk¨Ï¥Î IMUL ªºÂù¹Bºâ¤¸¡B©Î¤T¹Bºâ¤¸ªº§Î¦¡¡A©Î¬O°£ªk¤¤°£¼Æ¬°¹s¡A©Î³Q°£¼Æ¤Ó¤j¥B°£¼Æ¤Ó¤p¡C