- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
BCD to binary conversion in 8051
In this problem, we will see how to convert 8-bit BCD number to its Binary (Hexadecimal)equivalent. The BCD number is stored at location 20H. After converting, the results will be stored at 30H.
So let us assume the data is D5H. The program converts the binary value ofD5H to BCD value 213D.
Address | Value |
---|---|
. . . | |
20H | 94 |
21H | |
. . . |
Program
MOVR0,#20H; Initialize the address of the data MOVA,@R0;Get the data from an address, which is stored in R0 MOVR2,A; Store the content of A into R2 CLRA;Clear the content of A to 00H MOVR3,#00H LOOP: ADDA,#01H;increment A by 01H DAA; Decimal Adjust of the accumulator content INCR3; Increase R3 to keep track of the hex value CJNEA,02H,LOOP ;Run the loop until A = R2 MOVR0,#30H; Point the destination location MOVA,R3; Move R3 to A MOV@R0,A; Store A content to the memory location pointed by R0 HALT: SJMPHALT
Here the logic is very simple. We are just taking the number from the memory. And storing that value into R2. By this value, we can compare while executing the loop.
The accumulator (A) and register R3 is set to 00H at first. So we are just increasing the value of A by 01H. We can use the INC A instruction to increment the value, but in this case, the value is incremented by using ADDA, #01H. The reason behind it is the INCinstruction does not affect the CY and AC flags. But these flags are needed to use DA A instruction for decimal adjusting. After increasing the value of A, DA A instruction is executed to convert the value to decimal. By using this decimal value, we can compare with the number stored inR2. In each iteration the value of R3 is incremented by 1, this is acting like a counter. So at the end, we are getting the output from register R3.
Output
Address | Value |
---|---|
. . . | |
20H | 94 |
21H | |
. . . | |
30H | 5E |
31H | |
. . . |
Method 2
We can do the same thing using some other logic also. Here no additional loops are needed to do the task. We are just multiplying the digit of 10th place of the BCD number with 0AH. Then adding the second digit with the result to get the number.
If the number is 94, then it is multiplying 0AH with 9.
(9 * 0AH) = 5AH, thenadding 4 with 5AH. (5AH + 4) = 5EH.
Program
MOVR0,#20H; Initialize the address of the data MOVA,@R0; Get the data from an address, which is stored in R0 MOVR2,A;Store the content of A into R2 SWAPA; Swap the nibbles of A register ANLA,#0FH;Mask the Higher Nibble of A MOVB,0AH;Take 0AH = 10D into B MULAB ;Multiply A with B, and get result into A MOVA,R2;Copy R2 content to A ANLA,#0FH;Mask the higher nibble of A ADDA,R2 MOVR0,#30H;Point the destination location MOVA,R3;Move R3 to A MOV@R0,A;Store A content to memory location pointed by R0 HALT: SJMPHALT
- Related Articles
- Binary to BCD conversion in 8051
- Hex to ASCII conversion in 8051
- Conversion of four-digit hex to ASCII in 8051
- Shift a multi-byte BCD number to the right in 8051
- Binary Tree to Binary Search Tree Conversion in C++
- Decimal to Binary conversion\n
- Decimal to binary list conversion in Python
- Binary to original string conversion in JavaScript
- Program for Binary To Decimal Conversion in C++
- Program for Decimal to Binary Conversion in C++
- Decimal to binary conversion using recursion in JavaScript
- Conversion of Binary to Gray Code\n
- Conversion of Gray Code to Binary\n
- C Program for Decimal to Binary Conversion?
- Decimal to Binary conversion using C Programming
