Rotate Instruction

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

The Accumulator

as a Shift and a
Rotate Register
   
As we shall see, the ability to use the accumulator as a shift register is very
useful in assembly programming. Almost all microcontrollers allow the
  programmer to use some register in this fashion. The 8051 has a number
of instructions, known as the rotate instructions, that rotate the bits around
the accumulator.
   
  RR A
This instruction is rotate right the accumulator. Its operation is illustrated
 
below:

Each bit is shifted one location to the right, with bit 0 going to bit 7.
   
  RL A
  Rotate left the accumulator.

Each bit is shifted one location to the left, with bit 7 going to bit 0.
   
   
  Rotating through the Carry
  There are two instructions that, in effect, create a 9-bit rotate register.
   
  RRC A
  Rotate right through the carry.

  Each bit is shifted one location to the right, with bit 0 going into the carry
bit in the PSW, while the carry was at goes into bit 7 (ie; if the carry was
set prior to the execution of RRC A, then bit 7 of the accumulator will
contain 1 after execution of RRC A. Similarly, if the carry was clear prior
to execution of RRC A, then bit 7 of the accumulator will contain 0 after
execution of RRC A).
   
  RLC A
  Rotate left through the carry.

Each bit is shifted one location to the left, with bit 7 going into the carry
bit in the PSW, while the carry goes into bit 0.
   
   
  Shift Register
So far, we have seen how to use the accumulator as a rotate register. But
what if we need a shift register? With a right shift register, the LSB is
dropped off the edge and the MSB is replaced by zero. With a left shift
  register, the MSB is dropped of the edge and the LSB is replaced by 0. All
the instructions above result in either the LSB going into the MSB (in the
case of right rotate) or the MSB going into the LSB (in the case of left
rotate).
To achieve shift register functions, we use the rotate through carry
 
instructions, making sure the carry is zero beforehand.
   
  Right Shift Register
The code below could be used for shifting the contents of the accumulator
  to the right a number of times. The number of times is specified by the
value in R0.
   
  shiftRight:
CJNE R0, #0, skip
  RET
  skip:
PUSH PSW
  PUSH AR0
  again:
CLR C
RRC A
DJNZ R0, again
 
POP AR0
POP PSW
RET
   
  Left Shift Register
  Obviously, to create a left shift register the code would be the same as that
above, except the instruction RRC A is replaced by RLC A.
   

You might also like