Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction Arithmetic instructions are used to perform arithmetic operation such as Addition Subtraction Multiplication Division These operations can."— Presentation transcript:

1

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 CS7

8 Example of the ADD and SUB instructions: BYTE1DB24H;Data elements WORD1DW4000H... MOVCL, BYTE1; byte processing MOVDL, 40H ADDCL, DL; register to register SUBCL, 20H; Immediate from register ADD BYTE1, BL; register to memory MOVCX, WORD1; word processing MOV DX, 2000H SUBCX, DX; register from register SUBCX, 124H; Immediate from memory ADDWORD1, DX; register to memory Addition and Subtraction Of Binary Data

9 Exercise InstructionBeforeAfter (a)MOV CX, 25HCX = 0000HCX=0025 (b)MOV CL, 0CX = FFFFHCX=FF00 (c)MOV AX, BYTE1AX=1234HAX= invalid size (d)ADD DL, BYTE1DX=0120HDX=0125 (e)XCHG AH,ALAX=1234HAX=3412 (f)SUB CX,CXCX=1234HCX=0000 (g)XCHG CX,CXCX=1234HCX=1234 Assume that BYTE1 is defined as DB 05, show the result for the following instructions:

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: BYTE1DB80H BYTE2DB40H WORD1DW8000H WORD2DW2000H DWORD1DD00018402H DWORD2DD00012501H (a)MOV AL, BYTE1 ; AL (multiplicand)=80H MULBYTE2 ; byte x byte, product in AX ; 80H x 40H, AX= 2000H (b)MOVAX, WORD1 ; AX (multiplicand)=8000H MULWORD2 ; word x word, product in DX:AX ; 80000H x 2000H, ; DX:AX= 1000 0000H

15 Example 1 : (MUL Instruction) MOV AL, BYTE1 MULBYTE2; 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. MOVAX, WORD1 MULWORD2; 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: BYTE1DB80H BYTE2DB40H WORD1DW8000H WORD2DW2000H DWORD1DD00018402H DWORD2DD00012501H (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 IMULBYTE2; 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 MOVAX, WORD1 IMULWORD2; 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

19 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.

20 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

21 The following are a few examples of the DIV instruction using the data definition below: BYTE1DB80H; Byte value BYTE2DB16H WORD1DW2000H ; Word value WORD2DW0010H WORD3DW1000H (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

22 DIV Instruction MOVAX, WORD1 DIVBYTE1 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) DIVWORD1; 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.

23 The following are a few examples of the IDIV instruction using the data definition below: BYTE1DB80H; Byte value BYTE2DB16H WORD1DW2000H ; Word value WORD2DW0010H WORD3DW1000H (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

24 IDIV Instruction (using the same data definition) MOVAX, WORD1 IDIVBYTE1 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) DIVWORD1; 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.

25 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 DIV Examples

26 The End


Download ppt "Introduction Arithmetic instructions are used to perform arithmetic operation such as Addition Subtraction Multiplication Division These operations can."

Similar presentations


Ads by Google