1 / 30

Assembly Language Lecture 9

Assembly Language Lecture 9. Multiplication and Division Instructions. Signed vs. Unsigned Multiplication. Signed multiplication and unsigned multiplication lead to different results. Consider two bytes: 11111111 and 00000001 Signed multiplication of these two numbers gives:

ivan
Télécharger la présentation

Assembly Language Lecture 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Assembly LanguageLecture 9 Multiplication and Division Instructions

  2. Signed vs. Unsigned Multiplication • Signed multiplication and unsigned multiplication lead to different results. Consider two bytes: 11111111 and 00000001 Signed multiplication of these two numbers gives: 11111111 (-1) * 00000001 (1) = 1111111111111111 (-1) Unsigned multiplication of these two numbers gives: 11111111 (255) * 00000001 (1) = 0000000011111111 (255) • REMEMBER: • If you multiply an 8-bit number by an 8-bit number, you will get a 16-bit number! • If you multiply a 16-bit number by a 16-bit number, you will get a 32-bit number!

  3. Signed vs. Unsigned Multiplication • Thus there is one instruction to multiply signed (IMUL)and another instruction to multiply unsigned (MUL). • These instructions execute one way if you're working with bytes and they execute in a different way if you're working with words.

  4. MUL and IMUL Instructions • Unsigned Multiplication • Syntax: MUL source • Signed Multiplication • IMUL source • Byte Form • The multiplier is contained in the source field and themultiplicand is assumed to be in AL. The 16-bit product will be in AX. The source may be a byte register or a memory byte. It cannot be a constant. • Word Form • The multiplier is contained in the source field and the multiplicand is assumed to be in AX.The 32 bit product will be split over two registers. The most significant 16 bits will be in DX and the least significant bits will be in AX (This is referred to as DX:AX). The source may be a word register or a memory word. It cannot be a constant.

  5. MUL and IMUL Instructions Effect of MUL/IMUL on the status flags: • SF,ZF,AF,PF : undefined • CF / OF : • If the product is too big to fit in the lower half of the destination (AL for byte multiplication and AX for word multiplication), the CF/OF will be set to 1. • If the upper half of the result on a MUL is a zero or the upper half of the result on an IMUL is the sign extension of the lower half, the CF/OF will be set to 0.

  6. Examples • Example 1 Before the instruction executes: DX:???? AX: 0002 BX: 0010 (16) CF/OF:? IMUL BXmultiplies the contents of AX by BX placing the 32 bit answer in DX:AX After the instruction executes: DX:0000 AX: 0020 (32) BX: 0010 CF/OF:0 (Result fits in AX) • Example 2 Before the instruction executes: DX:???? AX: 0003 BX: 7000 (28672) CF/OF:? IMUL BXmultiplies the contents of AX by BX placing the 32 bit answer in DX:AX After the instruction executes: DX:0001 AX: 5000 BX: 7000 CF/OF:1 (Result [86016] does not fit in AX)

  7. Examples • Example 3 Before: AL: 30 (48) BL: 02 CF/OF:? IMUL BL multiplies the contents of AL by BL placing the 16 bit answer in AX After: AX: 0060 (96) BL: 02 CF/OF:0 (Result fits in AL) • Example 4 Before: AL: 80 (-128) BL: 02 CF/OF:? IMUL BL multiplies the contents of AL by BL placing the 16 bit answer in AX After: AX: FF00(-256) BL: 02 CF/OF:1 (High 8 bits are not the signed extension of the lower half)

  8. Examples (cont’d) • Example 5 Before: AL: 80 (-128) BL: FF (-1) CF/OF:? IMUL BL multiplies the contents of AL by BL placing the 16 bit answer in AX After: AX: 0080 (128) BL: FF CF/OF:1 (High 8 bits are not the signed sense signed extension of the lower half) • Example 6 Before: AL: 70 (112) BL: 02 CF/OF:? MUL BL multiplies the contents of AL by BL placing the 16 bit answer in AX After:AX: 00E0 (224) BL: 02 CF/OF:0 (High 8 bits are zero)

  9. Examples (cont’d) • Example 7 Before: AL: A0 (160) BL: 02 CF/OF:? MUL BL multiplies the contents of AL by BL placing the 16 bit answer in AX After: AX: 0140 (320) BX: 02 CF/OF:1 (High 8 bits are not zero)

  10. Problem 1 • Translate into ASSMBLER the following high-level assignment statement: ANSWER = 13 * A + B * C - D where ANSWER, A, and D are words in memory and B and C are bytes in memory. Assume no overflow and use signed multiplication. MOV AX,13 ;move constant to reg IMUL A ;assume A*13 in AX MOV ANSWER,AX ;ANSWER=13*A MOV AL,B IMUL C ;assume B*C in AX ADD ANSWER,AX ;ANSWER=13*A+B*C MOV BX,D ;Put D in register so you can ;subtract it from ANSWER SUB ANSWER,BX ;ANSWER=13*A+B*C-D

  11. Problem 2 Write a procedure to calculate N! Remember N! = 1*2*3*...* N-1 * N For example, 4! = 1*2*3*4 = 24 0! and 1! are defined to be 1 and the factorial for a negative number is undefined and should be returned as 0.

  12. Solution ; compute N! ; input : CX = N ;output: AX = N! MOV AX,1 ; AX holds product TOP: MUL CX ; product = product X term LOOP TOP

  13. Signed and Unsigned Division • When any kind of division is performed, two results are obtained - the quotient and the remainder. • The effect of the division instructions on the flags register is that all status flags are undefined after a divide has taken place !!!

  14. Signed and Unsigned Division • Unsigned Division Instruction • Syntax: DIV divisor • Signed Division Instruction • Syntax: IDIV divisor • These instructions divide 8 (or 16) bits into 16 (or 32) bits. The quotient and remainder will be the same size as the divisor. • Byte Form • The divisor is an 8-bit register or memory byte. The 16-bit dividend is assumed to be in AX. After the division the 8 bit quotient is in AL and the 8-bit remainder is in AH.The divisor cannot be a constant!

  15. Signed and Unsigned Division • Word Form • The divisor is 16-bit register or memory word. The 32 bit dividend is assumed to be in DX:AX. After the division, the 16 bit quotient is in AX and the 16-bit remainder is in DX. The divisor cannot be a constant! • The divide instruction does not set the status flags to any particular settings. • If the quotient will not fit into AL or AX, the system will display "DIVIDE OVERFLOW".

  16. Signed and Unsigned Division • The sign of the quotient will be determined by the laws of algebra. The sign of the remainder will be the sign of the original dividend. That is: QUOTIENT REMAINDER +/+ = + +/+ = + +/- = - +/- = + -/+ = - -/+ = - -/- = + -/- = -

  17. Signed and Unsigned Division To set to divide one word by another word 1. Place the dividend in AX 2. For DIV, clear DX (XOR DX,DX will clear DX) 3. For IDIV, extend the sign of the value in AX to DX by issuing the CWD instruction (Convert a Word to a Doubleword). To set up to divide one byte by another byte 1. Place the dividend in AL 2. For DIV, clear AH (XOR AH,AH will clear AH) 3. For IDIV, extend the sign of the value in AL to AH by issuing the CBW instructions (Convert a Byte to a Word).

  18. CWD Instruction • Converts the signed 16-bit number in AX into a signed 32-bit number in DX:AX • NOTE: Use this instruction only with signed arithmetic!! • Syntax: CWD • If bit 15 of AX is a 1, the instruction will place FFFF in DX. • If bit 15 of AX is a 0, the instruction will place 0000 in DX. • No flags are affected by this instruction. • EXAMPLES: • Before: DX:???? AX:7000 (28672) CWD converts the signed 16-bit number in AX to a 32-bit signed number in DX:AX • After: DX:0000 AX:7000 (28672)

  19. CWD Instruction • Example 2: Before the instruction executes: DX:???? AX:8000 (-32768) CWD converts the signed 16-bit number in AX to a 32-bit signed number in DX:AX After the instruction executes: DX:FFFF AX:8000 (-32768)

  20. CBW Instruction • This instruction converts the signed 8-bit number in AL into a signed 16-bit number in AX • NOTE: Use this instruction only with signed arithmetic!! • Syntax: CBW If bit 7 of AL is a 1, the instruction will place FF in AH. If bit 7 of AL is a 0, the instruction will place 00 in AH. No flags are affected by this instruction. • Examples: Before: AX:??40 (64) CBW converts the signed 8-bit number in AL to a 16-bit signed number in AX After: AX:0040 (64)

  21. CBW Instruction • Example 2 • Before: AX:??FC (-4) CBW converts the signed 8-bit number in AL to a 16-bit signed number in AX • After: AX:FFFC (-4)

  22. DIV and IDIV Examples • Example 1 MOV AX,9 CWD Thus, before the instruction executes: DX:0000 AX: 0009 BX: 0002 IDIV BX divides the contents of DX:AX by BX placing the quotient in AX and the remainder in DX After the instruction executes: DX:0001 AX: 0004 BX: 0002

  23. DIV and IDIV Examples • Example 2 MOV AX,9 CWD Thus, before the instruction executes: DX:0000 AX: 0009 BX: FFFE (-2) IDIV BX divides the contents of DX:AX by BX placing the quotient in AX and the remainder in DX After the instruction executes: DX:0001 AX: FFFC (-4) BX: FFFE

  24. DIV and IDIV Examples • Example 3 Before the instruction executes: AX: 0A00 (2560) BL: 02 IDIV BLdivides the contents of AX by BL placing the quotient in AL and the remainder in AH As the instruction executes, the system will display on the screen: DIVIDE OVERFLOW because the quotient (1280 or 500h) will not fit into AL

  25. DIV and IDIV Examples • Example 4 MOV AX,9 XOR DX,DX Thus, before the instruction executes: DX:0000 AX: 0009 BX: FFFE (65534) DIV BX divides the contents of DX:AX by BX placing the quotient in AX and the remainder in DX After the instruction executes: DX:0009 AX: 0000 BX: FFFE

  26. DIV and IDIV Examples • Example 5 Before the instruction executes: AX: FFFF (65535) BL: 02 DIV BL divides the contents of AX by BL placing the quotient in AL and the remainder in AH As the instruction executes, the system will display on the screen: DIVIDE OVERFLOW because the quotient (32767 or 7FFFh) will not fit into AL

  27. DIV and IDIV Examples • Example 6 Divide -1250 by 7 AX: -1250 BX: 7 CODE : MOV AX,-1250 ; AX gets dividend CWD ;Extend sign to DX MOV BX, 7 ;BX has divisor IDIV BX ;AX gets quotient, DX has remainder

  28. DIV and IDIV Examples • Example 7 Divide the signed value of the byte variable XBYTE by -7 AL: XBYTE BL: -7 CODE : MOV AL, XBYTE ;AL has dividend CBW ;Extend sign to AH MOV BL, -7 ;BL has divisor IDIV BL ;AL has quotient, AH has remainder

  29. Problem • Translate into ASSMBLER the following high-level assignment statement: ANSWER = 130 / A + B / C - D / 7 where ANSWER, A, and D are words in memory and B and C are bytes in memory. Assume no overflow and use signed multiplication. MOV AX,130 ;move constant to reg CWD ;expand to DX:AX IDIV A ;assume QUOTIENT 130/A in AX MOV ANSWER,AX ;ANSWER=130/A MOV AL,B CBW ;expand to AX IDIV C ;assume quotient B/C in AX

  30. Problem (cont’d) CBW ;convert byte quotient in AL to a word ADD ANSWER,AX ;ANSWER=130/A + B/C MOV AX,D MOV BL,7 ;put constant in reg IDIV BL ;assume quotient D/7 in AL CBW ;expand to AX so you can SUB from ANSWER SUB ANSWER,AX ;ANSWER=130/A + B/C - D/7

More Related