Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright 2000ELEC 242 Arithmetic Instructions1 Arithmetic Instructions Arithmetic and logical instructions modify the contents of the Flag (Status) register.

Similar presentations


Presentation on theme: "Copyright 2000ELEC 242 Arithmetic Instructions1 Arithmetic Instructions Arithmetic and logical instructions modify the contents of the Flag (Status) register."— Presentation transcript:

1 Copyright 2000ELEC 242 Arithmetic Instructions1 Arithmetic Instructions Arithmetic and logical instructions modify the contents of the Flag (Status) register based on the operation performed and the result of the operation.

2 Copyright 2000ELEC 242 Arithmetic Instructions2 ADD ADD destination,source DEST <= DEST + SOURCE The operand may be immediate, a register, or memory. Both Operands must be the same size. ADD AL,BH ;AL <= AL + BH ADD [SUM],AX ;[SUM] <= [SUM] + AX ADD EAX, 5 ;EAX <= EAX + 0000 0005H

3 Copyright 2000ELEC 242 Arithmetic Instructions3 ADD – Flag Modification mov al, 0FFh; AL <= AL + 0FFH add al,1; AL=00h, OF=0, CF=1 add al, 07FH; AL=80h, OF=1, CF=0 add al,80h; AL=00h, OF=1, CF=1 Therefore, we may have either type of overflow or both types of overflow at the same time

4 Copyright 2000ELEC 242 Arithmetic Instructions4 SUB SUB destination,source DEST <= DEST - SOURCE The operand may be immediate, a register, or memory. Both operands must be of the same size and they cannot be both mem operands Recall that to perform A - B the CPU in fact performs A + (-B) or 2’s complement addition.

5 Copyright 2000ELEC 242 Arithmetic Instructions5 SUB – Flag Modification affect all the status flags based on the result ZF (zero flag) = 1 iff (if and only if) the result is zero SF (sign flag) = 1 iff the msb of the result is one OF (overflow flag) = 1 iff there is a signed overflow CF (carry flag) = 1 iff there is an unsigned overflow Signed overflow: when the operation generates an out-of- range signed value Unsigned overflow: when the operation generates an out- of-range unsigned value Both types of overflow occur independently and are indicated individually by CF and OF

6 Copyright 2000ELEC 242 Arithmetic Instructions6 SUB Examples mov ax,2;flags not modified on mov sub ax,1;sf=0 cf=0 sub ax,1;sf=1 cf=1 sub ax,80H;sf=1 cf=0

7 Copyright 2000ELEC 242 Arithmetic Instructions7 INC and DEC The INC (increment) and DEC (decrement) instructions add 1 or subtract 1 from a single operand (either mem or reg operand) INC destination INCSI;SI <= SI + 1 INCDX;DX <= DX + 1 INC[COUNT];a memory location COUNT DEC destination DEC [DI] ;decrement a memory location DECCX;CX <= CX - 1 DEC[COUNT];a memory location COUNT

8 Copyright 2000ELEC 242 Arithmetic Instructions8 Flags for INC and DEC mov BH,0FFh; CF=0, OF=0 inc bh ; BH=00h, CF=0, OF=0 mov BH,7Fh; CF=0, OF=0 inc bh; BH=80h, CF=0, OF=1

9 Copyright 2000ELEC 242 Arithmetic Instructions9 Multiply Instructions The multiply instruction multiplies an 8-bit, 16- bit, or 32-bit operand in AL, AX, or EAX by a register or memory multiplier. The operand cannot be immediate. The instruction format is: mulreg mulmem

10 Copyright 2000ELEC 242 Arithmetic Instructions10 Multiply Format MultiplicandMultiplierResult AL8-bitAX 16-bitDX:AX EAX32-bitEDX:EAX

11 Copyright 2000ELEC 242 Arithmetic Instructions11 MULT Examples ;Example 1 moval,20 movbl,4 mulbl; The product is in AX ;Example 2.data val1dw2000 val2dw1000 movax,val1 mulval2; The product is in DX:AX

12 Copyright 2000ELEC 242 Arithmetic Instructions12 MULT Examples ; Make sure to enable 386 instruction set.data val1dw75 000 val2dw2 500 000 moveax,val1 mulval2 ; The product is in EDX:EAX

13 Copyright 2000ELEC 242 Arithmetic Instructions13 Divide Instructions The divide instruction multiplies an 8-bit, 16-bit, or 32-bit operand in, AX, or EAX by a register or memory multiplier. The operand cannot be immediate. The instruction format is: divreg divmem

14 Copyright 2000ELEC 242 Arithmetic Instructions14 Divide Instruction Format DividendDividerQuotientRemainder AX8-bitALAH DX:AX16-bitAXDX EDX:EAX32-bitEAXEDX

15 Copyright 2000ELEC 242 Arithmetic Instructions15 DIV Examples movax,21 movbl,4 divbl ; The quotient is in AL ; The remainder is in AH Example 2: Divide 2000 by 1000 using a memory operand:.data val1dw2000 val2dw1000 mov dx,0 movax,val1 divval2 ; The remainder:quotient is located in DX:AX

16 Copyright 2000ELEC 242 Arithmetic Instructions16 DIV Examples ;Example 3.data val1dw50 000 val2dw2 505 000 mov edx,0 moveax,val2 divval1 ; The remainder:quotient is in EDX:EAX

17 Copyright 2000ELEC 242 Arithmetic Instructions17 Compare Instruction Compare is similar to subtract The result is not retained. Only the flags are set or cleared. The syntax of the instruction is CMP DEST,SOURCE Both operands must be the same data size. Based on the results of the compare, all flags are set or cleared.

18 Copyright 2000ELEC 242 Arithmetic Instructions18 Compare Example Copy a character into a register See if it’s a specific character. ;Compare DS:SI contents to a tilde CMP [SI], “~”;IF memory (SI is pointer) is ~ JZFINI;THEN Jump if zero to FINI MOVDL, [SI] ;ELSE Copy memory to DL In the above code: CMP is an IF JZ is a THEN MOV is an ELSE


Download ppt "Copyright 2000ELEC 242 Arithmetic Instructions1 Arithmetic Instructions Arithmetic and logical instructions modify the contents of the Flag (Status) register."

Similar presentations


Ads by Google