
Site Search
SECTION III - DECIMAL ADJUST INSTRUCTION IN 8051
SECTION III - DECIMAL ADJUST [DA] INSTRUCTION IN 8051
'DA' means decimal adjust. The decimal adjust for addition instruction is designed to correct the BCD addition problems. The decimal adjust instruction will add 6 to the lower nibble or higher nibble if needed. The example below will clarify it.
MOV A, #36H | ;Load accumulator with first BCD operand 36H |
---|---|
MOV B, #24H | ;Load accumulator with second BCD operand 24H |
ADD A, B | ;Adding the value in A register with B register and save the result in accumulator register A i.e A=36H+24H=5A [01011010] |
DA A | ;Decimal adjust for BCD addition at lower nibble bits 5A+06=60H or 01100000 after corrected by DA instruction |
The decimal "DA" instruction works only on A. The source can be an operand of any addressing mode but the destination must always be in register accumulator A. The BCD operand can never have any digit greater than 9 so the "DA" instruction must always be used after the addition of BCD operands. The digits, A, B, C, D, E, F are not allowed in BCD system. The "DA" instruction only works after an ADD instruction. The "DA" instruction will not work after the "INC" or "DEC" instruction.
Working of "DA" Instruction:
- The "DA" instruction is used after the ADD of ADDC instruction.
- Add "0110" in binary or "6H" in the lower nibble of 4 bits, if lower nibble of 4 bits is greater than 9 or if auxiliary carry is 1 i.e AC=1, to correct the BCD problems.
- Add "0110" in binary or "6H" in the upper nibble of 4 bits, if upper nibble of 4 bits is greater than 9 or if auxiliary carry is 1 i.e AC=1, to correct the BCD problems.
- The only use of auxiliary carry AC flag bit is when BCD addition and correction occurs.
Example # 1:
HEX | BCD |
---|---|
28 | 0010 1000 |
+ 27 | + 0010 0111 |
4F | 0100 1111 |
+ 6 | 0110 |
= 55 | 0101 0101 |
In the above example, the 28H is added to 19H, the result is 4FH. The lower nibble in "4F" is "F", which is not a BCD number. The BCD number must be in between 0 to 9. So after adding "6" in hex or "0110" in binary with the lower nibble we get the result "55" in hex or in binary "0101 0101". 55H is the corrected result, as the lower nibble bits and the upper nibble bits is less than 9.
The auxiliary carry AC=1, after the addition, "DA A" will add 6 to the lower nibble. The final result is in the BCD format which is 55H
Example # 2:
10 data items are stored in RAM locations starting at 30H. Lets write a program to find the sum of all the numbers and the result is in binary coded format.
30H = 31 |
31H = 32 |
32H = 33 |
33H = 34 |
34H = 35 |
35H = 36 |
36H = 37 |
37H = 38 |
38H = 39 |
39H = 40 |
Answer:
MOV R0, #30H | ;Load the 30H pointer location |
---|---|
MOV R3, #10 | ;Load the counter |
CLR A | ;Clear the accumulator register A i.e A=0 |
MOV R6, A | ;Clear the R6 register |
AGAIN: ADD A, @R0 | ;Add the byte pointer to by R0 |
DA A | ;Decimal adjust for BCD |
JNC NEXT | ;If carry flag is zero, then do not accumulate carry |
INC R6 | ;Keep track of carries, when there is a carry increment the R6 register |
NEXT: INC R0 | ;Increment pointer |
DJNZ R3, AGAIN | ;Repeat the loop until R3 is zero. |
END | ;End of asm file |