---
title: "8085 ALP: transfer data with more than 3 ones"
subject: "Microprocessor"
chapter: 2
key: "semester_4/microprocessor:alp-8085-ones-greater-than-3"
source: https://www.bctengineer.com/q/semester_4/microprocessor/alp-8085-ones-greater-than-3
site: BCT Engineering
---

# 8085 ALP: transfer data with more than 3 ones

Microprocessor · Chapter 2: Programming with 8085 Microprocessor

- Asked in: 2071 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-ones-greater-than-3
- Whole chapter: https://www.bctengineer.com/md/semester_4/microprocessor/chapter-2

## Question

Write an assembly language program for 8085. Table1 contains 16 no. of 8 bit data, transfer data which have number of 1s greater than 3, from table1 to table2, otherwise store FFH in table2.

## Short answer

Count the 1s in each byte by rotating right eight times, incrementing a counter when carry is set. If the count is 4 or greater, copy the byte to table2; otherwise store FFH. For instance, 0FH is copied, 07H becomes FFH.

## Answer

**Assumptions:** table 1 is at 2000H (16 bytes) and table 2 at 2100H. Data and FFH are written at the same index in table 2.

**Method:** the number of 1s is counted by rotating the byte right 8 times and incrementing a counter each time CY = 1. `CPI 04H` sets CY if the count is less than 4. For 4 or more ones (more than 3) the original byte is stored; otherwise FFH is stored.


```asm
        LXI H,2000H     ; HL -> table 1 (16 bytes)
        MVI D,10H       ; 16 numbers
LOOP:   MOV A,M
        MOV B,A         ; keep the data in B
        MVI C,00H       ; C = number of ones
        MVI E,08H       ; 8 bits to test
CNT:    RRC
        JNC NEXT
        INR C           ; bit = 1
NEXT:   DCR E
        JNZ CNT
        MOV A,C
        CPI 04H         ; CY = 1 if ones < 4
        MOV A,B         ; A = data (MOV keeps the flags)
        JNC STORE       ; ones >= 4, i.e. more than 3 -> keep data
        MVI A,0FFH      ; otherwise store FFH
STORE:  INR H           ; table 2 = table 1 + 0100H (2100H)
        MOV M,A
        DCR H
        INX H
        DCR D
        JNZ LOOP
        HLT
```


**Example:** byte 0FH has four 1s, so 0FH is copied; byte 07H has three 1s, so FFH is stored.

## Check yourself

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

1. How does the program count the number of 1s in a byte?
   A. It rotates the byte right 8 times and increments a counter each time CY = 1
   B. It uses the parity flag after ORA A
   C. It subtracts 1 from the byte until it becomes zero
   D. It rotates the byte 4 times and checks the zero flag
   Answer: A. Each RRC moves one bit into CY. After 8 rotations every bit has been tested, and the counter C holds the number of 1s. (Key point: Count 1s by rotating 8 times and incrementing when CY = 1)

2. After counting, C = 04H. What happens after CPI 04H?
   A. CY = 1, so FFH is stored
   B. Z = 0 and CY = 1, so the loop ends
   C. CY = 0, so the original byte is stored in table 2
   D. CY = 1, so the original byte is stored in table 2
   Answer: C. CPI sets CY only when A is less than the operand. 4 is not less than 4, so CY = 0, JNC is taken and the data (four 1s, more than 3) is stored. (Key point: CPI 04H: CY = 1 if count < 4; count of 4 or more keeps the data)

3. The program does MOV A,B between CPI 04H and JNC STORE. Why does this not spoil the decision?
   A. Because JNC tests the zero flag, not CY
   B. Because MOV does not affect any flags, so CY from CPI is kept
   C. Because MOV A,B sets CY again from bit D7
   D. Because CPI stores its result in B
   Answer: B. Data transfer instructions like MOV do not change flags. CY still holds the CPI result when JNC runs. (Key point: MOV does not change flags, so CY from CPI is still valid)

4. Table 1 contains 07H. What is stored at the same index in table 2?
   A. 07H
   B. 03H
   C. 00H
   D. FFH
   Answer: D. 07H = 0000 0111 has three 1s, which is not more than 3, so FFH is stored. (Key point: Fewer than 4 ones: store FFH (07H gives FFH))

## Related in Microprocessor

- [8085 ALP: compare two tables of 16-bit data](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-compare-tables-1ffffh) — Microprocessor, chapter 2
- [8085 ALP: count ones in upper nibble](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-ones-upper-nibble) — 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: odd parity transfer with bit change](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-odd-parity-transfer) — 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
- [8086 ALP: max and min of 16-bit list](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-max-min-16bit) — Microprocessor, chapter 3

## Related in other subjects

- [Count number of 1s in ROM data](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-count-ones-asm) — Embedded System, chapter 9, asked 2×
- [RE: no more than 3 a's](https://www.bctengineer.com/question.md?key=semester_3%2Ftheory_of_computation%3Are-at-most-3-as) — Theory of Computation, chapter 2
- [FFH AND C0H; (16)₁₀ − (26)₁₀ by 2's complement](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Ahex-and-twos-complement-subtract) — Digital Logic, chapter 5
- [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
