Finding the maximum in a block is the classic scanning algorithm: keep the largest value seen so far in the accumulator, compare each byte with it using CMP, and replace the accumulator whenever a byte is larger. CMP subtracts internally and sets flags without changing the accumulator.
Scanning a memory block for the largest value; Find the largest of 5 bytes stored at 2000H-2004H.
; Store the result at 2100H.
LXI H, 2000H ; HL = start of block
MVI B, 05H ; count = 5
MOV A, M ; first byte is the largest so far
INX H ; move to the next byte
DCR B ; one byte already examined
LOOP: DCR B ; decrement the counter first
JM DONE ; when B goes negative, block done
CMP M ; compare A with (HL)
JNC NEXT ; if A >= (HL), no change
MOV A, M ; otherwise A = (HL) (new largest)
NEXT: INX H ; advance pointer
JMP LOOP
DONE: STA 2100H ; store the largest
HLT
; Simpler count-down form:
LXI H, 2000H
MVI B, 05H
MOV A, M
DCR B
LOOP2: INX H
CMP M
JNC SKIP
MOV A, M
SKIP: DCR B
JNZ LOOP2
STA 2100H
HLTBoth versions work; the second is shorter and easier to trace. The first form shows how a 16-bit-ish scan can be driven with a signed counter test (JM) when the count can reach zero first.