Performing Additions (ADD, ADDC)

This code assumes that a 16-bit number is in Internal RAM address 30H (high byte) and address 31H (low byte). The code will add 1045H to the number, leav- ing the result in addresses 32H (high byte) and 33H (low byte).

MOV A,31h ;Move value from IRAM address 31h (low byte) to ;accumulator

ADD A,#45h ;Add 45h

to the

accumulator (45h is low

;byte of

1045h)

 

MOV 33h,A ;Move the result from accumulator to ;IRAM address 33h

MOV A,30h ;Move value from IRAM address 30h (hi byte) to ;accumulator

ADDC A,#10h ;Add 10h to the accumulator (10h is the high ;byte of 1045h)

MOV 32h,A ;Move result from accumulator to ;IRAM address 32h

This code first loads the accumulator with the low byte of the original number from internal RAM address 31H. It then adds 45H to it. It does not matter what the carry bit holds because the ADD instruction is used. The result is moved to 33H and the high byte of the original address is moved to the accumulator from address 30H. The ADDC instruction then adds 10H to it, plus any carry that might have occurred in the first ADD step.

Both ADD and ADDC set the carry flag if an addition of unsigned integers re- sults in an overflow that cannot be held in the accumulator. For example, if the accumulator holds the value F0H and the value 20H is added to it, the accumu- lator holds the result of 10H and the carry bit is set. The fact that the carry bit is set can subsequently be used with the ADDC to add the carry bit into the next addition instruction.

The auxiliary carry (AC) bit is set if there is a carry from bit 3, and cleared other- wise. For example, if the accumulator holds the value 2EH and the value 05H is added to it, the accumulator then equals 33H as expected, but the AC bit is set because the low nibble overflowed from EH to 3H.

The overflow (OV) bit is set if there is a carry out of bit 7 but not out of bit 6, or out of bit 6 but not out of bit 7. This is used in the addition of signed numbers to indicate that a negative number was produced as a result of the addition of two positive numbers, or that a positive number was produced by the addition of two negative numbers. For example, adding 20H to 70H (two positive num- bers) would produce the value 90H. However, if the accumulator is being treated as a signed number the value 90H would represent the number –10H. The fact that the OV bit was set means that the value in the accumulator should not really be interpreted as a negative number.

Note:

Many other (non-8052) architectures only have a single type of ADD instruction— one that always includes the carry bit in the addition. The reason 8052 assembly language has two different types of ADD instructions is to avoid the need to start every addition calculation with a CLR C instruction. Using the ADD instruction is the same as using the CLR C instruction followed by the ADDC instruction.

8052 Assembly Language

16-19

 

Page 213
Image 213
Texas Instruments MSC1210 manual Performing Additions ADD, Addc