Ora

What is the Function of the DAD Instruction in an 8085 Microprocessor?

Published in 8085 Instruction Set 3 mins read

The DAD instruction in an 8085 microprocessor is a powerful 16-bit arithmetic instruction primarily used to double the 16-bit value stored in the HL register pair. It achieves this by adding the contents of the HL register pair to itself and then storing the sum back into the HL register pair.


Understanding DAD: Double Add

DAD stands for Double Add. This mnemonic perfectly describes its core operation. When executed, it performs a 16-bit addition where the source and destination operands are both implicitly the HL register pair.

Key characteristics of the DAD H instruction:

  • Operation: It adds the 16-bit value currently held in the HL register pair to itself.
  • Result Storage: The 16-bit sum generated from this addition is stored back into the HL register pair, overwriting its original contents.
  • Effect: The overall effect is that the numerical value within the HL register pair is efficiently doubled.
  • Implicit Operand: Unlike some other instructions, DAD H doesn't require an explicit operand for the source as it implicitly uses HL for both the addend and augend.

How the DAD H Instruction Works

The DAD H instruction operates on 16-bit data, treating the HL register pair as a single 16-bit entity.

Step-by-Step Execution

  1. Fetch HL Value: The microprocessor retrieves the 16-bit value from the HL register pair (where H holds the higher 8 bits and L holds the lower 8 bits).
  2. Self-Addition: This retrieved 16-bit value is then added to itself.
  3. Store Result: The 16-bit sum resulting from this addition is written back into the HL register pair, effectively replacing the original value.

Flag Effects

A crucial aspect of the DAD instruction is its impact on the 8085's flag register:

  • Carry Flag (CY): This is the only flag affected by the DAD instruction. It is set to 1 if there is a carry out from the most significant bit (bit 15) of the 16-bit addition, indicating an overflow. Otherwise, it is reset to 0.
  • Other Flags: The Zero (Z), Sign (S), Parity (P), and Auxiliary Carry (AC) flags remain unaffected by the DAD instruction. This is a significant difference from 8-bit arithmetic instructions, which typically update all condition flags.

Example of DAD H in Action

Let's illustrate the operation of the DAD H instruction with a simple assembly code snippet.

Scenario

Suppose the HL register pair initially holds the hexadecimal value 2000H. We want to double this value.

    LXI H, 2000H   ; Load the HL register pair with the value 2000H
                   ; (H = 20H, L = 00H)

    DAD H          ; Double the value in HL
                   ; HL <= HL + HL
                   ; HL <= 2000H + 2000H
                   ; HL now contains 4000H
                   ; (H = 40H, L = 00H)

Explanation

  1. LXI H, 2000H: This instruction initializes the HL register pair. After this instruction, H contains 20H and L contains 00H, forming the 16-bit value 2000H.
  2. DAD H: This instruction takes the current value of HL (2000H) and adds it to itself.
    • 2000H + 2000H = 4000H.
    • The result, 4000H, is then stored back into the HL register pair. Now, H contains 40H and L contains 00H.
    • Since 4000H fits within 16 bits, the Carry Flag (CY) would typically be reset to 0 in this particular example. If the sum had exceeded FFFFH, the CY flag would be set.

Practical Applications and Use Cases

The DAD H instruction is a versatile tool in 8085 programming, particularly useful in scenarios requiring efficient 16-bit manipulation:

  • Fast Multiplication by Two: It provides the quickest way to multiply a 16-bit number by two. This is often more efficient than performing a series of shifts or general 16-bit additions with other register pairs.
  • Memory Addressing Calculations: When working with arrays or data structures where elements are spaced by multiples of two bytes, DAD H can be used to quickly calculate the address of the next or a subsequent element. For instance, if HL points to an address and you need to access the element two positions away in an array of words, DAD H can effectively advance the pointer by two addresses.
  • Offset Generation: It can be used in routines that generate memory offsets, especially when calculating addresses relative to a base pointer stored in HL.
  • Loop Control: In certain loop structures where an index or counter needs to increment by two, DAD H can be a concise way to update the counter if it's stored in HL.

DAD H Instruction Properties Summary

The following table summarizes key properties of the DAD H instruction:

Property Description
Mnemonic DAD H
Description Adds the contents of HL to HL, stores the result in HL
Operation HL ← HL + HL
Bytes 1 (It's a single-byte instruction)
Machine Cycles 3
T-States 10
Flags Affected Only the Carry Flag (CY). Other flags are unaffected.

Further Reading

For a deeper dive into the 8085 microprocessor and its instruction set, consider exploring these resources: