- 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
Shift a multi-byte BCD number to the right in 8051
Here we will see a problem to shift some multi-byte BCD number to the right. The BCD numbers are shifted by two digits (8-bit).
Let us consider a four-byte BCD number (45 86 02 78) is stored at location 20H, 21H, 22H,23H. The address 10H holds the number of bytes of the whole BCD number. So after executing this code, the contents will be shifted to the right and 20H will hold 00H.
Address | Value |
---|---|
. . . | |
20H | 45 |
21H | 86 |
22H | 02 |
23H | 78 |
. . . |
Program
CLRA;Clear the Register A MOVR2,10H;TakeByte Count INCR2;Increase R2 for loop MOVR1,#20H;Takethe address 20H into R1 LOOP: XCHA,@R1; Get content, which is pointed out by R1 value INCR1;IncreaseR1 for next location JNZR2, LOOP ;Check R2 is 0 or not to loop back HALT: SJMPHALT ;Stop the program
Here the XCH instruction is used. By using this instruction, the value of register A and content of address pointed by R1 is swapped. Afters wapping, the address is moving one byte to the right, then exchange again to put the old value and take the new value. By using this procedure, the BCD bytes are shifted.
The counter value is increased by 1 at the beginning to run the program one more than the byte count. If the value is not incremented, the last byte will not be shifted, it will be discarded.
Output
Address | Value |
---|---|
. . . | |
20H | 00 |
21H | 45 |
22H | 86 |
23H | 02 |
24H | 78 |
. . . |