---
title: "8085 ALP: count ones in upper nibble"
subject: "Microprocessor"
chapter: 2
key: "semester_4/microprocessor:alp-8085-ones-upper-nibble"
source: https://www.bctengineer.com/q/semester_4/microprocessor/alp-8085-ones-upper-nibble
site: BCT Engineering
---

# 8085 ALP: count ones in upper nibble

Microprocessor · Chapter 2: Programming with 8085 Microprocessor

- Asked in: 2072 Asoj, 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-upper-nibble
- Whole chapter: https://www.bctengineer.com/md/semester_4/microprocessor/chapter-2

## Question

Write a program in 8085 to calculate the number of ones in the upper nibble of ten 8-bit numbers stored in a table. Store the count of ones in a location just after the table.

## Short answer

Isolate upper nibble with ANI F0H. Rotate left four times to test each bit in carry flag, counting ones in a register. Store the total count of ones in the upper nibbles of ten numbers at the location just after the table (e.g., 200AH).

## Answer

**Assumptions:** the table is at 2000H-2009H. The question is read as the total number of 1s in the upper nibbles of all ten numbers, stored at 200AH (the location just after the table).

**Method:** the upper nibble is isolated with ANI F0H. Then A is rotated left four times, and each time the bit that goes into CY is tested; the count register E is incremented when it is 1.


```asm
        LXI H,2000H     ; HL -> table
        MVI C,0AH       ; 10 numbers
        MVI E,00H       ; E = total count of ones
LOOP:   MOV A,M
        ANI 0F0H        ; keep upper nibble only
        MVI B,04H       ; four bits (D7..D4) to test
BITLP:  RLC             ; move next bit into CY
        JNC SKIP
        INR E           ; bit is 1 -> count it
SKIP:   DCR B
        JNZ BITLP
        INX H
        DCR C
        JNZ LOOP
        MOV M,E         ; HL now points just after the table (200AH)
        HLT
```


**Example:** table values A5H and 0FH give upper nibbles A (two ones) and 0 (none), so 2 is added to the count.
(If a separate count for each number is wanted, store E in a result table after each number and clear E at the start of each number.)

## Check yourself

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

1. Which instruction keeps only the upper nibble of the number in A?
   A. ANI 0FH
   B. ANI 0F0H
   C. ORI 0F0H
   D. XRI 0F0H
   Answer: B. ANDing with F0H keeps D7-D4 and clears D3-D0. ANI 0FH would keep the lower nibble instead. (Key point: ANI F0H isolates the upper nibble)

2. How does the program test each bit of the upper nibble?
   A. It compares A with 01H using CPI and counts when they are equal
   B. It uses RRC so the bit goes into D0, then checks the Z flag
   C. It decrements A until it reaches zero, counting each step
   D. RLC moves the next bit into CY, and JNC skips INR E for a 0
   Answer: D. RLC copies D7 into CY. If CY = 1, the count in E is incremented, and JNC skips the increment when the bit is 0. (Key point: Each RLC moves a bit into CY; JNC skips, otherwise INR E)

3. Why is B loaded with 04H for the inner loop?
   A. Only D7-D4 need testing, so four RLCs bring each of them into CY
   B. There are four numbers in each row of the table
   C. RLC moves two bits at a time, so 4 rotations cover all 8 bits
   D. The total count can never exceed 4 for the whole table
   Answer: A. After ANI F0H, only D7-D4 can be 1. Four RLCs push D7, D6, D5 and D4 into CY one after another. (Key point: Only 4 rotations are needed for the bits D7-D4)

4. Where is the count stored, and why does MOV M,E store it in the right place?
   A. At 2009H, because HL still points to the last number
   B. At 2000H, because HL is reset before the store
   C. At 200AH, because after ten INX H instructions HL points just past the table
   D. In register E only; nothing is stored in memory
   Answer: C. The table occupies 2000H-2009H, and HL is incremented once for each of the ten numbers. When the loop ends, HL = 200AH, so MOV M,E stores the total there. (Key point: The total is stored at 200AH, just after the table)

## Related in Microprocessor

- [8085 ALP: add upper and lower nibbles](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-add-nibbles) — 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: multiplication table of a number](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-multiplication-table) — Microprocessor, chapter 2
- [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: 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: transfer with bit set/reset](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-set-reset-bit-80h) — 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
- [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×
- [Count number of 0s in ROM data](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-count-zeros-asm) — Embedded System, chapter 9, asked 2×
- [Greatest and smallest of ten numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-greatest-smallest-ten) — Computer Programming, chapter 11
