Free Electronic Circuits & 8085 projects

Electronic projects with circuit diagram and 8085 microprocessor projects.

Explanation of Instruction set of 8086 with free Sample Programs – Arithmetic Instructions2

Published on Nov 04 2009 // 8086 Instruction set, 8086 Sample programs

SUBTRACTION INSTRUCTIONS



  • SUB Instruction – Subtract two numbers
  • 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 type , means they must be a byte location or a word location.

    • SBB Instruction – Subtract with borrow SBB destination, source

    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.

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

    Example:

    SUB CX, BX                                 ;CX – BX . Result in CX
    SUBB CH, AL                               ; Subtract contents of AL and contents CF from contents of CH .
    ;Result in CH
    SUBB AX, 3427H                          ;Subtract immediate number from AX

    Example:

    Subtracting unsigned number
                                                   ; CL = 10011100 = 156 decimal BH = 00110111 = 55 decimal
    SUB CL, BH                               ; CL = 01100101 = 101 decimal CF, AF, SF, ZF = 0, OF, PF = 1
    Subtracting signed number
                                                   ; CL = 00101110 = + 46 decimal BH = 01001010= + 74 decimal
    SUB CL, BH                               ;CL = 11100100 = – 28 decimal,CF = 1, AF, ZF =0,SF = 1 result negative

    • DEC Instruction – Decrement destination register or memory DEC destination.

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

    Example:

                                                  ; AX =8000h
    DEC AX                                    ;After this instruction AX = 7999h
    DEC BL                                    ; Subtract 1 from the contents of BL register

    • NEG Instruction – From 2’s complement – NEG destination

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

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

    Example:

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

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

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

    • CMP Instruction – Compare byte or word -CMP destination, source.

    The CMP instruction compares the destination and source ie.,it subtracts the  source from destination.The result is not stored anywhere. It neglects the  results, but sets the flags accordingly.This instruction is usually used before a conditional jump instruction.

    Example:

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

    • AAS Instruction – ASCII Adjust for Subtraction

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

    MOV AX,0901H                     ;BCD 91
    SUB AL, 9                            ;Minus 9
    AAS                                    ; Give AX =0802 h (BCD 82)

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

    Example:( b )

                                             ;AL = 0011 0101 =ASCII 5
                                             ;BL = 0011 1001 = ASCII 9
    SUB AL, BL                          ;( 5 – 9 ) Result : AL = 1111 1100 = – 4 in 2’s complement CF = 1
    AAS                                    ;Results :AL = 0000 0100 =BCD 04, CF = 1 borrow needed

    • DAS Instruction – Decimal Adjust after Subtraction

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

    if low nibble of AL > 9 or AF = 1 then:

    – AL = AL – 6

    – AF = 1

    if AL > 9Fh or CF = 1 then:

    – AL = AL – 60h

    – CF = 1

    Example:

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