Luis B
Luis B

Reputation: 1

DAW Instruction capabilites

I know that the DAW instruction adjusts the 8bit result in the WREG after adding two packed BCD numbers but is the DAW instruction capable of adjusting a much higher bit result in WREG ?

Upvotes: 0

Views: 548

Answers (1)

njuffa
njuffa

Reputation: 26165

During an ADDWF or a SUBWF the DC (digit carry) flag captures the carry out from bit 3 during addition, while the C (carry) flag captures the carry out from bit 7 during addition. Carries in subtraction are generated based on x - y = x + NOT (y) + 1, where NOT is the one's complement. This means the value of these carry flags is the complement of the respective borrows.

DAW adds 6 to the least significant nibble of W, i.e. W<3:0>, if the nibble's value is greater than 9, or DC is set.
DAW adds 6 to the most significant nibble of W, i.e. W<7:4>, if the nibble's value is greater than 9, or C is set.

If two nibbles representing BCD digits are added, the resulting nibble value is in [0x0, 0x12], which is why two conditions need to be combined to detect a decimal carry out from that nibble. For example, when we add 0x99 to 0x99, the sum is 0x32, and DC=1 and C=1, since 0x9 + 0x9 = 0x12 meaning there is a carry out from each nibble. Because both flags are set, a following DAW adds 6 to each nibble, resulting in 0x98, which is the correct BCD result.

The DAW instruction simply follows the above rules and has no way of knowing whether the content of the W register and the flags are actually the result of the addition of two BCD digits. So lets assume we add 0xee (not a valid BCD number) and 0x11, resulting in 0xff. Both DC and C will be zero. A following DAW will find that each nibble is greater than 9, and add 6 to each nibble, resulting in 0x55.

Upvotes: 2

Related Questions