Binary to BCD conversion in 8051

Microprocessor8085

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.

Address
Value


.
.
.
20H
D5
21H








.
.
.


Program

MOVR1,#20H;Takethe address 20H into R1
MOVA,@R1;Takethe data into Acc
MOVB,#0AH;LoadB with AH = 10D
DIVAB ;DivideA with B
MOVR5,B;Storethe remainder
MOVB,#0AH;LoadB with AH = 10D
DIVAB ;DivideA with B
MOVR1,#30H;Loaddestination address
MOV@R1,A;Storethe MS portion
MOVA,B;LoadB content to A
SWAPA;Swapthe nibbles
ADDA,R5;Addstored remainder with A
INCR1;Increasethe address
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.

Output

Address
Value


.
.
.
20H
D5
21H




.
.
.
30H
02
31H
13


.
.
.
raja
Published on 31-Dec-2018 09:48:30
Advertisements