---
title: "Count number of 1s in ROM data"
subject: "Embedded System"
chapter: 9
key: "semester_6/embedded_system:8051-count-ones-asm"
source: https://www.bctengineer.com/q/semester_6/embedded_system/8051-count-ones-asm
site: BCT Engineering
---

# Count number of 1s in ROM data

Embedded System · Chapter 9: Microcontrollers in Embedded Systems

- Asked in: 2079 Asoj, 4 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/embedded_system/8051-count-ones-asm
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-9

## Question

Write an assembly program for 8051 to count number of 1's in an 8-bit data stored in ROM at 75H and store result in RAM at 45H.

## Short answer

Read the byte at ROM 0075H using MOVC. Rotate left through carry eight times, incrementing a counter on each carry = 1. Store the count in RAM 45H. Example: B7H yields 06H.

## Answer

The byte stored in code memory (ROM) at 0075H is read with MOVC. It is rotated through the carry flag eight times, and the counter is incremented each time the carry is 1. The result goes to RAM location 45H.

```asm
        ORG 0000H
        MOV DPTR,#0075H     ; DPTR points to the data byte in ROM
        CLR A               ; offset = 0
        MOVC A,@A+DPTR      ; A = byte stored at 0075H
        MOV R0,#8           ; 8 bits to test
        MOV R1,#0           ; R1 = count of 1s
NEXT:   RLC A               ; move MSB into carry
        JNC SKIP            ; carry = 0 -> bit was 0
        INC R1              ; carry = 1 -> count it
SKIP:   DJNZ R0,NEXT        ; repeat for all 8 bits
        MOV 45H,R1          ; store result in RAM 45H
HERE:   SJMP HERE

        ORG 0075H
        DB 0B7H             ; example data 10110111B
        END
```

How it works:
1. `MOVC A,@A+DPTR` with A = 0 reads the ROM byte at DPTR = 0075H.
2. `RLC A` shifts the MSB into carry; `JNC SKIP` skips the increment when the bit is 0.
3. `DJNZ R0,NEXT` repeats for 8 bits; R1 holds the count, copied to 45H.

Example: data B7H = 10110111B contains six 1s, so 45H = 06H (checked in a simulator). The data byte is placed in ROM with `ORG 0075H` / `DB`; the program itself must be shorter than 75H bytes.

## Check yourself

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

1. Which instruction reads the data byte from ROM at 0075H?
   A. MOV A,75H
   B. MOVX A,@DPTR
   C. MOVC A,@A+DPTR with DPTR = 0075H and A = 0
   D. MOV A,@R0
   Answer: C. Code memory is read with MOVC; with A = 0 the address is just DPTR. (Key point: Reading ROM data)

2. How does the program examine each bit?
   A. Rotates A left through carry with RLC and tests the carry
   B. Uses DIV AB
   C. Uses SWAP A
   D. Increments A
   Answer: A. RLC A moves the MSB into carry each time, and JNC decides whether to count it. (Key point: Bit testing)

3. Why is R0 loaded with 8 and used with DJNZ?
   A. There are 8 bytes of data
   B. To repeat the test once for each of the 8 bits
   C. To select register bank 8
   D. To set the delay
   Answer: B. DJNZ R0 repeats the loop for all 8 bits of the byte. (Key point: Loop count)

4. For data B7H = 10110111B, what is stored at 45H?
   A. 02H
   B. 08H
   C. 05H
   D. 06H
   Answer: D. B7H has six 1 bits, so 45H = 06H. (Key point: Example result)

## Close variants set in other exams

- [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×

## Related in Embedded System

- [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
