8051 loop behavior — will the following code execute continuously (infinite loop)? Code at label STAT: STAT: MOV A, #01H JNZ STAT Decide whether execution remains stuck at STAT forever.
-
ATrue
-
BFalse
-
COnly if A starts as 0
-
DOnly if JNZ tests the zero flag, not A
-
EIt loops only once, then falls through
Answer
Correct Answer: True
Explanation
Introduction / Context:Branch-on-zero instructions in the 8051 evaluate the Accumulator directly for JZ/JNZ. Recognizing how the condition is formed helps quickly reason about loop termination or infinite loops during code reviews.
Given Data / Assumptions:
- Code: STAT: MOV A, #01H ; JNZ STAT
- JNZ branches if Accumulator is not zero at the time of evaluation
- No intervening instructions modify A between MOV and JNZ
Concept / Approach:MOV A, #01H sets A to 1 on every pass. JNZ checks whether A ≠ 0. Since A is 1, the condition is always true, so control jumps back to STAT and repeats the same sequence forever—an infinite loop.
Step-by-Step Solution:
First pass: A ← 01HEvaluate JNZ: A ≠ 0 ⇒ branch to STATOn each iteration: A is reset to 01H before the check ⇒ still nonzero ⇒ branch againVerification / Alternative check:If any instruction cleared A before JNZ (e.g., CLR A) or if JNZ tested a different flag, the loop might exit. Here, neither occurs, confirming the infinite loop.
Why Other Options Are Wrong:
- False / loops only once / depends on zero flag semantics: JNZ directly tests A; A remains 1.
- Only if A starts as 0: initial value is overwritten by MOV.
Common Pitfalls:
- Assuming JNZ reads a separate zero flag; it examines the Accumulator value.
Final Answer:True