Looping and Indexing
Counters, pointers, array processing.
Many practical programs require executing a block of instructions multiple times or processing a series of data values stored in memory. The 8085 microprocessor supports this through loop structures using counter registers and conditional jump instructions, and through indexed memory access using register pairs as memory pointers. Understanding counters, pointers, and array processing is essential for writing efficient 8085 assembly programs and for solving GATE problems involving memory operations.
Core Concept Explanation
A loop in 8085 assembly is implemented by combining a counter register, typically B or C, with the DCR instruction and a conditional jump. The DCR instruction decrements the register by one and sets the Zero flag when the value reaches zero. The JNZ instruction then checks the Zero flag: if it is not set (meaning the counter has not reached zero), control jumps back to the loop label. This continues until the counter reaches zero and JNZ falls through.
For memory array processing, the 8085 uses register pairs as 16-bit memory pointers. The HL register pair is the most commonly used indirect addressing register. The instruction MOV A, M reads the byte stored at the memory address currently held in HL into the accumulator. After processing, INX H advances HL to the next memory address. This combination of MOV A, M and INX H is the standard pattern for traversing a byte array in ascending order.
The LXI H, address instruction loads a 16-bit immediate address into the HL pair. This sets the starting point of the pointer. Similarly, LXI B and LXI D can load addresses into BC and DE pairs. However, only HL can be used directly with MOV A, M and MOV M, A for memory read and write. DE cannot be used with MOV M directly. The instruction LDAX D or LDAX B is required to use DE or BC pairs for indirect memory read.
Mathematical Expression
For a loop that executes N times: the counter B is initialized to N. After each iteration, B is decremented by 1. The loop continues while B is not equal to 0. The total number of executions is exactly N. For array traversal, if the base address is Start and the array has N elements, element at index i is located at address: Start + i. The HL pointer starts at Start and is incremented i times using INX H to access element i.
Practical Understanding
A typical array summation program initializes HL to the base address of the array, initializes a counter in B to the number of elements, clears the accumulator, and then enters a loop where it adds the value at memory location M to the accumulator and advances the pointer. After N iterations, the accumulator holds the sum of all array elements.
One important practical consideration is the maximum counter value. Since DCR operates on an 8-bit register, the counter can hold a maximum value of 255. For arrays longer than 255 elements, a 16-bit counter using register pairs and the DCX instruction must be used. However, since DCX does not set the Zero flag, a different termination condition must be designed, such as comparing the pointer to an end address using subtraction.
Given:
Array of 5 bytes starting at memory address 2000H: 05H, 0AH, 0FH, 14H, 19H
Find the sum of all elements.
Why this formula applies:
Sum = element[0] + element[1] + ... + element[N-1]
HL pointer traverses each address. MOV A, M reads each byte.
Program:
LXI H, 2000H ; HL = base address (2000H)
MVI B, 05H ; B = counter (5 elements)
MVI A, 00H ; A = 0 (accumulator cleared)
LOOP: ADD M ; A = A + memory[HL]
INX H ; HL = HL + 1 (next element)
DCR B ; B = B - 1
JNZ LOOP ; repeat if B != 0
HLT
Calculation:
Iteration 1: A = 00H + 05H = 05H
Iteration 2: A = 05H + 0AH = 0FH
Iteration 3: A = 0FH + 0FH = 1EH
Iteration 4: A = 1EH + 14H = 32H
Iteration 5: A = 32H + 19H = 4BH
Final Answer:
A = 4BH = 75 decimal = sum of 5+10+15+20+25Exam Tip: Only HL can be used with MOV A, M and MOV M, A for indirect memory access. To read using BC or DE pairs, use LDAX B or LDAX D respectively. A common GATE trap is assuming MOV A, (DE) is valid — it is not. Use LDAX D instead.
Key Points on Looping and Indexing
- DCR r decrements an 8-bit register and sets the Zero flag when the register reaches 0. This flag is checked by JNZ for loop control.
- LXI H, address loads a 16-bit address into the HL pair. This sets the starting pointer for array traversal.
- MOV A, M reads the byte at address (HL) into the accumulator. ADD M adds the byte at (HL) to the accumulator directly.
- INX H advances the HL pointer to the next memory location. It does not affect any flags.
- For arrays longer than 255 elements, a 16-bit counter using register pairs and a comparison-based exit condition must replace the simple DCR/JNZ pattern.
- LDAX B and LDAX D are used to read memory indirectly using BC or DE pairs when HL is unavailable or reserved.
Quick Revision
- Loop = counter (MVI B, N) + body + DCR B + JNZ LOOP. Loop executes exactly N times.
- Array access = LXI H, base + MOV A, M (read) + INX H (advance). Repeat in loop.
- Only HL works with MOV A, M and MOV M, A. BC and DE use LDAX B / LDAX D for indirect reads.
- DCR sets Zero flag. DCX does not. Use DCR for loop counters, not DCX.
- Element i in array starting at address X is at address X+i. HL pointer reaches it after i INX H operations.
- ADD M is a compact instruction that adds memory[HL] to A directly without a separate MOV. Saves one instruction per iteration.
- Exam trap: JNZ checks the Zero flag, not the Carry flag. Use JNC to branch on Carry.
Looping and Indexing Quiz
Test your ability to implement counter-based loops and pointer-based array processing in 8085 assembly.
Q1.In a loop to process 10 bytes of an array starting at address 2000H, which register pair is most appropriate as a memory pointer, and which register is typically used as a counter?
Related Articles
Time Delay Loops
Calculating delay using T-states.
11 min read
Code Conversion
BCD to Binary, Binary to BCD, ASCII.
9 min read
BCD Arithmetic
DAA instruction usage.
6 min read
Assembly Language Basics
Labels, mnemonics, operands.
9 min read
8086 Addressing Modes
Based, Indexed, Based-Indexed, Relative.
9 min read