0% found this document useful (0 votes)
84 views42 pages

8051 Arithmetic Flags Overview

The document discusses arithmetic operations and jump instructions in an 8051 microcontroller. It describes the flags used in arithmetic like carry, auxiliary carry, overflow and parity. It explains how instructions like addition, subtraction, multiplication and division affect these flags. It also discusses incrementing, decrementing and conditional/unconditional jump instructions. The CALL instruction is used to call subroutines and return from interrupts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views42 pages

8051 Arithmetic Flags Overview

The document discusses arithmetic operations and jump instructions in an 8051 microcontroller. It describes the flags used in arithmetic like carry, auxiliary carry, overflow and parity. It explains how instructions like addition, subtraction, multiplication and division affect these flags. It also discusses incrementing, decrementing and conditional/unconditional jump instructions. The CALL instruction is used to call subroutines and return from interrupts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit - IV

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)

The parity flag reflects the number


of 1s in the A register only.
If the A-register contains an odd
number of 1s, then P=1.
 If A-register has even number of 1s,
then P=0.
Overflow Flag (OV)
The Overflow flag is set whenever the result
of a signed number operation is too large,
causing the high-order bit to overflow into the
sign bit.
This flag is only used to detect errors in
Signed arithmetic operations.

Note: The Carry flag is used to detect errors in


Unsigned arithmetic operations.
Instructions that affect flag bits

The Flag bits CY,


OV, AC and P are
affected by ADD
and SUB
instructions.
Example: 3-1
Example: 3-2
Example: 3-3
Incrementing and Decrementing
Mnemonic Operation

INC A Add a one to the A-register.

INC Rr Add a one to register Rr.

INC add Add a one to direct address.

INC @Rp Add a one to the contents of the address of Rp.

INC DPTR Add a one to the 16-bit register.

DEC A Subtract a one from the A-register.

DEC Rr Subtract a one from register Rr.

DEC add Subtract a one from direct address.


Subtract a one from the contents of the address of
DEC @Rp Rp.
Addition Instruction
ADD A, source ;A = A + source
The instruction ADD is used to add two
operands.
• Destination operand is always in register A.
• Source operand can be a register, immediate
data, or in memory.
• Memory-to-memory arithmetic operations
are never allowed in 8051 Assembly
language.
Example:

PF=0, since there are


zero ones

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=41H, R2= 4 0111 1110


1110 1011
• ADD A, @R0 » A= 7D+EB= 68H 10110 1000
• A=68H and CY=1. R7=1

• 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.

SUBB A, source ;A = A – source – CY


To make SUB out of SUBB, we have to make CY=0
prior to the execution of the instruction.
SUBB when CY = 0
The steps of the hardware of the CPU in
executing the SUBB instruction.
1. Take the 2’s complement of the subtrahend
(source operand)
2. Add it to the minuend (A)
3. Invert the carry
These three steps are performed for every
SUBB instruction by the internal hardware of
the 8051 CPU.
After these three steps the result is obtained and
flags are set.
Example:
Show the steps involved in the following.
CLR C ; make CY=0
MOV A, #3FH ; load 3FH into A (A=3FH)
MOV R3, #23H ; load 23H into R3 (R3=23H)
SUBB A, R3 ; subtract A-R3, place result in A
(Step 1)
A = 3F (Step 2)
0011 1111 1) 0010 0011
R3 = 23 0010 0011 2) 1101 1100
1C 3) 1
1101 1101

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

Common questions

Powered by AI

Because the 8051 microprocessor supports only byte by byte multiplication and division, handling larger numerics or floating-point operations requires constructing complex algorithms with multiple instructions to simulate multi-byte arithmetic. These operations involve managing overflow and carry manually and require breaking down larger tasks into several smaller ones, maintaining data in arrays or wider registers if available. Efficient program design and understanding of the 8051's limitations can mitigate these challenges by using logic circuits or external maths coprocessors as hardware solutions .

Flag handling critically affects arithmetic results in the 8051 by indicating carry, borrow, overflow, and parity states, which inform decision branches during operations. A carry flag, for instance, denotes overflow in unsigned additions, essential for multi-byte addition handling. The overflow flag alerts to errors in signed arithmetic, necessitating error management. Correct interpretation and management of these flags are crucial for reflective and efficient arithmetic, ensuring precision and error-free computation in more complex operations .

The 8051 microprocessor uses two main types of jump instructions: conditional and unconditional. Conditional jumps are executed based on the result of specific tests, such as zero or non-zero values, which affect program flow based on conditions, often utilized in loops and decision-making processes. Unconditional jumps move to a specified address regardless of conditions, used to structure program flow, enabling transitions between different program blocks. Their efficient use is crucial in optimizing program control structures .

The Register Bank Selector in the 8051 microprocessor selects different banks for register R0 to R7, optimizing data storage and access speed. Using PSW bits to switch bank access allows efficient management of CPU states by arranging data storage to minimize read/write times, crucial during context switches or interrupt service routines. This enhances processing efficiency by tailoring memory usage according to specific task demands, effectively managing limited register space and improving operation execution time .

In the 8051 microprocessor, 'SUBB when CY=0' involves three steps. Step 1: the CPU takes the 2's complement of the subtrahend . Step 2: it adds this complement to the accumulator (minuend). Step 3: it inverts the result's carry flag. This approach allows the execution of a subtraction as a modified addition, where initially setting CY to 0 ensures the SUBB behaves as a normal subtraction without borrowing effects .

In the 8051 microprocessor, the addition instruction affects several flags: the Carry Flag (CY) is set if there is a carry out from the D7 bit, the Auxiliary Carry (AC) is used if a carry occurs from D3 to D4, and the Parity Flag (P) reflects the number of 1s in the accumulator. For instance, if you perform an addition where A=7D and add an immediate data resulting in a greater value than 8F, the CY would become 1, indicating a carry has occurred. The Parity Flag would adjust according to the new binary result of A .

Subroutines facilitate modular programming in the 8051 by allowing a block of code to perform a specific task to be reused with CALL and RET instructions, enhancing code manageability and reuse. The CALL instruction stores the return address on the stack and jumps to the subroutine's start, while RET returns execution to the next instruction after CALL by retrieving the stored address. This method optimizes resource usage, streamlines complex operations, and enhances program clarity and efficiency .

The 8051 microprocessor has four arithmetic flags: Carry Flag (CY), Auxiliary Carry (AC), Overflow (OV), and Parity (P). The Carry Flag is set whenever there is a carry out from the D7 bit, indicating unsigned overflow. The Auxiliary Carry is set if there is a carry from D3 to D4, commonly used in BCD arithmetic. The Overflow Flag is set when the result of a signed operation is too large, causing the most significant bit to overflow into the sign bit, being key for detect errors in signed arithmetic operations. The Parity Flag reflects the number of 1s in the accumulator; if it's odd, P=1, otherwise P=0 .

In the 8051 microprocessor, subtraction is performed using the 'SUBB' instruction, which subtracts the source operand and the carry bit from the accumulator. The execution involves taking the 2's complement of the source operand, adding it to the accumulator, and inverting the carry. Initially, CY is cleared to 0 to perform a plain subtraction (A - Source). The carry flag helps manage borrow which might occur if the subtraction results in a negative outcome .

Incrementing and decrementing instructions in the 8051 allow for efficient arithmetic operations on register contents and memory addresses. Using 'INC', a value is increased by 1. This can be performed on the accumulator, a register, a direct address, or indirectly via a register pointer. Similarly, 'DEC' reduces the value by 1. They are crucial for iterative operations and loops within programming, enabling counting and iterative computation tasks effectively .

You might also like