Coursify

Microprocessor

Arithmetic Instructions

1 hour

Learning Goals

  • Understand basic arithmetic operations: ADD for addition with carry (CY) and overflow (OV) flag generation, SUBB for subtraction with borrow, INC/DEC for single-step increments and decrements, and recognizing appropriate flag conditions for program flow decisions
  • Master multiplication and division timing: analyzing MUL AB instruction that produces 16-bit product in B:A registers requiring 48 oscillator periods (4 machine cycles), understanding DIV AB instruction behavior for division operations, and recognizing timing constraints for real-time applications
  • Analyze flag behavior in arithmetic: carry flag (CY) indication for add overflow and borrow in subtraction, overflow flag (OV) for signed arithmetic overflow detection, auxiliary carry flag (AC) for BCD (Binary Coded Decimal) arithmetic, and parity flag (P) for parity checking
  • Apply multi-byte arithmetic: implementing 16-bit addition and subtraction using ADD/SUBB with register pairs, managing carry propagation across byte boundaries, and extending operations to 24-bit or 32-bit values through instruction chaining
  • Understand single operand arithmetic: INC instruction incrementing register or memory location without affecting overflow (OV) or carry (CY) flags, DEC instruction decrementing similarly without flag side-effects, and distinguishing from ADD/SUBB which do modify flags
  • Trace arithmetic instruction execution: MOV A, #35H followed by arithmetic operations showing flag updates, analyzing XRL A, A (XOR accumulator with itself) producing 0 result with P flag clear, and recognizing instruction sequences for common algorithms
  • Compare 8085 and 8051 arithmetic: understanding basic operation similarities (ADD, SUB, MUL, DIV availability), recognizing 8051's enhanced flag set for BCD support, and adapting arithmetic sequences between architectures
  • Apply arithmetic to practical problems: implementing parity conversion algorithms, performing offset calculations with ADD/SUBB, utilizing MUL for scaling operations, and optimizing arithmetic-heavy code for embedded applications

Arithmetic instructions form the core of data processing in the 8051 microprocessor. This group includes operations for addition, subtraction, multiplication, division, and increment/decrement. Unlike the 8085, the 8051 offers hardware-level support for multiplication and division, significantly enhancing its performance in mathematical computations and real-time control applications .

The primary execution unit for these instructions is the Arithmetic Logic Unit (ALU). Most operations involve the Accumulator (A) as a primary operand. The results of these operations often influence the Program Status Word (PSW) register, specifically the Carry (CY), Auxiliary Carry (AC), Overflow (OV), and Parity (P) flags .

Footnotes

  1. 8051 Instruction Set Manual - Official documentation on instruction timings and byte counts.

  2. Arithmetic Operations in 8051 - Detailed breakdown of flag behavior for ADD and MUL.

Arithmetic Instructions of 8051 Microcontroller Explained

Basic Arithmetic: Addition and Subtraction

The 8051 provides three main instructions for basic 8-bit math. A critical distinction in the 8051 is that there is no "SUB" instruction; only SUBB (Subtract with Borrow) is available .

  1. ADD A, : Adds the source operand to the Accumulator.
    • CY: Set if there is a carry out of bit 7.
    • AC: Set if there is a carry out of bit 3 (used for BCD).
    • OV: Set if there is a signed overflow (carry into bit 7 \neq carry out of bit 7).
  2. ADDC A, : Adds the source, the Accumulator, and the current Carry flag. This is essential for multi-byte addition.
  3. SUBB A, : Subtracts the source and the Carry flag from the Accumulator (A=AsrcCYA = A - src - CY).

Footnotes

  1. 8051 Arithmetic Instructions Overview - Video tutorial covering the logic of SUBB and DAA.

The SUBB Requirement

Because the 8051 only has SUBB, you MUST execute CLR C before starting a single-byte subtraction or the first byte of a multi-byte subtraction. Failing to clear the carry will result in an accidental borrow from a previous unrelated operation .

