🎓 BookMCQ
← Back to 6. Programming of 8085

📝 Programs to find largest or smallest number in 8085 Microprocessor (10 MCQs)

Comparing Multiple Numbers Using the CMP Instruction

The 8085 can identify the largest or smallest number in a memory array by repeatedly comparing an assumed result with successive elements. The accumulator holds the current maximum or minimum, while HL points to the next memory location. CMP performs comparison through subtraction and updates the status flags without changing either operand.

Carry and Zero Flags in Numerical Comparison

After CMP M, the carry flag is set when the accumulator value is smaller than the memory operand. The zero flag is set when both values are equal. These conditions allow conditional jumps such as JC, JNC and JZ to determine whether the current result should be retained or replaced.

Sequential Scanning and Updating the Selected Value

The first array element initializes the accumulator as the current maximum or minimum. HL advances to the next element, CMP compares its value with A, and an appropriate conditional branch decides whether to replace A. The pointer advances again, and a counter controls the number of remaining comparisons.

Program to Find the Largest Number Among Five Values

Place five values at 2050H–2054H. Execute LXI H,2050H, MOV A,M, INX H and MVI C,04H. In the loop, execute CMP M, JNC NEXT and MOV A,M. Then advance HL, decrement C and repeat with JNZ. Finally, STA 2060H stores the largest value.

Changing the Comparison Logic to Find the Smallest Value

To find the smallest value, retain the current accumulator value when JC is true and otherwise replace it with the memory operand. This reverses the maximum-selection condition without requiring a different comparison instruction. The same pointer movement and loop counter can process the complete array.

Misconceptions About Number Comparison Programs

'CMP M changes the accumulator contents' is incorrect because it updates flags without storing the subtraction result. 'JNC means the accumulator is always greater' overlooks equality, where carry is also clear. 'The program compares all numbers simultaneously' is incorrect because comparisons occur sequentially between the current result and one memory element.

Array Comparison and Conditional-Jump Examination Questions

Practise tracing array addresses, accumulator contents and loop-counter values. Determine how CMP affects CY and Z and select the correct conditional branch for maximum or minimum detection. Examination questions may provide an array and ask for the final result, number of comparisons, memory address of the answer or the effect of equal elements.

Comparison and Array-Processing Terms

  • CMP - Compares operands through subtraction.
  • Array - Collection of stored data values.
  • Maximum - Largest value in a collection.
  • Minimum - Smallest value in a collection.
  • HL Pointer - Identifies the current array element.
  • JC - Jumps when carry is set.
2
Easy
5
Medium
3
Hard

📝 All Programs to find largest or smallest number in 8085 Microprocessor MCQs

Q1. Which 8085 instruction produces the 1's complement of the accumulator?

A.CMA ✅
B.RLC
C.DCR
D.INR
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: The CMA instruction flips every bit in the accumulator, giving the 1's complement of the original value. RLC rotates bits left, DCR decrements a register, and INR increments a register; none of these generate a complement. Understanding that CMA directly complements the accumulator helps students select the right opcode when writing a program to invert data, as required in many bit‑wise tasks.

Q2. When implementing a routine to find the largest number among a list, which register is typically used to hold the current maximum value?

A.Accumulator ✅
B.B register
C.H register
D.Stack Pointer
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: The accumulator is the central data register for arithmetic and logic operations, making it the natural choice to store the current maximum while iterating through a list. The B register is often a general‑purpose register, H is part of the HL pair for addressing, and the Stack Pointer controls the stack; they are not suited for holding the running maximum in a simple scan.

Q3. Three unsigned bytes are stored at memory locations 2500H, 2501H, and 2502H. Which code fragment correctly finds the largest value and stores it at 2503H?

A.LDA 2500H → MOV B,A → LDA 2501H → CMP B → JC SKIP → MOV B,A → SKIP: LDA 2502H → CMP B → JC END → MOV B,A → END: MOV A,B → STA 2503H
B.LDA 2500H → MOV C,A → LDA 2501H → CMP C → JNC NEXT → MOV C,A → NEXT: LDA 2502H → CMP C → JNC DONE → MOV C,A → DONE: MOV A,C → STA 2503H ✅
C.LDA 2500H → MOV A,B → LDA 2501H → CMP B → JC KEEP → MOV B,A → KEEP: LDA 2502H → CMP B → JC DONE → MOV B,A → DONE: MOV A,B → STA 2503H
D.LDA 2500H → MOV D,A → LDA 2501H → CMP D → JZ SKIP → MOV D,A → SKIP: LDA 2502H → CMP D → JZ END → MOV D,A → END: MOV A,D → STA 2503H
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Option B correctly uses the accumulator to load each byte, moves it to register C, and employs the JNC (Jump if No Carry) condition after each CMP to keep the larger value in C. After the final comparison, the largest byte is transferred back to the accumulator and stored at 2503H. The other options misuse registers or use inappropriate jumps, causing the program to retain an incorrect value or never store the result.

