8051 Logical Operations Rotate and Swap Operations Example Programs and Summary

Rotate and Swap Operations

The ability to rotate data is useful for inspecting bits of a byte without using individual bit opcodes. The A register can be rotated one bit position to the left or right with or without including the C flag in the rotation. If the C flag is not included, then the rotation involves the eight bits of the A register. If the C flag is included, then nine bits are involved in the rotation. Including the C flag enables the programmer to construct rotate operations in­volving any number of bytes.

The SWAP instruction can be thought of as a rotation of nibbles in the A register.

Figure 4.2 diagrams the rotate and swap operations. which are given in the following table:

Mnemonic

 Operation

RL A

Rotate the A register one bit position to the left; bit A0 to bit A I, A I to A2. A2 to A3. A3 to A4, A4 to A5, A5 to A6. A6 to A 7, and A 7 to A0

RLC A

Rotate the A register and the carry flag. as a ninth bit, one bit position to the left; bit A0 to bit Al, Al to A2, A2 to A3, A3 to A4, A4 to A5. A5 to A6, A6 to A 7, A 7 to the carry flag, and the carry flag to A0

RR A

Rotate the A register one bit position to the right; bit A0 to bit A 7, A6 to A5, A5 to A4, A4 to A3, A3 to A2. A2 to Al, and At to A0

RRC A

 Rotate the A register and the carry flag, as a ninth bit, one bit position to the right; bit A0 to the carry flag. carry flag to A 7, A 7 to A6, A6 to A5, A5 to A4, A4 to A3, A3 to A2, A2 to Al, and Al to A0

SWAP A

 Interchange the nibbles of register A; put the high nibble in the low nibble position and the low nibble in the high nibble position

Note that no flags. other than the carry flag in RRC and RLC, are affected. If the carry is used as part of a rotate instruction, the state of the carry flag should be known before the rotate is done.

 

clip_image002[4]


The following table shows examples of rotate and swap operations:

Mnemonic

Operation

OY A,#OA5h

A = I0l00I0lb = A5h

RR A

 A = I I0I00I0b = D2h

RR A

A = 0l I0l00lb = 69h

RR A

A = I0I l0I00b = B4h

RR A

 A = 0I0l I0I0b = 5Ah

SWAP A

A= I0I00I0lb = A5h

CLR  C

C = 0; A = I0I00I0lb = A5h

RRC  A

C = I; A= 0I0I00I0b = 52h

RRC A

 C = 0; A = l0I0J00lb = A9h

RL A

 A= 0I0I00llb = 53h

RL A

A = I0I00l I0b = A6h

SWAP A

C = 0; A = 0l 101010b = 6Ah

RLC A

 C = 0; A = 1 I0I0l00b = D4h

RLC A

C = l; A= I0I0l000b = A8h

 SWAP A

 C = l; A= l000I0I0b = 8Ah

 

CAUTION

Know the state of the carry flag when using RRC or RRL.

 Rotation and swap operations are limited to the A register.

Example Programs on 8051 Logical Operations

The programs in this section are written using only opcodes covered to this point in the text. The challenge is to minimize the number of lines of code.

Ø  EXAMPLE PROBLEM 4. 1

Double the number in register R2, and put the result in registers R3 (high byte) and R4 (low byte).

Thoughts on the Problem The largest number in R2 is FFh; the largest result is 1 FEh. There are at least three ways to solve this problem: Use the MUL instruction (multiply, covered in Chapter 5). add R2 to itself, or shift R2 left one time. The solution that shifts R2 left is as follows:

Mnemonic

Operation

 MOY R3,#00h

Clear R3 to receive high byte

 CLRC

Clear the carry to receive high bit of 2 x R2

MOY A.R2

 Get R2 to A

RLC A

Rotate left, which doubles the number in A

MOY R4,A

Put low byte of result in R4

CLR A

Clear A to receive carry

RLC A

 

MOY R3.A

 

The carry bit is now bit 0 of A

 

Transfer any carry bit to R3

 

COMMENT

Note how the carry flag has to be cleared to a known state before being used in a rotate operation.

Ø  EXAMPLE PROBLEM 4.2

