0

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 ?

Luis B
  • 1
  • 5
  • Why don't you try it and see? – Erik Eidt Mar 12 '21 at 01:32
  • @ErikEidt I dont have software to try it. I am being lectured with powerpoint slides. – Luis B Mar 12 '21 at 01:42
  • Well that's probably the first thing you should fix. If you want to learn assembly for any ISA, get a simulator (or real hardware with a working debugger) so you can single-step any instruction you're wondering about. And so you can assemble or disassemble stuff if you want to know what's legal or look at the machine code. If there's any ambiguity you're wondering about in the docs, you can always test. – Peter Cordes Mar 12 '21 at 09:12

1 Answers1

2

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.

njuffa
  • 23,970
  • 4
  • 78
  • 130