Now we can see the affects of the DAA instruction, or at least it will become
clear if you can unscramble the above table. Remember, the DAA instruction is to
enable the use of packed BCD arithmetic. All right, what is packed BCD arithmetic.
Well you all know we’ve got eight bits available in the accumulator, and to date we’ve
been plugging it with data in HEX, two digits at a time, the characters 0 thro’ F. Now
BCD stands for Binary Coded Decimal, and uses four bits to represent a decimal number,
0 thro’ 9. As we’ve got eight pits in the accumulator we can accommodate two decimal
numbers, giving a decimal number range of O thro’ 99. The packed bit in ‘packed BCD’
simply means that there is more than one digit.
OK, so let’s add two numbers in decimal:
Now lets add the packed binary representations of the two numbers
0001 0101 see that 0001 is the 1 and 0101 is the 5
0011 1100 Something went wrong, the result is 3C
Well it’s obvious what has happened, the numbers were added in pure binary and not in
packed BCD, now the Z80 doesn’t have an ‘ADD packed BCD’ instruction, but the DAA
instruction is provided to affect the result in such a way that the effect of having
an ‘ADD packed BCD’ instruction IS provided. The reason for doing it this way is
because only one ‘correction’ instruction is required for seven operations, whereas
seven additional instructions would be needed in the Z80 if this were to be
implemented directly. Add up the numbers, low nibble first, 0101 + O111 = 1100 = C,
notice there is no ‘carry’ into the high nibble, so the invisible ‘half carry’ flag,
H, is not set. By the same process, Q0001 + 0010 = 0011 = 3, again, there is no carry,
so the C flag wasn’t set. The result is 3C with neither the H or C flags set. Now to
determine what the DAA instruction will do with it. By inspection, it looks as if the
second row in the table satisfies these conditions, the upper digit is between O and 8
and there was no C, the H flag is O and the lower digit is between A and F. So the
table says the DAA instruction will add 06 to the 3C in the accumulator and there will
be no carry. Let’s try it:
se ie se me te oem ws se ms
0100 0010 Well the result is now 42 which is what we wanted.
Got it, w- e- 1-1, I know it’s difficult, but if you indulge in a bit of
practice you’ll soon see how it works. Go on try a few random two digit numbers and
see if you can work it out from the table. Anyway, if you relate the above to the
previous episode you’ll see how it all falls into place. As I said then, the trick is
really knowing how the instructions work, and making use of what is provided.
So, having put the lid on the DAA instruction and how B2HEX works, onto the
subject of todays lecture on the painful art. A thought has just passed its way
through my feeble brain! If you’ve been waiting for me to get on with the job of
explaining this opaque business, and have been moving towards the light at the rate at
which I write this stuff, aren’t you fed up with it yet? If not, there are still some
masochists out there!