Digital Electronics - The 8051 Microcontroller - Discussion
Discussion Forum : The 8051 Microcontroller - General Questions (Q.No. 11)
11.
The following program will receive data from port 1, determine whether bit 2 is high, and then send the number FFH to port 3:
READ: MOV A,P1
ANL A,#2H
CJNE A,#02H,READ
MOV P3,#FFH
READ: MOV A,P1
ANL A,#2H
CJNE A,#02H,READ
MOV P3,#FFH
Discussion:
16 comments Page 1 of 2.
RAMESH MADDARA said:
3 weeks ago
The program as provided aims to read data from port 1, check whether bit 2 is high, and then send FFH to port 3 if the condition is true. Let’s analyse each step:
Program Breakdown:
MOV A, P1: Loads the entire port 1 data into accumulator A.
ANL A, #2H: Performs a bitwise AND with 02H, which isolates bit 1 (the second least significant bit), not bit 2.
CJNE A, #02H, READ: Compares accumulator A with 02H, jumps to label READ if they are not equal.
MOV P3, #FFH: Sends FFH to port 3.
Key Point:
The instruction ANL A, #2H is used to test bit 1, not bit 2. To test bit 2, the mask should be 04H (binary 00000100).
The comparison CJNE A, #02H is also comparing against 02H, which is again bit 1, not bit 2.
Conclusion:
Since the program is checking bit 1 rather than bit 2, the statement "will receive data from port 1, determine whether bit 2 is high, and then send FFH to port 3" is FALSE based on the current code.
So,the Final answer is FALSE. The provided code checks the wrong bit (bit 1 instead of bit 2) for high status. To correctly check bit 2, the program should use mask #04H (binary 00000100).
Program Breakdown:
MOV A, P1: Loads the entire port 1 data into accumulator A.
ANL A, #2H: Performs a bitwise AND with 02H, which isolates bit 1 (the second least significant bit), not bit 2.
CJNE A, #02H, READ: Compares accumulator A with 02H, jumps to label READ if they are not equal.
MOV P3, #FFH: Sends FFH to port 3.
Key Point:
The instruction ANL A, #2H is used to test bit 1, not bit 2. To test bit 2, the mask should be 04H (binary 00000100).
The comparison CJNE A, #02H is also comparing against 02H, which is again bit 1, not bit 2.
Conclusion:
Since the program is checking bit 1 rather than bit 2, the statement "will receive data from port 1, determine whether bit 2 is high, and then send FFH to port 3" is FALSE based on the current code.
So,the Final answer is FALSE. The provided code checks the wrong bit (bit 1 instead of bit 2) for high status. To correctly check bit 2, the program should use mask #04H (binary 00000100).
Dahlan Sitompul said:
3 years ago
READ: MOV A,P1; Copy the P1 content into the accumulator (A=P1).
ANL A,#2H; do logic AND between accumulator (A) and an immediate data 02h (00000010b), and it will force the content of the accumulator to be 02h.
CJNE A,#02H, READ; if A is not equal to 02 ( in this case A will be 02h) then jump to a statement labelled as READ.
MOV P3,#FFH; fill an immediate data FFh into the P3.
NB.
This program will always go to the statement MOV P3,#0FFH without taking into account the content read from P1 into the accumulator.
ANL A,#2H; do logic AND between accumulator (A) and an immediate data 02h (00000010b), and it will force the content of the accumulator to be 02h.
CJNE A,#02H, READ; if A is not equal to 02 ( in this case A will be 02h) then jump to a statement labelled as READ.
MOV P3,#FFH; fill an immediate data FFh into the P3.
NB.
This program will always go to the statement MOV P3,#0FFH without taking into account the content read from P1 into the accumulator.
(1)
Gandrapu Navya said:
6 years ago
In Port by default, it has FF SO.
1. We move FF to a.
2. Anding FF with to 2 and stored in the accumulator.
3. Compare a and 2 it is equal and comes out of the loop.
4. FF is sent to port 3.
Hence true.
1. We move FF to a.
2. Anding FF with to 2 and stored in the accumulator.
3. Compare a and 2 it is equal and comes out of the loop.
4. FF is sent to port 3.
Hence true.
Anonymo said:
9 years ago
@Jithendra.
If the port p1.2 is pressed Then P1 has 0000 0010 as data.
Now if we AND 0000 0010 (i.e. #2H) to P1.
Then,
0000 0010 (P1)
0000 0010 (ANDIng #02)
---------------
0000 0010
----------------
To check if any bit is high.
Best way is to check any bit is by ANDING the same data that way the answer in the accumulator will be same. If any of the bits is 0 and we AND 1/0 to it the answer will have 0
If both of the bits are 1 then AND will result to 1.
If the port p1.2 is pressed Then P1 has 0000 0010 as data.
Now if we AND 0000 0010 (i.e. #2H) to P1.
Then,
0000 0010 (P1)
0000 0010 (ANDIng #02)
---------------
0000 0010
----------------
To check if any bit is high.
Best way is to check any bit is by ANDING the same data that way the answer in the accumulator will be same. If any of the bits is 0 and we AND 1/0 to it the answer will have 0
If both of the bits are 1 then AND will result to 1.
Jithendra said:
9 years ago
How AND operation is performed? Please explain me.
Vaishnavi said:
1 decade ago
Only when AND operation is performed we will be able to identify if bit no-2 is high or not.
Sathya said:
1 decade ago
In 0000 00010 the second bit is active high to know whether the second bit is active high adding with 02 that is 0000 0010 will help us to know because an and operation output is active high only if both the inputs are active high.
Mahesh said:
1 decade ago
But what is the necessary of adding it with 2H, please answer me?
Mahesh said:
1 decade ago
Line 1: We will move the content of P1 to accumulator.
Line2: Why to AND with 2H please reply, they have asked to find whether second bit is high, so what's the necessary of adding here.
Line2: Why to AND with 2H please reply, they have asked to find whether second bit is high, so what's the necessary of adding here.
Edison said:
1 decade ago
@Ruben its correct by masking all bits except 2nd bit and checking whether second bit is high or low and jumping actions are taken.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers