# Binary to BCD conversion in 8051

In this problem, we will see how to convert an 8-bit binary number to its BCD equivalent. The binary number is stored at location 20H. After converting, the results will be stored at 30H and 31H. The 30H will hold the MS portion, and 31H will hold the LS portion.

So let us assume the data is D5H. The program converts the binary value of D5H to BCD value 213D.

Value

.
.
.
20H
D5
21H

.
.
.

## Program

MOVR1,#20H;Takethe address 20H into R1
MOVA,@R1;Takethe data into Acc
DIVAB ;DivideA with B
MOVR5,B;Storethe remainder
DIVAB ;DivideA with B
MOV@R1,A;Storethe MS portion
SWAPA;Swapthe nibbles
MOV@R1,A
HALT:   SJMPHALT

Here we are just taking the binary number into the accumulator. And then divide the content of accumulator by 0AH (10D). So the remainder partis stored into a separate register. This will be added later. Then again divide the quotient by 0AH, and generate the MS bits. After storing the MS bits, get the number from register B to the accumulator. Swap the nibbles of the accumulator to generate four zeros at the LSbits. Then add the previously stored remainder to generate the result.

Value

.
.
.
20H
D5
21H

.
.
.
30H
02
31H
13

.
.
.