• No results found

ADDITION INSTRUCTIONS

In document 47633033-8086-Full (Page 40-46)

ADDITION INSTRUCTIONS

 ADD Instruction - ADD dADD Instruction - ADD destination, sourceestination, source

These instructions add a number from source to a number from some destination and put These instructions add a number from source to a number from some destination and put the result in the specified destination. The source and destination must be of same type , the result in the specified destination. The source and destination must be of same type , means they must be a byte location or a word location. If you want to add a byte to a means they must be a byte location or a word location. If you want to add a byte to a word, you must copy the byte to a word location and fill the upper byte of the word with word, you must copy the byte to a word location and fill the upper byte of the word with zeroes before adding.

zeroes before adding.

 ADC Instruction - Add ADC Instruction - Add with carrywith carry

After performing the addition, the add with carry instruction ADC, adds the status of the After performing the addition, the add with carry instruction ADC, adds the status of the carry flag into the result.

EXAMPLE: EXAMPLE:

ADD

ADD AL,74H AL,74H ;Add immediate ;Add immediate number number 74H 74H to to content content of of ALAL ADC

ADC CL,BL CL,BL ;Add ;Add contents contents of of BL BL plus plus carry carry status status to to contents contents of of CL CL Results Results inin CL

CL ADD

ADD DX, DX, BX BX ;Add ;Add contents contents of of BX BX to to contents contents of of DXDX ADD

ADD DX, DX, [SI] [SI] ;Add ;Add word word from from memory memory at at offset offset [SI] [SI] in in DS DS to to contents contents of of DXDX ; Addition of Un Signed numbers

; Addition of Un Signed numbers ADD

ADD CL, CL, BL BL ;CL = ;CL = 01110011 01110011 =115 =115 decimal decimal + + BL BL = = 01001111 01001111 = = 79 79 decimaldecimal Result in CL = 11000010 = 194 decimal

Result in CL = 11000010 = 194 decimal

; Addition of Signed numbers ; Addition of Signed numbers ADD

ADD CL, CL, BL BL ;CL = ;CL = 01110011 01110011 = = + + 115 115 decimal decimal + + BL BL = = 01001111 01001111 = = +79 +79 decimaldecimal Result in CL = 11000010 = - 62 decimal

Result in CL = 11000010 = - 62 decimal

; Incorrect because result is too large to fit in 7 bits. ; Incorrect because result is too large to fit in 7 bits.

 INC Instruction - Increment - INC destinationINC Instruction - Increment - INC destination

INC instruction adds one to the operand and sets the flag according to the result. INC INC instruction adds one to the operand and sets the flag according to the result. INC instruction is treated as an unsigned binary number.

instruction is treated as an unsigned binary number.

Example: Example:

; AX = 7FFFh ; AX = 7FFFh INC

INC AX AX ;After ;After this this instruction instruction AX AX = = 8000h8000h INC

INC BL BL ; ; Add Add 1 1 to to the the contents contents of of BL BL registerregister INC

INC CL CL ; ; Add Add 1 1 to to the the contents contents of of CX CX register.register.

 AAA Instruction - ASCII Adjust after AdditionAAA Instruction - ASCII Adjust after Addition

AAA converts the result of the addition of two valid unpacked BCD digits to a valid 2- AAA converts the result of the addition of two valid unpacked BCD digits to a valid 2- digit BCD number and takes the AL register as its implicit operand.

digit BCD number and takes the AL register as its implicit operand.

Two operands of the addition must have its lower 4 bits contain a number in the range Two operands of the addition must have its lower 4 bits contain a number in the range from 0-9.The AAA instruction then adjust AL so that it contains a correct BCD digit. If  from 0-9.The AAA instruction then adjust AL so that it contains a correct BCD digit. If  the addition produce carry (AF=1), the AH register is incremented and the carry CF and the addition produce carry (AF=1), the AH register is incremented and the carry CF and auxiliary carry AF flags are set to 1. If the addition did not produce a decimal carry, CF auxiliary carry AF flags are set to 1. If the addition did not produce a decimal carry, CF and AF are cleared to 0 and AH is not altered. In both cases the higher 4 bits of AL are and AF are cleared to 0 and AH is not altered. In both cases the higher 4 bits of AL are cleared to 0.

cleared to 0.

AAA will adjust the result of the two ASCII characters that were in the range from 30h AAA will adjust the result of the two ASCII characters that were in the range from 30h (“0”) to 39h(“9”).This is because the lower 4 bits of those character fall in the range of 0- (“0”) to 39h(“9”).This is because the lower 4 bits of those character fall in the range of 0- 9.The result of addition is not a ASCII character but it is a BCD digit.

