"Use of mnemonics" demonstrations
BCD coding

This test demonstrates the use of, and difference between the mnemonics DAA (decimal adjust after addition) and AAA (ascii adjust after addition). These are just two of the useful BCD (binary coded decimal) mnemonics which allow you to count numbers up to 10 instead of 16.
Set a breakpoint at BCD_CODING and run the test.
The first test adds the BCD number 66666666 to 87654321. Each ADD AL,DL adds two BCD numbers at once because they are kept in the first and second nibbles of AL and DL. DAA is called immediately after the ADD to set the carry flag if there was a carry. If the carry flag is set, EAX is incremented after the whole register has been rotated by 8 bits thereby giving effect to the carry. DAA works because internally it changes the first nibble and then the second nibble to give a BCD result and alters the carry flag to suit.
The second test examines the limit of DAA and shows that it still works even if one of the BCD digits is 10, but not if 11. This is handy since it means that you can add a carry to BCD number ready for the next DAA without worrying that the add should have itself caused another carry.
The next test uses AAA which works on a single BCD value in the AL register. This is more convenient that DAA if you want to write the result in AL to the screen since there is no need to separate the two nibbles in AL. Here you can see that AAA is also content to work if one of the BCD digits has changed to 10 decimal as a result of an earlier carry.
The final test uses the fact that AAA increments AH if there is a carry. Because the number rotates, when each BCD digit is added AH will hold the next higher digit ready to receive the increment if necessary. The result is kept in EBX and the carry flag which may show the last carry, is preserved on exit.
See also FPU (binary coded decimal) as to how the FPU can be used to create BCD numbers, and as to how BCD numbers can be used to convert large numbers to ascii.
Here is the code:-

```ADD_BCD:
DAA
PUSHF
ROR EAX,8
ROR EDX,8
POPF
RET
;
BCD_CODING:
MOV EAX,87654321h       ;get bcd number
MOV EDX,66666666h       ;number to be added to it
JNC >L4
INC EAX
L4:
JNC >L6
INC EAX
L6:
JNC >L8
INC EAX
L8:
CALL ADD_BCD            ;result in eax and carry flag
NOP                     ;************* finished first test
MOV AL,99h
DAA
MOV AL,99h
DAA
MOV AL,99h
DAA
NOP                     ;************* finished second test
XOR AH,AH
MOV AL,9h
INC AL                  ;this is ok
AAA                     ;adjust result to base 10, incrementing ah if carry
XOR AH,AH
MOV AL,9h
INC AL,AL               ;this is not ok
AAA                     ;adjust result to base 10, incrementing ah if carry
XOR AH,AH
MOV AL,9h
AAA                     ;adjust result to base 10, incrementing ah if carry
NOP                     ;************* finished third test
MOV EAX,09050206h       ;using the ah increment
MOV ECX,4
L10:
AAA                     ;adjust result to base 10, incrementing ah if carry
MOV BL,AL               ;save result in ebx
DEC ECX
JZ >L12                 ;finish
SHR EAX,8
SHR EDX,8
ROR EBX,8
JMP L10
L12:
PUSHFD	  	  ;keep carry flag for result
ROR EBX,8
POPF
RET
```