8085 program to reverse 8 bit number
Problem: Write an assembly language program in 8085 microprocessor to reverse 8-bit numbers.
Example:
Assume that number to be reversed is stored at memory location 2050, and reversed number is stored at memory location 3050.
Algorithm –
- Load content of memory location 2050 in accumulator A
- Use RLC instruction to shift the content of A by 1 bit without carry. Use this instruction 4 times to reverse the content of A
- Store content of A in memory location 3050
Program –
MEMORY ADDRESS | MNEMONICS | COMMENT |
---|---|---|
2000 | LDA 2050 | A <- M[2050] |
2003 | RLC | Rotate content of accumulator left by 1 bit |
2004 | RLC | Rotate content of accumulator left by 1 bit |
2005 | RLC | Rotate content of accumulator left by 1 bit |
2006 | RLC | Rotate content of accumulator left by 1 bit |
2007 | STA 3050 | M[2050] <- A |
200A | HLT | END |
Explanation – Register A used:
- LDA 2050: load value of memory location 2050 in Accumulator A.
- RLC: Rotate content of accumulator left by 1 bit
- RLC: Rotate content of accumulator left by 1 bit
- RLC: Rotate content of accumulator left by 1 bit
- RLC: Rotate content of accumulator left by 1 bit
- STA 3050: store content of A in memory location 3050.
- HLT: stops executing the program and halts any further execution.
Another Alternative and simple way to execute the above program and the magic of 8085 rotate instructions :
This can be done by using 4 times RRC instruction.
LDA 2050H
RRC
RRC // 4 RRC do same work as 4 RLC. So we can use anyone alternatively.
RRC
RRC //After this 4th RRC instruction our 8 bit number is reversed and stored in accumulator.
STA 3050H
HLT
Step-by-Step and Bit-by-Bit explanation of the Above Program with an example :
Example : 98H in Binary Written as : 1001 1000 RLC 1st Time : 0011 0001 {Carry Flag = 1} RLC 2nd Time : 0110 0010 {Carry Flag = 0} RLC 3rd Time : 1100 0100 {Carry Flag = 0} RLC 4th Time : 1000 1001 { Carry Flag = 1} Converted Number after 4th RLC : 1000 1001 [89H] Hence our number is reversed from 98H to 89H. For Example : 98H in Binary Written as : 1001 1000 RRC 1st Time : 0100 1100 {Carry Flag = 0} RRC 2nd Time : 0010 0110 {Carry Flag = 0} RRC 3rd Time : 0001 0011 { Carry Flag = 0} RRC 4th Time : 1000 1001 { Carry Flag = 1} Converted Number after 4th RRC : 1000 1001 [89H] Hence our number is reversed from 98H to 89H. Hence Instead of 4 RLC, we can also use 4 RRC instructions in our code alternatively.
Please Login to comment...