Write a Program to Monitor Ports Continuously for the Value 45h

SECTION 6.3: LOGIC AND COMPARE INSTRUCTIONS

Apart from I/O and arithmetic instructions, logic instructions are some of most widely used instructions. In this section we cover Boolean logic instructions such as AND, OR, exclusive-or (XOR), and complement. We will also study the compare instruction.

AND








This instruction will perform a logical AND on the two operands and place the result in the destination. The destination is normally the accumulator. The source operand can be a register, in memory, or immediate. See Appendix A.I for more on the addressing modes for this instruction. The ANL instruction for byte-size operands has no effect on any of the flags. The ANL instruction is often used to mask (set to 0) certain bits of an operand. See Example 6-17.

Logical AND Function






OR

Logical OR Function

Inputs Output

X Y X OR Y

ORL destination,source ;dest = dest OR source



The destination and source operands are ORed, and the result is placed in the destination. The ORL instruction can be used to set certain bits of an operand to 1. The destination is normally the accumulator. The source operand can be a register, in memory, or immediate. See Appendix A for more on the addressing modes supported by this instruction. The ORL instruction for byte-size operands has no effect on any of the flags. See Example 6-18.
















Example 6-18

XOR

XRL destination,source ;dest = dest XOR source

This instruction will perform the XOR operation on the two operands, and place the result in the destination. The destination is

normally the accumulator. The source operand can Logical XQR Function be a register, in memory, or immediate. See Appendix A.I for the addressing modes of this instruction. The XRL instruction for byte-size operands has no effect on any of the flags. See Examples 6-19 and 6-20.

XRL can also be used to see if two registers have the same value. "XRL A, Rl" will exclusive-or register A and register Rl, and put the result in A. If both registers have the same value, 00 is placed in A. Then we can use the JZ instruction to make a deci sion based on the result. See Example 6-20.

Example 6-19


Example 6-20

The XRL instruction can be used to clear the contents of a register by XORing it with itself. Show how "XRL A, A" clears A, assuming that A = 45H.


Example 6-21

Read and test PI to see whether it has the value 45H. If it does, send 99H to P2; otherwise, it stays' cleared.




In the program in Example 6-21 notice the use of the JNZ instruction. JNZ and JZ test the contents of the accumulator only. In other words, there is no such thing as a zero flag in the 8051.

Another widely used application of XRL is to toggle bits of an operand. For example, to
toggle bit 2 of register A, we could use the
following code. This code causes D2 of register A to change to the opposite value, while all the other bits remain unchanged.




CPL A (complement accumulator)

This instruction complements the contents of register A. The complement action changes the Os to Is and the 1 s to Os. This is also called 1 s complement.


MOV A,#55H CPL A ;now A=AAH ,-01010101 becomes 10101010

(AAH)

To get the 2′s complement, all we have to do is to add 1 to the 1 's complement. See Example 6-22. In other words, there is no 2′s complement instruction in the 8051. Notice that in comple-

Logical Inverter

Example 6-22








meriting a byte, the data must be in register A. Although the CPL instruction cannot be used to complement RO-R7, it does work on PO-P3 ports. See Appendix A to see which addressing mode is available for the CPL instruction.

Compare instruction

The 8051 has an instruction for the compare operation. It has the following syntax.

CJNE destination,source,relative address

In the 8051, the actions of comparing and jumping are combined into a single instruction called CJNE (compare and jump if not equal). The CJNE instruction compares two operands, and jumps if they are not equal. In addition, it changes the CY flag to indicate if the destination operand is larger or smaller. It is important to notice that the operands themselves remain unchanged. For example, after the execution of the instruction "CJNE A, #67H, NEXT", register A still has its original value. This instruction compares register A with value 67H and jumps to the target address NEXT only if register A has a value other than 67H.

Example 6-23

Examine the following code, then answer the following questions.

  1. Will it jump to NEXT?

  2. What is in A after the CJNE instruction is executed?

    Solution:

  3. Yes, it jumps because 55H and 99H are not equal.

  4. A = 55H, its original value before the comparison.


In CJNE, the destination operand can be in the accumulator or in one of the Rn registers. The source operand can be in a register, in memory, or immediate. See Appendix A for the addressing modes of this instruction. This instruction affects the carry flag only. CY is changed as shown in Table 6-3.

Example 6-24

Write code to determine if register A contains the value 99H. If so, make Rl = FFH; otherwise, make Rl = 0.


The following shows how the comparison works for all possible conditions.

Notice in the CJNE instruction that any Rn register can be compared with an immediate value. There is no need for register A to be involved. Also notice that CY is always checked for cases of greater or less than, but only after it is determined that they are not equal. See Examples 6-25 through 6-27.

Example 6-25

Assume that PI is an input port connected to a temperature sensor. Write a program to read the temperature and test it for the value 75. According to the test results, place the temperature value into the registers indicated by the following.






Example 6-26

Write a program to monitor PI continuously for the value 63H. It should stop monitoring only if PI = 63H.


Example 6-27

Assume internal RAM memory locations 40H – 44H contain the daily temperature for five days, as shown below. Search to see if any of the values equals 65. If value 65 does exist in the table, give its location to R4; otherwise, make R4 = 0.

40H=(76) 41H=(79) 42H=(69) 43H=(65) 44H=(62)


The compare instruction is really a subtraction, except that the values of the operands do not change. Flags are changed according to the execution of the SUBB instruction. It must be emphasized again that in the CJNE instruction, the operands are not affected, regardless of the result of the comparison. Only the CY flag is affected. This is despite the fact that CJNE uses the subtract operation to set or reset the CY flag.

pippinsshoseathe.blogspot.com

Source: https://what-when-how.com/8051-microcontroller/logic-and-compare-instructions/

0 Response to "Write a Program to Monitor Ports Continuously for the Value 45h"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel