Instruction Set Summary (Arithmetic Operations)

9:49 AM 2 Comments





ADDC A, (src-byte)

Function: Add with Carry

Description: ADDC simultaneously adds the byte variable indicated, the carry flag and the Accumulator contents, leaving the result in the Accumulator. The carry and auxiliary carry flags are set respectively, if there is a carry-out from bit 7 or bit 3, and cleared
otherwise. When adding unsigned integers, the carry flag indicates an overflow occurred.

OV is set if there is a carry-out of bit 6 but not out of bit 7, or a carry-out of bit 7 but not out of bit 6; otherwise OV is cleared. When adding signed integers, OV indicates a negative number produced as the sum of two positive operands or a positive sum from two negative operands.

Four source operand addressing modes are allowed: register, direct, register-indirect, or
immediate.

Example: The Accumulator holds 0C3H (11000011B) and register 0 holds 0AAH (10101010B) with the carry flag set. The following instruction,

ADDC A,             R0

leaves 6EH (01101110B) in the Accumulator with AC cleared and both the Carry flag and OV set to 1.

ADDC A,Rn
       Bytes: 1
     Cycles: 1
Operation: ADDC
                 (A) ← (A) + (C) + (Rn)

ADDC A,direct
       Bytes: 2
     Cycles: 1
Operation: ADDC
                 (A) ← (A) + (C) + (direct)

ADDC A,@Ri
       Bytes: 1
     Cycles: 1
Operation: ADDC
                 (A) ← (A) + (C) + ((Ri))

ADDC A,#data
       Bytes: 2
     Cycles: 1
Operation: ADDC
                 (A) ← (A) + (C) + #data



SUBB A,(src-byte)

Function: Subtract with borrow

Description: SUBB subtracts the indicated variable and the carry flag together from the Accumulator, leaving the result in the Accumulator. SUBB sets the carry (borrow) flag if a borrow is needed for bit 7 and clears C otherwise. (If C was set before executing a SUBB instruction, this indicates that a borrow was needed for the previous step in a multiple- precision subtraction, so the carry is subtracted from the Accumulator along with the source operand.)
AC is set if a borrow is needed for bit 3 and cleared otherwise. OV is set if a borrow is needed into bit 6, but not into bit 7, or into bit 7, but not bit 6.
When subtracting signed integers, OV indicates a negative number produced when a negative value is subtracted from a positive value, or a positive result when a positive number is subtracted from a negative number.

The source operand allows four addressing modes: register, direct, register-indirect, or immediate.

Example: The Accumulator holds 0C9H (11001001B), register 2 holds 54H (01010100B), and the carry flag is set. The instruction,

SUBB A,           R2

will leave the value 74H (01110100B) in the accumulator, with the carry flag and AC cleared but OV set.


Notice that 0C9H minus 54H is 75H. The difference between this and the above result is due to the carry (borrow) flag being set before the operation. If the state of the carry is not known before starting a single or multiple-precision subtraction, it should be explicitly cleared by CLR C instruction.

SUBB A,Rn
       Bytes: 1
     Cycles: 1
Operation: SUBB
                (A) ← (A) - (C) – (Rn)

SUBB A,direct
       Bytes: 2
     Cycles: 1
Operation: SUBB
                (A) ← (A) - (C) – (direct)

SUBB A,@Ri
       Bytes: 1
     Cycles: 1
Operation: SUBB
                (A) ← (A) - (C) – ((Ri))

SUBB A,#data
       Bytes: 2
     Cycles: 1
Operation: SUBB
                (A) ← (A) - (C) - #data




INC (byte)

Function: Increment

Description: INC increments the indicated variable by 1. An original value of 0FFH overflows to 00H. No flags are affected. Three addressing modes are allowed: register, direct, or register-indirect.

Note: When this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins.

Example: Register 0 contains 7EH (011111110B). Internal RAM locations 7EH and 7FH contain 0FFH and 40H, respectively. The following instruction sequence,

INC             @R0
INC                R0
INC             @R0


leaves register 0 set to 7FH and internal RAM locations 7EH and 7FH holding 00H and 41H, respectively.

INC A
       Bytes: 1
     Cycles: 1
Operation: INC
                (A) ← (A) + 1

INC Rn
       Bytes: 1
     Cycles: 1
Operation: INC
                (Rn) ← (Rn) + 1

INC direct
       Bytes: 2
     Cycles: 1
Operation: INC
                (direct) ← (direct) + 1

INC @Ri
      Bytes: 1
     Cycles: 1
Operation: INC
                ((Ri)) ← ((Ri)) + 1



INC DPTR

Function: Increment Data Pointer

Description: INC DPTR increments the 16-bit data pointer by 1. A 16-bit increment (modulo 2 16 ) is performed, and an overflow of the low-order byte of the data pointer (DPL) from 0FFH to 00H increments the high-order byte (DPH). No flags are affected.

This is the only 16-bit register which can be incremented.

Example: Registers DPH and DPL contain 12H and 0FEH, respectively. The following instruction sequence,

INC DPTR
INC DPTR
INC DPTR

changes DPH and DPL to 13H and 01H.

       Bytes: 1
     Cycles: 2
Operation: INC
                (DPTR) ← (DPTR) + 1



DEC byte

Function: Decrement

Description: DEC byte decrements the variable indicated by 1. An original value of 00H underflows to 0FFH. No flags are affected. Four operand addressing modes are allowed: accumulator, register, direct, or register-indirect.

Note: When this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins.

Example: Register 0 contains 7FH (01111111B). Internal RAM locations 7EH and 7FH contain 00H and 40H, respectively. The following instruction sequence,

DEC                @R0
DEC                   R0
DEC               @R0

