Difference between revisions of "DAA"
From GbdevWiki
(Created page with ' +---+---+---+--------------+--------------+--------------+---------+ | N | H | C | Upper Nybble | Lower Nybble | Number Added | C After | +---+---+---+--------------+---------…') |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | {{Opcode}} | |
− | + | ||
− | + | 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 == | == Instructions == | ||
Line 25: | Line 28: | ||
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: | + | H: 0 |
− | C: | + | 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; | ||
+ | } |
Latest revision as of 08:55, 25 May 2012
This is an article about the GB-Z80 CPU.
Opcodes:
Arithmetic and logical: ADD - ADD (16-bit) including LD HL,SP+nn - ADC - AND - CP - CPL - DAA - DEC - DEC and INC (16-bit) - INC - SBC - SUB - OR - XOR
Conditional: CALL - JP - JR - RET
Load: LD (8-bit) - LD (16-bit)
Extended Set: BIT - RES - RL - RLC - RR - RRC - SET - SLA - SRA - SRL - SWAP
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; }