Question

I'm pretty new to 8051 and was testing it out. After CJNE executes, it sets PSW to 0x80. Why does it do that? Below is the code. I am using the EdSim51DI simulator.

Any help would greatly be appreciated

Was it helpful?

Solution

The PSW is set to 0x80 because your first operand to the CJNE instruction is less than the second operand. Read on to better understand why.


The Program Status Word (PSW) contains status bits that reflect the current CPU state. The most significant bit (bit 7) in the PSW is the carry bit (C).

Operation: CJNE
Function: Compare and Jump If Not Equal
Syntax: CJNE operand1,operand2,reladdr

The CJNE instruction compares the value of operand1 and operand2 and branches to the indicated relative address if they are not equal. If the two operands are equal, program flow continues with the instruction following the CJNE instruction. This instruction also affects the carry flag in the PSW. The carry bit (C) is set if operand1 is less than operand2, otherwise it is cleared. This functionality allows you to use the CJNE instruction to perform a greater than/less than test for decision making purposes as demonstrated in the example below.


; The following code sample checks if the value in A is equal to, less
; than, or greater than 0x55.  The NOP instructions can be replaced
; with code to handle each condition as desired.

            CJNE   A, #55h, CHK_LESS   ; If A is not 0x55, check 
            LJMP   EQUAL               ; A is 0x55, so jump to EQUAL code
CHK_LESS:   JC     IS_LESS             ; If carry is set, A is less than 0x55

IS_GREATER: NOP                        ; A is greater than 0x55
            LJMP   DONE

IS_LESS:    NOP                        ; A is less than 0x55
            LJMP   DONE

EQUAL:      NOP                        ; A is equal to 0x55

DONE:       NOP                        ; Done with the comparison
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top