leaves register 0 set to 7EH and internal RAM locations 7EH and 7FH set to 0FFH and 3FH.


DEC A
       Bytes: 1
     Cycles: 1
Operation: DEC
                (A) ← (A) – 1

DEC Rn
       Bytes: 1
     Cycles: 1
Operation: DEC
                (Rn) ← (Rn) – 1

DEC direct
       Bytes: 2
     Cycles: 1
Operation: DEC
                (direct) ← (direct) – 1

DEC @Ri
       Bytes: 1
     Cycles: 1
Operation: DEC

                ((Ri)) ← ((Ri)) - 1



MUL AB

Function: Multiply

Description: MUL AB multiplies the unsigned 8-bit integers in the Accumulator and register B. The low-order byte of the 16-bit product is left in the Accumulator, and the high-order byte in B. If the product is greater than 255 (0FFH), the overflow flag is set; otherwise it is cleared. The carry flag is always cleared.

Example: Originally the Accumulator holds the value 80 (50H). Register B holds the value 160 (0A0H). The instruction,

MUL AB

will give the product 12,800 (3200H), so B is changed to 32H (00110010B) and the Accumulator is cleared. The overflow flag is set, carry is cleared.

       Bytes: 1
     Cycles: 4
Operation: MUL
                (A)7-0 ← (A) X (B)
                (B)15-8



DIV AB

Function: Divide

Description: DIV AB divides the unsigned eight-bit integer in the Accumulator by the unsigned eight- bit integer in register B. The Accumulator receives the integer part of the quotient; register B receives the integer remainder. The carry and OV flags are cleared.

Exception: if B had originally contained 00H, the values returned in the Accumulator and B-register are undefined and the overflow flag are set. The carry flag is cleared in any case.

Example: The Accumulator contains 251 (0FBH or 11111011B) and B contains 18 (12H or 00010010B). The following instruction,

DIV AB

leaves 13 in the Accumulator (0DH or 00001101B) and the value 17 (11H or 00010001B) in B, since 251 = (13 x 18) + 17. Carry and OV are both cleared.

       Bytes: 1
     Cycles: 4
Operation: DIV
                (A)15-8 ← (A)/(B)
                (B)7-0



DA A

Function: Decimal-adjust Accumulator for Addition

Description: DA A adjusts the eight-bit value in the Accumulator resulting from the earlier addition of two variables (each in packed-BCD format), producing two four-bit digits. Any ADD or ADDC instruction may have been used to perform the addition.

If Accumulator bits 3 through 0 are greater than nine (xxxx1010-xxxx1111), or if the AC flag is one, six is added to the Accumulator producing the proper BCD digit in the low- order nibble. This internal addition sets the carry flag if a carry-out of the low-order four-bit field propagates through all high-order bits, but it does not clear the carry
flag otherwise.

If the carry flag is now set, or if the four high-order bits now exceed nine (1010xxxx- 1111xxxx), these high-order bits are incremented by six, producing the proper BCD digit in the high-order nibble. Again, this sets the carry flag if there is a carry-out of the high- order bits, but does not clear the carry. The carry flag thus indicates if the sum of the original two BCD variables is greater than 100, allowing multiple precision decimal addition. OV is not affected.

All of this occurs during the one instruction cycle. Essentially, this instruction performs the decimal conversion by adding 00H, 06H, 60H, or 66H to the Accumulator, depending on initial Accumulator and PSW conditions.

Note: DA A cannot simply convert a hexadecimal number in the Accumulator to BCD notation, nor does DAA apply to decimal subtraction.

Example: The Accumulator holds the value 56H (01010110B), representing the packed BCD digits of the decimal number 56. Register 3 contains the value 67H (01100111B), representing the packed BCD digits of the decimal number 67. The carry flag is set. The following instruction sequence

ADDC                   A,R3
DA                         A

first performs a standard two’s-complement binary addition, resulting in the value 0BEH (10111110) in the Accumulator. The carry and auxiliary carry flags are cleared.

The Decimal Adjust instruction then alters the Accumulator to the value 24H (00100100B), indicating the packed BCD digits of the decimal number 24, the low-order two digits of the decimal sum of 56, 67, and the carry-in. The carry flag is set by the Decimal Adjust instruction, indicating that a decimal overflow occurred. The true sum of
56, 67, and 1 is 124.

BCD variables can be incremented or decremented by adding 01H or 99H. If the Accumulator initially holds 30H (representing the digits of 30 decimal), then the following instruction sequence,

ADD                    A, # 99H
DA                       A

leaves the carry set and 29H in the Accumulator, since 30 + 99 = 129. The low-order byte of the sum can be interpreted to mean 30 - 1 = 29.

       Bytes: 1
     Cycles: 1
Operation: DA
-contents of Accumulator are BCD

IF                   [[(A 3-0 ) > 9] [(AC) = 1]]
                      THEN (A 3-0 ) ← (A 3-0 ) + 6
                                        AND
IF                   [[(A 7-4 ) > 9] [(C) = 1]]
                      THEN (A 7-4 ) ← (A 7-4 ) + 6


2 comments:

  1. What will be the problem if I use the Mnemonic ADD A,@Ri instead of ADD A,direct? and When will I use these two?

    ReplyDelete
    Replies
    1. Hi Karen! One thing that I can think of the advantage of using ADD A, @Ri instead of ADD A, direct is that it only uses 1 byte of instruction. ADD A, direct consumes 2 bytes so it is one way of using your Internal RAM wisely. Also, direct addressing (ADD A, direct) can only be use on the first 128 bytes of the memory, meaning from 00H-07FH only. But, if you want to access the remaining higher bytes, you use Indirect Addressing (ADD A, @Ri) Hope this helps! :)

      Delete