Arithmetic Operations
Arithmetic Operations
Arithmetic Operations
Contents
Arithmetic Operations
– Introduction
– Flags
– Incrementing & Decrementing
– Addition
– Subtraction
– Multiplication
– Division
– Decimal Arithmetic
Jump and Call Opcodes
– Introduction
– Jump and Call program range
– Call and Jump Subroutines
– Call and Returns
– Interrupts and Returns
Program Status Word
Register Bank Selector
Flags
The 8051 has four Arithmetic Flags:
–Carry Flag (CY)
–Auxiliary Carry (AC)
–Overflow (OV)
–Parity (P)
Carry Flag (CY)
The Carry flag is set whenever there is a
carry out from D7 bit.
This flag bit is affected after an Addition or
Subtraction.
It can also be set to ‘1’ or ‘0’ directly by an
instruction such as “SETB C” and “CLR C”.
Where “SETB C” is “Set bit Carry”
“CLR C” is “Clear carry”
Auxiliary Flag (AC)
If there is a carry from D3 to D4 during
an ADD or SUB operation, this bit is set.
This flag is used by instructions that
perform BCD arithmetic.
Parity Flag (P)
CY=1
PF=0
AC=1
• R0=40H, R2=5, A=0, R7=0 0000 0000
• ADD A, @R0 » A= 0+7D=7DH 0111 1110
0111 1110
• A=7D and CY=0. R7=0 & jumps to NEXT
instruction.
• R0=42H, R2= 3
0110 1000
• ADD A, @R0 » A= 68+C5= 2DH 1100 0101
• A=2DH and CY=1. R7=2 10010 1101
• R0=43H, R2=2 0010 1101
• ADD A, @R0 » A= 2D+5B=88H 0101 1011
• A=88H and CY=0 R7=2. 1000 1000
• R0=44H, R2= 1
• ADD A, @R0 » A= 88+30=B8H 1000 1000
0011 0000
• A=B8H and CY=0 R7=2. 1011 1000
• R0=45H, R2= 0
• ADD A, @R0 » A= B8+0= B8H
A=B8H and R7=2
Subtraction
In many microprocessor there are two different
instructions for subtraction:
SUB and SUBB (subtract with borrow)
In the 8051 we have only SUBB
The 8051 uses adder circuitry to perform
the subtraction.
0 CF=0 (Step 3)
Multiplication
• The 8051 supports byte by byte multiplication only
• The byte are assumed to be unsigned data.
Division
• The 8051 supports byte over byte division only
• The byte are assumed to be unsigned data.
DIV
Jump Instructions
Conditional Jump Instructions
Bit Jumps:
Bit jumps all operate according to the status of the
carry flag in the PSW or the status of any bit-
addressable location. All bit jumps are relative to the
program counter.
Bit Jump Instructions:
Byte Jumps:
Byte jumps- jump instructions that test bytes of
data- behave as bit jumps.
If the condition that is tested is true, the jump is
taken.
If the condition is false, the instruction after the
jump is executed.
All byte jumps are relative to the program
counter.
Conditional Jump Instructions
Unconditional Jump Instructions
CALL Instructions