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

# Count number of 0s in ROM data

Embedded System · Chapter 9: Microcontrollers in Embedded Systems

- Asked in: 2078 Poush, 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-zeros-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 0'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 eight times, incrementing a counter whenever carry = 0. Store the count in RAM 45H. Example: B7H gives 02H.

## Answer

The byte at ROM address 0075H is read using MOVC, rotated left eight times through the carry, and a counter is incremented whenever the carry is 0. The count is stored in 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 0s
NEXT:   RLC A               ; move MSB into carry
        JC SKIP             ; carry = 1 -> bit was 1, skip
        INC R1              ; carry = 0 -> count the zero
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 byte at 0075H.
2. `RLC A` moves each bit into carry; `JC SKIP` skips the counter when the bit is 1, so only zero bits are counted.
3. After 8 rotations R1 holds the number of zeros and is saved in 45H.

Example: B7H = 10110111B has two 0s, so 45H = 02H (checked in a simulator). Alternatively, the number of zeros equals 8 minus the number of ones.

## Check yourself

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

1. Which change from the count-ones program makes it count zeros?
   A. Use JC SKIP so the counter is incremented only when carry = 0
   B. Use RRC instead of RLC
   C. Load R0 with 0
   D. Use MOVX instead of MOVC
   Answer: A. JC skips the increment when the bit is 1, so only 0 bits are counted. (Key point: Counting condition)

2. For B7H = 10110111B, what is stored at 45H?
   A. 06H
   B. 02H
   C. 08H
   D. 00H
   Answer: B. B7H has two 0 bits, so the count is 02H. (Key point: Example result)

3. How is the number of zeros in an 8-bit byte related to the number of ones?
   A. zeros = ones + 8
   B. zeros = ones
   C. zeros = 8 - ones
   D. zeros = ones / 8
   Answer: C. Each of the 8 bits is either 0 or 1, so zeros = 8 minus ones. (Key point: Zeros from ones)

4. Where is the final count held before it is saved to RAM 45H?
   A. In DPTR
   B. In register R1
   C. In the stack
   D. In port P1
   Answer: B. R1 is the counter and MOV 45H,R1 stores it. (Key point: Storing result)

## Close variants set in other exams

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

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