21 shifting bits (rr, rrc, rl, rlc) – Texas Instruments MSC1210 User Manual

Page 217

Advertising
background image

Shifting Bits (RR, RRC, RL, RLC)

16-23

8052 Assembly Language

16.21 Shifting Bits (RR, RRC, RL, RLC)

The 8052 offers four instructions that are used to shift the bits in the accumulator
to the left or right by one bit: RR A, RRC A, RL A, RLC A. There are two instruc-
tions that shift bits to the right, RR A and RRC A, and two that shift bits to the
left, RL A and RLC A. The RRC and RLC instructions are different in that they
rotate bits through the carry bit, whereas RR and RL do not involve the carry bit.

RR A

;Rotate

accumulator

one

bit

to

right,

bit

0

is

rotated

into

;bit

7

RRC A ;Rotate accumulator to right, bit 0 is rotated into

;carry, carry into bit 7

RL A

;Rotate

accumulator

one

bit

to

left,

bit

7

is

rotated

into

;bit

0

RLC A ;Rotate the accumulator to the left, bit 7 is

;rotated into carry, carry into bit 0

Figure 16−1 shows how each of the instructions manipulates the eight bits of
the accumulator and the carry bit.

Using the shift instructions is, obviously, useful for bit manipulations. However,
they can also be used to quickly multiply or divide by multiples of two.

For example, there are two ways to multiply the accumulator by two:

MOV B,#02h

;Load B with 2

MUL AB

;Multiply accumulator by B (2), leaving low
;byte in accumulator

Or you could simply use the RLC instruction:

CLR C

;Make sure carry bit is initially clear

RLC A

;Rotate left, multiplying by two

This may look like the same amount of work, but to the MCU it is not. The first
approach requires four bytes of program memory and takes six instruction
cycles, whereas the second approach requires only two bytes of program
memory and two instruction cycles. Therefore, the RLC approach requires half
as much memory and is three times as fast.

Figure 16−1. Rotate Operations

Advertising