Skip to main content

8085 ALP: compare two tables of 16-bit data

Short answer

For each of 10 pairs of 16-bit numbers (low byte first), subtract table2 from table1 using SUB and SBB. If borrow (CY=1) indicates table1 < table2, store 1FFFh (FFH, 1FH) in table3; else store 0000h. Tables at 2000H, 2020H, 2040H.

Two tables contain ten 16-bit data each. Write an assembly language program to generate the 3rd table which contains 1FFFh if the corresponding data in the 1st table is less than that of 2nd table, else store 0000h.

Assumptions: table 1 at 2000H, table 2 at 2020H, table 3 at 2040H, each element stored low byte first (little-endian). The tables are in the same page, so only L is changed.

Method: data1 - data2 is calculated with SUB (low bytes) and SBB (high bytes). The final borrow, CY = 1, means data1 < data2. INR L is used to move to the next byte because it does not change CY. If CY = 1, 1FFFH is stored; otherwise 0000H.

        LXI H,2000H     ; HL -> table 1 (2000H); L = index
        MVI C,0AH       ; 10 pairs
LOOP:   MOV B,M         ; B = low byte of data 1
        INR L
        MOV D,M         ; D = high byte of data 1
        MOV A,L
        ADI 1FH         ; L -> table 2 low byte (2020H + 2i)
        MOV L,A
        MOV A,B
        SUB M           ; low bytes: data1 - data2
        INR L           ; INR does not affect CY
        MOV A,D
        SBB M           ; high bytes with borrow; CY = 1 if data1 < data2
        JC LESS
        MVI B,00H       ; data1 >= data2 -> 0000H
        MVI D,00H
        JMP SAVE
LESS:   MVI B,0FFH      ; data1 < data2 -> 1FFFH
        MVI D,1FH
SAVE:   MOV A,L
        ADI 1FH         ; L -> table 3 low byte (2040H + 2i)
        MOV L,A
        MOV M,B         ; store low byte
        INR L
        MOV M,D         ; store high byte
        MOV A,L
        SUI 3FH         ; back to table 1, next element (i + 2)
        MOV L,A
        DCR C
        JNZ LOOP
        HLT

Note: the program stores the low byte first, so 1FFFH appears as FFH, 1FH in memory.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Microprocessor chapter 2 questions →