Texas Instruments MSC1210 User Manual

Page 213

Advertising
background image

Performing Additions (ADD, ADDC)

16-19

8052 Assembly Language

This code assumes that a 16-bit number is in Internal RAM address 30

H

(high

byte) and address 31

H

(low byte). The code will add 1045

H

to the number, leav-

ing the result in addresses 32

H

(high byte) and 33

H

(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 31

H

. It then adds 45

H

to it. It does not matter what

the carry bit holds because the ADD instruction is used. The result is moved
to 33

H

and the high byte of the original address is moved to the accumulator

from address 30

H

. The ADDC instruction then adds 10

H

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 F0

H

and the value 20

H

is added to it, the accumu-

lator holds the result of 10

H

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 2E

H

and the value 05

H

is added to it, the accumulator then equals 33

H

as expected, but the AC bit is

set because the low nibble overflowed from E

H

to 3

H

.

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 20

H

to 70

H

(two positive num-

bers) would produce the value 90

H

. However, if the accumulator is being

treated as a signed number the value 90

H

would represent the number –10

H

.

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.

Advertising