OR the contents of pons I and 2; put the result in external RAM location 0I00h.

·         Thoughts on the Problem The ports should be input ports for this problem to make any physical sense; otherwise, we would not know whether to use the pin data or the port SFR latch data.

The solution is as follows:

Mnemonic

Operation

 MOY A,90h

Copy the pin data from port 1 to A

ORL A,0A0h

OR the contents of A with port 2; results in A

MOY DPTR,#0100h

 Set the DPTR to point to external RAM address

 MOYX @DPTR.A

Store the result

COMMENT

Any time the port is the source of data, the pin levels are read; when the port is the destination, the latch is written. If the port is both source and destination (read-modify-write instruc­tions), then the latch is used.

Ø  EXAMPLE PROBLEM 4.3

Find a number that, when X0 Red to the A register, results in the number 3Fh in A.

·         Thoughts on the Problem Any number can be in A, so we will work backwards:

3Fh =A XOR N

A XOR 3Fh = A XOR A XOR N = N

The solution is as follows:

Mnemonic

Operation

 MOV R0,A

 Save A in R0

 XOR A,#3Fh

XOR A and 3Fh; forming N

XOR A,R0

 XOR A and N yielding 3Fh

COMMENT

Does this program work? Let’s try several A’s and see.

A= FFh

A XOR 3Fh = C0h

C0h XOR FFh = 3Fh

A= 00h

A XOR 3Fh = 3Fh

3Fh XOR 00h = 3Fh

A= 5Ah

A XOR 3Fh = 65h

65h XOR 5Ah = 3Fh

Summary

Boolean logic, rotate, and swap instructions are covered in this chapter. Byte-level opera­tions involve each individual bit of a source byte operating on the same bit position in the destination byte; the results are put in the destination, while the source is not changed:

ANL destination. Source

ORL destination. Source

XRL destination. Source

CLR A

CPL A

RR A

RLA

RRCA

RLC A

SWAP A

Bit-level operations involve individual bits found in one area of internal RAM and certain SFRs that may be addressed both by the assigned direct-byte address and eight individual bit addresses. The following Boolean logical operations may be done on each of these addressable bits:

ANLbit

ORL bit

CLR bit

CPL bit

SETB bit

MOY destination bit, source bit

Problems

Write programs that perform the tasks listed using only opcodes that have been discussed in this and previous chapters. Write comments for each line of code and try to use as few lines as possible.

1. Set Port 0. bits 1,3,5, and 7. to one: set the rest to zero.

2. Clear bit 3 of RAM location 22h without affecting any other bit.

3. Invert the data on the port 0 pins and write the data to port I.

4. Swap the nibbles of R0 and RI so that the low nibble of R0 swaps with the high nibble of RI and the high nibble of R0 swaps with the low nibble of R l .

5. Complement the lower nibble of RAM location 2Ah.

6. Make the low nibble of RS the complement of the high nibble of R6.

7. Make the high nibble of RS the complement of the low nibble of R6.

8. Move bit 6 of R0 to bit 3 of port 3.

9. Move bit 4 of RAM location 30h to bit 2 of A. to. XOR a number with whatever is in A so that the result is FFh.

11. Store the most significant nibble of A in both nibbles of register RS: for example, if A = B6h. then RS = BBh.

12. Store the least significant nibble of A in both nibbles of RAM address 3Ch; for example. if A = 36h. then 3Ch = 66h.

13. Set the carry flag to one if the number in A is even; set the carry flag to zero if the number in A is odd.

14. Treat registers R0 and RI as 16-bit registers, and rotate them one place to the left: bit 7 of R0 becomes bit 0 of RI. bit 7 of RI becomes bit 0 of R0, and so on.

15. Repeat Problem 14 but rotate the registers one place to the right.

16. Rotate the DPTR one place to the left; bit IS becomes bit 0.

17. Repeat problem 16 but rotate the DPTR one place to the right.

18. Shift register B one place to the left: bit 0 becomes a zero. bit 6 becomes bit 7. and so on. Bit 7 is lost.

Leave a comment

Your email address will not be published. Required fields are marked *