Q4. Why must a program initialize the maximum register with the first data element before entering the comparison loop?

A.To provide a valid baseline for subsequent comparisons ✅
B.To speed up execution by skipping the first iteration
C.Because the accumulator cannot be used without initialization
D.To avoid overflow errors during addition
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: Initializing the maximum with the first element establishes a concrete reference point; each later element is compared against this baseline using CMP. If the register were left undefined, comparisons would be meaningless, potentially leading to incorrect results. Skipping the first iteration would miss a data point, while accumulator usage or overflow concerns are unrelated to the need for a proper initial value.

Q5. If the carry flag is not cleared before executing the CMP instruction while searching for the smallest number, what incorrect behavior may occur?

A.The program may treat a larger number as smaller
B.The program will always jump to the label after CMP ✅
C.The comparison will be performed on the wrong register
D.The program will halt unexpectedly
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: When the carry flag remains set, the subsequent CMP will see the flag already active, causing the JC (Jump if Carry) condition to fire even if the newly fetched value is not smaller. This makes the routine incorrectly assume every new value is smaller, leading to the wrong minimum being stored. The other options describe unrelated effects; the key issue is the premature jump caused by an uncleared flag.

Q6. In a routine that finds the smallest number, which conditional jump should follow the CMP instruction after loading a new value into the accumulator?

A.JNC (Jump if No Carry)
B.JC (Jump if Carry) ✅
C.JNZ (Jump if Not Zero)
D.JZ (Jump if Zero)
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: The CMP instruction sets the carry flag when the accumulator is less than the compared register. To update the minimum when the new value is smaller, the program must jump on the carry condition, i.e., JC. JNC would skip the update when the new value is smaller, while JNZ and JZ react to the zero flag, which is unrelated to the magnitude comparison needed for finding the minimum.

Q7. The following fragment aims to find the largest of three numbers, but it contains a bug. Identify the bug and the correct fix.\nLDA 3000H\nCMP B\nJC SKIP\nMOV B,A\nSKIP: LDA 3001H\nCMP B\nJC DONE\nMOV B,A\nDONE: STA 3002H

A.The bug is using CMP B instead of CMP M; fix by replacing CMP B with CMP M
B.The bug is loading the first number into B instead of A; fix by using MOV A,B after LDA
C.The bug is comparing the accumulator with B before B holds a value; fix by initializing B with the first number using MOV B,A after the first LDA ✅
D.The bug is storing the result in the wrong address; fix by changing STA 3002H to STA 3003H
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: The code compares the accumulator with register B before B has been loaded with the first data element, so the first comparison is meaningless. The proper approach is to load the first number with LDA, then move it into B (MOV B,A) to establish the initial maximum. Subsequent CMP instructions then correctly compare new values against this stored maximum, ensuring the final result is accurate.

Q8. What is the time complexity, in terms of instruction count, for finding the largest of N numbers using a linear scan on the 8085?

A.O(N) ✅
B.O(N^2)
C.O(log N)
D.O(1)
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: A linear scan visits each of the N elements exactly once, executing a constant number of instructions per element (load, compare, conditional jump, possible move). Hence the total instruction count grows proportionally with N, giving a complexity of O(N). Quadratic, logarithmic, or constant complexities would imply multiple passes, nested loops, or no iteration, none of which match the straightforward single‑pass algorithm used in assembly.

Q9. How can a program be modified to find both the largest and smallest numbers in a single pass through a data block?

A.Use two separate loops, one for max and one for min
B.Store each number twice and compare separately
C.Maintain two registers, updating them with CMP and JC/JNC as appropriate
D.First find the max, then reverse the data and find the min ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: The most efficient method is to keep two registers—one for the current maximum and one for the current minimum. After loading each new value, the program uses CMP followed by JC to update the minimum and JNC to update the maximum. This allows both extremes to be determined during the same traversal, avoiding extra passes or redundant storage.

Q10. When using the PSW flags to decide whether a newly read number is larger than the current maximum, which sequence correctly updates the maximum and resets the flags?

A.LDA addr → CMP A → JC UPDATE → MOV A,MAX → STC\nUPDATE: NOP
B.LDA addr → CMP A → JNC SKIP → MOV A,MAX → CMC\nSKIP: NOP
C.LDA addr → CMP A → JC SKIP → MOV A,MAX → STC\nSKIP: NOP ✅
D.LDA addr → CMP A → JNC UPDATE → MOV A,MAX → CMC\nUPDATE: NOP
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: The correct sequence loads the new value, compares it with the current maximum, and jumps if the carry flag indicates the new value is smaller (JC SKIP). If the jump is not taken, the new value is larger, so MOV A,MAX updates the maximum, and STC sets the carry flag for the next iteration. Using JNC or CMC in this context would invert the logic, causing the program to update the maximum on the wrong condition.

🔗 Related Topics (MCQs)