1 / 26

Arithmetic Instructions

Arithmetic Instructions. Introduction. Arithmetic instructions are used to perform arithmetic operation such as Addition Subtraction Multiplication Division

ima
Download Presentation

Arithmetic Instructions

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. Arithmetic Instructions

  2. Introduction • Arithmetic instructions are used to perform arithmetic operation such as • Addition • Subtraction • Multiplication • Division • These operations can be performed on numbers expressed in a variety of formats such as unsigned binary, signed binary, packed or unpacked decimals and signed packed decimal numbers. The flags that are affected by the arithmetic instructions are carry flag, auxiliary carry flag, sign flag, zero flag, parity flag and overflow flag.

  3. ADD instructions • This instruction adds the content of the source operand with the destination operand. The result is stored in the destination operand. All flags are affected. • ADD ADD operand1, operand 2 operand1 =operand 1 + operand 2 Operand 1: register/memory Operand 2: register/memory/immediate

  4. ADC Instruction • This instruction stands for add with carry. This instruction adds the content of source, destination and the carry flag. The result is stored in the destination operand. • Example: • ADC AX, [BX]

  5. INC instruction • This instruction increments the value of register or the data in the memory location by 1. the result is stored in the operand itself. In this carry flag (CF) is not affected. Also in many cases the used of INC instruction generated less machine code, and resulting in faster execution. • Examples • INC SP • This instruction increments the value of SP register by 1. the result is stored in the SP register itself.

  6. SUB Instruction • This instruction is used to subtract the value of source operand from the destination operand. The result is stored in the destination operand. If the source operand is larger than the destination operand the resulting borrow is indicated by setting the carry flag. • General format • SUB destination, source

  7. SUB instructions • SUB SUB operand1, operand 2 operand1 =operand 1 - operand 2 operand 1: register/memory operand 2: register/memory/immediate Motaz K. Saad, Dept. of CS

  8. Addition and Subtraction Of Binary Data Example of the ADD and SUB instructions: BYTE1 DB 24H ;Data elements WORD1 DW 4000H . . . MOV CL , BYTE1 ; byte processing MOV DL , 40H ADD CL , DL ; register to register SUB CL , 20H ; Immediate from register ADD BYTE1 , BL ; register to memory MOV CX , WORD1 ; word processing MOV DX , 2000H SUB CX , DX ; register from register SUB CX , 124H ; Immediate from memory ADD WORD1 , DX ; register to memory

  9. Exercise Assume that BYTE1 is defined as DB 05, show the result for the following instructions: • Instruction Before After • MOV CX, 25H CX = 0000H CX=0025 • MOV CL, 0 CX = FFFFH CX=FF00 • MOV AX, BYTE1 AX=1234H AX=invalid size • ADD DL, BYTE1 DX=0120H DX=0125 • XCHG AH,AL AX=1234H AX=3412 • SUB CX,CX CX=1234H CX=0000 • XCHG CX,CX CX=1234H CX=1234

  10. Multiplication instructions • For multiplication process, • MUL instruction is used for unsigned data • IMUL is used for signed data. • Both of these instructions affect the Carry and Overflow flag. The format for MUL and IMUL instructions: • Observe that MUL/IMUL is a one address instruction, hence it requires an the accumulator register to hold operand1 and result (refer Chapter 5.3 - Instruction format) In IBM PC, the AX register (AL/AH/EAX) acts as accumulator. • The multiplication operations are byte times byte, word times word and doubleword times doubleword.

  11. Cont.., For multiplying two one-byte values, the multiplicand (first operand) is in AL register, and the multiplier (second operand) is a byte data in memory or another register that determined by the address field in the MUL/IMUL instruction. Example: MUL DL the operation multiplies the content of AL by the contents of DL. The generated product is in AX. The operation ignores and erases any data that may already be in AH.

  12. Field Size Address field in the MUL/IMUL instruction refers only to the multiplier that will determine the field sizes (whether byte, word, or doubleword). The instruction will use the size of the address field (multiplier) to determine the position of the multiplicand whether it is in AL, AX or EAX A few examples on the MUL instruction together with the multiplier size, multiplicand position and product:

  13. Field Size

  14. MUL is used for unsigned data Examples on the usage of the MUL instructions using the data as defined below: BYTE1 DB 80H BYTE2 DB 40H WORD1 DW 8000H WORD2 DW 2000H DWORD1 DD 00018402H DWORD2 DD 00012501H (a) MOV AL, BYTE1 ; AL (multiplicand)=80H MUL BYTE2 ; byte x byte, product in AX ; 80H x 40H, AX= 2000H (b) MOV AX, WORD1 ; AX (multiplicand)=8000H MUL WORD2 ; word x word, product in DX:AX ; 80000H x 2000H, ; DX:AX= 1000 0000H

  15. Example 1 : (MUL Instruction) MOV AL, BYTE1 MUL BYTE2 ; byte x byte, product in AX in the above example, 80H (128) is multiplied with 40H (64) and the result is 2000H (8,192) kept in AX register. MOV AX, WORD1 MUL WORD2 ; word x word, product in DX:AX in the above example, 8000H is multiplied with 2000H and the result, 1000 0000H is kept in a pair of registers, DX:AX.

  16. IMUL is used with signed data Examples on the usage of the IMUL instructions using the data as defined below: BYTE1 DB 80H BYTE2 DB 40H WORD1 DW 8000H WORD2 DW 2000H DWORD1 DD 00018402H DWORD2 DD 00012501H (a) MOV AL, BYTE1 ; AL (multiplicand)=80H (-ve value) IMUL BYTE2 ; byte x byte, product in AX ; 80H (-ve) x 40H (+ve), AX= E000H (b) MOV AX, WORD1 ; AX (multiplicand)=8000H (-ve value) IMUL WORD2 ; word x word, product in DX:AX ; 80000H (-ve) x 2000H (+ve), ; DX:AX= F000 0000H

  17. Example 2 : (Arahan IMUL) MOV AL, BYTE1 IMUL BYTE2 ; byte x byte, product in AX in the above example, BYTE1 and BYTE2 is considered as signed data, that is –128 (80H) and +64 (40H) and the result –8192 (E000H) is stored in the AX register MOV AX, WORD1 IMUL WORD2 ; word x word, product in DX:AX in the above example, WORD1 and WORD2 is considered as signed data, that is 8000H a is –ve value and 2000H is a +ve value. The result, F000 0000H is a–ve value and is kept in a pair of registers, DX:AX.

  18. Division Instructions For division operation, the DIV instruction is used for unsigned data whereas IDIV is used for signed data. Its format: The basic divide operations are byte into word, word into doubleword, and doubleword into quad word.

  19. Field Size As in MUL/IMUL, address field in the DIV/IDIV instruction refers to the divisor (second operand) that determines the field sizes. The following example shows the divisor is in the register (example 1) and memory (example2) with a certain size. Example1 : using register addressing mode Example 2 : using direct addressing mode - divisor is predefined in the memory

  20. The following are a few examples of the DIV instruction using the data definition below: BYTE1 DB 80H ; Byte value BYTE2 DB 16H WORD1 DW 2000H ; Word value WORD2 DW 0010H WORD3 DW 1000H (a) MOV AX, WORD1 ;AX=2000H DIV BYTE1 ;2000H/80H, quotient=40H, remainder=00H ;AL=40H, AH=00H (b) MOV DX, WORD2 ;DX=0010H MOV AX, WORD3 ;AX=1000H,dividend in DX:AX (WORD2:WORD3) ;DX:AX = 0010 1000H DIV WORD1 ; 00101000H/2000H remainder:quotient in DX:AX ; 1000H:0080H

  21. DIV Instruction • MOV AX, WORD1 • DIV BYTE1 In the above example, the value 2000H (8092) will be divided with 80H (128), the quotient, 40H (64) will be kept in the AL register while its remainder, 00H will be kept in the AH register. • MOV DX, WORD2 • MOV AX, WORD3 ; dividend in DX:AX (WORD2:WORD3) • DIV WORD1 ; remainder:quotient in DX:AX • in the above example, the value 00101000H will be divided with 2000H. The remainder, 1000H will be kept in the DX register, whereas its result, 0080H will be kept in the AX register.

  22. The following are a few examples of the IDIV instruction using the data definition below: BYTE1 DB 80H ; Byte value BYTE2 DB 16H WORD1 DW 2000H ; Word value WORD2 DW 0010H WORD3 DW 1000H (a) MOV AX, WORD1 ; AX=2000H IDIV BYTE1 ; 2000H(+ve)/80H (-ve), ; quotient=C0H (-ve), remainder=00H ; AL=C0H, AH=00H (b) MOV DX, WORD2 ;DX=0010H MOV AX, WORD3 ;AX=1000H,dividend in DX:AX (WORD2:WORD3) ;DX:AX = 0010 1000H (+ve) IDIV WORD1 ;00101000H (+ve)/2000H (+ve) ;remainder:quotient in DX:AX ;1000H:0080H

  23. IDIV Instruction (using the same data definition) MOV AX, WORD1 IDIV BYTE1 in the above example, the value WORD1 is a +ve number whereas BYTE1 is a –ve number. If WORD1 is divided with BYTE1, its result will be a –ve number (64 or C0H) will be kept in the AL register whereas its remainder will be kept in the AH.register MOV DX, WORD2 MOV AX, WORD3 ; dividend in DX:AX (WORD2:WORD3) DIV WORD1 ; remainder: quotient in DX:AX in the above example the value 00101000H (+ve) will be divided with 2000H (+ve). Its remainder, 1000H (+ve) will be kept in the DX register, whereas its result, 0080H (+ve) will be kept in the AX register.

  24. DIV Examples Divide 8003h by 100h, using 16-bit operands: mov dx,0 ; clear dividend mov ax,8003h ; dividend mov cx,100h ; divisor div cx ; AX = 0080h, DX = 3 Same division, using 32-bit operands: mov edx,0 ; clear dividend mov eax,8003h ; dividend mov ecx,100h ; divisor div ecx ; EAX = 00000080h, EDX = 3

  25. The End

More Related