UMBC CMSC 391 -- Programming Microcontrollers  


Introduction

What instructions are considered arithmetic?
TypeInstruction
Add ADD A, direct
ADD A, @Ri
ADD A, #data
ADD A, Rn
ADDC A, direct
ADDC A, @Ri
ADDC A, #data
ADDC A, Rn
Decimal Adjust DA A
Decrement DEC A
DEC direct
DEC @Ri
DEC Rn
Division DIV A/B
Increment INC A
INC direct
INC DPTR
INC @Ri
INC Rn
Multiplication MUL AB
Subtraction SUBB A,@Ri
SUBB A, #data
SUBB A, Rn

Notes

Flags

The complete set of flags that can affect the flags is in the following table:

Unless indicated otherwise, the flags will be set according to the results of the operation
InstructionCarryAxillary Carry Overflow
ADD C AC OV
ADDC C AC OV
ANL C, direct C    
CJNE C    
CLR C C = 0    
CPL C = not C    
DA A C    
DIV C = 0   OV
MOV C, direct C    
MUL C = 0   OV
ORL C, direct C    
RLC C    
RRC C    
SETB C C = 1    
SBBC C AC OV

Signed Vs. Unsigned Operations

The OV flag is only used for signed addition and subtraction. If there is a carry from bit 6 it goes into the OV flag. When you are doing signed operations and the OV flag is set, you must complement the sign. When operating on signed values, the minimum value is zero. The maximum value is 255. Unsigned values range from -128 to +127.

Suppose we want to add -50 to -50. 5010 is 3216. Two's complement makes it CE16. Converting that binary and adding looks like:


  1  1 11    <-- Carry
  1100 1110
  1100 1110
-----------
1 0001 1100
  
Notice two things, there was a carry out of bit 6 and the was a carry into the Carry flag. When working with signed values you ignore the Carry flag and when the OVerflow flag is set, you must complement the sign bit. So in our example, the correct result now becomes 1001 1100 which is 9C16. The 2's complement is 6416, which is 10010 which means that binary value is -10010. One problem here is that the Carry flag is now set and does not mean anything. Might want to remember that.

Decimal Arithmetic

Sometimes it is more convenient to use the decimal number system to represent the numbers. That causes a problem when adding 1 to 9. When working with packed BCD numbers (each nybble holds a BCD number), you have to do a decimal adjust (DA), which adds six to the least significant nybble.

Example

2000:                       .org    2000h
                            
                   start:
2000: 74 39                 mov     A, #39h         ; ASCII '9'
2002: 54 0F                 ANL     A, #0Fh         ; convert to BCD  -
2004: 24 09                 ADD     A, #09h         ; Now it holds 0001
                                                    ;  We want it to be 18h (Pac
2006: D4                    DA      A               ; Add 6 to the value, giving
                                                    ; 00011000b or 18h
  

Multiplication

The Phillips Semiconductors Manual Chapter 2: 8051 Family Programmer's Guide and Instruction Set states on page 43:
QUOTE

Description: MUL AB multiplies the unsigned eight-bit integers in the Accumulator and register B. The low-order byte of the sixteen-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.

UNQUOTE

Division

The Phillips Semiconductors Manual Chapter 2: 8051 Family Programmer's Guide and Instruction Set states on page 29:
QUOTE

Description: DIV AB divides the unsigned eight-big integer in the Accumulator by the unsigned eight-bit integer in register B. The Accumulator receives the integer part of the quotient; register B the integer remainder. The carry and OV flags will cleared. Exception: if B had originally contained 00H, the values returned in the Accumulator and B-register will be undefined and the overflow flag will be 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 instruction, DIV AB will leave 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 both be cleared.

UNQUOTE


©2004, Gary L. Burt