๐ Programs to arrange series in 8085 Microprocessor (15 MCQs)
Sorting Numerical Data Stored in Memory
Arranging a series in the 8085 involves placing stored numerical values into ascending or descending order. A common introductory technique is bubble sorting, which compares adjacent elements and exchanges them when they appear in the wrong order. The processor uses the HL register pair to access array elements, the accumulator for comparisons and a temporary register during exchanges.
Adjacent Element Comparison and Flag Interpretation
CMP M compares the accumulator with the memory byte addressed by HL without changing the accumulator. For unsigned ascending sorting, the current value should remain before the next value when it is smaller or equal. Carry and zero conditions identify these cases, while a larger current value requires an exchange.
Register and Memory Operations During Element Exchange
The accumulator temporarily stores the first element, and a general-purpose register preserves the second. MOV M,A writes the first value into the next location, while DCX H selects the previous location for storing the saved value. The pointer is then advanced to continue comparing adjacent elements.
Valid Comparison and Exchange Fragment for Ascending Order
With HL pointing to the current element, execute MOV A,M, INX H, CMP M, JC NEXT and JZ NEXT. Otherwise, execute MOV D,M, MOV M,A, DCX H, MOV M,D and INX H. This exchanges adjacent values only when the first is greater, producing ascending order after repeated passes.
Repeated Passes and Descending Series Arrangement
A complete bubble-sort program repeats adjacent comparisons across the array for multiple passes. After each pass, the largest remaining value reaches the end of the unsorted section. For descending order, the exchange condition is reversed so that smaller values move toward the end. Loop counters determine the comparisons and passes required.
Misconceptions About Sorting and Memory Exchange
'CMP M automatically exchanges two numbers' is incorrect because comparison changes flags, not stored values. 'Ascending sorting places the largest value first' reverses the required order. 'One adjacent comparison completely sorts an array' overlooks the repeated passes needed to arrange all elements.
Sorting Algorithm Tracing and Array-Order Questions
Practise tracing adjacent comparisons and identify when a swap occurs. Understand the roles of HL, accumulator and temporary register D. Examination questions may require completing a partially sorted array, counting comparisons, identifying ascending or descending conditions and determining the final memory arrangement after repeated passes.
Sorting and Memory-Exchange Terminology
- Sorting - Arranging values in a specified order.
- Bubble Sort - Repeatedly exchanges adjacent elements.
- Ascending - Arranges values from smallest to largest.
- Descending - Arranges values from largest to smallest.
- CMP - Compares two numerical values.
- Swap - Exchanges two stored values.
๐ All Programs to arrange series in 8085 Microprocessor MCQs
Q1. An array of five unsigned bytes is stored beginning at memory address 2500H. Which sequence of 8085 instructions will correctly sort the array in ascending order using a simple bubbleโsort method?
๐ Explanation: The bubbleโsort routine works by repeatedly comparing adjacent elements and swapping them when out of order. Option A shows the correct use of LDA to load, CMP for comparison, JNC to skip swapping, and XCHG/STA to store swapped values, matching the bubbleโsort logic. Options B, C, and D misuse registers or omit essential steps like swapping or proper loop control, so they would not produce a sorted array. This mirrors realโworld sorting where adjacent comparisons and swaps are fundamental.
Q2. You need to arrange a list of numbers in descending order on an 8085 microcontroller. After comparing two bytes, which instruction should be used to move the larger value into the accumulator for the next comparison?
๐ Explanation: When sorting descending, the larger value must stay in the accumulator after a compare. The JNC (Jump if No Carry) instruction triggers when the accumulator is greater than or equal to the compared memory byte, keeping the larger value. JC would jump when the accumulator is smaller, and CMA inverts bits, which is unrelated. STA stores the accumulator but does not affect the comparison flow. This reflects typical descending sort logic in embedded systems.
Q3. A programmer wants to sort ten 8โbit numbers stored from 2500H to 2509H in ascending order using insertion sort. Which register pair is best suited to hold the temporary key value during the inner loop?
๐ Explanation: Insertion sort requires a temporary storage for the key while shifting larger elements. The DE pair is often used for temporary data because D can hold the key while E can be used as an index register, allowing easy movement of values with DCR/E. HL is typically used for address pointers, BC for counters, and AF includes the accumulator and flags, which are needed for comparisons. Using DE aligns with common 8085 coding patterns for temporary storage.
Q4. When implementing a descending sort, why is it important to initialize the loop counter to the number of elements minus one rather than the total number of elements?
๐ Explanation: In a descending sort, after each outer loop iteration the smallest element settles at the end of the array, so the effective unsorted portion reduces by one. Initializing the counter to Nโ1 avoids an unnecessary comparison of the already sorted last element, improving efficiency. Options B, C, and D describe unrelated processor constraints. This mirrors real sorting where reducing the loop range each pass saves time.
Q5. Which of the following 8085 instructions directly swaps the contents of the accumulator with the memory location pointed to by HL?
๐ Explanation: XCHG exchanges the contents of HL with DE, not directly swapping accumulator and memory. The correct instruction to move accumulator contents to memory is STA, but that does not swap. MOV M,A copies accumulator to memory without swapping. The instruction that effectively swaps is XCHG when combined with MOV instructions, but none does a direct swap alone. Therefore none of the options correctly describe a direct swap, making B the closest as it moves data, though not a swap.
Q6. In a program that sorts numbers stored consecutively in memory, which flag condition indicates that the value in the accumulator is greater than the value in the memory location being compared?
๐ Explanation: The CMP instruction subtracts the memory operand from the accumulator without storing the result. If the accumulator is larger, the subtraction produces a borrow, clearing the carry flag. Hence, a cleared carry flag (or specifically, the condition 'Carry not set') indicates the accumulator is greater. The zero flag signals equality, sign flag reflects the result's sign, and parity flag relates to even/odd parity, none of which directly indicate a greaterโthan relationship.
Q7. A microcontroller must sort sensor readings stored at 3000Hโ300Fh in ascending order before transmitting them. Which highโlevel approach best reduces code size while still using 8085 assembly?
๐ Explanation: Selection sort repeatedly selects the smallest remaining element and swaps it into place, requiring only a few nested loops and minimal code, which is ideal for limited 8085 memory. Quicksort needs recursion or a stack, increasing code size. A hardware comparator would simplify comparisons but still needs sorting logic. Hardcoding permutations is impractical for 16 values. Thus selection sort balances size and functionality.
Q8. When sorting an array of bytes in descending order, why might a programmer choose to complement each byte before applying an ascending sort algorithm?
๐ Explanation: Complementing each byte transforms larger values into smaller ones (e.g., 0xFFโx), so an existing ascending sort routine can be reused to achieve descending order after reconverting. This technique saves development time and code space. Complementing does not reverse bit order (option A), nor does it inherently reduce instruction count or speed (options B and D). Reusing code is a common optimization in embedded programming.
Q9. A student writes an 8085 program that loads a value from 2500H, increments it, and stores it back at 2500H. Which instruction is missing if the program should instead store the incremented value at 2501H?
๐ Explanation: The program currently uses STA to write back to the same address. To store the incremented value at the next location, the address pointer must be incremented using INX H before the STA instruction. Without INX H, the value overwrites the original location. CMA inverts bits, unrelated to address change, and MOV A,M copies without storing. Therefore the missing step is INX H.
Q10. Which 8085 instruction pair is most appropriate for swapping two memory bytes located at addresses 2500H and 2501H during a sort operation?
๐ Explanation: Option A loads each byte into the accumulator and stores it in the other's location, effectively swapping them. Option B misuses XCHG, which swaps HL with DE, not memory bytes. Option C combines LDA with XCHG incorrectly, as XCHG does not operate on memory directly. Option D uses indirect addressing but lacks proper register setup. Thus A correctly performs a byte swap in assembly.
Q11. What is the primary advantage of writing the sorting routine in assembly language rather than a highโlevel language for an 8085 microcontroller?
๐ Explanation: Assembly allows the programmer to manipulate registers and flags directly, minimizing instruction count and cycle time, which yields faster execution on the 8085. Highโlevel languages abstract these details, making debugging more straightforward but often slower. Automatic memory management and portability are features of highโlevel languages, not assembly. In real embedded systems, speed is often critical, making option A the key advantage.
Q12. Which opcode corresponds to the CMA instruction that finds the 1's complement of the accumulator?
๐ Explanation: The opcode for CMA (Complement Accumulator) in the 8085 instruction set is 2Fh. This singleโbyte opcode directly inverts all bits of the accumulator. The other listed opcodes represent different instructions (e.g., 3F is CMC, 4F is CMA in some other CPUs, 5F is not defined for 8085). Knowing the exact opcode is essential when handโassembling machine code.
Q13. During a sort, a programmer uses the DCR instruction on register B as a loop counter. What condition must be tested to correctly exit the loop?
๐ Explanation: DCR decrements B and sets the zero flag when the result becomes zero. To exit the loop when B reaches zero, the program should test the zero flag with JZ or continue looping with JNZ while B is nonโzero. Since the question asks for the condition to exit, checking for zero with JZ is appropriate. The other options reference unrelated flags or conditions.
Q14. In an 8085 sorting routine, why is it important to preserve the contents of the HL register pair before entering a nested loop?
๐ Explanation: The HL pair typically points to the current element in memory. Nested loops may modify HL for innerโloop indexing; restoring it ensures the outer loop continues from the correct array position. HL does not hold the accumulator, flag status, or program counter. Preserving HL avoids losing the base address, which is crucial for correct array traversal in real sorting tasks.
Q15. When sorting a series of numbers stored in consecutive memory locations, which flag indicates that two compared values are equal, allowing the algorithm to skip a swap?
๐ Explanation: After executing CMP, if the accumulator and memory operand are equal, the subtraction result is zero, setting the zero flag. The algorithm can then decide not to swap. The carry flag indicates a borrow, the sign flag reflects the result's sign, and the parity flag relates to even/odd parity, none of which directly signal equality.