Difference between revisions of "DAA"

From GbdevWiki
Jump to: navigation, search
(Created page with ' +---+---+---+--------------+--------------+--------------+---------+ | N | H | C | Upper Nybble | Lower Nybble | Number Added | C After | +---+---+---+--------------+---------…')
 
Line 1: Line 1:
+---+---+---+--------------+--------------+--------------+---------+
+
If an addition operation is performed between 15 (BCD) and 27 (BCD), simple decimal arithmetic gives this result:
| N | H | C | Upper Nybble | Lower Nybble | Number Added | C After |
+
 
  +---+---+---+--------------+--------------+--------------+---------+
+
  15
| 0 | 0 | 0 | 9-0          | 0-9          | 00          | 0      |
+
  + 27
| 0 | 0 | 0 | 0-8          | A-F          | 06          | 0      |
+
  --
| 0 | 1 | 0 | 0-9          | 0-3          | 06          | 0      |
+
  42
| 0 | 0 | 0 | A-F          | 0-9          | 60          | 1      |
+
 
| 0 | 0 | 0 | 9-F          | A-F          | 66          | 1      |
+
But when the binary representations are added in the Accumulator according to standard binary arithmetic.
| 0 | 1 | 0 | A-F          | 0-3          | 66          | 1      |
+
 
  | 0 | 0 | 1 | 0-2          | 0-9          | 60          | 1      |
+
  0001 0101
| 0 | 0 | 1 | 0-2          | A-F          | 66          | 1      |
+
  + 0010 0111
| 0 | 1 | 1 | 0-3          | 0-3          | 66          | 1      |
+
  ---------
+---+---+---+--------------+--------------+--------------+---------+
+
  0011 1100 = $3C
| 1 | 0 | 0 | 0-9          | 0-9          | 00          | 0      |
+
 
| 1 | 1 | 0 | 0-8          | 6-F          | FA          | 0      |
+
the sum is ambiguous. The DAA instruction adjusts this result so that the correct BCD representation is obtained:
| 1 | 0 | 1 | 7-F          | 0-9          | A0          | 1      |
+
  0011 1100
  +---+---+---+--------------+--------------+--------------+---------+
+
  + 0000 0110
 +
  0100 0010 = $42
  
 
== Instructions ==
 
== Instructions ==
Line 25: Line 26:
 
  Z: 1 if Accumulator is zero after operation; 0 otherwise
 
  Z: 1 if Accumulator is zero after operation; 0 otherwise
 
  N: not affected
 
  N: not affected
  H: see instruction
+
  H: 0
  C: see instruction
+
  C: 1 if N is 0 and A is greater than $99, 0 otherwise
 +
 
 +
== Pseudo-code ==
 +
 
 +
void DAA(void)
 +
{
 +
    if (n)
 +
    {
 +
        if (c) { reg.a -= 0x60; }
 +
        if (h) { reg.a -= 0x06; }
 +
    }
 +
    else
 +
    {
 +
        if (c || (reg.a & 0xFF) > 0x99) { reg.a += 0x60; c = 1; }
 +
        if (h || (reg.a & 0x0F) > 0x09) { reg.a += 0x06; }
 +
    }
 +
 +
    z = reg.a == 0;
 +
    h = 0;
 +
}

Revision as of 08:46, 25 May 2012

If an addition operation is performed between 15 (BCD) and 27 (BCD), simple decimal arithmetic gives this result:

  15
+ 27
  --
  42

But when the binary representations are added in the Accumulator according to standard binary arithmetic.

  0001 0101
+ 0010 0111
  ---------
  0011 1100 = $3C

the sum is ambiguous. The DAA instruction adjusts this result so that the correct BCD representation is obtained:

  0011 1100
+ 0000 0110
  0100 0010 = $42

Instructions

DAA - $27 - 1 Machine Cycle

Flags

Z: 1 if Accumulator is zero after operation; 0 otherwise
N: not affected
H: 0
C: 1 if N is 0 and A is greater than $99, 0 otherwise

Pseudo-code

void DAA(void)
{
    if (n)
    {
        if (c) { reg.a -= 0x60; }
        if (h) { reg.a -= 0x06; }
    }
    else
    {
        if (c || (reg.a & 0xFF) > 0x99) { reg.a += 0x60; c = 1; }
        if (h || (reg.a & 0x0F) > 0x09) { reg.a += 0x06; }
    }

    z = reg.a == 0;
    h = 0;
}