GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPUTER ORGANIZATION & ARCHITECTURE

Trace an 8085 bounded memory search

2 questions solved the same way, newest first. Back to Computer Organization & Architecture

How these are solved

Follow CMP M, RZ, INX H, DCR B and JNZ to infer the search and conditional final HL. Same source program starts at 1C40H and searches for 05H over at most ten bytes.

Descriptive
Report issue

Explain an 8085 memory search

Instructions & Addressing Modes

Question 16(a), GATE 1995: MVI B,0AH; MVI A,05H; LXI H,1C40H; CALL SUB; HLT; SUB: CMP M; RZ; INX H; DCR B; JNZ SUB; RET. What does this program do?
Question 16(a), GATE 1995: MVI B,0AH; MVI A,05H; LXI H,1C40H; CALL SUB; HLT; SUB: CMP M; RZ; INX H; DCR B; JNZ SUB; RET. What does this program do?
Tap to enlarge
Answer & solution

Answer: Searches up to ten bytes at 1C40H–1C49H for the first 05H. (no official key was published)

B starts at decimal 10, A contains the search value 05H, and HL points to 1C40H. In 8085 notation M refers to the memory byte addressed by HL. CMP M computes flags as if subtracting that byte from A, without modifying A. If the byte equals 05H, the zero flag is set and RZ returns immediately to the caller.

On a mismatch, INX H advances the address, DCR B reduces the remaining count, and JNZ repeats while the count remains nonzero. Importantly, that JNZ tests the flags from DCR B, not the earlier comparison. After ten mismatches, B becomes zero and RET returns normally. HLT then stops the main program.

Thus the routine performs a bounded linear search, examining addresses 1C40H through 1C49H and stopping on the first match. On success HL addresses that match and B is the number of positions remaining including the matched position. On failure HL points one byte beyond the searched interval and B=0. A stays 05H throughout.

Original paper, page 7

Descriptive
Report issue

Determine the conditional final search address

Instructions & Addressing Modes

Question 16(c), GATE 1995: MVI B,0AH; MVI A,05H; LXI H,1C40H; CALL SUB; HLT; SUB: CMP M; RZ; INX H; DCR B; JNZ SUB; RET. Find HL after execution; no memory contents are supplied.
Question 16(c), GATE 1995: MVI B,0AH; MVI A,05H; LXI H,1C40H; CALL SUB; HLT; SUB: CMP M; RZ; INX H; DCR B; JNZ SUB; RET. Find HL after execution; no memory contents are supplied.
Tap to enlarge
Answer & solution

Answer: HL=1C40H+k for first match offset k∈{0,…,9}; if absent, HL=1C4AH. (no official key was published)

Let k be the offset of the first byte equal to 05H among the ten searched locations. For every preceding mismatch the program increments HL exactly once. It therefore reaches the matching address 1C40H+k after k increments. CMP sets zero and RZ returns before INX H, leaving HL pointing directly at the match. In particular, an immediate match leaves HL=1C40H; a match in the last tested byte leaves HL=1C49H.

If all ten comparisons fail, every iteration executes INX H, including the last one. Starting at 1C40H, ten increments give 1C4AH. DCR B then makes B=0 and the routine returns without testing that next address.

No single unconditional final address can be inferred because the source gives no values for memory 1C40H–1C49H. The no-match value 1C4AH is a conditional answer, not an established fact. This distinction also avoids the common off-by-one error of returning 1C49H after ten failures.

Original paper, page 7