GATE / CS ARCHIVE

GATE CSE 1995 / QUESTION 16(a)

Explain an 8085 memory search

Computer Organization & Architecture · Instructions & Addressing Modes · Full GATE CSE 1995 paper

GATE 1995, Q16(a) Descriptive
Report issue

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