1 / 26

Chapter 5

Chapter 5. Arithmetic and Logic Instructions Barry B. Brey bbrey@ee.net. Addition. The ADD instruction is used for binary addition. The addition causes the flag bits to change. Addition can be 8-, 16-, and 32-bits. All of the addressing modes presented in Chapter 2 are used by addition.

andra
Télécharger la présentation

Chapter 5

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. Chapter 5 Arithmetic and Logic Instructions Barry B. Brey bbrey@ee.net

  2. Addition • The ADD instruction is used for binary addition. • The addition causes the flag bits to change. • Addition can be 8-, 16-, and 32-bits. • All of the addressing modes presented in Chapter 2 are used by addition. ADD EAX,EBX ;EAX = EAX + EBX

  3. Increment • The INC instruction adds a one to a register or the contents of a memory location. INC BX ;BX = BX + 1 INC BYTE PTR [EBX]

  4. Add with Carry • The ADC instruction adds the carry bit into the sum. Used for wide additions (wider than 32-bits) and other reasons. ADC AX,DX ;AX = AX + DX + C ADX ESI,[EBX]

  5. Subtraction • The SUB instruction performs subtraction and the flags change to reflect condition of the result. • As with other arithmetic and logic instructions, subtraction exists for 8-, 16-, and 32-bit data. SUB AL,3 ;AL = AL - 3 SUB ECX,ESI

  6. Decrement • The DEC instruction subtracts one from a register or the contents of a memory location. DEC EBX ;EBX = EBX - 1 DEC DWORD PTR [EAX]

  7. Subtract with Borrow • The SBB instruction subtracts with borrow (where the borrow is held in the carry flag). SBB EAX,EBX ;EAX = EAX – EBX – C

  8. Compare • The CMP instruction is a special form of the SUB instruction. A compare does not result in a difference that is saved, on the flag bits change to reflect the difference. CMP AL,3 ;if AL = 3 the result to zero (flag)

  9. Multiplication • The MUL (unsigned) and IMUL (signed) instruction exist to perform 8-, 16-, or 32-bit multiplication. • The result is always a double wide result. • The carry and overflow bits indicate conditions about the multiplication. • A special IMUL exists with 3 operands.

  10. Division • The DIV (unsigned) and IDIV (signed) instruction exist to perform division on 8-, 16-, or 32-bit numbers. • Division is always performed o a double wide dividend. • The result is always in the form of an integer quotient and an integer remainder.

  11. AND • The AND instruction performs logical multiplication (the AND operation).

  12. OR • The OR instruction generates the logical sum (OR operation).

  13. Exclusive OR • The XOR instruction performs the Exclusive OR operation.

  14. NEG and NOT • The NEG (negate) instruction 2’s complements a number, • The NOT instruction 1’s complements a number. NOT EAX NEG DWORD PTR [EBX]

  15. Shifts • There are 4 shift instructions. Two are logical shifts and two are arithmetic shifts. • The logical shifts reposition the bits in a number. The arithmetic shifts multiply or divide signed numbers by powers of two. • SHR and SHL are logical and SAR and SAL are arithmetic shifts. SHL AL,3 or SHL AL,CL

  16. Rotates • Rotates are shifts that re-circulate the bit that moves out of an end of the register or memory location. • Four rotates exist where two just rotate the target and two rotate the target through the carry flag. ROL AL,3 or RCL AL,3

  17. TEST • The TEST instruction is a special form of the AND instruction that produces no result except for a change of the flags. • This instruction is often used to test multiple bits of a number. TEST AL,3 ;test the right two bits (if both are zero the result is zero)

  18. Bit Test Instructions • There are four bit test instructions BT (bit test), BTR (bit test and reset), BTS (bit test and set), and BTC (bit test and complement). • Each tests the prescribed bit by moving it into carry. Then the bit is modified (except for BT) BT AL,3 ;bit 3 is moved to carry BTS AL,3 ;bit 3 is moved to carry then set BTR AL,3 ;bit 3 is moved to carry then reset BTC AL,3 ;bit 3 is moved to carry then inverted.

  19. String Compares • The SCAS and CMPS instruction perform comparisons on blocks of data. • SCAS compares a memory block to the accumulator and CMPS compares two blocks of memory. • SCASB, SCASW, and SCASD are available for 8-, 16-, and 32-bit comparisons as are CMPSB, CMPSW, and CMPSD.

  20. SCAS is often used to search for a value and CMPS is often used to compare two blocks. • Both instruction change the flags to indicate the outcome of the comparison. • The Direction flag determines whether the pointer increments or decrements. • REPE and REPNE are often used to repeat the SCAS or CMPS instructions.

  21. AAM • About the only ASCII adjust instruction commonly used is the AAM instruction. • This instruction divides by 10, but leaves the quotient in AH instead of AL. The remainder is found in AL instead of AH. • By dividing by 10, a number up to 100 decimal can be converted to BCD.

  22. DAA and DAS • These instructions are used to adjust the result after a BCD addition or subtraction. • DAS and DAA does find some application in systems.

More Related