9.The result of addition is not a ASCII character but it is a BCD digit.

Example: Example:

MOV

ADD

ADD AL,5 AL,5 ;Add ;Add BCD BCD 5 5 to to digit digit in in ALAL AAA

AAA ;AH=1, ;AH=1, AL=1 AL=1 representrepresenting ing BCD BCD 11.11.

 DAA Instruction - Decimal Adjust after AdditionDAA Instruction - Decimal Adjust after Addition

The contents after addition are changed from a binary value to two 4-bit binary coded The contents after addition are changed from a binary value to two 4-bit binary coded decimal (BCD) digits. S, Z, AC, P, CY flags are altered to reflect the results of the decimal (BCD) digits. S, Z, AC, P, CY flags are altered to reflect the results of the operation.

operation.

If the value of the low-order 4-bits in the accumulator is greater than 9 or if AC flag is If the value of the low-order 4-bits in the accumulator is greater than 9 or if AC flag is set, the instruction adds 6 to the low-order four bits.

set, the instruction adds 6 to the low-order four bits.

If the value of the high-order 4-bits in the accumulator is greater than 9 or if the Carry If the value of the high-order 4-bits in the accumulator is greater than 9 or if the Carry flag is set, the instruction adds 6 to the high-order four bits.

flag is set, the instruction adds 6 to the high-order four bits.

Example: Example:

MOV

MOV AL, AL, 0Fh 0Fh ; ; AL AL = = 0Fh 0Fh (15)(15) DAA DAA ; ; AL AL = = 15h15h RET RET

SUBTRACTION INSTRUCTIONS

SUBTRACTION INSTRUCTIONS

 SUB Instruction - Subtract two nSUB Instruction - Subtract two numbersumbers

These instructions subtract the source number from destination number destination and These instructions subtract the source number from destination number destination and put the result in the specified destination. The source and destination must be of same put the result in the specified destination. The source and destination must be of same type , means they must be a byte location or a word location.

type , means they must be a byte location or a word location.

 SBB Instruction - Subtract with borrow SBB destination, sourceSBB Instruction - Subtract with borrow SBB destination, source

SBB instruction subtracts source from destination, and then subtracts 1 from source if CF SBB instruction subtracts source from destination, and then subtracts 1 from source if CF flag is set and result is stored destination and it is used to set the flag.

flag is set and result is stored destination and it is used to set the flag. ie.,destination = destination -(source + CF)

ie.,destination = destination -(source + CF)

Example: Example:

SUB

SUB CX, CX, BX BX ;CX ;CX – – BX BX . . Result Result in in CXCX SUBB

contents of CH . contents of CH . ;Result in CH ;Result in CH SUBB

SUBB AX, AX, 3427H 3427H ;Subtract ;Subtract immediate immediate number number from from AXAX

Example: Example:

-- Subtracting unsigned numberSubtracting unsigned number

; CL = 10011100 = 156 decimal BH = 00110111 = 55 ; CL = 10011100 = 156 decimal BH = 00110111 = 55 decimal

decimal SUB

SUB CL, CL, BH BH ; ; CL CL = = 01100101 01100101 = = 101 101 decimal decimal CF, CF, AF, AF, SF, SF, ZF ZF = = 0,0, OF, PF = 1

OF, PF = 1

-- Subtracting signed numberSubtracting signed number

; CL = 00101110 = + 46 decimal BH = 01001010= + 74 ; CL = 00101110 = + 46 decimal BH = 01001010= + 74 decimal

decimal SUB

SUB CL, CL, BH BH ;CL ;CL = = 11100100 11100100 = = - - 28 28 decimal,CF decimal,CF = = 1, 1, AF, AF, ZF ZF =0,SF =0,SF == 1 result negative

1 result negative

 DEC Instruction - Decrement destination DEC Instruction - Decrement destination register or memory DECregister or memory DEC destination.

destination.

DEC instruction subtracts one from the operand and sets the flag according to the result. DEC instruction subtracts one from the operand and sets the flag according to the result. DEC instruction is treated as an unsigned binary number.

DEC instruction is treated as an unsigned binary number.

Example: Example:

; AX =8000h ; AX =8000h DEC

DEC AX AX ;After ;After this this instruction instruction AX AX = = 7999h7999h DEC

DEC BL BL ; ; Subtract Subtract 1 1 from from the the contents contents of of BL BL registerregister

 NEG Instruction - From 2’s complement – NEG destinationNEG Instruction - From 2’s complement – NEG destination