Footnotes

  1. Flag Register in Microprocessors - Comparison of flag logic between 8085 and 8051 architectures.

Implementing 16-bit Addition

  1. 1
    Step 1

    Move the lower 8 bits of the first number into the Accumulator and the lower 8 bits of the second number into a register or memory .

    Footnotes

    1. Multi-byte Math in 8051 - Techniques for extending 8-bit operations to 16-bit and 32-bit.

  2. 2
    Step 2

    Use ADD A, R0 to add the lower bytes. This generates the lower byte of the result and sets the Carry (CY) flag if the sum exceeds 255.

  3. 3
    Step 3

    Move the result from the Accumulator to the target memory location for the low byte.

  4. 4
    Step 4

    Load the higher 8 bits of the first number into the Accumulator. Use ADDC A, R1 to add the higher bytes plus any carry generated from the lower byte addition.

  5. 5
    Step 5

    Store the Accumulator (high byte) and check the CY flag for a final overflow beyond 16 bits.

Multiplication and Division Timing

The 8051 handles 8-bit multiplication and division using the A and B registers. These are the most time-consuming arithmetic instructions, requiring 4 machine cycles (48 oscillator periods) .

MUL AB (Multiplication)

Multiplies the unsigned 8-bit integers in A and B.

  • Result: 16-bit product. The Low Byte is stored in A, and the High Byte is stored in B.
  • Flags: CY is always cleared. OV is set if the product is greater than 255 (i.e., if register B is non-zero) .

DIV AB (Division)

Divides the unsigned 8-bit integer in A by the integer in B (A/BA / B).

  • Result: The Quotient is stored in A, and the Remainder is stored in B.
  • Flags: CY and AC are always cleared. OV is set only if the divisor (B) was 00H (Division by Zero error) .

Footnotes

  1. 8051 Instruction Set Manual - Official documentation on instruction timings and byte counts. 2

  2. Arithmetic Operations in 8051 - Detailed breakdown of flag behavior for ADD and MUL.

Instruction Execution Time (Standard 8051)

Comparison of clock cycles (Oscillator Periods) for various arithmetic instructions

Single Operand and Flag Behavior

INC (Increment) and DEC (Decrement) are used for simple stepping. A unique feature of these instructions in the 8051 is their flag independence.

  • INC/DEC : Increments or decrements the operand by 1.
  • Flag Side-effects: These instructions do not affect the Carry (CY), Auxiliary Carry (AC), or Overflow (OV) flags .
  • Parity Flag (P): Since the P flag always reflects the parity of the Accumulator, INC A or DEC A will update the P flag, but INC R0 will not .

Tracing Algorithm: Parity and Zeroing

Consider the sequence:

1MOV A, #35H ; A = 0011 0101B (4 ones, Even Parity) 2XRL A, A ; XOR A with itself. Result A = 00H

In this case, after XRL A, A, the result is 0. Since 0 has an even number of 1s (zero 1s), the P flag is cleared (0) .

Footnotes

  1. 8051 Arithmetic Instructions Overview - Video tutorial covering the logic of SUBB and DAA.

  2. Flag Register in Microprocessors - Comparison of flag logic between 8085 and 8051 architectures.

  3. Arithmetic Operations in 8051 - Detailed breakdown of flag behavior for ADD and MUL.

  • Includes MUL AB and DIV AB (hardware support).
  • Uses SUBB exclusively (requires CLR C).
  • OV flag specifically for signed arithmetic.
  • INC DPTR (16-bit increment) available.
  • 4 machine cycles for MUL/DIV.

Advanced Arithmetic Concepts

Pro Tip: Optimization

For real-time applications, avoid using MUL AB or DIV AB inside high-frequency interrupt service routines (ISRs) if possible, as they take 4x longer than standard instructions. Use bit-shifting (RL / RR) for powers of 2 instead.

Knowledge Check

Question 1 of 4
Q1Single choice

Which register pair is used to store the result of the MUL AB instruction?