Tuesday, March 22, 2016

8051 Binary Coded Decimal

Binary representation of decimal numbers (0 to 9) is called BCD. There are two terms for BCD numbers: Unpacked and Packed.

Unpacked BCD:
In unpacked BCD, the lower 4 bits of the number represent the BCD number, and the rest of the bits are 0. For example, “0000 1001″ and “0000 0101″ are unpacked BCD for 9 and 5, respectively. Unpacked BCD requires 1 byte of memory or an 8-bit register to contain it.

Packed BCD:
In packed BCD, a single byte has two BCD numbers in it, one in the lower 4 bits, and one in the upper 4 bits. For example, “0101 1001″ is packed BCD for 59H. It takes only 1 byte of memory to store the packed BCD operands.

Addition of two BCD numbers is no longer a BCD number.

MOV A, #17H
ADD A, #28H

Adding these two numbers gives 0011 111 IB (3FH), which is not a BCD number. To correct this problem, the programmer must add 6 (0110) to the lower nibble and higher nibble. The 8051 have an instruction to deal with it. In the 8051 the instruction “DA A” is designed/to correct the BCD addition problem. The DA instruction will add 6 to the lower nibble or higher nibble if needed; otherwise, it will leave the result alone.

ORG 0H
CLR A
MOV A, #17H
ADD A, #28H
DA A
END


After the program is executed, register A will contain 45H (17 + 28 = 45).

DA Instruction:
The “DA” instruction works only on A. AC (auxiliary carry) flag bit except for BCD addition and correction.
After an ADD or ADDC instruction,
If the lower nibble (4 bits) is greater than 9, or if AC = 1, add 0110 to the lower 4 bits.
If the upper nibble is greater 1. than 9, or if CY = 1, add 0110 to the upper 4 bits.

Sum of BCD numbers:
BCD numbers are stored in RAM 40= (71) 41=(11) 42=(65) 43=(59) 44=(37). Calculate the SUM.

ORG 0H
MAIN :
MOV R0, #40H; load pointer
MOV R2, #5; load counter
CLR A; clear accumulator
MOV R7, A; clear R7
AGAIN :ADD A, @R0; A= A+ value at R0
DA A ; adjust for BCD
JNC NEXT ; Jump if No carry
INC R7; accumulate carry, if set
NEXT :INC R0 ; increment pointer
DJNZ R2, AGAIN; repeat until R2 = 0
END; end of asm source file



Related topics:
8051 Addition   |   8051 Addition with Carry   |   8051 Addition of Unsigned Numbers   |   8051 Addition of 16-bit Numbers   |   8051 ADDC Instruction   |   8051 ADD Instruction   |   8051 Signed Numbers

List of topics: 8051

No comments:

Post a Comment