Wednesday, March 23, 2016

8051 Compare and Jump

Compare and Jump if Not Equal

CJNE dest-byte,src-byte, relative address
CJNE compares the magnitudes of the first two operands and branches if their values are not equal. The branch destination is computed by adding the signed relative-displacement in the last instruction byte to the PC, after incrementing the PC to the start of the next instruction. The carry flag is set if the unsigned integer value of dest-byte is less than the unsigned integer value of src-byte; otherwise, the carry is cleared. Neither operand is affected.

Compare>Carry Flag
destination >= source C = 0
Destination < source C = 1

The first two operands allow four addressing mode combinations: the Accumulator may be compared with any directly addressed byte or immediate data, and any indirect RAM location or working register can be compared with an immediate constant.

CJNE A, direct address, relative address
Encoding: 1 0 1 1 0 1 1 1 direct address rel.address

CJNE A, #immediate data, relative address
Encoding: 1 0 1 1 0 1 0 0 immediate address rel.address

CJNE Rn, #immediate data, relative address
Encoding: 1 0 1 1 1 r r r immediate address rel.address

CJNE @Ri, #immediate data, relative address
Encoding: 1 0 1 1 0 1 1 i immediate address rel.address

The Accumulator contains 34H. Register 7 contains 56H. The first instruction in the sequence,

CJNE R7, # 60H, NOT_EQ
; . . . . . . . . ;R7 = 60H.
; . . . . . . . . ;R7 > 60H.

sets the carry flag and branches to the instruction at label NOT_EQ. By testing the carry flag, this instruction determines whether R7 is greater or less than 60H.

If the data being presented to Port 1 is also 34H, then the following instruction,


clears the carry flag and continues with the next instruction in sequence, since the Accumulator does equal the data read from P1. (If some other value was being input on P1, the program loops at this point until the P1 data changes to 34H.)

MOV R4, P0
ADD A, #10H

Related topics:
8051 Unconditional and Conditional Jump   |   8051 Long Jump   |   8051 Absolute Jump   |   8051 Short Jump   |   8051 Indirect Jump   |   8051 Jump if Accumulator Zero   |   8051 Jump if Accumulator Not Zero   |   8051 Decrement and Jump if Not Zero   |   8051 Jump if Carry is Set   |   8051 Jump if Carry is Not Set   |   8051 Jump if Bit Set   |   8051 Jump if Bit Not Set   |   8051 Jump if Bit is Set and Clear Bit

List of topics: 8051

No comments:

Post a Comment