You need to gain an understanding of boolean logic functions.

Bitwise OR will return a 1 if any of the input bits are a 1 -

1 OR 0 = 1

1 OR 1 = 1

0 OR 1 = 1

If both of the input bits are a zero, the equation will return a 0 -

0 OR 0 = 0

In a bitwise XOR equation (XOR = Exclusively OR), the equation returns a 1 only if input bit 1 OR input bit 2 are a 1-

1 XOR 0 = 1

0 XOR 1 = 1

If both equation bits are a 1 or both equation bits are a 0, the equation returns a 0 -

1 XOR 1 = 0

0 XOR 0 = 0

In a bitwise AND equation, the equation will return a 1 if and ONLY if all of the input bits are a 1 -

1 AND 1 = 1

If any of the input bits are 0, the result will be 0 -

1 AND 0 = 0

0 AND 1 = 0

0 AND 0 = 0

So the instruction ORL states to OR the two operand bits together. ORL C states to OR the carry bit with the second operand bit and place the result in the carry bit -

In this instruction, bit P2.2 is OR'ed with the carry bit. If -

Carry Bit = 1

P2.2 = 0

Result is 1 and the carry flag is now set

Carry Bit = 0

P2.2 = 0

Result is 0 and the carry flag is now clear

Carry Bit = 0

P2.2 = 1

Result is 1 and the carry flag is now set

Carry Bit = 1

P2.2 = 1

Result is 1 and the carry flag is now set

If -

Carry Bit = 1

P2.5 = 1

Result is 1 and carry flag is now set

Carry Bit = 1

P2.5 = 0

Result is 0 and carry flag is now clear

Carry Bit = 0

P2.5 = 1

Result is 0 and carry flag is now clear

Carry Bit = 0

P2.5 = 0

Result is 0 and carry flag is now clear

To summarize, in an OR equation returns a 1 if ANY of the bits in the OR equation are a 1 (result is 1 if either OR bit is 1). An AND equation returns a 1 only if all of the bits in the equation are a 1 (result is 1 if equation bit 1 AND equation bit 2 are a 1). An XOR equation returns a 1 only if bit 1 OR bit 2 is a 1, else it returns a 0.

So in the book code, an outdoor light is being activated when an indoor light is deactivated and vice versa. P2.2 controls the outdoor light and P2.5 controls the indoor light.

Code:

```
setb C ;set carry bit
orl C,P2.2 ;OR p2.2 with carry bit
mov P2.2,C ;turn light on if not already on
```

The first instruction sets the carry bit high. Then the next instruction OR's the carry bit with bit P2.2.

If P2.2 = 1 -

1 OR 1 = 1 - carry bit set

If P2.2 = 0 -

1 OR 0 = 1 - carry bit set

Then the carry bit is moved into P2.2 and P2.2 is set high

The next 3 instructions -

Code:

```
clr C ;clear carry bit
anl C,P2.5 ;CY = P2.5 AND'ed with CY
mov P2.5,C ;turn light off if not already off
```

The first instruction clears the carry bit. The next instruction AND's P2.5 with the carry bit.

If P2.5 = 1 -

0 AND 1 = 0

If P2.5 = 0 -

0 AND 0 = 0

Then the carry bit is moved into P2.5 and P2.5 is set low.

The above code is example code that ensures that P2.2 is set high when P2.5 is set low if they're not already set to those states.