NEG performs the two’s complement subtraction of the operand from zero and sets the NEG performs the two’s complement subtraction of the operand from zero and sets the flags according to the result.

flags according to the result.

;AX = 2CBh ;AX = 2CBh NEG

NEG AX AX ;after ;after executing executing NEG NEG result result AX AX =FD35h.=FD35h.

Example: Example:

NEG

NEG AL AL ;Replace ;Replace number number in in AL AL with with its its 2’s 2’s complementcomplement NEG

NEG BX BX ;Replace ;Replace word word in in BX BX with with its its 2’s 2’s complementcomplement NEG

NEG BYTE BYTE PTR[BX] PTR[BX] ; ; Replace Replace byte byte at at offset offset BX BX in in DS DS with with its its 2’s2’s complement

 CMP Instruction - Compare byte or word -CMP destination, source.CMP Instruction - Compare byte or word -CMP destination, source.

The CMP i

The CMP instruction compares the destination annstruction compares the destination and source ie.,it subd source ie.,it subtracttracts the s the source fromsource from destination.The result is not stored anywhere. It neglects the

destination.The result is not stored anywhere. It neglects the results, but sets the flresults, but sets the flagsags accordingly.This instruction is usually used before a conditional jump instruction. accordingly.This instruction is usually used before a conditional jump instruction.

Example: Example: MOV AL, 5 MOV AL, 5 MOV BL, 5 MOV BL, 5 CMP

CMP AL, AL, BL BL ; ; AL AL = = 5, 5, ZF ZF = = 1 1 (so (so equal!)equal!) RET

RET

 AAS Instruction - ASCII Adjust for SubtractionAAS Instruction - ASCII Adjust for Subtraction

AAS converts the result of the subtraction of two valid unpacked BCD digits to a single AAS converts the result of the subtraction of two valid unpacked BCD digits to a single valid BCD number and takes the AL register as an implicit operand. The two operands of  valid BCD number and takes the AL register as an implicit operand. The two operands of  the subtraction must have its lower 4 bit contain number in the range from 0 to 9 .The the subtraction must have its lower 4 bit contain number in the range from 0 to 9 .The AAS instruction then adjust AL so that it contain a correct BCD digit.

AAS instruction then adjust AL so that it contain a correct BCD digit. MOV

MOV AX,0901H AX,0901H ;BCD ;BCD 9191 SUB

SUB AL, AL, 9 9 ;Minus ;Minus 99 AAS

AAS ; ; Give Give AX AX =0802 =0802 h h (BCD (BCD 82)82)

Example:( a ) Example:( a ) ;AL =0011 1001 =ASCII 9 ;AL =0011 1001 =ASCII 9 ;BL=0011 0101 =ASCII 5 ;BL=0011 0101 =ASCII 5 SUB

SUB AL, AL, BL BL ;(9 ;(9 - - 5) 5) Result Result : : ;AL ;AL = = 00000100 00000100 = = BCD BCD 04,CF 04,CF = = 00 AAS

AAS ;Result ;Result : : AL=00000100 AL=00000100 =BCD =BCD 04 04 , , CF CF = = 0 0 NO NO BorrowBorrow required required Example:( b ) Example:( b ) ;AL = 0011 0101 =ASCII 5 ;AL = 0011 0101 =ASCII 5 ;BL = 0011 1001 = ASCII 9 ;BL = 0011 1001 = ASCII 9 SUB

SUB AL, AL, BL BL ;( ;( 5 5 - - 9 9 ) ) Result Result : : AL AL = = 1111 1111 1100 1100 = = – – 4 4 in in 2’s 2’s complementcomplement CF = 1

CF = 1 AAS

AAS ;Results ;Results :AL :AL = = 0000 0000 0100 0100 =BCD =BCD 04, 04, CF CF = = 1 1 borrow borrow neededneeded

 DAS Instruction - Decimal Adjust after SubtractionDAS Instruction - Decimal Adjust after Subtraction

This instruction corrects the result (in AL) of subtraction of two packed BCD values. The This instruction corrects the result (in AL) of subtraction of two packed BCD values. The flags which modify are AF, CF, PF, SF, ZF

flags which modify are AF, CF, PF, SF, ZF if low nibble of AL > 9 or AF = 1 then: if low nibble of AL > 9 or AF = 1 then:

- AL = AL – 6 - AL = AL – 6 - AF = 1 - AF = 1 if AL > 9Fh or CF = 1 then: if AL > 9Fh or CF = 1 then: - AL = AL - 60h - AL = AL - 60h - CF = 1 - CF = 1 Example: Example: MOV

MOV AL, AL, 0FFh 0FFh ; ; AL AL = = 0FFh 0FFh (-1)(-1) DAS DAS ; AL ; AL = = 99h, 99h, CF CF = = 11 RET RET

MULTIPLICATION INSTRUCTIONS

MULTIPLICATION INSTRUCTIONS

 MUL Instruction - Multiply unsigned bytes or words-MUL sourceMUL Instruction - Multiply unsigned bytes or words-MUL source

MUL Instruction - This instruction multiplies an unsigned multiplication of the MUL Instruction - This instruction multiplies an unsigned multiplication of the

accumulator by the operand specified by op. The size of op may be a register or memory accumulator by the operand specified by op. The size of op may be a register or memory operand . operand . Syntax: MUL op Syntax: MUL op Example: Example:

;AL = 21h (33 decimal),BL = A1h(161 decimal ) ;AL = 21h (33 decimal),BL = A1h(161 decimal ) MUL

MUL BL BL ;AX ;AX =14C1h =14C1h (5313 (5313 decimal) decimal) since since AHAH≠0, CF and OF will≠0, CF and OF will

set to 1. set to 1. MUL

MUL BH BH ; ; AL AL times times BH, BH, result result in in AXAX MUL

MUL CX CX ;AX ;AX times times CX, CX, result result high high word word in in DX,low DX,low word word in in AX.AX.

 IMUL Instruction - Multiply signed nuIMUL Instruction - Multiply signed number-IMUL sourcember-IMUL source

This instruction performs a signed multiplication.There are two types of syntax for this This instruction performs a signed multiplication.There are two types of syntax for this instruction. They are:

instruction. They are: IMUL

IMUL op op ;In ;In this this form form the the accumulator accumulator is is the the multiplicand multiplicand and and op op isis the multiplier. op may be a register or a memory operand.

the multiplier. op may be a register or a memory operand. IMUL

IMUL op1, op1, op2 op2 ;In ;In this this form form op1 op1 is is always always be be a a register register operand operand and and op2op2 may be a register or a memory operand.

IMUL

IMUL BH BH ;Signed ;Signed byte byte in in AL AL times times multiplied multiplied by by signed signed byte byte in in BHBH and result in AX . and result in AX . Example: Example: ; 69 * 14 ; 69 * 14 ; AL = 01000101 = 69 decimal ; AL = 01000101 = 69 decimal ; BL = 00001110 = 14 decimal ; BL = 00001110 = 14 decimal IMUL

IMUL BL BL ;AX ;AX = = 03C6H 03C6H = = + + 966 966 decimal decimal ,MSB ,MSB = = 0 0 because because positivepositive result , - 28 * 59

result , - 28 * 59

; AL = 11100100 = - 28 decimal ,BL = 00001110 = 14 decimal ; AL = 11100100 = - 28 decimal ,BL = 00001110 = 14 decimal IMUL

IMUL BL BL ;AX ;AX = = F98Ch F98Ch = = - - 1652 1652 decimal, decimal, MSB MSB = = 1 1 because because negativenegative result

result

 AAM Instruction - ASCII adjuAAM Instruction - ASCII adjust after Multiplicationst after Multiplication

AAM Instruction - AAM converts the result of the multiplication of two valid unpacked AAM Instruction - AAM converts the result of the multiplication of two valid unpacked BCD digits into a valid 2-digit unpacked BCD number and takes AX as an implicit BCD digits into a valid 2-digit unpacked BCD number and takes AX as an implicit

operand. To give a valid result the digits that have been multiplied must be in the range of  operand. To give a valid result the digits that have been multiplied must be in the range of  0 – 9 and the result should have been placed in the AX register. Because both operands of  0 – 9 and the result should have been placed in the AX register. Because both operands of  multiply are required to be 9 or less, the result must be less than 81 and thus is

multiply are required to be 9 or less, the result must be less than 81 and thus is

completely contained in AL. AAM unpacks the result by dividing AX by 10, placing the completely contained in AL. AAM unpacks the result by dividing AX by 10, placing the quotient (MSD) in AH and the remainder (LSD) in AL.

quotient (MSD) in AH and the remainder (LSD) in AL.

Example: Example: MOV AL, 5 MOV AL, 5 MOV BL, 7 MOV BL, 7 MUL

MUL BL BL ;Multiply AL ;Multiply AL by by BL BL , , result result in in AXAX AAM

AAM ;After ;After AAM, AAM, AX AX =0305h =0305h (BCD (BCD 35)35)

In document 47633033-8086-Full (Page 40-46)

Related documents