---
title: "8085 ALP: compare two tables of 16-bit data"
subject: "Microprocessor"
chapter: 2
key: "semester_4/microprocessor:alp-8085-compare-tables-1ffffh"
source: https://www.bctengineer.com/q/semester_4/microprocessor/alp-8085-compare-tables-1ffffh
site: BCT Engineering
---

# 8085 ALP: compare two tables of 16-bit data

Microprocessor · Chapter 2: Programming with 8085 Microprocessor

- Asked in: 2073 Magh, 8 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/microprocessor/alp-8085-compare-tables-1ffffh
- Whole chapter: https://www.bctengineer.com/md/semester_4/microprocessor/chapter-2

## Question

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.

## 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.

## Answer

**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.


```asm
        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.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does the program find that a 16-bit data1 is less than data2?
   A. It compares only the high bytes with CMP M
   B. It does SUB on the low bytes and SBB on the high bytes; a final borrow CY = 1 means data1 < data2
   C. It adds the two numbers and checks the zero flag
   D. It does SUB on the high bytes only; CY = 0 means data1 < data2
   Answer: B. data1 - data2 is done in two 8-bit steps, with SBB including the borrow from the low bytes. A borrow at the end (CY = 1) means data1 < data2. (Key point: SUB low bytes, SBB high bytes; final CY = 1 means data1 < data2)

2. Why is it safe to use INR L between SUB M and SBB M?
   A. INR L clears CY, so SBB starts with no borrow
   B. INR L sets CY when L overflows, which SBB needs
   C. SBB does not use the carry flag
   D. INR does not affect CY, so the borrow from SUB is kept for SBB
   Answer: D. INR changes S, Z, P and AC but not CY. The borrow from the low-byte subtraction is still there when SBB M runs. (Key point: INR L does not change CY, so the borrow survives until SBB)

3. When data1 < data2, how does 1FFFH appear in table 3 memory?
   A. FFH at the lower address, then 1FH at the next address
   B. 1FH at the lower address, then FFH at the next address
   C. 1FH in both bytes
   D. FFH in both bytes
   Answer: A. The program stores the low byte first (MOV M,B with B = FFH), then the high byte 1FH at the next address. (Key point: Little-endian: 1FFFH stored as FFH then 1FH)

4. Why does the program change only L (not H) to move between the three tables?
   A. Because H is used as the loop counter
   B. Because INX H would change the carry flag
   C. Because all three tables are in the same page (20xxH), so H stays 20H
   D. Because the tables are stored in reverse order
   Answer: C. Tables at 2000H, 2020H and 2040H all have high byte 20H. Only the low byte L needs to be adjusted, using ADI/SUI and INR L. (Key point: Tables are in the same page, so only L needs to change)

## Related in Microprocessor

- [8085 ALP: transfer data with more than 3 ones](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-ones-greater-than-3) — Microprocessor, chapter 2
- [8085 ALP: square of ten numbers](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-square-numbers) — Microprocessor, chapter 2
- [8085 ALP: copy data within a range](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-copy-range) — Microprocessor, chapter 2
- [8085 ALP: add two tables, sum and carry](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-add-two-tables) — Microprocessor, chapter 2
- [8085 ALP: transfer with bit set/reset](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-set-reset-bit-80h) — Microprocessor, chapter 2
- [8085 ALP: multiplication table of a number](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-multiplication-table) — Microprocessor, chapter 2

## Related in other subjects

- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
- [3-phase IM principle; speed less than sync](https://www.bctengineer.com/question.md?key=semester_4%2Felectrical_machine%3Athree-phase-im-principle-slip) — Electrical Machine, chapter 5
- [Detection gain of DSB-AM is less than 1](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Adetection-gain-dsbam-less-than-1) — Communication Systems, chapter 9
- [When terminal voltage is less or more than emf](https://www.bctengineer.com/question.md?key=semester_5%2Felectric_machines_ii%3Aterminal-voltage-less-or-more) — Electric Machines II, chapter 1
